changeset 3ef7ff12c788 in /z/repo/m5
details: http://repo.m5sim.org/m5?cmd=changeset;node=3ef7ff12c788
description:
        python: Add mechanism to override code compiled into the exectuable
        If the user sets the environment variable M5_OVERRIDE_PY_SOURCE to
        True, then imports that would normally find python code compiled into
        the executable will instead first check in the absolute location where
        the code was found during the build of the executable.  This only
        works for files in the src (or extras) directories, not automatically
        generated files.

        This is a developer feature!

diffstat:

 src/SConscript         |  17 +++++++++++------
 src/python/importer.py |  12 +++++++++---
 src/sim/init.cc        |   3 ++-
 src/sim/init.hh        |   1 +
 4 files changed, 23 insertions(+), 10 deletions(-)

diffs (91 lines):

diff -r a75564db03c3 -r 3ef7ff12c788 src/SConscript
--- a/src/SConscript    Wed Jul 21 09:55:57 2010 -0700
+++ b/src/SConscript    Wed Jul 21 15:53:52 2010 -0700
@@ -929,12 +929,17 @@
     dst = file(str(target[0]), 'w')
 
     def dump_mod(sym, endchar=','):
+        def c_str(string):
+            if string is None:
+                return "0"
+            return '"%s"' % string
         pysource = PySource.symnames[sym]
-        print >>dst, '    { "%s",' % pysource.arcname
-        print >>dst, '      "%s",' % pysource.modpath
-        print >>dst, '       %s_beg, %s_end,' % (sym, sym)
-        print >>dst, '       %s_end - %s_beg,' % (sym, sym)
-        print >>dst, '       *(int *)%s_end }%s'  % (sym, endchar)
+        print >>dst, '    { %s,' % c_str(pysource.arcname)
+        print >>dst, '      %s,' % c_str(pysource.abspath)
+        print >>dst, '      %s,' % c_str(pysource.modpath)
+        print >>dst, '      %s_beg, %s_end,' % (sym, sym)
+        print >>dst, '      %s_end - %s_beg,' % (sym, sym)
+        print >>dst, '      *(int *)%s_end }%s'  % (sym, endchar)
     
     print >>dst, '#include "sim/init.hh"'
 
@@ -953,7 +958,7 @@
             # Skip the importer since we've already exported it
             continue
         dump_mod(sym)
-    print >>dst, "    { 0, 0, 0, 0, 0, 0 }"
+    print >>dst, "    { 0, 0, 0, 0, 0, 0, 0 }"
     print >>dst, "};"
 
 
diff -r a75564db03c3 -r 3ef7ff12c788 src/python/importer.py
--- a/src/python/importer.py    Wed Jul 21 09:55:57 2010 -0700
+++ b/src/python/importer.py    Wed Jul 21 15:53:52 2010 -0700
@@ -33,11 +33,11 @@
     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"
 
-        self.modules[modpath] = (filename, code)
+        self.modules[modpath] = (filename, abspath, code)
 
     def find_module(self, fullname, path):
         if fullname in self.modules:
@@ -59,7 +59,13 @@
 
         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 = file(abspath, 'r').read()
+                code = compile(src, abspath, 'exec')
+
             if os.path.basename(srcfile) == '__init__.py':
                 mod.__path__ = fullname.split('.')
             mod.__file__ = srcfile
diff -r a75564db03c3 -r 3ef7ff12c788 src/sim/init.cc
--- a/src/sim/init.cc   Wed Jul 21 09:55:57 2010 -0700
+++ b/src/sim/init.cc   Wed Jul 21 15:53:52 2010 -0700
@@ -154,7 +154,8 @@
     while (pymod->filename) {
         PyObject *code = getCode(pymod);
         PyObject *result = PyObject_CallMethod(module, PyCC("add_module"),
-            PyCC("ssO"), pymod->filename, pymod->modpath, code);
+            PyCC("sssO"), pymod->filename, pymod->abspath, pymod->modpath,
+            code);
         if (!result) {
             PyErr_Print();
             return 1;
diff -r a75564db03c3 -r 3ef7ff12c788 src/sim/init.hh
--- a/src/sim/init.hh   Wed Jul 21 09:55:57 2010 -0700
+++ b/src/sim/init.hh   Wed Jul 21 15:53:52 2010 -0700
@@ -37,6 +37,7 @@
 struct EmbeddedPyModule
 {
     const char *filename;
+    const char *abspath;
     const char *modpath;
     const char *code;
     const char *code_end;
_______________________________________________
m5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/m5-dev

Reply via email to