changeset cc3b8601f582 in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=cc3b8601f582
description:
        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.

diffstat:

 src/python/importer.py |  11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)

diffs (28 lines):

diff -r 20ae86ebd4c2 -r cc3b8601f582 src/python/importer.py
--- a/src/python/importer.py    Mon Jun 03 13:40:05 2013 +0200
+++ b/src/python/importer.py    Mon Jun 03 13:51:03 2013 +0200
@@ -54,8 +54,12 @@
         import imp
         import os
         import sys
-        mod = imp.new_module(fullname)
-        sys.modules[fullname] = mod
+
+        try:
+            mod = sys.modules[fullname]
+        except KeyError:
+            mod = imp.new_module(fullname)
+            sys.modules[fullname] = mod
 
         try:
             mod.__loader__ = self
@@ -68,6 +72,9 @@
 
             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__
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to