From 35184169179ad873b96bc99e5c9bdc4d3dd5ead6 Mon Sep 17 00:00:00 2001 From: Nathan Binkert Date: Wed, 21 Jul 2010 15:53:52 -0700 Subject: 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! --- src/python/importer.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/python') diff --git a/src/python/importer.py b/src/python/importer.py index fe099fdb8..4e364873f 100644 --- a/src/python/importer.py +++ b/src/python/importer.py @@ -33,11 +33,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" - 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 @@ class CodeImporter(object): 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 -- cgit v1.2.3