sparc,sim: Remove special handling of SPARC in the clone system call.
[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 from __future__ import print_function
28 from __future__ import absolute_import
29
30 # Simple importer that allows python to import data from a dict of
31 # code objects. The keys are the module path, and the items are the
32 # filename and bytecode of the file.
33 class CodeImporter(object):
34 def __init__(self):
35 self.modules = {}
36
37 def add_module(self, filename, abspath, modpath, code):
38 if modpath in self.modules:
39 raise AttributeError("%s already found in importer" % modpath)
40
41 self.modules[modpath] = (filename, abspath, code)
42
43 def find_module(self, fullname, path):
44 if fullname in self.modules:
45 return self
46
47 return None
48
49 def load_module(self, fullname):
50 # Because the importer is created and initialized in its own
51 # little sandbox (in init.cc), the globals that were available
52 # when the importer module was loaded and CodeImporter was
53 # defined are not available when load_module is actually
54 # called. Soooo, the imports must live here.
55 import imp
56 import os
57 import sys
58
59 try:
60 mod = sys.modules[fullname]
61 except KeyError:
62 mod = imp.new_module(fullname)
63 sys.modules[fullname] = mod
64
65 try:
66 mod.__loader__ = self
67 srcfile,abspath,code = self.modules[fullname]
68
69 override = os.environ.get('M5_OVERRIDE_PY_SOURCE', 'false').lower()
70 if override in ('true', 'yes') and os.path.exists(abspath):
71 src = open(abspath, 'r').read()
72 code = compile(src, abspath, 'exec')
73
74 if os.path.basename(srcfile) == '__init__.py':
75 mod.__path__ = fullname.split('.')
76 mod.__package__ = fullname
77 else:
78 mod.__package__ = fullname.rpartition('.')[0]
79 mod.__file__ = srcfile
80
81 exec(code, mod.__dict__)
82 except Exception:
83 del sys.modules[fullname]
84 raise
85
86 return mod
87
88 # Create an importer and add it to the meta_path so future imports can
89 # use it. There's currently nothing in the importer, but calls to
90 # add_module can be used to add code.
91 import sys
92 importer = CodeImporter()
93 add_module = importer.add_module
94 sys.meta_path.append(importer)