Author: christian.heimes Date: Thu Jan 17 08:49:45 2008 New Revision: 60016
Modified: python/branches/py3k-importhook/Doc/library/imputil.rst python/branches/py3k-importhook/Lib/imputil.py python/branches/py3k-importhook/Lib/test/test_imp.py Log: Implemented when_imported decorator. I've moved the decorator to imputils. Implementing the decorator in C is too complex and time consuming for a rather simple task. Modified: python/branches/py3k-importhook/Doc/library/imputil.rst ============================================================================== --- python/branches/py3k-importhook/Doc/library/imputil.rst (original) +++ python/branches/py3k-importhook/Doc/library/imputil.rst Thu Jan 17 08:49:45 2008 @@ -14,6 +14,17 @@ approach to custom :keyword:`import` functions. +.. function:: when_imported(name) + + When imported decorator for post import hooks. The *when_imported* + decorator provides a convenient way to register a post import + callback for a module:: + + @when_imported('name') + def callback(module): + do_something_with_module(module) + + .. class:: ImportManager([fs_imp]) Manage the import process. Modified: python/branches/py3k-importhook/Lib/imputil.py ============================================================================== --- python/branches/py3k-importhook/Lib/imputil.py (original) +++ python/branches/py3k-importhook/Lib/imputil.py Thu Jan 17 08:49:45 2008 @@ -19,10 +19,26 @@ import struct import marshal -__all__ = ["ImportManager","Importer","BuiltinImporter"] +__all__ = ["ImportManager","Importer","BuiltinImporter", "when_imported"] _ModuleType = type(sys) ### doesn't work in JPython... +def when_imported(name): + """When imported decorator for callbacks + + @when_imported('name') + def callback(module): + do_something_with_module(module) + + The callback is called with the module object as argument when the module + is loaded. If the module is already loaded the callback is called + immediately. + """ + def register(hook): + imp.register_post_import_hook(hook, name) + return register + + class ImportManager: "Manage the import process." Modified: python/branches/py3k-importhook/Lib/test/test_imp.py ============================================================================== --- python/branches/py3k-importhook/Lib/test/test_imp.py (original) +++ python/branches/py3k-importhook/Lib/test/test_imp.py Thu Jan 17 08:49:45 2008 @@ -1,5 +1,6 @@ import os import imp +from imputil import when_imported import sys import thread import unittest @@ -7,13 +8,6 @@ import tempfile from test import test_support - -def when_imported(name): - def register(hook): - imp.register_post_import_hook(hook, name) - return register - - class LockTests(unittest.TestCase): """Very basic test of import lock functions.""" _______________________________________________ Python-3000-checkins mailing list Python-3000-checkins@python.org http://mail.python.org/mailman/listinfo/python-3000-checkins