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

Skip to content

Commit 5c6b3e2

Browse files
committed
Issue #15001: fix segfault on "del sys.module['__main__']"
Patch by Victor Stinner.
1 parent 692b023 commit 5c6b3e2

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
@@ -364,6 +364,18 @@ def test_hash_randomization(self):
364364
self.assertEqual(rc, 0)
365365
self.assertIn(b'random is 1', out)
366366

367+
def test_del___main__(self):
368+
# Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a
369+
# borrowed reference to the dict of __main__ module and later modify
370+
# the dict whereas the module was destroyed
371+
filename = test.support.TESTFN
372+
self.addCleanup(test.support.unlink, filename)
373+
with open(filename, "w") as script:
374+
print("import sys", file=script)
375+
print("del sys.modules['__main__']", file=script)
376+
assert_python_ok(filename)
377+
378+
367379
def test_main():
368380
test.support.run_unittest(CmdLineTest)
369381
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.2.4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #15001: fix segfault on "del sys.module['__main__']". Patch by Victor
14+
Stinner.
15+
1316
- Issue #5057: the peepholer no longer optimizes subscription on unicode
1417
literals (e.g. u'foo'[0]) in order to produce compatible pyc files between
1518
narrow and wide builds.

Python/pythonrun.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,25 +1257,26 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
12571257
{
12581258
PyObject *m, *d, *v;
12591259
const char *ext;
1260-
int set_file_name = 0, ret;
1260+
int set_file_name = 0, ret = -1;
12611261
size_t len;
12621262

12631263
m = PyImport_AddModule("__main__");
12641264
if (m == NULL)
12651265
return -1;
1266+
Py_INCREF(m);
12661267
d = PyModule_GetDict(m);
12671268
if (PyDict_GetItemString(d, "__file__") == NULL) {
12681269
PyObject *f;
12691270
f = PyUnicode_DecodeFSDefault(filename);
12701271
if (f == NULL)
1271-
return -1;
1272+
goto done;
12721273
if (PyDict_SetItemString(d, "__file__", f) < 0) {
12731274
Py_DECREF(f);
1274-
return -1;
1275+
goto done;
12751276
}
12761277
if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
12771278
Py_DECREF(f);
1278-
return -1;
1279+
goto done;
12791280
}
12801281
set_file_name = 1;
12811282
Py_DECREF(f);
@@ -1288,7 +1289,6 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
12881289
fclose(fp);
12891290
if ((fp = fopen(filename, "rb")) == NULL) {
12901291
fprintf(stderr, "python: Can't reopen .pyc file\n");
1291-
ret = -1;
12921292
goto done;
12931293
}
12941294
/* Turn on optimization if a .pyo file is given */
@@ -1302,14 +1302,14 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
13021302
flush_io();
13031303
if (v == NULL) {
13041304
PyErr_Print();
1305-
ret = -1;
13061305
goto done;
13071306
}
13081307
Py_DECREF(v);
13091308
ret = 0;
13101309
done:
13111310
if (set_file_name && PyDict_DelItemString(d, "__file__"))
13121311
PyErr_Clear();
1312+
Py_DECREF(m);
13131313
return ret;
13141314
}
13151315

0 commit comments

Comments
 (0)