comparison Python/pythonrun.c @ 86966:af822a6c9faf

Issue #19512: Add PyRun_InteractiveOneObject() function Only decode the filename once. PyRun_InteractiveOneObject() uses an identifier for "<string>" string, so the byte string is only decoded once.
author Victor Stinner <victor.stinner@gmail.com>
date Wed, 06 Nov 2013 18:41:07 +0100
parents 55517661a053
children 954167ce92a3
comparison
equal deleted inserted replaced
86965:55517661a053 86966:af822a6c9faf
71 static void initmain(PyInterpreterState *interp); 71 static void initmain(PyInterpreterState *interp);
72 static int initfsencoding(PyInterpreterState *interp); 72 static int initfsencoding(PyInterpreterState *interp);
73 static void initsite(void); 73 static void initsite(void);
74 static int initstdio(void); 74 static int initstdio(void);
75 static void flush_io(void); 75 static void flush_io(void);
76 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *, 76 static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
77 PyCompilerFlags *, PyArena *); 77 PyCompilerFlags *, PyArena *);
78 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *, 78 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
79 PyCompilerFlags *); 79 PyCompilerFlags *);
80 static void err_input(perrdetail *); 80 static void err_input(perrdetail *);
81 static void err_free(perrdetail *); 81 static void err_free(perrdetail *);
1263 else 1263 else
1264 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags); 1264 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
1265 } 1265 }
1266 1266
1267 int 1267 int
1268 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags) 1268 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
1269 { 1269 {
1270 PyObject *v; 1270 PyObject *filename, *v;
1271 int ret; 1271 int ret, err;
1272 PyCompilerFlags local_flags; 1272 PyCompilerFlags local_flags;
1273
1274 filename = PyUnicode_DecodeFSDefault(filename_str);
1275 if (filename == NULL) {
1276 PyErr_Print();
1277 return -1;
1278 }
1273 1279
1274 if (flags == NULL) { 1280 if (flags == NULL) {
1275 flags = &local_flags; 1281 flags = &local_flags;
1276 local_flags.cf_flags = 0; 1282 local_flags.cf_flags = 0;
1277 } 1283 }
1283 v = PySys_GetObject("ps2"); 1289 v = PySys_GetObject("ps2");
1284 if (v == NULL) { 1290 if (v == NULL) {
1285 PySys_SetObject("ps2", v = PyUnicode_FromString("... ")); 1291 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
1286 Py_XDECREF(v); 1292 Py_XDECREF(v);
1287 } 1293 }
1294 err = -1;
1288 for (;;) { 1295 for (;;) {
1289 ret = PyRun_InteractiveOneFlags(fp, filename, flags); 1296 ret = PyRun_InteractiveOneObject(fp, filename, flags);
1290 PRINT_TOTAL_REFS(); 1297 PRINT_TOTAL_REFS();
1291 if (ret == E_EOF) 1298 if (ret == E_EOF) {
1292 return 0; 1299 err = 0;
1300 break;
1301 }
1293 /* 1302 /*
1294 if (ret == E_NOMEM) 1303 if (ret == E_NOMEM)
1295 return -1; 1304 break;
1296 */ 1305 */
1297 } 1306 }
1307 Py_DECREF(filename);
1308 return err;
1298 } 1309 }
1299 1310
1300 /* compute parser flags based on compiler flags */ 1311 /* compute parser flags based on compiler flags */
1301 static int PARSER_FLAGS(PyCompilerFlags *flags) 1312 static int PARSER_FLAGS(PyCompilerFlags *flags)
1302 { 1313 {
1320 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \ 1331 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1321 PyPARSE_WITH_IS_KEYWORD : 0)) : 0) 1332 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
1322 #endif 1333 #endif
1323 1334
1324 int 1335 int
1325 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags) 1336 PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
1326 { 1337 {
1327 PyObject *m, *d, *v, *w, *oenc = NULL; 1338 PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name;
1328 mod_ty mod; 1339 mod_ty mod;
1329 PyArena *arena; 1340 PyArena *arena;
1330 char *ps1 = "", *ps2 = "", *enc = NULL; 1341 char *ps1 = "", *ps2 = "", *enc = NULL;
1331 int errcode = 0; 1342 int errcode = 0;
1332 _Py_IDENTIFIER(encoding); 1343 _Py_IDENTIFIER(encoding);
1344 _Py_IDENTIFIER(__main__);
1345
1346 mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */
1347 if (mod_name == NULL) {
1348 PyErr_Print();
1349 return -1;
1350 }
1333 1351
1334 if (fp == stdin) { 1352 if (fp == stdin) {
1335 /* Fetch encoding from sys.stdin if possible. */ 1353 /* Fetch encoding from sys.stdin if possible. */
1336 v = PySys_GetObject("stdin"); 1354 v = PySys_GetObject("stdin");
1337 if (v && v != Py_None) { 1355 if (v && v != Py_None) {
1373 Py_XDECREF(v); 1391 Py_XDECREF(v);
1374 Py_XDECREF(w); 1392 Py_XDECREF(w);
1375 Py_XDECREF(oenc); 1393 Py_XDECREF(oenc);
1376 return -1; 1394 return -1;
1377 } 1395 }
1378 mod = PyParser_ASTFromFile(fp, filename, enc, 1396 mod = PyParser_ASTFromFileObject(fp, filename, enc,
1379 Py_single_input, ps1, ps2, 1397 Py_single_input, ps1, ps2,
1380 flags, &errcode, arena); 1398 flags, &errcode, arena);
1381 Py_XDECREF(v); 1399 Py_XDECREF(v);
1382 Py_XDECREF(w); 1400 Py_XDECREF(w);
1383 Py_XDECREF(oenc); 1401 Py_XDECREF(oenc);
1384 if (mod == NULL) { 1402 if (mod == NULL) {
1385 PyArena_Free(arena); 1403 PyArena_Free(arena);
1388 return E_EOF; 1406 return E_EOF;
1389 } 1407 }
1390 PyErr_Print(); 1408 PyErr_Print();
1391 return -1; 1409 return -1;
1392 } 1410 }
1393 m = PyImport_AddModule("__main__"); 1411 m = PyImport_AddModuleObject(mod_name);
1394 if (m == NULL) { 1412 if (m == NULL) {
1395 PyArena_Free(arena); 1413 PyArena_Free(arena);
1396 return -1; 1414 return -1;
1397 } 1415 }
1398 d = PyModule_GetDict(m); 1416 d = PyModule_GetDict(m);
1404 return -1; 1422 return -1;
1405 } 1423 }
1406 Py_DECREF(v); 1424 Py_DECREF(v);
1407 return 0; 1425 return 0;
1408 } 1426 }
1427
1428 int
1429 PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
1430 {
1431 PyObject *filename;
1432 int res;
1433
1434 filename = PyUnicode_DecodeFSDefault(filename_str);
1435 if (filename == NULL) {
1436 PyErr_Print();
1437 return -1;
1438 }
1439 res = PyRun_InteractiveOneObject(fp, filename, flags);
1440 Py_DECREF(filename);
1441 return res;
1442 }
1443
1409 1444
1410 /* Check whether a file maybe a pyc file: Look at the extension, 1445 /* Check whether a file maybe a pyc file: Look at the extension,
1411 the file type, and, if we may close it, at the first few bytes. */ 1446 the file type, and, if we may close it, at the first few bytes. */
1412 1447
1413 static int 1448 static int
2008 PyRun_StringFlags(const char *str, int start, PyObject *globals, 2043 PyRun_StringFlags(const char *str, int start, PyObject *globals,
2009 PyObject *locals, PyCompilerFlags *flags) 2044 PyObject *locals, PyCompilerFlags *flags)
2010 { 2045 {
2011 PyObject *ret = NULL; 2046 PyObject *ret = NULL;
2012 mod_ty mod; 2047 mod_ty mod;
2013 PyArena *arena = PyArena_New(); 2048 PyArena *arena;
2049 _Py_static_string(PyId_string, "<string>");
2050 PyObject *filename;
2051
2052 filename = _PyUnicode_FromId(&PyId_string); /* borrowed */
2053 if (filename == NULL)
2054 return NULL;
2055
2056 arena = PyArena_New();
2014 if (arena == NULL) 2057 if (arena == NULL)
2015 return NULL; 2058 return NULL;
2016 2059
2017 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena); 2060 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
2018 if (mod != NULL) 2061 if (mod != NULL)
2019 ret = run_mod(mod, "<string>", globals, locals, flags, arena); 2062 ret = run_mod(mod, filename, globals, locals, flags, arena);
2020 PyArena_Free(arena); 2063 PyArena_Free(arena);
2021 return ret; 2064 return ret;
2022 } 2065 }
2023 2066
2024 PyObject * 2067 PyObject *
2025 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals, 2068 PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
2026 PyObject *locals, int closeit, PyCompilerFlags *flags) 2069 PyObject *locals, int closeit, PyCompilerFlags *flags)
2027 { 2070 {
2028 PyObject *ret; 2071 PyObject *ret = NULL;
2029 mod_ty mod; 2072 mod_ty mod;
2030 PyArena *arena = PyArena_New(); 2073 PyArena *arena = NULL;
2074 PyObject *filename;
2075
2076 filename = PyUnicode_DecodeFSDefault(filename_str);
2077 if (filename == NULL)
2078 goto exit;
2079
2080 arena = PyArena_New();
2031 if (arena == NULL) 2081 if (arena == NULL)
2032 return NULL; 2082 goto exit;
2033 2083
2034 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0, 2084 mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
2035 flags, NULL, arena); 2085 flags, NULL, arena);
2036 if (closeit) 2086 if (closeit)
2037 fclose(fp); 2087 fclose(fp);
2038 if (mod == NULL) { 2088 if (mod == NULL) {
2089 goto exit;
2090 }
2091 ret = run_mod(mod, filename, globals, locals, flags, arena);
2092
2093 exit:
2094 Py_XDECREF(filename);
2095 if (arena != NULL)
2039 PyArena_Free(arena); 2096 PyArena_Free(arena);
2040 return NULL;
2041 }
2042 ret = run_mod(mod, filename, globals, locals, flags, arena);
2043 PyArena_Free(arena);
2044 return ret; 2097 return ret;
2045 } 2098 }
2046 2099
2047 static void 2100 static void
2048 flush_io(void) 2101 flush_io(void)
2073 2126
2074 PyErr_Restore(type, value, traceback); 2127 PyErr_Restore(type, value, traceback);
2075 } 2128 }
2076 2129
2077 static PyObject * 2130 static PyObject *
2078 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals, 2131 run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
2079 PyCompilerFlags *flags, PyArena *arena) 2132 PyCompilerFlags *flags, PyArena *arena)
2080 { 2133 {
2081 PyCodeObject *co; 2134 PyCodeObject *co;
2082 PyObject *v; 2135 PyObject *v;
2083 co = PyAST_Compile(mod, filename, flags, arena); 2136 co = PyAST_CompileObject(mod, filename, flags, -1, arena);
2084 if (co == NULL) 2137 if (co == NULL)
2085 return NULL; 2138 return NULL;
2086 v = PyEval_EvalCode((PyObject*)co, globals, locals); 2139 v = PyEval_EvalCode((PyObject*)co, globals, locals);
2087 Py_DECREF(co); 2140 Py_DECREF(co);
2088 return v; 2141 return v;