summaryrefslogtreecommitdiffstats
path: root/Python/sysmodule.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2013-11-06 17:27:13 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2013-11-06 17:27:13 (GMT)
commitd02fbb8f715a90079737e3048535b98c5a4e94e9 (patch)
tree1618ae986abb9c2ddd559b47f17738b8c5c530fa /Python/sysmodule.c
parent6853108ccd09d2be6d1af828053db35577b30200 (diff)
downloadcpython-d02fbb8f715a90079737e3048535b98c5a4e94e9.zip
cpython-d02fbb8f715a90079737e3048535b98c5a4e94e9.tar.gz
cpython-d02fbb8f715a90079737e3048535b98c5a4e94e9.tar.bz2
Issue #19512: sys_displayhook() now uses an identifier for "builtins"
dictionary key and only decodes "\n" string once to write a newline. So "builtins" and "\n" are only decoded once from UTF-8, at the first call.
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r--Python/sysmodule.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 97ce059..b8cf31d 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -137,10 +137,13 @@ sys_displayhook(PyObject *self, PyObject *o)
PyObject *outf;
PyInterpreterState *interp = PyThreadState_GET()->interp;
PyObject *modules = interp->modules;
- PyObject *builtins = PyDict_GetItemString(modules, "builtins");
+ PyObject *builtins;
+ static PyObject *newline = NULL;
int err;
_Py_IDENTIFIER(_);
+ _Py_IDENTIFIER(builtins);
+ builtins = _PyDict_GetItemId(modules, &PyId_builtins);
if (builtins == NULL) {
PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
return NULL;
@@ -173,7 +176,12 @@ sys_displayhook(PyObject *self, PyObject *o)
return NULL;
}
}
- if (PyFile_WriteString("\n", outf) != 0)
+ if (newline == NULL) {
+ newline = PyUnicode_FromString("\n");
+ if (newline == NULL)
+ return NULL;
+ }
+ if (PyFile_WriteObject(newline, outf, Py_PRINT_RAW) != 0)
return NULL;
if (_PyObject_SetAttrId(builtins, &PyId__, o) != 0)
return NULL;