Commit 1e8ae990 authored by R. David Murray's avatar R. David Murray

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

Patch by Simon Cross, crasher test code by Martin von Löwis.
parent 252165d5
...@@ -9,11 +9,13 @@ import random ...@@ -9,11 +9,13 @@ import random
import stat import stat
import sys import sys
import unittest import unittest
import textwrap
from test.support import ( from test.support import (
EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython, EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython,
make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask, make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask,
unlink, unload) unlink, unload)
from test import script_helper
def remove_files(name): def remove_files(name):
...@@ -284,6 +286,17 @@ class ImportTests(unittest.TestCase): ...@@ -284,6 +286,17 @@ class ImportTests(unittest.TestCase):
self.assertEqual("Import by filename is not supported.", self.assertEqual("Import by filename is not supported.",
c.exception.args[0]) c.exception.args[0])
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): class PycRewritingTests(unittest.TestCase):
# Test that the `co_filename` attribute on code objects always points # Test that the `co_filename` attribute on code objects always points
......
...@@ -7,6 +7,12 @@ What's New in Python 3.2 Beta 2? ...@@ -7,6 +7,12 @@ What's New in Python 3.2 Beta 2?
*Release date: XXXX-XX-XX* *Release date: XXXX-XX-XX*
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__.
Library Library
------- -------
......
...@@ -63,8 +63,9 @@ PyModule_Create2(struct PyModuleDef* module, int module_api_version) ...@@ -63,8 +63,9 @@ PyModule_Create2(struct PyModuleDef* module, int module_api_version)
PyMethodDef *ml; PyMethodDef *ml;
const char* name; const char* name;
PyModuleObject *m; PyModuleObject *m;
if (!Py_IsInitialized()) PyInterpreterState *interp = PyThreadState_Get()->interp;
Py_FatalError("Interpreter not initialized (version mismatch?)"); if (interp->modules == NULL)
Py_FatalError("Python import machinery not initialized");
if (PyType_Ready(&moduledef_type) < 0) if (PyType_Ready(&moduledef_type) < 0)
return NULL; return NULL;
if (module->m_base.m_index == 0) { if (module->m_base.m_index == 0) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment