[issue25879] Code objects from same line can compare equal

Kirk McDonald report at bugs.python.org
Tue Dec 15 23:07:48 EST 2015


New submission from Kirk McDonald:

The following code gives an unexpected result:

>>> a, b = lambda: 1, lambda: 1.0
>>> a()
1
>>> b()
1
>>> type(b())
<class 'int'>
>>> a.__code__ is b.__code__
True

The cause boils down to this line of code:

https://hg.python.org/cpython/file/33ff335da34c/Objects/codeobject.c#l442

When it compiles the two lambdas, their code objects compare equal. They have the same name, the same bytecode, and start on the same line. And, because 1 == 1.0, their constants compare equal. This then prompts the compiler to combine the two code objects into a single constant in the enclosing code object, discarding the second one.

I think the solution is to have code_richcompare also check whether the types of the constants are equal, in addition to the constants themselves.

----------
messages: 256508
nosy: KirkMcDonald
priority: normal
severity: normal
status: open
title: Code objects from same line can compare equal

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25879>
_______________________________________


More information about the Python-bugs-list mailing list