base: Make the Python module loader PEP302 compliant
The custom Python loader didn't comply with PEP302 for two reasons: * Previously, we would overwrite old modules on name conflicts. PEP302 explicitly states that: "If there is an existing module object named 'fullname' in sys.modules, the loader must use that existing module". * The "__package__" attribute wasn't set. PEP302: "The __package__ attribute must be set." This changeset addresses both of these issues.
This commit is contained in:
parent
d989a3ad50
commit
63dae28703
1 changed files with 9 additions and 2 deletions
|
@ -54,6 +54,10 @@ class CodeImporter(object):
|
|||
import imp
|
||||
import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
mod = sys.modules[fullname]
|
||||
except KeyError:
|
||||
mod = imp.new_module(fullname)
|
||||
sys.modules[fullname] = mod
|
||||
|
||||
|
@ -68,6 +72,9 @@ class CodeImporter(object):
|
|||
|
||||
if os.path.basename(srcfile) == '__init__.py':
|
||||
mod.__path__ = fullname.split('.')
|
||||
mod.__package__ = fullname
|
||||
else:
|
||||
mod.__package__ = fullname.rpartition('.')[0]
|
||||
mod.__file__ = srcfile
|
||||
|
||||
exec code in mod.__dict__
|
||||
|
|
Loading…
Reference in a new issue