]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 87251 via svnmerge from
authorR. David Murray <rdmurray@bitdance.com>
Tue, 14 Dec 2010 23:30:42 +0000 (23:30 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Tue, 14 Dec 2010 23:30:42 +0000 (23:30 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r87251 | r.david.murray | 2010-12-14 18:06:25 -0500 (Tue, 14 Dec 2010) | 4 lines

  #4236: avoid possible Fatal Error when import is called from __del__

  Patch by Simon Cross, crasher test code by Martin von Löwis.
........

Lib/test/test_import.py
Misc/NEWS
Objects/moduleobject.c

index e50cd5340f77d149e551578fd75c1cc00a19b1e7..6f935c758105e9d9dd234a4e6362703855f63a8a 100644 (file)
@@ -9,10 +9,12 @@ import stat
 import sys
 import unittest
 import warnings
+import textwrap
 
 from test.support import (
     EnvironmentVarGuard, TESTFN, forget, is_jython,
     rmtree, run_unittest, unlink, unload)
+from test import script_helper
 
 
 def remove_files(name):
@@ -284,6 +286,17 @@ class ImportTests(unittest.TestCase):
         else:
             self.fail("import by path didn't raise an exception")
 
+    def test_import_in_del_does_not_crash(self):
+        # Issue 4236
+        testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\
+            import sys
+            class C:
+               def __del__(self):
+                  import imp
+            sys.argv.insert(0, C())
+            """))
+        script_helper.assert_python_ok(testfn)
+
 
 class PycRewritingTests(unittest.TestCase):
     # Test that the `co_filename` attribute on code objects always points
index 6b6dcba60ab62f02355f0b2a8353e79b8778fbe8..cd670e7850fb04ddaddb7d8b8a17f5d8f37290ec 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 3.1.4?
 Core and Builtins
 -----------------
 
+- Issue #4236: PyModule_Create2 now checks the import machinery directly
+  rather than the Py_IsInitialized flag, avoiding a Fatal Python
+  error in certain circumstances when an import is done in __del__.
+
 - Issue #10596: Fix float.__mod__ to have the same behaviour as
   float.__divmod__ with respect to signed zeros.  -4.0 % 4.0 should be
   0.0, not -0.0.
index 5dd9c017e05a201ace075a0510fb521223776268..d96d7bd16a1a4e92d02e84c9809eb47212c7e8f9 100644 (file)
@@ -67,8 +67,9 @@ PyModule_Create2(struct PyModuleDef* module, int module_api_version)
     PyMethodDef *ml;
     const char* name;
     PyModuleObject *m;
-    if (!Py_IsInitialized())
-        Py_FatalError("Interpreter not initialized (version mismatch?)");
+    PyInterpreterState *interp = PyThreadState_Get()->interp;
+    if (interp->modules == NULL)
+        Py_FatalError("Python import machinery not initialized");
     if (PyType_Ready(&moduledef_type) < 0)
         return NULL;
     if (module->m_base.m_index == 0) {