sparc,sim: Remove special handling of SPARC in the clone system call.
[gem5.git] / src / python / importer.py
index 4e364873f15d9da796458746b62bcd0742a73048..c29fb7bd10f04fe1820601bce80a5d07881621b9 100644 (file)
@@ -23,8 +23,9 @@
 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
-# Authors: Nathan Binkert
+
+from __future__ import print_function
+from __future__ import absolute_import
 
 # Simple importer that allows python to import data from a dict of
 # code objects.  The keys are the module path, and the items are the
@@ -35,7 +36,7 @@ class CodeImporter(object):
 
     def add_module(self, filename, abspath, modpath, code):
         if modpath in self.modules:
-            raise AttributeError, "%s already found in importer"
+            raise AttributeError("%s already found in importer" % modpath)
 
         self.modules[modpath] = (filename, abspath, code)
 
@@ -54,8 +55,12 @@ class CodeImporter(object):
         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
@@ -63,14 +68,17 @@ class CodeImporter(object):
 
             override = os.environ.get('M5_OVERRIDE_PY_SOURCE', 'false').lower()
             if override in ('true', 'yes') and  os.path.exists(abspath):
-                src = file(abspath, 'r').read()
+                src = open(abspath, 'r').read()
                 code = compile(src, abspath, 'exec')
 
             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__
+            exec(code, mod.__dict__)
         except Exception:
             del sys.modules[fullname]
             raise