sparc,sim: Remove special handling of SPARC in the clone system call.
[gem5.git] / src / python / importer.py
index fe099fdb8ef88353aede88dff10b09f6cb9b8e67..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
@@ -33,11 +34,11 @@ class CodeImporter(object):
     def __init__(self):
         self.modules = {}
 
-    def add_module(self, filename, modpath, code):
+    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, code)
+        self.modules[modpath] = (filename, abspath, code)
 
     def find_module(self, fullname, path):
         if fullname in self.modules:
@@ -54,17 +55,30 @@ 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
-            srcfile,code = self.modules[fullname]
+            srcfile,abspath,code = self.modules[fullname]
+
+            override = os.environ.get('M5_OVERRIDE_PY_SOURCE', 'false').lower()
+            if override in ('true', 'yes') and  os.path.exists(abspath):
+                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