STINNER Victor added the comment:

Let me try to explain this issue again.

"f1, f2 = lambda: 1, lambda: 1.0" is compiled to two MAKE_FUNCTION 
instructions, MAKE_FUNCTION takes a code object as parameter (and a name). The 
Python compiler merges constants which are seen as "equal", with exceptions to 
not merge values of different types, or float of different sign.

Merging duplicate code objects is a cool micro optimization, I prefer to keep 
it. My patch keeps this micro optimization, but fix the bug: it ensures that 
equal constants having different types are not seen as equal. For example, 0 is 
equal to 0.0, but if when used for code constants, the code objects are seen a 
different.

Patch version 3:

* as suggested by Armin Rigo & Serhiy Storchaka: use id(obj) in the constant 
key for unknown constant types -- in practice, this patch is never taken, it's 
just to be extra safe (I checked manually by running the whole test suite when 
an assertion, assertion not in the posted patch)
* add a lot of unit tests
* add a documentation to _PyCode_ConstantKey()

@Serhiy: does it look good to you now?

> Would option (1) work if wrap 1 and 1.0 in a tuple? In a list? In a custom 
> collection?

My patch now uses id(obj) in the "constant key" for unknown types. The compiler 
only emits simple type (int, str, ...), tuple, frozenset and code objects.

You *can* other types if you patch manually constants with custom objects, 
since code_richcomp() now uses the "constant key" function to compare 
constants. For example, my fat project has a replace_consts() function to 
inject builtin functions in constants:
http://fatoptimizer.readthedocs.org/en/latest/fat.html#replace_consts

There is also @asconstants decorator of codetransformer which allow to inject 
arbitrary types in function constants:
https://pypi.python.org/pypi/codetransformer

----------
Added file: http://bugs.python.org/file41671/code_richcompare-3.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25843>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to