[Python-checkins] cpython (3.2): Issue #15001: fix segfault on "del sys.module['__main__']"

hynek.schlawack python-checkins at python.org
Wed Nov 7 09:14:30 CET 2012


http://hg.python.org/cpython/rev/b256d054f229
changeset:   80289:b256d054f229
branch:      3.2
parent:      80268:0dfa3b09a6fe
user:        Hynek Schlawack <hs at ox.cx>
date:        Wed Nov 07 09:02:24 2012 +0100
summary:
  Issue #15001: fix segfault on "del sys.module['__main__']"

Patch by Victor Stinner.

files:
  Lib/test/test_cmd_line.py |  12 ++++++++++++
  Misc/NEWS                 |   3 +++
  Python/pythonrun.c        |  12 ++++++------
  3 files changed, 21 insertions(+), 6 deletions(-)


diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -364,6 +364,18 @@
         self.assertEqual(rc, 0)
         self.assertIn(b'random is 1', out)
 
+    def test_del___main__(self):
+        # Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a
+        # borrowed reference to the dict of __main__ module and later modify
+        # the dict whereas the module was destroyed
+        filename = test.support.TESTFN
+        self.addCleanup(test.support.unlink, filename)
+        with open(filename, "w") as script:
+            print("import sys", file=script)
+            print("del sys.modules['__main__']", file=script)
+        assert_python_ok(filename)
+
+
 def test_main():
     test.support.run_unittest(CmdLineTest)
     test.support.reap_children()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #15001: fix segfault on "del sys.module['__main__']". Patch by Victor
+  Stinner.
+
 - Issue #5057: the peepholer no longer optimizes subscription on unicode
   literals (e.g. u'foo'[0]) in order to produce compatible pyc files between
   narrow and wide builds.
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -1257,25 +1257,26 @@
 {
     PyObject *m, *d, *v;
     const char *ext;
-    int set_file_name = 0, ret;
+    int set_file_name = 0, ret = -1;
     size_t len;
 
     m = PyImport_AddModule("__main__");
     if (m == NULL)
         return -1;
+    Py_INCREF(m);
     d = PyModule_GetDict(m);
     if (PyDict_GetItemString(d, "__file__") == NULL) {
         PyObject *f;
         f = PyUnicode_DecodeFSDefault(filename);
         if (f == NULL)
-            return -1;
+            goto done;
         if (PyDict_SetItemString(d, "__file__", f) < 0) {
             Py_DECREF(f);
-            return -1;
+            goto done;
         }
         if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
             Py_DECREF(f);
-            return -1;
+            goto done;
         }
         set_file_name = 1;
         Py_DECREF(f);
@@ -1288,7 +1289,6 @@
             fclose(fp);
         if ((fp = fopen(filename, "rb")) == NULL) {
             fprintf(stderr, "python: Can't reopen .pyc file\n");
-            ret = -1;
             goto done;
         }
         /* Turn on optimization if a .pyo file is given */
@@ -1302,7 +1302,6 @@
     flush_io();
     if (v == NULL) {
         PyErr_Print();
-        ret = -1;
         goto done;
     }
     Py_DECREF(v);
@@ -1310,6 +1309,7 @@
   done:
     if (set_file_name && PyDict_DelItemString(d, "__file__"))
         PyErr_Clear();
+    Py_DECREF(m);
     return ret;
 }
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list