This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: -hex/oct const generates wrong code
Type: Stage:
Components: Interpreter Core Versions: Python 2.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: gvanrossum, mwh, nnorwitz, rhettinger
Priority: normal Keywords:

Created on 2002-12-31 18:11 by gvanrossum, last changed 2022-04-10 16:06 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
compile.diff rhettinger, 2003-01-12 11:36 Patch with test
comp2.diff nnorwitz, 2003-01-16 15:18 alternate compile patch (no test, can re-use test in compile.diff)
Messages (12)
msg13746 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-12-31 18:11
Since Python 2.2, there's code in com_factor() in
compile.c that expands negative constants inline. For
example, the expression -1 generates a single
LOAD_CONST opcode for -1, rather than (as in Python 2.1
and before) generating a LOAD_CONST for +1 followed by
a UNARY_NEGATIVE opcode.

Unfortunately there's an end case where this causes
surprising behavior: when the constant is an octal or
hexadecimal constant that already has a negative value
(despite having no sign), the generated code yields a
different result than expected.  For example:

>>> print -(0xffffffff)
1
>>> x = 0xffffffff 
>>> print -x
1
>>> print -0xffffffff 
-4294967295
>>> 

Despite the fact that in Python 2.4 (!) all of these
will print the third outcome, in Python 2.2 and 2.3, I
think the third outcome is wrong and unexpected.

No time to fix this for 2.3a1, but should fix it before
2.3a2.
msg13747 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-01-12 11:35
Logged In: YES 
user_id=80475

See attached patch.
msg13748 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-01-16 15:18
Logged In: YES 
user_id=33168

Ideally, I'd like to keep the code, rather than rip it out.
 Is it possible to keep the optimization, but if it is -
followed by a 0, then fallback to the original technique
(ie, UNARY_NEGATIVE, followed by the value)?

I have attached a patch which seems to implement this.
msg13749 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-01-16 17:32
Logged In: YES 
user_id=6380

I'd like to keep the optimization too.

I think you have to do something different. The code
currently tries to save some time by prepending a minus sign
to the literal. I think it should just evaluate the literal
and then apply PyNumber_Negative() to the result, all the
while catching exceptions and falling back to unoptimized
code if there are exceptions.
msg13750 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-01-16 18:38
Logged In: YES 
user_id=33168

mwh pointed out that my patch breaks test_grammar.  I'll
take another look later, unless Raymond beats me to it.
msg13751 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-01-16 19:45
Logged In: YES 
user_id=33168

The test fails because of line test_grammar.py:40:

  if -2147483647-1 != -020000000000:

What should -020000000000 be?
Without the -, it is:  -(2**31)-1, so it can't be
represented in an int (on 32-bit arches).
With the patch, it is: 2147483648L.

Later, I'll take a look at doing what Guido suggested.
msg13752 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-01-16 19:53
Logged In: YES 
user_id=6380

I believe that test relies on the bug. :-(

So the test should be fixed.

The value ought to be equal to -(020000000000L).
msg13753 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2003-01-17 10:47
Logged In: YES 
user_id=6656

Oh, so maybe the fact that Lib/compiler has a problem in the
same place *isn't* a problem with the compiler package... 

What ever gets done here should probably coincide with
whatever happens in patch 661536.
msg13754 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-02-05 18:24
Logged In: YES 
user_id=6380

Hmm...  There's a philosophical issue here. I'll post to
python-dev (if anyone still reads that).
msg13755 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-02-12 15:58
Logged In: YES 
user_id=6380

I've decided to fix this to be compatible with Python 2.1;
I'll hvae to backport the fix to 2.2.
msg13756 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-02-12 17:34
Logged In: YES 
user_id=6380

All checked in.

Keeping this open because it needs to be backported to 2.2.
msg13757 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-02-12 19:18
Logged In: YES 
user_id=6380

OK, backported to 2.2.3.
History
Date User Action Args
2022-04-10 16:06:04adminsetgithub: 37668
2002-12-31 18:11:30gvanrossumcreate