ASPN ActiveState Programmer Network
  ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups | Web Services
SEARCH
advanced | search help

Reference
ActivePython 2.4
What's New
What's new in Python 2.3?
Contents
1 PEP 218: A Standard Set Datatype
2 PEP 255: Simple Generators
3 PEP 263: Source Code Encodings
4 PEP 273: Importing Modules from Zip Archives
5 PEP 277: Unicode file name support for Windows NT
6 PEP 278: Universal Newline Support
7 PEP 279: enumerate()
8 PEP 282: The logging Package
9 PEP 285: A Boolean Type
10 PEP 293: Codec Error Handling Callbacks
11 PEP 301: Package Index and Metadata for Distutils
12 PEP 302: New Import Hooks
13 PEP 305: Comma-separated Files
14 PEP 307: Pickle Enhancements
15 Extended Slices
16 Other Language Changes
17 New, Improved, and Deprecated Modules
18 Pymalloc: A Specialized Object Allocator
19 Build and C API Changes
20 Other Changes and Fixes
21 Porting to Python 2.3
22 Acknowledgements
About this document ...

MyASPN >> Reference >> ActivePython 2.4 >> What's New >> What's new in Python 2.3?
ActivePython 2.4 documentation

 
12 PEP 302: New Import Hooks

While it's been possible to write custom import hooks ever since the ihooks module was introduced in Python 1.3, no one has ever been really happy with it because writing new import hooks is difficult and messy. There have been various proposed alternatives such as the imputil and iu modules, but none of them has ever gained much acceptance, and none of them were easily usable from C code.

PEP 302 borrows ideas from its predecessors, especially from Gordon McMillan's iu module. Three new items are added to the sys module:

  • sys.path_hooks is a list of callable objects; most often they'll be classes. Each callable takes a string containing a path and either returns an importer object that will handle imports from this path or raises an ImportError exception if it can't handle this path.

  • sys.path_importer_cache caches importer objects for each path, so sys.path_hooks will only need to be traversed once for each path.

  • sys.meta_path is a list of importer objects that will be traversed before sys.path is checked. This list is initially empty, but user code can add objects to it. Additional built-in and frozen modules can be imported by an object added to this list.

Importer objects must have a single method, find_module(fullname, path=None). fullname will be a module or package name, e.g. "string" or "distutils.core". find_module() must return a loader object that has a single method, load_module(fullname), that creates and returns the corresponding module object.

Pseudo-code for Python's new import logic, therefore, looks something like this (simplified a bit; see PEP 302 for the full details):

for mp in sys.meta_path:
    loader = mp(fullname)
    if loader is not None:
        <module> = loader.load_module(fullname)
        
for path in sys.path:
    for hook in sys.path_hooks:
        try:
            importer = hook(path)
        except ImportError:
            # ImportError, so try the other path hooks
            pass
        else:
            loader = importer.find_module(fullname)
            <module> = loader.load_module(fullname)

# Not found!
raise ImportError

See Also:

PEP 302, New Import Hooks
Written by Just van Rossum and Paul Moore. Implemented by Just van Rossum.

See About this document... for information on suggesting changes.

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState 2004 All rights reserved