arch-power: Fix disassembly for compare instructions
[gem5.git] / src / python / importer.py
1 # Copyright (c) 2008 The Hewlett-Packard Development Company
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met: redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer;
8 # redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution;
11 # neither the name of the copyright holders nor the names of its
12 # contributors may be used to endorse or promote products derived from
13 # this software without specific prior written permission.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 # Simple importer that allows python to import data from a dict of
28 # code objects. The keys are the module path, and the items are the
29 # filename and bytecode of the file.
30 class CodeImporter(object):
31 def __init__(self):
32 self.modules = {}
33
34 def add_module(self, filename, abspath, modpath, code):
35 if modpath in self.modules:
36 raise AttributeError("%s already found in importer" % modpath)
37
38 self.modules[modpath] = (filename, abspath, code)
39
40 def find_module(self, fullname, path):
41 if fullname in self.modules:
42 return self
43
44 return None
45
46 def load_module(self, fullname):
47 # Because the importer is created and initialized in its own
48 # little sandbox (in init.cc), the globals that were available
49 # when the importer module was loaded and CodeImporter was
50 # defined are not available when load_module is actually
51 # called. Soooo, the imports must live here.
52 import imp
53 import os
54 import sys
55
56 try:
57 mod = sys.modules[fullname]
58 except KeyError:
59 mod = imp.new_module(fullname)
60 sys.modules[fullname] = mod
61
62 try:
63 mod.__loader__ = self
64 srcfile,abspath,code = self.modules[fullname]
65
66 override = os.environ.get('M5_OVERRIDE_PY_SOURCE', 'false').lower()
67 if override in ('true', 'yes') and os.path.exists(abspath):
68 src = open(abspath, 'r').read()
69 code = compile(src, abspath, 'exec')
70
71 if os.path.basename(srcfile) == '__init__.py':
72 mod.__path__ = fullname.split('.')
73 mod.__package__ = fullname
74 else:
75 mod.__package__ = fullname.rpartition('.')[0]
76 mod.__file__ = srcfile
77
78 exec(code, mod.__dict__)
79 except Exception:
80 del sys.modules[fullname]
81 raise
82
83 return mod
84
85 # Create an importer and add it to the meta_path so future imports can
86 # use it. There's currently nothing in the importer, but calls to
87 # add_module can be used to add code.
88 import sys
89 importer = CodeImporter()
90 add_module = importer.add_module
91 sys.meta_path.append(importer)