mirror of
				https://github.com/python/cpython.git
				synced 2025-10-26 03:04:41 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									6853108ccd
								
							
						
					
					
						commit
						d02fbb8f71
					
				
					 1 changed files with 10 additions and 2 deletions
				
			
		|  | @ -137,10 +137,13 @@ sys_displayhook(PyObject *self, PyObject *o) | ||||||
|     PyObject *outf; |     PyObject *outf; | ||||||
|     PyInterpreterState *interp = PyThreadState_GET()->interp; |     PyInterpreterState *interp = PyThreadState_GET()->interp; | ||||||
|     PyObject *modules = interp->modules; |     PyObject *modules = interp->modules; | ||||||
|     PyObject *builtins = PyDict_GetItemString(modules, "builtins"); |     PyObject *builtins; | ||||||
|  |     static PyObject *newline = NULL; | ||||||
|     int err; |     int err; | ||||||
|     _Py_IDENTIFIER(_); |     _Py_IDENTIFIER(_); | ||||||
|  |     _Py_IDENTIFIER(builtins); | ||||||
| 
 | 
 | ||||||
|  |     builtins = _PyDict_GetItemId(modules, &PyId_builtins); | ||||||
|     if (builtins == NULL) { |     if (builtins == NULL) { | ||||||
|         PyErr_SetString(PyExc_RuntimeError, "lost builtins module"); |         PyErr_SetString(PyExc_RuntimeError, "lost builtins module"); | ||||||
|         return NULL; |         return NULL; | ||||||
|  | @ -173,7 +176,12 @@ sys_displayhook(PyObject *self, PyObject *o) | ||||||
|             return NULL; |             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; |         return NULL; | ||||||
|     if (_PyObject_SetAttrId(builtins, &PyId__, o) != 0) |     if (_PyObject_SetAttrId(builtins, &PyId__, o) != 0) | ||||||
|         return NULL; |         return NULL; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner