Mercurial > cpython-lru_cache-weakref
comparison Python/pythonrun.c @ 80290:215297665098 3.3
Issue #15001: fix segfault on "del sys.module['__main__']"
Patch by Victor Stinner.
| author | Hynek Schlawack <hs@ox.cx> |
|---|---|
| date | Wed, 07 Nov 2012 09:07:22 +0100 |
| parents | 02d25098ad57 b256d054f229 |
| children | 68d1ac152b5d c40f4c19d20b |
comparison
equal
deleted
inserted
replaced
| 80282:55cc29d7e83a | 80290:215297665098 |
|---|---|
| 1388 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, | 1388 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, |
| 1389 PyCompilerFlags *flags) | 1389 PyCompilerFlags *flags) |
| 1390 { | 1390 { |
| 1391 PyObject *m, *d, *v; | 1391 PyObject *m, *d, *v; |
| 1392 const char *ext; | 1392 const char *ext; |
| 1393 int set_file_name = 0, ret; | 1393 int set_file_name = 0, ret = -1; |
| 1394 size_t len; | 1394 size_t len; |
| 1395 | 1395 |
| 1396 m = PyImport_AddModule("__main__"); | 1396 m = PyImport_AddModule("__main__"); |
| 1397 if (m == NULL) | 1397 if (m == NULL) |
| 1398 return -1; | 1398 return -1; |
| 1399 Py_INCREF(m); | |
| 1399 d = PyModule_GetDict(m); | 1400 d = PyModule_GetDict(m); |
| 1400 if (PyDict_GetItemString(d, "__file__") == NULL) { | 1401 if (PyDict_GetItemString(d, "__file__") == NULL) { |
| 1401 PyObject *f; | 1402 PyObject *f; |
| 1402 f = PyUnicode_DecodeFSDefault(filename); | 1403 f = PyUnicode_DecodeFSDefault(filename); |
| 1403 if (f == NULL) | 1404 if (f == NULL) |
| 1404 return -1; | 1405 goto done; |
| 1405 if (PyDict_SetItemString(d, "__file__", f) < 0) { | 1406 if (PyDict_SetItemString(d, "__file__", f) < 0) { |
| 1406 Py_DECREF(f); | 1407 Py_DECREF(f); |
| 1407 return -1; | 1408 goto done; |
| 1408 } | 1409 } |
| 1409 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) { | 1410 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) { |
| 1410 Py_DECREF(f); | 1411 Py_DECREF(f); |
| 1411 return -1; | 1412 goto done; |
| 1412 } | 1413 } |
| 1413 set_file_name = 1; | 1414 set_file_name = 1; |
| 1414 Py_DECREF(f); | 1415 Py_DECREF(f); |
| 1415 } | 1416 } |
| 1416 len = strlen(filename); | 1417 len = strlen(filename); |
| 1420 /* Try to run a pyc file. First, re-open in binary */ | 1421 /* Try to run a pyc file. First, re-open in binary */ |
| 1421 if (closeit) | 1422 if (closeit) |
| 1422 fclose(fp); | 1423 fclose(fp); |
| 1423 if ((pyc_fp = fopen(filename, "rb")) == NULL) { | 1424 if ((pyc_fp = fopen(filename, "rb")) == NULL) { |
| 1424 fprintf(stderr, "python: Can't reopen .pyc file\n"); | 1425 fprintf(stderr, "python: Can't reopen .pyc file\n"); |
| 1425 ret = -1; | |
| 1426 goto done; | 1426 goto done; |
| 1427 } | 1427 } |
| 1428 /* Turn on optimization if a .pyo file is given */ | 1428 /* Turn on optimization if a .pyo file is given */ |
| 1429 if (strcmp(ext, ".pyo") == 0) | 1429 if (strcmp(ext, ".pyo") == 0) |
| 1430 Py_OptimizeFlag = 1; | 1430 Py_OptimizeFlag = 1; |
| 1449 closeit, flags); | 1449 closeit, flags); |
| 1450 } | 1450 } |
| 1451 flush_io(); | 1451 flush_io(); |
| 1452 if (v == NULL) { | 1452 if (v == NULL) { |
| 1453 PyErr_Print(); | 1453 PyErr_Print(); |
| 1454 ret = -1; | |
| 1455 goto done; | 1454 goto done; |
| 1456 } | 1455 } |
| 1457 Py_DECREF(v); | 1456 Py_DECREF(v); |
| 1458 ret = 0; | 1457 ret = 0; |
| 1459 done: | 1458 done: |
| 1460 if (set_file_name && PyDict_DelItemString(d, "__file__")) | 1459 if (set_file_name && PyDict_DelItemString(d, "__file__")) |
| 1461 PyErr_Clear(); | 1460 PyErr_Clear(); |
| 1461 Py_DECREF(m); | |
| 1462 return ret; | 1462 return ret; |
| 1463 } | 1463 } |
| 1464 | 1464 |
| 1465 int | 1465 int |
| 1466 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) | 1466 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) |
