]> granicus.if.org Git - python/commitdiff
Issue #15578: Correctly incref the parent module while importing.
authorEric Snow <ericsnowcurrently@gmail.com>
Thu, 8 Sep 2016 02:08:02 +0000 (19:08 -0700)
committerEric Snow <ericsnowcurrently@gmail.com>
Thu, 8 Sep 2016 02:08:02 +0000 (19:08 -0700)
Lib/test/test_import.py
Misc/NEWS
Python/import.c

index c17da100113d13717c0afbeef3e9229cfe6a9ebb..2abf5b29400363708e227f8444db23a3700a3f35 100644 (file)
@@ -397,6 +397,24 @@ class ImportTests(unittest.TestCase):
         finally:
             sys.path.pop(0)
 
+    def test_replace_parent_in_sys_modules(self):
+        dir_name = os.path.abspath(TESTFN)
+        os.mkdir(dir_name)
+        try:
+            pkg_dir = os.path.join(dir_name, 'sa')
+            os.mkdir(pkg_dir)
+            with open(os.path.join(pkg_dir, '__init__.py'), 'w') as init_file:
+                init_file.write("import v1")
+            with open(os.path.join(pkg_dir, 'v1.py'), 'w') as v1_file:
+                v1_file.write("import sys;"
+                              "sys.modules['sa'] = sys.modules[__name__];"
+                              "import sa")
+            sys.path.insert(0, dir_name)
+            # a segfault means the test failed!
+            import sa
+        finally:
+            rmtree(dir_name)
+
 
 class PycRewritingTests(unittest.TestCase):
     # Test that the `co_filename` attribute on code objects always points
index 8e235868dfb24f2cf0ee64a36a7451373b2a2516..b4538424e210a5053c3dfd353513a53e68d68ba4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -7082,6 +7082,8 @@ Core and Builtins
 
 - Add Py3k warnings for parameter names in parentheses.
 
+- Issue #15578: Correctly incref the parent module while importing.
+
 - Issue #7362: Give a proper error message for ``def f((x)=3): pass``.
 
 - Issue #7085: Fix crash when importing some extensions in a thread on MacOSX
index 96f7d47544e94beeb8b9112590c58dd02aac9e29..af47b0bdb4db393b35b8f00b1c939ee8e2a9f524 100644 (file)
@@ -2243,8 +2243,10 @@ import_module_level(char *name, PyObject *globals, PyObject *locals,
     if (parent == NULL)
         goto error_exit;
 
+    Py_INCREF(parent);
     head = load_next(parent, level < 0 ? Py_None : parent, &name, buf,
                         &buflen);
+    Py_DECREF(parent);
     if (head == NULL)
         goto error_exit;