comparison Python/pythonrun.c @ 80289:b256d054f229 3.2

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:02:24 +0100
parents 09408b990ca5
children 215297665098
comparison
equal deleted inserted replaced
80268:0dfa3b09a6fe 80289:b256d054f229
1255 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, 1255 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
1256 PyCompilerFlags *flags) 1256 PyCompilerFlags *flags)
1257 { 1257 {
1258 PyObject *m, *d, *v; 1258 PyObject *m, *d, *v;
1259 const char *ext; 1259 const char *ext;
1260 int set_file_name = 0, ret; 1260 int set_file_name = 0, ret = -1;
1261 size_t len; 1261 size_t len;
1262 1262
1263 m = PyImport_AddModule("__main__"); 1263 m = PyImport_AddModule("__main__");
1264 if (m == NULL) 1264 if (m == NULL)
1265 return -1; 1265 return -1;
1266 Py_INCREF(m);
1266 d = PyModule_GetDict(m); 1267 d = PyModule_GetDict(m);
1267 if (PyDict_GetItemString(d, "__file__") == NULL) { 1268 if (PyDict_GetItemString(d, "__file__") == NULL) {
1268 PyObject *f; 1269 PyObject *f;
1269 f = PyUnicode_DecodeFSDefault(filename); 1270 f = PyUnicode_DecodeFSDefault(filename);
1270 if (f == NULL) 1271 if (f == NULL)
1271 return -1; 1272 goto done;
1272 if (PyDict_SetItemString(d, "__file__", f) < 0) { 1273 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1273 Py_DECREF(f); 1274 Py_DECREF(f);
1274 return -1; 1275 goto done;
1275 } 1276 }
1276 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) { 1277 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1277 Py_DECREF(f); 1278 Py_DECREF(f);
1278 return -1; 1279 goto done;
1279 } 1280 }
1280 set_file_name = 1; 1281 set_file_name = 1;
1281 Py_DECREF(f); 1282 Py_DECREF(f);
1282 } 1283 }
1283 len = strlen(filename); 1284 len = strlen(filename);
1286 /* Try to run a pyc file. First, re-open in binary */ 1287 /* Try to run a pyc file. First, re-open in binary */
1287 if (closeit) 1288 if (closeit)
1288 fclose(fp); 1289 fclose(fp);
1289 if ((fp = fopen(filename, "rb")) == NULL) { 1290 if ((fp = fopen(filename, "rb")) == NULL) {
1290 fprintf(stderr, "python: Can't reopen .pyc file\n"); 1291 fprintf(stderr, "python: Can't reopen .pyc file\n");
1291 ret = -1;
1292 goto done; 1292 goto done;
1293 } 1293 }
1294 /* Turn on optimization if a .pyo file is given */ 1294 /* Turn on optimization if a .pyo file is given */
1295 if (strcmp(ext, ".pyo") == 0) 1295 if (strcmp(ext, ".pyo") == 0)
1296 Py_OptimizeFlag = 1; 1296 Py_OptimizeFlag = 1;
1300 closeit, flags); 1300 closeit, flags);
1301 } 1301 }
1302 flush_io(); 1302 flush_io();
1303 if (v == NULL) { 1303 if (v == NULL) {
1304 PyErr_Print(); 1304 PyErr_Print();
1305 ret = -1;
1306 goto done; 1305 goto done;
1307 } 1306 }
1308 Py_DECREF(v); 1307 Py_DECREF(v);
1309 ret = 0; 1308 ret = 0;
1310 done: 1309 done:
1311 if (set_file_name && PyDict_DelItemString(d, "__file__")) 1310 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1312 PyErr_Clear(); 1311 PyErr_Clear();
1312 Py_DECREF(m);
1313 return ret; 1313 return ret;
1314 } 1314 }
1315 1315
1316 int 1316 int
1317 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) 1317 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)