mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Issue #25557: Refactor _PyDict_LoadGlobal()
Don't fallback to PyDict_GetItemWithError() if the hash is unknown: compute the hash instead. Add also comments to explain the optimization a little bit.
This commit is contained in:
		
							parent
							
								
									c50ec007ff
								
							
						
					
					
						commit
						b4efc963d6
					
				
					 2 changed files with 38 additions and 28 deletions
				
			
		|  | @ -2347,26 +2347,33 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) | |||
|             PyObject *name = GETITEM(names, oparg); | ||||
|             PyObject *v; | ||||
|             if (PyDict_CheckExact(f->f_globals) | ||||
|                 && PyDict_CheckExact(f->f_builtins)) { | ||||
|                 && PyDict_CheckExact(f->f_builtins)) | ||||
|             { | ||||
|                 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, | ||||
|                                        (PyDictObject *)f->f_builtins, | ||||
|                                        name); | ||||
|                 if (v == NULL) { | ||||
|                     if (!_PyErr_OCCURRED()) | ||||
|                     if (!_PyErr_OCCURRED()) { | ||||
|                         /* _PyDict_LoadGlobal() returns NULL without raising
 | ||||
|                          * an exception if the key doesn't exist */ | ||||
|                         format_exc_check_arg(PyExc_NameError, | ||||
|                                              NAME_ERROR_MSG, name); | ||||
|                     } | ||||
|                     goto error; | ||||
|                 } | ||||
|                 Py_INCREF(v); | ||||
|             } | ||||
|             else { | ||||
|                 /* Slow-path if globals or builtins is not a dict */ | ||||
| 
 | ||||
|                 /* namespace 1: globals */ | ||||
|                 v = PyObject_GetItem(f->f_globals, name); | ||||
|                 if (v == NULL) { | ||||
|                     if (!PyErr_ExceptionMatches(PyExc_KeyError)) | ||||
|                         goto error; | ||||
|                     PyErr_Clear(); | ||||
| 
 | ||||
|                     /* namespace 2: builtins */ | ||||
|                     v = PyObject_GetItem(f->f_builtins, name); | ||||
|                     if (v == NULL) { | ||||
|                         if (PyErr_ExceptionMatches(PyExc_KeyError)) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner