# 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
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)
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
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