comparison Python/pythonrun.c @ 86974:954167ce92a3

Issue #19512: add some common identifiers to only create common strings once, instead of creating temporary Unicode string objects Add also more identifiers in pythonrun.c to avoid temporary Unicode string objets for the interactive interpreter.
author Victor Stinner <victor.stinner@gmail.com>
date Wed, 06 Nov 2013 22:41:44 +0100
parents af822a6c9faf
children e9a865620788
comparison
equal deleted inserted replaced
86973:6348764bacdd 86974:954167ce92a3
32 #ifdef MS_WINDOWS 32 #ifdef MS_WINDOWS
33 #undef BYTE 33 #undef BYTE
34 #include "windows.h" 34 #include "windows.h"
35 #define PATH_MAX MAXPATHLEN 35 #define PATH_MAX MAXPATHLEN
36 #endif 36 #endif
37
38 /* Common identifiers */
39 _Py_Identifier _PyId_argv = _Py_static_string_init("argv");
40 _Py_Identifier _PyId_path = _Py_static_string_init("path");
41 _Py_Identifier _PyId_stdin = _Py_static_string_init("stdin");
42 _Py_Identifier _PyId_stdout = _Py_static_string_init("stdout");
43 _Py_Identifier _PyId_stderr = _Py_static_string_init("stderr");
44
45 /* local identifiers */
46 _Py_IDENTIFIER(excepthook);
47 _Py_IDENTIFIER(ps1);
48 _Py_IDENTIFIER(ps2);
49 _Py_IDENTIFIER(last_type);
50 _Py_IDENTIFIER(last_value);
51 _Py_IDENTIFIER(last_traceback);
37 52
38 #ifdef Py_REF_DEBUG 53 #ifdef Py_REF_DEBUG
39 static 54 static
40 void _print_total_refs(void) { 55 void _print_total_refs(void) {
41 PyObject *xoptions, *value; 56 PyObject *xoptions, *value;
410 /* Set up a preliminary stderr printer until we have enough 425 /* Set up a preliminary stderr printer until we have enough
411 infrastructure for the io module in place. */ 426 infrastructure for the io module in place. */
412 pstderr = PyFile_NewStdPrinter(fileno(stderr)); 427 pstderr = PyFile_NewStdPrinter(fileno(stderr));
413 if (pstderr == NULL) 428 if (pstderr == NULL)
414 Py_FatalError("Py_Initialize: can't set preliminary stderr"); 429 Py_FatalError("Py_Initialize: can't set preliminary stderr");
415 PySys_SetObject("stderr", pstderr); 430 _PySys_SetObjectId(&_PyId_stderr, pstderr);
416 PySys_SetObject("__stderr__", pstderr); 431 PySys_SetObject("__stderr__", pstderr);
417 Py_DECREF(pstderr); 432 Py_DECREF(pstderr);
418 433
419 _PyImport_Init(); 434 _PyImport_Init();
420 435
495 } 510 }
496 511
497 static void 512 static void
498 flush_std_files(void) 513 flush_std_files(void)
499 { 514 {
500 PyObject *fout = PySys_GetObject("stdout"); 515 PyObject *fout = _PySys_GetObjectId(&_PyId_stdout);
501 PyObject *ferr = PySys_GetObject("stderr"); 516 PyObject *ferr = _PySys_GetObjectId(&_PyId_stderr);
502 PyObject *tmp; 517 PyObject *tmp;
503 _Py_IDENTIFIER(flush); 518 _Py_IDENTIFIER(flush);
504 519
505 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) { 520 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
506 tmp = _PyObject_CallMethodId(fout, &PyId_flush, ""); 521 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
774 /* Set up a preliminary stderr printer until we have enough 789 /* Set up a preliminary stderr printer until we have enough
775 infrastructure for the io module in place. */ 790 infrastructure for the io module in place. */
776 pstderr = PyFile_NewStdPrinter(fileno(stderr)); 791 pstderr = PyFile_NewStdPrinter(fileno(stderr));
777 if (pstderr == NULL) 792 if (pstderr == NULL)
778 Py_FatalError("Py_Initialize: can't set preliminary stderr"); 793 Py_FatalError("Py_Initialize: can't set preliminary stderr");
779 PySys_SetObject("stderr", pstderr); 794 _PySys_SetObjectId(&_PyId_stderr, pstderr);
780 PySys_SetObject("__stderr__", pstderr); 795 PySys_SetObject("__stderr__", pstderr);
781 Py_DECREF(pstderr); 796 Py_DECREF(pstderr);
782 797
783 _PyImportHooks_Init(); 798 _PyImportHooks_Init();
784 799
1168 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); 1183 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
1169 if (std == NULL) 1184 if (std == NULL)
1170 goto error; 1185 goto error;
1171 } /* if (fd < 0) */ 1186 } /* if (fd < 0) */
1172 PySys_SetObject("__stdin__", std); 1187 PySys_SetObject("__stdin__", std);
1173 PySys_SetObject("stdin", std); 1188 _PySys_SetObjectId(&_PyId_stdin, std);
1174 Py_DECREF(std); 1189 Py_DECREF(std);
1175 1190
1176 /* Set sys.stdout */ 1191 /* Set sys.stdout */
1177 fd = fileno(stdout); 1192 fd = fileno(stdout);
1178 if (!is_valid_fd(fd)) { 1193 if (!is_valid_fd(fd)) {
1183 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); 1198 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
1184 if (std == NULL) 1199 if (std == NULL)
1185 goto error; 1200 goto error;
1186 } /* if (fd < 0) */ 1201 } /* if (fd < 0) */
1187 PySys_SetObject("__stdout__", std); 1202 PySys_SetObject("__stdout__", std);
1188 PySys_SetObject("stdout", std); 1203 _PySys_SetObjectId(&_PyId_stdout, std);
1189 Py_DECREF(std); 1204 Py_DECREF(std);
1190 1205
1191 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ 1206 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
1192 /* Set sys.stderr, replaces the preliminary stderr */ 1207 /* Set sys.stderr, replaces the preliminary stderr */
1193 fd = fileno(stderr); 1208 fd = fileno(stderr);
1217 1232
1218 if (PySys_SetObject("__stderr__", std) < 0) { 1233 if (PySys_SetObject("__stderr__", std) < 0) {
1219 Py_DECREF(std); 1234 Py_DECREF(std);
1220 goto error; 1235 goto error;
1221 } 1236 }
1222 if (PySys_SetObject("stderr", std) < 0) { 1237 if (_PySys_SetObjectId(&_PyId_stderr, std) < 0) {
1223 Py_DECREF(std); 1238 Py_DECREF(std);
1224 goto error; 1239 goto error;
1225 } 1240 }
1226 Py_DECREF(std); 1241 Py_DECREF(std);
1227 #endif 1242 #endif
1279 1294
1280 if (flags == NULL) { 1295 if (flags == NULL) {
1281 flags = &local_flags; 1296 flags = &local_flags;
1282 local_flags.cf_flags = 0; 1297 local_flags.cf_flags = 0;
1283 } 1298 }
1284 v = PySys_GetObject("ps1"); 1299 v = _PySys_GetObjectId(&PyId_ps1);
1285 if (v == NULL) { 1300 if (v == NULL) {
1286 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> ")); 1301 _PySys_SetObjectId(&PyId_ps1, v = PyUnicode_FromString(">>> "));
1287 Py_XDECREF(v); 1302 Py_XDECREF(v);
1288 } 1303 }
1289 v = PySys_GetObject("ps2"); 1304 v = _PySys_GetObjectId(&PyId_ps2);
1290 if (v == NULL) { 1305 if (v == NULL) {
1291 PySys_SetObject("ps2", v = PyUnicode_FromString("... ")); 1306 _PySys_SetObjectId(&PyId_ps2, v = PyUnicode_FromString("... "));
1292 Py_XDECREF(v); 1307 Py_XDECREF(v);
1293 } 1308 }
1294 err = -1; 1309 err = -1;
1295 for (;;) { 1310 for (;;) {
1296 ret = PyRun_InteractiveOneObject(fp, filename, flags); 1311 ret = PyRun_InteractiveOneObject(fp, filename, flags);
1349 return -1; 1364 return -1;
1350 } 1365 }
1351 1366
1352 if (fp == stdin) { 1367 if (fp == stdin) {
1353 /* Fetch encoding from sys.stdin if possible. */ 1368 /* Fetch encoding from sys.stdin if possible. */
1354 v = PySys_GetObject("stdin"); 1369 v = _PySys_GetObjectId(&_PyId_stdin);
1355 if (v && v != Py_None) { 1370 if (v && v != Py_None) {
1356 oenc = _PyObject_GetAttrId(v, &PyId_encoding); 1371 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
1357 if (oenc) 1372 if (oenc)
1358 enc = _PyUnicode_AsString(oenc); 1373 enc = _PyUnicode_AsString(oenc);
1359 if (!enc) 1374 if (!enc)
1360 PyErr_Clear(); 1375 PyErr_Clear();
1361 } 1376 }
1362 } 1377 }
1363 v = PySys_GetObject("ps1"); 1378 v = _PySys_GetObjectId(&PyId_ps1);
1364 if (v != NULL) { 1379 if (v != NULL) {
1365 v = PyObject_Str(v); 1380 v = PyObject_Str(v);
1366 if (v == NULL) 1381 if (v == NULL)
1367 PyErr_Clear(); 1382 PyErr_Clear();
1368 else if (PyUnicode_Check(v)) { 1383 else if (PyUnicode_Check(v)) {
1371 PyErr_Clear(); 1386 PyErr_Clear();
1372 ps1 = ""; 1387 ps1 = "";
1373 } 1388 }
1374 } 1389 }
1375 } 1390 }
1376 w = PySys_GetObject("ps2"); 1391 w = _PySys_GetObjectId(&PyId_ps2);
1377 if (w != NULL) { 1392 if (w != NULL) {
1378 w = PyObject_Str(w); 1393 w = PyObject_Str(w);
1379 if (w == NULL) 1394 if (w == NULL)
1380 PyErr_Clear(); 1395 PyErr_Clear();
1381 else if (PyUnicode_Check(w)) { 1396 else if (PyUnicode_Check(w)) {
1750 just let the else clause below print the error. */ 1765 just let the else clause below print the error. */
1751 } 1766 }
1752 if (PyLong_Check(value)) 1767 if (PyLong_Check(value))
1753 exitcode = (int)PyLong_AsLong(value); 1768 exitcode = (int)PyLong_AsLong(value);
1754 else { 1769 else {
1755 PyObject *sys_stderr = PySys_GetObject("stderr"); 1770 PyObject *sys_stderr = _PySys_GetObjectId(&_PyId_stderr);
1756 if (sys_stderr != NULL && sys_stderr != Py_None) { 1771 if (sys_stderr != NULL && sys_stderr != Py_None) {
1757 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW); 1772 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1758 } else { 1773 } else {
1759 PyObject_Print(value, stderr, Py_PRINT_RAW); 1774 PyObject_Print(value, stderr, Py_PRINT_RAW);
1760 fflush(stderr); 1775 fflush(stderr);
1793 PyException_SetTraceback(v, tb); 1808 PyException_SetTraceback(v, tb);
1794 if (exception == NULL) 1809 if (exception == NULL)
1795 return; 1810 return;
1796 /* Now we know v != NULL too */ 1811 /* Now we know v != NULL too */
1797 if (set_sys_last_vars) { 1812 if (set_sys_last_vars) {
1798 PySys_SetObject("last_type", exception); 1813 _PySys_SetObjectId(&PyId_last_type, exception);
1799 PySys_SetObject("last_value", v); 1814 _PySys_SetObjectId(&PyId_last_value, v);
1800 PySys_SetObject("last_traceback", tb); 1815 _PySys_SetObjectId(&PyId_last_traceback, tb);
1801 } 1816 }
1802 hook = PySys_GetObject("excepthook"); 1817 hook = _PySys_GetObjectId(&PyId_excepthook);
1803 if (hook) { 1818 if (hook) {
1804 PyObject *args = PyTuple_Pack(3, exception, v, tb); 1819 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1805 PyObject *result = PyEval_CallObject(hook, args); 1820 PyObject *result = PyEval_CallObject(hook, args);
1806 if (result == NULL) { 1821 if (result == NULL) {
1807 PyObject *exception2, *v2, *tb2; 1822 PyObject *exception2, *v2, *tb2;
2007 2022
2008 void 2023 void
2009 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb) 2024 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
2010 { 2025 {
2011 PyObject *seen; 2026 PyObject *seen;
2012 PyObject *f = PySys_GetObject("stderr"); 2027 PyObject *f = _PySys_GetObjectId(&_PyId_stderr);
2013 if (PyExceptionInstance_Check(value) 2028 if (PyExceptionInstance_Check(value)
2014 && tb != NULL && PyTraceBack_Check(tb)) { 2029 && tb != NULL && PyTraceBack_Check(tb)) {
2015 /* Put the traceback on the exception, otherwise it won't get 2030 /* Put the traceback on the exception, otherwise it won't get
2016 displayed. See issue #18776. */ 2031 displayed. See issue #18776. */
2017 PyObject *cur_tb = PyException_GetTraceback(value); 2032 PyObject *cur_tb = PyException_GetTraceback(value);
2105 _Py_IDENTIFIER(flush); 2120 _Py_IDENTIFIER(flush);
2106 2121
2107 /* Save the current exception */ 2122 /* Save the current exception */
2108 PyErr_Fetch(&type, &value, &traceback); 2123 PyErr_Fetch(&type, &value, &traceback);
2109 2124
2110 f = PySys_GetObject("stderr"); 2125 f = _PySys_GetObjectId(&_PyId_stderr);
2111 if (f != NULL) { 2126 if (f != NULL) {
2112 r = _PyObject_CallMethodId(f, &PyId_flush, ""); 2127 r = _PyObject_CallMethodId(f, &PyId_flush, "");
2113 if (r) 2128 if (r)
2114 Py_DECREF(r); 2129 Py_DECREF(r);
2115 else 2130 else
2116 PyErr_Clear(); 2131 PyErr_Clear();
2117 } 2132 }
2118 f = PySys_GetObject("stdout"); 2133 f = _PySys_GetObjectId(&_PyId_stdout);
2119 if (f != NULL) { 2134 if (f != NULL) {
2120 r = _PyObject_CallMethodId(f, &PyId_flush, ""); 2135 r = _PyObject_CallMethodId(f, &PyId_flush, "");
2121 if (r) 2136 if (r)
2122 Py_DECREF(r); 2137 Py_DECREF(r);
2123 else 2138 else