Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 ed400adb7bbd463c2cc681ec7edb27942d56f063 1 parent 8bd59e1
Andreas Sandberg authored

Showing 1 changed file with 4 additions and 2 deletions. Show diff stats Hide diff stats

  1. 6  src/python/importer.py
6  src/python/importer.py
@@ -54,8 +54,7 @@ def load_module(self, fullname):
54 54
         import imp
55 55
         import os
56 56
         import sys
57  
-        mod = imp.new_module(fullname)
58  
-        sys.modules[fullname] = mod
  57
+        mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
59 58
 
60 59
         try:
61 60
             mod.__loader__ = self
@@ -68,6 +67,9 @@ def load_module(self, fullname):
68 67
 
69 68
             if os.path.basename(srcfile) == '__init__.py':
70 69
                 mod.__path__ = fullname.split('.')
  70
+                mod.__package__ = fullname
  71
+            else:
  72
+                mod.__package__ = fullname.rpartition('.')[0]
71 73
             mod.__file__ = srcfile
72 74
 
73 75
             exec code in mod.__dict__

0 notes on commit ed400ad

Please sign in to comment.
Something went wrong with that request. Please try again.