[pypy-commit] pypy default: hg merge issue1514

arigo noreply at buildbot.pypy.org
Wed Apr 16 17:40:13 CEST 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r70665:c2c709110379
Date: 2014-04-16 17:38 +0200
http://bitbucket.org/pypy/pypy/changeset/c2c709110379/

Log:	hg merge issue1514

	 Support strange manipulations of sys.modules (thanks yamt)
	involving deleting built-in modules from sys.modules and
	reimporting them. Required by eventlet.

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -443,10 +443,11 @@
 
         return name
 
-    def getbuiltinmodule(self, name, force_init=False):
+    def getbuiltinmodule(self, name, force_init=False, reuse=True):
         w_name = self.wrap(name)
         w_modules = self.sys.get('modules')
         if not force_init:
+            assert reuse
             try:
                 return self.getitem(w_modules, w_name)
             except OperationError, e:
@@ -462,9 +463,20 @@
                         "getbuiltinmodule() called with non-builtin module %s",
                         name)
         else:
-            # Initialize the module
+            # Add the module to sys.modules and initialize the module
+            # The order is important to avoid recursions.
             from pypy.interpreter.module import Module
             if isinstance(w_mod, Module):
+                if not reuse and w_mod.startup_called:
+                    # create a copy of the module.  (see issue1514)
+                    # eventlet patcher relies on this behaviour.
+                    w_mod2 = self.wrap(Module(self, w_name))
+                    self.setitem(w_modules, w_name, w_mod2)
+                    w_mod.getdict(self)  # unlazy w_initialdict
+                    self.call_method(w_mod2.getdict(self), 'update',
+                                     w_mod.w_initialdict)
+                    return w_mod2
+                #
                 w_mod.init(self)
 
             # Add the module to sys.modules
diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py
--- a/pypy/module/imp/importing.py
+++ b/pypy/module/imp/importing.py
@@ -579,7 +579,8 @@
         return space.call_method(find_info.w_loader, "load_module", w_modulename)
 
     if find_info.modtype == C_BUILTIN:
-        return space.getbuiltinmodule(find_info.filename, force_init=True)
+        return space.getbuiltinmodule(find_info.filename, force_init=True,
+                                      reuse=reuse)
 
     if find_info.modtype in (PY_SOURCE, PY_COMPILED, C_EXTENSION, PKG_DIRECTORY):
         w_mod = None
diff --git a/pypy/module/imp/test/test_app.py b/pypy/module/imp/test/test_app.py
--- a/pypy/module/imp/test/test_app.py
+++ b/pypy/module/imp/test/test_app.py
@@ -203,7 +203,6 @@
 
     def test_builtin_reimport(self):
         # from https://bugs.pypy.org/issue1514
-        skip("fix me")
         import sys, marshal
 
         old = marshal.loads
@@ -223,7 +222,6 @@
         # taken from https://bugs.pypy.org/issue1514, with extra cases
         # that show a difference with CPython: we can get on CPython
         # several module objects for the same built-in module :-(
-        skip("several built-in module objects: not supported by pypy")
         import sys, marshal
 
         old = marshal.loads
diff --git a/pypy/module/imp/test/test_import.py b/pypy/module/imp/test/test_import.py
--- a/pypy/module/imp/test/test_import.py
+++ b/pypy/module/imp/test/test_import.py
@@ -578,7 +578,6 @@
         assert hasattr(time, 'clock')
 
     def test_reimport_builtin_simple_case_2(self):
-        skip("fix me")
         import sys, time
         time.foo = "bar"
         del sys.modules['time']
@@ -586,7 +585,6 @@
         assert not hasattr(time, 'foo')
 
     def test_reimport_builtin(self):
-        skip("fix me")
         import sys, time
         oldpath = sys.path
         time.tzset = "<test_reimport_builtin removed this>"


More information about the pypy-commit mailing list