@@ -1165,39 +1165,42 @@ _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
11651165 return PyDict_GetItemWithError (dp , kv );
11661166}
11671167
1168- /* Fast version of global value lookup.
1168+ /* Fast version of global value lookup (LOAD_GLOBAL) .
11691169 * Lookup in globals, then builtins.
1170+ *
1171+ * Raise an exception and return NULL if an error occurred (ex: computing the
1172+ * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1173+ * exist. Return the value if the key exists.
11701174 */
11711175PyObject *
11721176_PyDict_LoadGlobal (PyDictObject * globals , PyDictObject * builtins , PyObject * key )
11731177{
1174- PyObject * x ;
1175- if (PyUnicode_CheckExact (key )) {
1176- PyObject * * value_addr ;
1177- Py_hash_t hash = ((PyASCIIObject * )key )-> hash ;
1178- if (hash != -1 ) {
1179- PyDictKeyEntry * e ;
1180- e = globals -> ma_keys -> dk_lookup (globals , key , hash , & value_addr );
1181- if (e == NULL ) {
1182- return NULL ;
1183- }
1184- x = * value_addr ;
1185- if (x != NULL )
1186- return x ;
1187- e = builtins -> ma_keys -> dk_lookup (builtins , key , hash , & value_addr );
1188- if (e == NULL ) {
1189- return NULL ;
1190- }
1191- x = * value_addr ;
1192- return x ;
1193- }
1178+ Py_hash_t hash ;
1179+ PyDictKeyEntry * entry ;
1180+ PyObject * * value_addr ;
1181+ PyObject * value ;
1182+
1183+ if (!PyUnicode_CheckExact (key ) ||
1184+ (hash = ((PyASCIIObject * ) key )-> hash ) == -1 )
1185+ {
1186+ hash = PyObject_Hash (key );
1187+ if (hash == -1 )
1188+ return NULL ;
11941189 }
1195- x = PyDict_GetItemWithError (( PyObject * ) globals , key );
1196- if ( x != NULL )
1197- return x ;
1198- if (PyErr_Occurred () )
1190+
1191+ /* namespace 1: globals */
1192+ entry = globals -> ma_keys -> dk_lookup ( globals , key , hash , & value_addr ) ;
1193+ if (entry == NULL )
11991194 return NULL ;
1200- return PyDict_GetItemWithError ((PyObject * )builtins , key );
1195+ value = * value_addr ;
1196+ if (value != NULL )
1197+ return value ;
1198+
1199+ /* namespace 2: builtins */
1200+ entry = builtins -> ma_keys -> dk_lookup (builtins , key , hash , & value_addr );
1201+ if (entry == NULL )
1202+ return NULL ;
1203+ return * value_addr ;
12011204}
12021205
12031206/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
0 commit comments