comparison Python/sysmodule.c @ 86964:a2f42d57b91d

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.
author Victor Stinner <victor.stinner@gmail.com>
date Wed, 06 Nov 2013 18:27:13 +0100
parents 4ef4578db38a
children 5e402c16a74c
comparison
equal deleted inserted replaced
86963:07fa1ed0d551 86964:a2f42d57b91d
135 sys_displayhook(PyObject *self, PyObject *o) 135 sys_displayhook(PyObject *self, PyObject *o)
136 { 136 {
137 PyObject *outf; 137 PyObject *outf;
138 PyInterpreterState *interp = PyThreadState_GET()->interp; 138 PyInterpreterState *interp = PyThreadState_GET()->interp;
139 PyObject *modules = interp->modules; 139 PyObject *modules = interp->modules;
140 PyObject *builtins = PyDict_GetItemString(modules, "builtins"); 140 PyObject *builtins;
141 static PyObject *newline = NULL;
141 int err; 142 int err;
142 _Py_IDENTIFIER(_); 143 _Py_IDENTIFIER(_);
143 144 _Py_IDENTIFIER(builtins);
145
146 builtins = _PyDict_GetItemId(modules, &PyId_builtins);
144 if (builtins == NULL) { 147 if (builtins == NULL) {
145 PyErr_SetString(PyExc_RuntimeError, "lost builtins module"); 148 PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
146 return NULL; 149 return NULL;
147 } 150 }
148 151
171 } 174 }
172 else { 175 else {
173 return NULL; 176 return NULL;
174 } 177 }
175 } 178 }
176 if (PyFile_WriteString("\n", outf) != 0) 179 if (newline == NULL) {
180 newline = PyUnicode_FromString("\n");
181 if (newline == NULL)
182 return NULL;
183 }
184 if (PyFile_WriteObject(newline, outf, Py_PRINT_RAW) != 0)
177 return NULL; 185 return NULL;
178 if (_PyObject_SetAttrId(builtins, &PyId__, o) != 0) 186 if (_PyObject_SetAttrId(builtins, &PyId__, o) != 0)
179 return NULL; 187 return NULL;
180 Py_INCREF(Py_None); 188 Py_INCREF(Py_None);
181 return Py_None; 189 return Py_None;