[Python-checkins] python/nondist/peps pep-0302.txt,1.14,1.15
pje at users.sourceforge.net
pje at users.sourceforge.net
Thu Sep 23 15:54:56 CEST 2004
Update of /cvsroot/python/python/nondist/peps
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9906
Modified Files:
pep-0302.txt
Log Message:
It's PyImport_AddModule, not PyImport_ModuleAdd. Fix Python code sample
to do the right thing for reload() to work. Revise explanation so it's
clear what to do to fulfill reload()'s requirements in both C and Python.
Index: pep-0302.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0302.txt,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- pep-0302.txt 23 Sep 2004 04:06:40 -0000 1.14
+++ pep-0302.txt 23 Sep 2004 13:54:53 -0000 1.15
@@ -240,22 +240,27 @@
The load_module() method has a few responsibilities that it must
fulfill *before* it runs any code:
- - It must use the existing module object from sys.modules, if one
- exists. (Otherwise, the reload() builtin will not work
- correctly.)
+ - If there is an existing module object named 'fullname' in
+ sys.modules, the loader must use that existing module.
+ (Otherwise, the reload() builtin will not work correctly.)
+ If a module named 'fullname' does not exist in sys.modules,
+ the loader must create a new module object and add it to
+ sys.modules.
- - If a module object is not already present in sys.modules, the
- loader must create a module object. From Python this can be done
- via the new.module() function, the imp.new_module() function or
- via the module type object; from C with the PyModule_New()
- function or the PyImport_ModuleAdd() function. The latter also
- does the following step:
+ 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:
- - It must add the module to sys.modules, if it was not already
- present there. 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.
+ 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.
- The __file__ attribute must be set. This must be a string, but it
may be a dummy value, for example "<frozen>". The privilege of
@@ -279,8 +284,7 @@
def load_module(self, fullname):
ispkg, code = self._get_code(fullname)
- mod = imp.new_module(fullname)
- sys.modules[fullname] = mod
+ mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
mod.__file__ = "<%s>" % self.__class__.__name__
mod.__loader__ = self
if ispkg:
More information about the Python-checkins
mailing list