Skip to content

Commit 3af5968

Browse files
improve comments
1 parent 3263498 commit 3af5968

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

Python/optimizer_bytecodes.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,23 @@ dummy_func(void) {
168168
PyObject *sub_o = sym_get_const(ctx, sub);
169169
if (sub_o != NULL) {
170170
if (PyUnicode_CheckExact(sub_o) || PyLong_CheckExact(sub_o) || PyBytes_CheckExact(sub_o)) {
171+
// PyObject_Hash can't fail on these types
171172
ADD_OP(_STORE_SUBSCR_DICT_KNOWN_HASH, 0, PyObject_Hash(sub_o));
172-
} else if (PyTuple_CheckExact(sub_o)) {
173+
}
174+
else if (PyTuple_CheckExact(sub_o)) {
175+
// only use known hash variant when hash of tuple is already computed
176+
// since computing it can call arbitrary code
173177
Py_hash_t hash = ((PyTupleObject *)sub_o)->ob_hash;
174178
if (hash != -1) {
175179
ADD_OP(_STORE_SUBSCR_DICT_KNOWN_HASH, 0, hash);
176180
}
177-
} else if (Py_TYPE(sub_o)->tp_hash == PyObject_GenericHash) {
181+
}
182+
else if (Py_TYPE(sub_o)->tp_hash == PyBaseObject_Type.tp_hash) {
183+
// for user-defined objects which don't override tp_hash
178184
Py_hash_t hash = PyObject_Hash(sub_o);
179185
ADD_OP(_STORE_SUBSCR_DICT_KNOWN_HASH, 0, hash);
180-
PyTypeObject *type = Py_TYPE(sub_o);
181-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
182-
_Py_BloomFilter_Add(dependencies, type);
186+
PyType_Watch(TYPE_WATCHER_ID, Py_TYPE(sub_o));
187+
_Py_BloomFilter_Add(dependencies, Py_TYPE(sub_o));
183188
}
184189
}
185190
(void)value;
@@ -504,14 +509,16 @@ dummy_func(void) {
504509
if (PyUnicode_CheckExact(sub) || PyLong_CheckExact(sub) || PyBytes_CheckExact(sub)) {
505510
// PyObject_Hash can't fail on these types
506511
ADD_OP(_BINARY_OP_SUBSCR_DICT_KNOWN_HASH, 0, PyObject_Hash(sub));
507-
} else if (PyTuple_CheckExact(sub)) {
512+
}
513+
else if (PyTuple_CheckExact(sub)) {
508514
// only use known hash variant when hash of tuple is already computed
509515
// since computing it can call arbitrary code
510516
Py_hash_t hash = ((PyTupleObject *)sub)->ob_hash;
511517
if (hash != -1) {
512518
ADD_OP(_BINARY_OP_SUBSCR_DICT_KNOWN_HASH, 0, hash);
513519
}
514-
} else if (Py_TYPE(sub)->tp_hash == PyBaseObject_Type.tp_hash) {
520+
}
521+
else if (Py_TYPE(sub)->tp_hash == PyBaseObject_Type.tp_hash) {
515522
// for user-defined objects which don't override tp_hash
516523
Py_hash_t hash = PyObject_Hash(sub);
517524
ADD_OP(_BINARY_OP_SUBSCR_DICT_KNOWN_HASH, 0, hash);

0 commit comments

Comments
 (0)