Specify what a loader must do if a load fails.

This commit is contained in:
Brett Cannon 2009-02-15 06:06:09 +00:00
parent 7a3465e69d
commit 2025481a4e
1 changed files with 4 additions and 9 deletions

View File

@ -251,21 +251,16 @@ Specification part 1: The Importer Protocol
the loader must create a new module object and add it to
sys.modules.
In C code, all of these requirements can be met simply by using
the PyImport_AddModule() function, which returns the existing
module or creates a new one and adds it to sys.modules for you.
In Python code, you can use something like:
module = sys.modules.setdefault(fullname, new.module(fullname))
to accomplish the same results.
Note that the module object *must* be in sys.modules before the
loader executes the module code. This is crucial because the
module code may (directly or indirectly) import itself; adding
it to sys.modules beforehand prevents unbounded recursion in the
worst case and multiple loading in the best.
If the load fails, the loader needs to remove any module it may have
inserted into sys.modules. If the module was already in
sys.modules then the loader should leave it alone.
- The __file__ attribute must be set. This must be a string, but it
may be a dummy value, for example "<frozen>". The privilege of
not having a __file__ attribute at all is reserved for built-in