[Python-Dev] constant folding of -0

Eugene Toder eltoder at gmail.com
Thu Mar 10 01:31:45 CET 2011


Hello,

I've noticed since version 3.2 python doesn't fold -0:

Python 3.1.3 (r313:86834, Nov 28 2010, 10:01:07)
>>> def foo(): return -0
>>> dis(foo)
  1           0 LOAD_CONST               1 (0)
              3 RETURN_VALUE

Python 3.2 (r32:88445, Feb 20 2011, 21:30:00)
>>> def foo(): return -0
>>> dis(foo)
  1           0 LOAD_CONST               1 (0)
              3 UNARY_NEGATIVE
              4 RETURN_VALUE

(version built from head behaves the same way).

It looks like folding -0 is disabled in peephole since this commit
http://hg.python.org/cpython/diff/660419bdb4ae/Python/compile.c
which was a long time ago. Before 3.2 -0 was likely folded in the
parser -- in a more complex case no folding happens in either version:

>>> def foo(): return -(1-1)
>>> dis(foo)
  1           0 LOAD_CONST               2 (0)
              3 UNARY_NEGATIVE
              4 RETURN_VALUE

In 3.2 parser no longer folds -0.

So I wanted to ask why folding of -0 was disabled in peephole? Commit
message makes me think this was a work-around for a problem in marshal
-- perhaps it couldn't save -0.0 properly and so not creating -0.0 in
the code objects was a simple fix. (This would mean the change
predates folding in the parser.) Was marshal fixed? If I revert the
change everything seems to work and all tests pass. Since tests are
run with .pyc-s I assume they test marshal?
Maybe this check is no longer needed and can be reverted? Or is it
there for some different reason which still holds?

Regards,
Eugene


More information about the Python-Dev mailing list