Issue 2440: Issues with getargs_n() and PyNumber_Index. - Python tracker

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
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: trent Nosy List: benjamin.peterson, loewis, trent
Priority: normal Keywords: 64bit, patch

Created on 2008-03-20 22:00 by trent, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
getargs_and_abstract.patch trent, 2008-03-20 22:09
Messages (7)
msg64212 - (view) Author: Trent Nelson (trent) * (Python committer) Date: 2008-03-20 22:00
test_getargs2 fails on Win x64:

test_getargs2 is failing on Windows x64:
test test_getargs2 failed -- Traceback (most recent call last):
  File
"S:\buildbots\python.x64\3.0.nelson-win64\build\lib\test\test_getargs2.py",
line 190, in test_n
    self.failUnlessEqual(99, getargs_n(Long()))
TypeError: 'Long' object cannot be interpreted as an integer

The problem is twofold: case 'n' on Win x64 (where SIZEOF_SIZE_T !=
SIZEOF_LONG) had a broken code path and needed updating.  Also, the
fallback to 'l' for systems where SIZEOF_SIZE_T == SIZEOF_LONG wasn't
correct -- it should still do a PyNumber_Index() check, and then use
PyLong_AsSize_t() to extract the value.

The attached patch corrects the behaviour on 32-bit and 64-bit systems,
including Windows.  However, it has now uncovered another bug in Windows
x64:

>>> from _testcapi import getargs_n
>>> getargs_n(sys.maxsize)
9223372036854775807
>>> getargs_n(-sys.maxsize)
1
>>> getargs_n(-sys.maxsize-1)
0

After a bit of investigation with Martin, the logic in PyLong_AsSize_t()
is incorrect and needs to be reworked to handle negative maximums properly.
msg64214 - (view) Author: Trent Nelson (trent) * (Python committer) Date: 2008-03-20 22:09
Attach a slightly cleaner patch, take 2.
msg65295 - (view) Author: Trent Nelson (trent) * (Python committer) Date: 2008-04-10 16:27
Committed patch in r62269.  I'll raise a separate tracker issue for 
PyLong_AsSsize_t as it isn't related to this issue.
msg65325 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-04-10 21:36
Trent, that commit broke several tests (test_builtin for one) that
relied on a floats not being interpreted as longs.
msg65326 - (view) Author: Trent Nelson (trent) * (Python committer) Date: 2008-04-10 22:06
Eek, so it does, thanks.  Applied the following patch on a bunch of 32-
bit/x64 systems, testing now, so far everything looks good...

Index: abstract.c
===================================================================
--- abstract.c  (revision 62279)
+++ abstract.c  (working copy)
@@ -1240,7 +1240,7 @@
                        return NULL;
                }
        }
-       else if (m && m->nb_int != NULL) {
+       else if (m && m->nb_int != NULL && m->nb_float == NULL) {
                result = m->nb_int(item);
                if (result && !PyLong_Check(result)) {
                        PyErr_Format(PyExc_TypeError,
msg65327 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-04-10 22:30
Thanks for fixing that.
msg65678 - (view) Author: Trent Nelson (trent) * (Python committer) Date: 2008-04-22 19:03
Update: the changes committed on r62269 and r62279 were incorrect and 
reverted in r62292.

Log:
Issue 2440: revert r62269 and r62279.  These changes were made in an 
effort to fix test_args2.Signed_TestCase.test_n(), which was failing on 
Windows x64 on the following line:  'self.failUnlessEqual(99, getargs_n
(Long()))'.  Although the two commits *did* fix the test on Windows 
x64, it's become clear that it's the test that's incorrect, and the 
changes to PyNumber_Index() in particular were not warranted (and 
actually violate PEP 357).  This commit will get us back to where we 
were at r62268, before I started butchering things.

The reworked patch fixes test_getargs2.py, such that it verifies Long() 
and Int() can't be used as indexes.  It also fixes the handling of 'n' 
in getargs.c's convertsimple().  Committed in r62462.