X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fpython%2Fimporter.py;h=c29fb7bd10f04fe1820601bce80a5d07881621b9;hb=007abdec6b5893d9e06789f61f31f2e67bda20aa;hp=4e364873f15d9da796458746b62bcd0742a73048;hpb=35184169179ad873b96bc99e5c9bdc4d3dd5ead6;p=gem5.git diff --git a/src/python/importer.py b/src/python/importer.py index 4e364873f..c29fb7bd1 100644 --- a/src/python/importer.py +++ b/src/python/importer.py @@ -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