Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit d7c236e78286ebfcfd637e25ac71424cf7e499ff 1 parent 0772f25
@andysan authored
Showing with 4 additions and 2 deletions.
  1. +4 −2 src/python/importer.py
View
6 src/python/importer.py
@@ -54,8 +54,7 @@ def load_module(self, fullname):
import imp
import os
import sys
- mod = imp.new_module(fullname)
- sys.modules[fullname] = mod
+ mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
try:
mod.__loader__ = self
@@ -68,6 +67,9 @@ def load_module(self, fullname):
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__
Please sign in to comment.
Something went wrong with that request. Please try again.