[Python-checkins] r53014 - sandbox/trunk/import_in_py/importer.py sandbox/trunk/import_in_py/mock_importer.py sandbox/trunk/import_in_py/test_importer.py
brett.cannon
python-checkins at python.org
Wed Dec 13 00:16:53 CET 2006
Author: brett.cannon
Date: Wed Dec 13 00:16:53 2006
New Revision: 53014
Modified:
sandbox/trunk/import_in_py/importer.py
sandbox/trunk/import_in_py/mock_importer.py
sandbox/trunk/import_in_py/test_importer.py
Log:
Have the .py/.pyc handler re-initialize a module if the module being
imported is found in sys.modules. This is needed for 'reload'.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Wed Dec 13 00:16:53 2006
@@ -319,17 +319,14 @@
"""
try:
- return sys.modules[fullname]
- except KeyError:
- try:
- module = self.handler.handle_code(self, fullname,
- self.file_path, self.package)
- except:
- if fullname in sys.modules:
- del sys.modules[fullname]
- raise
- return module
-
+ module = self.handler.handle_code(self, fullname,
+ self.file_path, self.package)
+ except:
+ if fullname in sys.modules:
+ del sys.modules[fullname]
+ raise
+ return module
+
def mod_time(self, path):
"""Return the modification time for the specified path as an integer."""
return int(os.stat(path).st_mtime)
@@ -459,16 +456,16 @@
source_path = None
source_timestamp = None
bytecode_path = None
- module = self.new_module(mod_name)
+ module = sys.modules.get(mod_name)
+ if module is None:
+ module = self.new_module(mod_name)
+ sys.modules[mod_name] = module
# __file__, __path__, and __loader__ *must* be set on the module before
# any code is executed by the import.
module.__loader__ = loader
module.__file__ = path
if package is not None:
module.__path__ = [package]
- # Store the module in sys.modules so as to prevent any circular import
- # dependency problems.
- sys.modules[mod_name] = module
base_path, type_ = loader.split_path(path)
if type_ in self.bytecode_handles:
# Attempt to use bytecode.
Modified: sandbox/trunk/import_in_py/mock_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/mock_importer.py (original)
+++ sandbox/trunk/import_in_py/mock_importer.py Wed Dec 13 00:16:53 2006
@@ -41,12 +41,18 @@
returned.
"""
- self.loader = loader
- self.module_name = mod_name
- self.path = path
- self.package = package
- sys.modules[mod_name] = self
- return self
+ module = sys.modules.get(mod_name)
+ if module is None:
+ module = self
+ sys.modules[mod_name] = self
+ try:
+ module.loader = loader
+ module.module_name = mod_name
+ module.path = path
+ module.package = package
+ except AttributeError:
+ pass
+ return module
class MockPyPycLoader(object):
@@ -77,7 +83,9 @@
self.pyc_path = (self.base_path, self.pyc_ext)
self.modification_time = 1
# Needed for read_data on source path.
- self.source = "test_attr = None"
+ self.attr_name = 'test_attr'
+ self.attr_value = None
+ self.source = "%s = %s" % (self.attr_name, self.attr_value)
code_object = compile(self.source, "<mock loader>", 'exec')
bytecode = marshal.dumps(code_object)
if good_magic:
Modified: sandbox/trunk/import_in_py/test_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/test_importer.py (original)
+++ sandbox/trunk/import_in_py/test_importer.py Wed Dec 13 00:16:53 2006
@@ -679,6 +679,21 @@
module = loader._handle_py(handler, pkg_path)
loader._verify_module(module)
self.failUnlessEqual(module.__path__, [pkg_path])
+
+ def test_sys_modules_used(self):
+ # Handler should re-initialize an existing module in sys.modules
+ # (needed for 'reload').
+ loader = mock_importer.MockPyPycLoader.setup()
+ original_module = mock_importer.MockModule(loader.module_name)
+ original_value = -13
+ assert loader.attr_value != original_value
+ setattr(original_module, loader.attr_name, original_value)
+ sys.modules[loader.module_name] = original_module
+ handler = loader._create_handler(importer.PyPycHandler)
+ new_module = loader._handle_pyc(handler)
+ self.failUnless(new_module is original_module)
+ self.failUnlessEqual(getattr(new_module, loader.attr_name),
+ loader.attr_value)
class ExtensionHandlerTests(unittest.TestCase):
More information about the Python-checkins
mailing list