Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b7e1eff

Browse files
bpo-33299: Return an object itself for some types in _PyCode_ConstantKey(). (GH-6513)
1 parent 9009f3e commit b7e1eff

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

Objects/codeobject.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,14 +488,21 @@ _PyCode_ConstantKey(PyObject *op)
488488
{
489489
PyObject *key;
490490

491-
/* Py_None and Py_Ellipsis are singleton */
491+
/* Py_None and Py_Ellipsis are singletons. */
492492
if (op == Py_None || op == Py_Ellipsis
493493
|| PyLong_CheckExact(op)
494-
|| PyBool_Check(op)
495-
|| PyBytes_CheckExact(op)
496494
|| PyUnicode_CheckExact(op)
497495
/* code_richcompare() uses _PyCode_ConstantKey() internally */
498-
|| PyCode_Check(op)) {
496+
|| PyCode_Check(op))
497+
{
498+
/* Objects of these types are always different from object of other
499+
* type and from tuples. */
500+
Py_INCREF(op);
501+
key = op;
502+
}
503+
else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
504+
/* Make booleans different from integers 0 and 1.
505+
* Avoid BytesWarning from comparing bytes with strings. */
499506
key = PyTuple_Pack(2, Py_TYPE(op), op);
500507
}
501508
else if (PyFloat_CheckExact(op)) {

Python/compile.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5299,10 +5299,12 @@ dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
52995299
return NULL;
53005300
while (PyDict_Next(dict, &pos, &k, &v)) {
53015301
i = PyLong_AS_LONG(v);
5302-
/* The keys of the dictionary are tuples. (see compiler_add_o
5303-
* and _PyCode_ConstantKey). The object we want is always second,
5304-
* though. */
5305-
k = PyTuple_GET_ITEM(k, 1);
5302+
/* The keys of the dictionary can be tuples wrapping a contant.
5303+
* (see compiler_add_o and _PyCode_ConstantKey). In that case
5304+
* the object we want is always second. */
5305+
if (PyTuple_CheckExact(k)) {
5306+
k = PyTuple_GET_ITEM(k, 1);
5307+
}
53065308
Py_INCREF(k);
53075309
assert((i - offset) < size);
53085310
assert((i - offset) >= 0);

0 commit comments

Comments
 (0)