importer.py (7801:f71f9634d809) | importer.py (9737:cc3b8601f582) |
---|---|
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 --- 40 unchanged lines hidden (view full) --- 49 # Because the importer is created and initialized in its own 50 # little sandbox (in init.cc), the globals that were available 51 # when the importer module was loaded and CodeImporter was 52 # defined are not available when load_module is actually 53 # called. Soooo, the imports must live here. 54 import imp 55 import os 56 import sys | 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 --- 40 unchanged lines hidden (view full) --- 49 # Because the importer is created and initialized in its own 50 # little sandbox (in init.cc), the globals that were available 51 # when the importer module was loaded and CodeImporter was 52 # defined are not available when load_module is actually 53 # called. Soooo, the imports must live here. 54 import imp 55 import os 56 import sys |
57 mod = imp.new_module(fullname) 58 sys.modules[fullname] = mod | |
59 60 try: | 57 58 try: |
59 mod = sys.modules[fullname] 60 except KeyError: 61 mod = imp.new_module(fullname) 62 sys.modules[fullname] = mod 63 64 try: |
|
61 mod.__loader__ = self 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 69 if os.path.basename(srcfile) == '__init__.py': 70 mod.__path__ = fullname.split('.') | 65 mod.__loader__ = self 66 srcfile,abspath,code = self.modules[fullname] 67 68 override = os.environ.get('M5_OVERRIDE_PY_SOURCE', 'false').lower() 69 if override in ('true', 'yes') and os.path.exists(abspath): 70 src = file(abspath, 'r').read() 71 code = compile(src, abspath, 'exec') 72 73 if os.path.basename(srcfile) == '__init__.py': 74 mod.__path__ = fullname.split('.') |
75 mod.__package__ = fullname 76 else: 77 mod.__package__ = fullname.rpartition('.')[0] |
|
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) | 78 mod.__file__ = srcfile 79 80 exec code in mod.__dict__ 81 except Exception: 82 del sys.modules[fullname] 83 raise 84 85 return mod 86 87# Create an importer and add it to the meta_path so future imports can 88# use it. There's currently nothing in the importer, but calls to 89# add_module can be used to add code. 90import sys 91importer = CodeImporter() 92add_module = importer.add_module 93sys.meta_path.append(importer) |