Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15c02e3

Browse files
committed
Issue #15001: fix segfault on "del sys.module['__main__']"
Patch by Victor Stinner.
2 parents 99dd8b5 + 33363f4 commit 15c02e3

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,18 @@ def test_hash_randomization(self):
375375
self.assertEqual(rc, 0)
376376
self.assertIn(b'random is 1', out)
377377

378+
def test_del___main__(self):
379+
# Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a
380+
# borrowed reference to the dict of __main__ module and later modify
381+
# the dict whereas the module was destroyed
382+
filename = test.support.TESTFN
383+
self.addCleanup(test.support.unlink, filename)
384+
with open(filename, "w") as script:
385+
print("import sys", file=script)
386+
print("del sys.modules['__main__']", file=script)
387+
assert_python_ok(filename)
388+
389+
378390
def test_main():
379391
test.support.run_unittest(CmdLineTest)
380392
test.support.reap_children()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.4.0 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #15001: fix segfault on "del sys.module['__main__']". Patch by Victor
14+
Stinner.
15+
1316
- Issue #8271: the utf-8 decoder now outputs the correct number of U+FFFD
1417
characters when used with the 'replace' error handler on invalid utf-8
1518
sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.

Python/pythonrun.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,25 +1390,26 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
13901390
{
13911391
PyObject *m, *d, *v;
13921392
const char *ext;
1393-
int set_file_name = 0, ret;
1393+
int set_file_name = 0, ret = -1;
13941394
size_t len;
13951395

13961396
m = PyImport_AddModule("__main__");
13971397
if (m == NULL)
13981398
return -1;
1399+
Py_INCREF(m);
13991400
d = PyModule_GetDict(m);
14001401
if (PyDict_GetItemString(d, "__file__") == NULL) {
14011402
PyObject *f;
14021403
f = PyUnicode_DecodeFSDefault(filename);
14031404
if (f == NULL)
1404-
return -1;
1405+
goto done;
14051406
if (PyDict_SetItemString(d, "__file__", f) < 0) {
14061407
Py_DECREF(f);
1407-
return -1;
1408+
goto done;
14081409
}
14091410
if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
14101411
Py_DECREF(f);
1411-
return -1;
1412+
goto done;
14121413
}
14131414
set_file_name = 1;
14141415
Py_DECREF(f);
@@ -1422,7 +1423,6 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
14221423
fclose(fp);
14231424
if ((pyc_fp = fopen(filename, "rb")) == NULL) {
14241425
fprintf(stderr, "python: Can't reopen .pyc file\n");
1425-
ret = -1;
14261426
goto done;
14271427
}
14281428
/* Turn on optimization if a .pyo file is given */
@@ -1451,14 +1451,14 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
14511451
flush_io();
14521452
if (v == NULL) {
14531453
PyErr_Print();
1454-
ret = -1;
14551454
goto done;
14561455
}
14571456
Py_DECREF(v);
14581457
ret = 0;
14591458
done:
14601459
if (set_file_name && PyDict_DelItemString(d, "__file__"))
14611460
PyErr_Clear();
1461+
Py_DECREF(m);
14621462
return ret;
14631463
}
14641464

0 commit comments

Comments
 (0)