@@ -1129,6 +1129,23 @@ dictkeys_generic_lookup(PyDictObject *mp, PyDictKeysObject* dk, PyObject *key, P
11291129 return do_lookup (mp , dk , key , hash , compare_generic );
11301130}
11311131
1132+ static Py_hash_t
1133+ check_keys_and_hash (PyDictKeysObject * dk , PyObject * key ) {
1134+ DictKeysKind kind = dk -> dk_kind ;
1135+ if (!PyUnicode_CheckExact (key ) || kind == DICT_KEYS_GENERAL ) {
1136+ return -1 ;
1137+ }
1138+ Py_hash_t hash = unicode_get_hash (key );
1139+ if (hash == -1 ) {
1140+ hash = PyUnicode_Type .tp_hash (key );
1141+ if (hash == -1 ) {
1142+ PyErr_Clear ();
1143+ return -1 ;
1144+ }
1145+ }
1146+ return hash ;
1147+ }
1148+
11321149/* Lookup a string in a (all unicode) dict keys.
11331150 * Returns DKIX_ERROR if key is not a string,
11341151 * or if the dict keys is not all strings.
@@ -1138,19 +1155,27 @@ dictkeys_generic_lookup(PyDictObject *mp, PyDictKeysObject* dk, PyObject *key, P
11381155Py_ssize_t
11391156_PyDictKeys_StringLookup (PyDictKeysObject * dk , PyObject * key )
11401157{
1141- DictKeysKind kind = dk -> dk_kind ;
1142- if (! PyUnicode_CheckExact ( key ) || kind == DICT_KEYS_GENERAL ) {
1158+ Py_hash_t hash = check_keys_and_hash ( dk , key ) ;
1159+ if (hash == -1 ) {
11431160 return DKIX_ERROR ;
11441161 }
1145- Py_hash_t hash = unicode_get_hash (key );
1162+ return unicodekeys_lookup_unicode (dk , key , hash );
1163+ }
1164+
1165+ Py_ssize_t
1166+ _PyDictKeys_StringLookupAndVersion (PyDictKeysObject * dk , PyObject * key , uint32_t * version )
1167+ {
1168+ Py_hash_t hash = check_keys_and_hash (dk , key );
11461169 if (hash == -1 ) {
1147- hash = PyUnicode_Type .tp_hash (key );
1148- if (hash == -1 ) {
1149- PyErr_Clear ();
1150- return DKIX_ERROR ;
1151- }
1170+ return DKIX_ERROR ;
11521171 }
1153- return unicodekeys_lookup_unicode (dk , key , hash );
1172+
1173+ Py_ssize_t ix ;
1174+ LOCK_KEYS (dk );
1175+ ix = unicodekeys_lookup_unicode (dk , key , hash );
1176+ * version = _PyDictKeys_GetVersionForCurrentState (_PyInterpreterState_GET (), dk );
1177+ UNLOCK_KEYS (dk );
1178+ return ix ;
11541179}
11551180
11561181#ifdef Py_GIL_DISABLED
0 commit comments