Skip to content

bpo-33299: Return an object itself for some types in _PyCode_ConstantKey(). #6513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,14 +488,21 @@ _PyCode_ConstantKey(PyObject *op)
{
PyObject *key;

/* Py_None and Py_Ellipsis are singleton */
/* Py_None and Py_Ellipsis are singletons. */
if (op == Py_None || op == Py_Ellipsis
|| PyLong_CheckExact(op)
|| PyBool_Check(op)
|| PyBytes_CheckExact(op)
|| PyUnicode_CheckExact(op)
/* code_richcompare() uses _PyCode_ConstantKey() internally */
|| PyCode_Check(op)) {
|| PyCode_Check(op))
{
/* Objects of these types are always different from object of other
* type and from tuples. */
Py_INCREF(op);
key = op;
}
else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
/* Make booleans different from integers 0 and 1.
* Avoid BytesWarning from comparing bytes with strings. */
key = PyTuple_Pack(2, Py_TYPE(op), op);
}
else if (PyFloat_CheckExact(op)) {
Expand Down
10 changes: 6 additions & 4 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5299,10 +5299,12 @@ dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
return NULL;
while (PyDict_Next(dict, &pos, &k, &v)) {
i = PyLong_AS_LONG(v);
/* The keys of the dictionary are tuples. (see compiler_add_o
* and _PyCode_ConstantKey). The object we want is always second,
* though. */
k = PyTuple_GET_ITEM(k, 1);
/* The keys of the dictionary can be tuples wrapping a contant.
* (see compiler_add_o and _PyCode_ConstantKey). In that case
* the object we want is always second. */
if (PyTuple_CheckExact(k)) {
k = PyTuple_GET_ITEM(k, 1);
}
Py_INCREF(k);
assert((i - offset) < size);
assert((i - offset) >= 0);
Expand Down