importer.py (5522:e56c3d89be79) importer.py (7502:3ef7ff12c788)
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

--- 19 unchanged lines hidden (view full) ---

28
29# Simple importer that allows python to import data from a dict of
30# code objects. The keys are the module path, and the items are the
31# filename and bytecode of the file.
32class CodeImporter(object):
33 def __init__(self):
34 self.modules = {}
35
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

--- 19 unchanged lines hidden (view full) ---

28
29# Simple importer that allows python to import data from a dict of
30# code objects. The keys are the module path, and the items are the
31# filename and bytecode of the file.
32class CodeImporter(object):
33 def __init__(self):
34 self.modules = {}
35
36 def add_module(self, filename, modpath, code):
36 def add_module(self, filename, abspath, modpath, code):
37 if modpath in self.modules:
38 raise AttributeError, "%s already found in importer"
39
37 if modpath in self.modules:
38 raise AttributeError, "%s already found in importer"
39
40 self.modules[modpath] = (filename, code)
40 self.modules[modpath] = (filename, abspath, code)
41
42 def find_module(self, fullname, path):
43 if fullname in self.modules:
44 return self
45
46 return None
47
48 def load_module(self, fullname):

--- 5 unchanged lines hidden (view full) ---

54 import imp
55 import os
56 import sys
57 mod = imp.new_module(fullname)
58 sys.modules[fullname] = mod
59
60 try:
61 mod.__loader__ = self
41
42 def find_module(self, fullname, path):
43 if fullname in self.modules:
44 return self
45
46 return None
47
48 def load_module(self, fullname):

--- 5 unchanged lines hidden (view full) ---

54 import imp
55 import os
56 import sys
57 mod = imp.new_module(fullname)
58 sys.modules[fullname] = mod
59
60 try:
61 mod.__loader__ = self
62 srcfile,code = self.modules[fullname]
62 srcfile,abspath,code = self.modules[fullname]
63
64 override = os.environ.get('M5_OVERRIDE_PY_SOURCE', 'false').lower()
65 if override in ('true', 'yes') and os.path.exists(abspath):
66 src = file(abspath, 'r').read()
67 code = compile(src, abspath, 'exec')
68
63 if os.path.basename(srcfile) == '__init__.py':
64 mod.__path__ = fullname.split('.')
65 mod.__file__ = srcfile
66
67 exec code in mod.__dict__
68 except Exception:
69 del sys.modules[fullname]
70 raise
71
72 return mod
73
74# Create an importer and add it to the meta_path so future imports can
75# use it. There's currently nothing in the importer, but calls to
76# add_module can be used to add code.
77import sys
78importer = CodeImporter()
79add_module = importer.add_module
80sys.meta_path.append(importer)
69 if os.path.basename(srcfile) == '__init__.py':
70 mod.__path__ = fullname.split('.')
71 mod.__file__ = srcfile
72
73 exec code in mod.__dict__
74 except Exception:
75 del sys.modules[fullname]
76 raise
77
78 return mod
79
80# Create an importer and add it to the meta_path so future imports can
81# use it. There's currently nothing in the importer, but calls to
82# add_module can be used to add code.
83import sys
84importer = CodeImporter()
85add_module = importer.add_module
86sys.meta_path.append(importer)