Kaydet (Commit) 6ce7ed23 authored tarafından Martin v. Löwis's avatar Martin v. Löwis

Revert previous checkin on getargs 'L' code. Try to convert all

numbers in PyLong_AsLongLong, and update test suite accordingly.
Backported to 2.4.
üst 4bf108d7
# Run the _testcapi module tests (tests for the Python/C API): by defn, # Run the _testcapi module tests (tests for the Python/C API): by defn,
# these are all functions _testcapi exports whose name begins with 'test_'. # these are all functions _testcapi exports whose name begins with 'test_'.
import sys, unittest import sys
from test import test_support from test import test_support
import _testcapi import _testcapi
...@@ -35,12 +35,6 @@ def TestThreadState(): ...@@ -35,12 +35,6 @@ def TestThreadState():
raise test_support.TestFailed, \ raise test_support.TestFailed, \
"Couldn't find main thread correctly in the list" "Couldn't find main thread correctly in the list"
# Tests which use _testcapi helpers
class OtherTests(unittest.TestCase):
def test_exc_L(self):
# This used to raise a SystemError(bad internal call)
self.assertRaises(TypeError, _testcapi.getargs_L, "String")
try: try:
_testcapi._test_thread_state _testcapi._test_thread_state
have_thread_state = True have_thread_state = True
...@@ -52,9 +46,3 @@ if have_thread_state: ...@@ -52,9 +46,3 @@ if have_thread_state:
import threading import threading
t=threading.Thread(target=TestThreadState) t=threading.Thread(target=TestThreadState)
t.start() t.start()
def test_main():
test_support.run_unittest(OtherTests)
if __name__=='__main__':
test_main()
...@@ -187,16 +187,10 @@ class LongLong_TestCase(unittest.TestCase): ...@@ -187,16 +187,10 @@ class LongLong_TestCase(unittest.TestCase):
def test_L(self): def test_L(self):
from _testcapi import getargs_L from _testcapi import getargs_L
# L returns 'long long', and does range checking (LLONG_MIN ... LLONG_MAX) # L returns 'long long', and does range checking (LLONG_MIN ... LLONG_MAX)
self.failUnlessRaises(TypeError, getargs_L, "Hello")
# XXX There's a bug in getargs.c, format code "L": self.failUnlessEqual(3, getargs_L(3.14))
# If you pass something else than a Python long, you self.failUnlessEqual(99, getargs_L(Long()))
# get "Bad argument to internal function". self.failUnlessEqual(99, getargs_L(Int()))
# So these three tests are commented out:
## self.failUnlessEqual(3, getargs_L(3.14))
## self.failUnlessEqual(99, getargs_L(Long()))
## self.failUnlessEqual(99, getargs_L(Int()))
self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1) self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1)
self.failUnlessEqual(LLONG_MIN, getargs_L(LLONG_MIN)) self.failUnlessEqual(LLONG_MIN, getargs_L(LLONG_MIN))
......
...@@ -783,9 +783,30 @@ PyLong_AsLongLong(PyObject *vv) ...@@ -783,9 +783,30 @@ PyLong_AsLongLong(PyObject *vv)
return -1; return -1;
} }
if (!PyLong_Check(vv)) { if (!PyLong_Check(vv)) {
PyNumberMethods *nb;
PyObject *io;
if (PyInt_Check(vv)) if (PyInt_Check(vv))
return (PY_LONG_LONG)PyInt_AsLong(vv); return (PY_LONG_LONG)PyInt_AsLong(vv);
PyErr_BadInternalCall(); if ((nb = vv->ob_type->tp_as_number) == NULL ||
nb->nb_int == NULL) {
PyErr_SetString(PyExc_TypeError, "an integer is required");
return -1;
}
io = (*nb->nb_int) (vv);
if (io == NULL)
return -1;
if (PyInt_Check(io)) {
bytes = PyInt_AsLong(io);
Py_DECREF(io);
return bytes;
}
if (PyLong_Check(io)) {
bytes = PyLong_AsLongLong(io);
Py_DECREF(io);
return bytes;
}
Py_DECREF(io);
PyErr_SetString(PyExc_TypeError, "integer conversion failed");
return -1; return -1;
} }
......
...@@ -610,7 +610,6 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf, ...@@ -610,7 +610,6 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf,
PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * ); PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
PY_LONG_LONG ival = PyLong_AsLongLong( arg ); PY_LONG_LONG ival = PyLong_AsLongLong( arg );
if( ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) { if( ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
PyErr_Clear();
return converterr("long<L>", arg, msgbuf, bufsize); return converterr("long<L>", arg, msgbuf, bufsize);
} else { } else {
*p = ival; *p = ival;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment