Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ce7ed2

Browse files
committed
Revert previous checkin on getargs 'L' code. Try to convert all
numbers in PyLong_AsLongLong, and update test suite accordingly. Backported to 2.4.
1 parent 4bf108d commit 6ce7ed2

4 files changed

Lines changed: 27 additions & 25 deletions

File tree

Lib/test/test_capi.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Run the _testcapi module tests (tests for the Python/C API): by defn,
22
# these are all functions _testcapi exports whose name begins with 'test_'.
33

4-
import sys, unittest
4+
import sys
55
from test import test_support
66
import _testcapi
77

@@ -35,12 +35,6 @@ def callback():
3535
raise test_support.TestFailed, \
3636
"Couldn't find main thread correctly in the list"
3737

38-
# Tests which use _testcapi helpers
39-
class OtherTests(unittest.TestCase):
40-
def test_exc_L(self):
41-
# This used to raise a SystemError(bad internal call)
42-
self.assertRaises(TypeError, _testcapi.getargs_L, "String")
43-
4438
try:
4539
_testcapi._test_thread_state
4640
have_thread_state = True
@@ -52,9 +46,3 @@ def test_exc_L(self):
5246
import threading
5347
t=threading.Thread(target=TestThreadState)
5448
t.start()
55-
56-
def test_main():
57-
test_support.run_unittest(OtherTests)
58-
59-
if __name__=='__main__':
60-
test_main()

Lib/test/test_getargs2.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,10 @@ class LongLong_TestCase(unittest.TestCase):
187187
def test_L(self):
188188
from _testcapi import getargs_L
189189
# L returns 'long long', and does range checking (LLONG_MIN ... LLONG_MAX)
190-
191-
# XXX There's a bug in getargs.c, format code "L":
192-
# If you pass something else than a Python long, you
193-
# get "Bad argument to internal function".
194-
195-
# So these three tests are commented out:
196-
197-
## self.failUnlessEqual(3, getargs_L(3.14))
198-
## self.failUnlessEqual(99, getargs_L(Long()))
199-
## self.failUnlessEqual(99, getargs_L(Int()))
190+
self.failUnlessRaises(TypeError, getargs_L, "Hello")
191+
self.failUnlessEqual(3, getargs_L(3.14))
192+
self.failUnlessEqual(99, getargs_L(Long()))
193+
self.failUnlessEqual(99, getargs_L(Int()))
200194

201195
self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1)
202196
self.failUnlessEqual(LLONG_MIN, getargs_L(LLONG_MIN))

Objects/longobject.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,30 @@ PyLong_AsLongLong(PyObject *vv)
783783
return -1;
784784
}
785785
if (!PyLong_Check(vv)) {
786+
PyNumberMethods *nb;
787+
PyObject *io;
786788
if (PyInt_Check(vv))
787789
return (PY_LONG_LONG)PyInt_AsLong(vv);
788-
PyErr_BadInternalCall();
790+
if ((nb = vv->ob_type->tp_as_number) == NULL ||
791+
nb->nb_int == NULL) {
792+
PyErr_SetString(PyExc_TypeError, "an integer is required");
793+
return -1;
794+
}
795+
io = (*nb->nb_int) (vv);
796+
if (io == NULL)
797+
return -1;
798+
if (PyInt_Check(io)) {
799+
bytes = PyInt_AsLong(io);
800+
Py_DECREF(io);
801+
return bytes;
802+
}
803+
if (PyLong_Check(io)) {
804+
bytes = PyLong_AsLongLong(io);
805+
Py_DECREF(io);
806+
return bytes;
807+
}
808+
Py_DECREF(io);
809+
PyErr_SetString(PyExc_TypeError, "integer conversion failed");
789810
return -1;
790811
}
791812

Python/getargs.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,6 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf,
610610
PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
611611
PY_LONG_LONG ival = PyLong_AsLongLong( arg );
612612
if( ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
613-
PyErr_Clear();
614613
return converterr("long<L>", arg, msgbuf, bufsize);
615614
} else {
616615
*p = ival;

0 commit comments

Comments
 (0)