Skip to content

Commit 2137d12

Browse files
committed
In code_hash(), only use safe fields
1 parent e692fee commit 2137d12

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

Objects/codeobject.c

+6-11
Original file line numberDiff line numberDiff line change
@@ -1538,20 +1538,15 @@ code_richcompare(PyObject *self, PyObject *other, int op)
15381538
static Py_hash_t
15391539
code_hash(PyCodeObject *co)
15401540
{
1541-
Py_hash_t h, h0, h1, h2, h3, h4;
1542-
h0 = PyObject_Hash(co->co_name);
1541+
// Hash only fields that are set even on dehydrated code objects.
1542+
Py_hash_t h, h0, h1;
1543+
h0 = PyObject_Hash(co->co_qualname);
15431544
if (h0 == -1) return -1;
1544-
h1 = PyObject_Hash(co->co_code);
1545+
h1 = PyObject_Hash(co->co_filename);
15451546
if (h1 == -1) return -1;
1546-
h2 = PyObject_Hash(co->co_consts);
1547-
if (h2 == -1) return -1;
1548-
h3 = PyObject_Hash(co->co_names);
1549-
if (h3 == -1) return -1;
1550-
h4 = PyObject_Hash(co->co_localsplusnames);
1551-
if (h4 == -1) return -1;
1552-
h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^
1547+
h = h0 ^ h1 ^
15531548
co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
1554-
co->co_flags;
1549+
co->co_flags ^ co->co_firstlineno;
15551550
if (h == -1) h = -2;
15561551
return h;
15571552
}

0 commit comments

Comments
 (0)