cpython: 618cca51a27e (original) (raw)
Mercurial > cpython
changeset 87899:618cca51a27e 3.3
Issue #17576: Deprecation warning emitted now when __int__() or __index__() return not int instance. Introduced _PyLong_FromNbInt() and refactored PyLong_As*() functions. [#17576]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Wed, 11 Dec 2013 21:07:54 +0200 |
parents | 46186736e91c |
children | 59fb79d0411e d842bc07d30b |
files | Include/longobject.h Lib/test/test_getargs2.py Lib/test/test_index.py Lib/test/test_int.py Misc/NEWS Objects/abstract.c Objects/longobject.c |
diffstat | 7 files changed, 319 insertions(+), 193 deletions(-)[+] [-] Include/longobject.h 6 Lib/test/test_getargs2.py 70 Lib/test/test_index.py 36 Lib/test/test_int.py 76 Misc/NEWS 3 Objects/abstract.c 92 Objects/longobject.c 229 |
line wrap: on
line diff
--- a/Include/longobject.h +++ b/Include/longobject.h @@ -152,6 +152,12 @@ PyAPI_FUNC(int) _PyLong_AsByteArray(PyLo unsigned char* bytes, size_t n, int little_endian, int is_signed); +/* _PyLong_FromNbInt: Convert the given object to a PyLongObject
- using the nb_int slot, if available. Raise TypeError if either the
- nb_int slot is not available or the result of the call to nb_int
- returns something not of type int. +*/ +PyAPI_FUNC(PyLongObject *)_PyLong_FromNbInt(PyObject *);
/* _PyLong_Format: Convert the long to a string object with given base, appending a base prefix of 0[box] if base is 2, 8 or 16. */
--- a/Lib/test/test_getargs2.py +++ b/Lib/test/test_getargs2.py @@ -50,12 +50,34 @@ class Int: def int(self): return 99 +class IntSubclass(int):
+ + class Unsigned_TestCase(unittest.TestCase): def test_b(self): from _testcapi import getargs_b # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX) self.assertRaises(TypeError, getargs_b, 3.14) self.assertEqual(99, getargs_b(Int()))
self.assertEqual(0, getargs_b(IntSubclass()))[](#l2.30)
self.assertRaises(TypeError, getargs_b, BadInt())[](#l2.31)
with self.assertWarns(DeprecationWarning):[](#l2.32)
self.assertEqual(1, getargs_b(BadInt2()))[](#l2.33)
self.assertEqual(0, getargs_b(BadInt3()))[](#l2.34)
self.assertRaises(OverflowError, getargs_b, -1) self.assertEqual(0, getargs_b(0)) @@ -70,6 +92,11 @@ class Unsigned_TestCase(unittest.TestCas # B returns 'unsigned char', no range checking self.assertRaises(TypeError, getargs_B, 3.14) self.assertEqual(99, getargs_B(Int()))
self.assertEqual(0, getargs_B(IntSubclass()))[](#l2.42)
self.assertRaises(TypeError, getargs_B, BadInt())[](#l2.43)
with self.assertWarns(DeprecationWarning):[](#l2.44)
self.assertEqual(1, getargs_B(BadInt2()))[](#l2.45)
self.assertEqual(0, getargs_B(BadInt3()))[](#l2.46)
self.assertEqual(UCHAR_MAX, getargs_B(-1)) self.assertEqual(0, getargs_B(0)) @@ -84,6 +111,11 @@ class Unsigned_TestCase(unittest.TestCas # H returns 'unsigned short', no range checking self.assertRaises(TypeError, getargs_H, 3.14) self.assertEqual(99, getargs_H(Int()))
self.assertEqual(0, getargs_H(IntSubclass()))[](#l2.54)
self.assertRaises(TypeError, getargs_H, BadInt())[](#l2.55)
with self.assertWarns(DeprecationWarning):[](#l2.56)
self.assertEqual(1, getargs_H(BadInt2()))[](#l2.57)
self.assertEqual(0, getargs_H(BadInt3()))[](#l2.58)
self.assertEqual(USHRT_MAX, getargs_H(-1)) self.assertEqual(0, getargs_H(0)) @@ -99,6 +131,11 @@ class Unsigned_TestCase(unittest.TestCas # I returns 'unsigned int', no range checking self.assertRaises(TypeError, getargs_I, 3.14) self.assertEqual(99, getargs_I(Int()))
self.assertEqual(0, getargs_I(IntSubclass()))[](#l2.66)
self.assertRaises(TypeError, getargs_I, BadInt())[](#l2.67)
with self.assertWarns(DeprecationWarning):[](#l2.68)
self.assertEqual(1, getargs_I(BadInt2()))[](#l2.69)
self.assertEqual(0, getargs_I(BadInt3()))[](#l2.70)
self.assertEqual(UINT_MAX, getargs_I(-1)) self.assertEqual(0, getargs_I(0)) @@ -115,6 +152,10 @@ class Unsigned_TestCase(unittest.TestCas # it does not accept float, or instances with int self.assertRaises(TypeError, getargs_k, 3.14) self.assertRaises(TypeError, getargs_k, Int())
self.assertEqual(0, getargs_k(IntSubclass()))[](#l2.78)
self.assertRaises(TypeError, getargs_k, BadInt())[](#l2.79)
self.assertRaises(TypeError, getargs_k, BadInt2())[](#l2.80)
self.assertEqual(0, getargs_k(BadInt3()))[](#l2.81)
self.assertEqual(ULONG_MAX, getargs_k(-1)) self.assertEqual(0, getargs_k(0)) @@ -131,6 +172,11 @@ class Signed_TestCase(unittest.TestCase) # h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX) self.assertRaises(TypeError, getargs_h, 3.14) self.assertEqual(99, getargs_h(Int()))
self.assertEqual(0, getargs_h(IntSubclass()))[](#l2.89)
self.assertRaises(TypeError, getargs_h, BadInt())[](#l2.90)
with self.assertWarns(DeprecationWarning):[](#l2.91)
self.assertEqual(1, getargs_h(BadInt2()))[](#l2.92)
self.assertEqual(0, getargs_h(BadInt3()))[](#l2.93)
self.assertRaises(OverflowError, getargs_h, SHRT_MIN-1) self.assertEqual(SHRT_MIN, getargs_h(SHRT_MIN)) @@ -145,6 +191,11 @@ class Signed_TestCase(unittest.TestCase) # i returns 'int', and does range checking (INT_MIN ... INT_MAX) self.assertRaises(TypeError, getargs_i, 3.14) self.assertEqual(99, getargs_i(Int()))
self.assertEqual(0, getargs_i(IntSubclass()))[](#l2.101)
self.assertRaises(TypeError, getargs_i, BadInt())[](#l2.102)
with self.assertWarns(DeprecationWarning):[](#l2.103)
self.assertEqual(1, getargs_i(BadInt2()))[](#l2.104)
self.assertEqual(0, getargs_i(BadInt3()))[](#l2.105)
self.assertRaises(OverflowError, getargs_i, INT_MIN-1) self.assertEqual(INT_MIN, getargs_i(INT_MIN)) @@ -159,6 +210,11 @@ class Signed_TestCase(unittest.TestCase) # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX) self.assertRaises(TypeError, getargs_l, 3.14) self.assertEqual(99, getargs_l(Int()))
self.assertEqual(0, getargs_l(IntSubclass()))[](#l2.113)
self.assertRaises(TypeError, getargs_l, BadInt())[](#l2.114)
with self.assertWarns(DeprecationWarning):[](#l2.115)
self.assertEqual(1, getargs_l(BadInt2()))[](#l2.116)
self.assertEqual(0, getargs_l(BadInt3()))[](#l2.117)
self.assertRaises(OverflowError, getargs_l, LONG_MIN-1) self.assertEqual(LONG_MIN, getargs_l(LONG_MIN)) @@ -174,6 +230,10 @@ class Signed_TestCase(unittest.TestCase) # (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX) self.assertRaises(TypeError, getargs_n, 3.14) self.assertRaises(TypeError, getargs_n, Int())
self.assertEqual(0, getargs_n(IntSubclass()))[](#l2.125)
self.assertRaises(TypeError, getargs_n, BadInt())[](#l2.126)
self.assertRaises(TypeError, getargs_n, BadInt2())[](#l2.127)
self.assertEqual(0, getargs_n(BadInt3()))[](#l2.128)
self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1) self.assertEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN)) @@ -192,6 +252,11 @@ class LongLong_TestCase(unittest.TestCas self.assertRaises(TypeError, getargs_L, 3.14) self.assertRaises(TypeError, getargs_L, "Hello") self.assertEqual(99, getargs_L(Int()))
self.assertEqual(0, getargs_L(IntSubclass()))[](#l2.136)
self.assertRaises(TypeError, getargs_L, BadInt())[](#l2.137)
with self.assertWarns(DeprecationWarning):[](#l2.138)
self.assertEqual(1, getargs_L(BadInt2()))[](#l2.139)
self.assertEqual(0, getargs_L(BadInt3()))[](#l2.140)
self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1) self.assertEqual(LLONG_MIN, getargs_L(LLONG_MIN)) @@ -206,6 +271,11 @@ class LongLong_TestCase(unittest.TestCas # K return 'unsigned long long', no range checking self.assertRaises(TypeError, getargs_K, 3.14) self.assertRaises(TypeError, getargs_K, Int())
self.assertEqual(0, getargs_K(IntSubclass()))[](#l2.148)
self.assertRaises(TypeError, getargs_K, BadInt())[](#l2.149)
self.assertRaises(TypeError, getargs_K, BadInt2())[](#l2.150)
self.assertEqual(0, getargs_K(BadInt3()))[](#l2.151)
+ self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX)) self.assertEqual(0, getargs_K(0)) self.assertEqual(0, getargs_K(ULLONG_MAX+1))
--- a/Lib/test/test_index.py +++ b/Lib/test/test_index.py @@ -9,7 +9,7 @@ class newstyle: class TrapInt(int): def index(self):
return self[](#l3.7)
return int(self)[](#l3.8)
class BaseTestCase(unittest.TestCase): def setUp(self): @@ -55,6 +55,40 @@ class BaseTestCase(unittest.TestCase): self.assertRaises(TypeError, slice(self.o).indices, 0) self.assertRaises(TypeError, slice(self.n).indices, 0)
- def test_int_subclass_with_index(self):
# __index__ should be used when computing indices, even for int[](#l3.17)
# subclasses. See issue #17576.[](#l3.18)
class MyInt(int):[](#l3.19)
def __index__(self):[](#l3.20)
return int(self) + 1[](#l3.21)
my_int = MyInt(7)[](#l3.23)
direct_index = my_int.__index__()[](#l3.24)
operator_index = operator.index(my_int)[](#l3.25)
self.assertEqual(direct_index, 8)[](#l3.26)
self.assertEqual(operator_index, 7)[](#l3.27)
# Both results should be of exact type int.[](#l3.28)
self.assertIs(type(direct_index), int)[](#l3.29)
#self.assertIs(type(operator_index), int)[](#l3.30)
- def test_index_returns_int_subclass(self):
class BadInt:[](#l3.33)
def __index__(self):[](#l3.34)
return True[](#l3.35)
class BadInt2(int):[](#l3.37)
def __index__(self):[](#l3.38)
return True[](#l3.39)
bad_int = BadInt()[](#l3.41)
with self.assertWarns(DeprecationWarning):[](#l3.42)
n = operator.index(bad_int)[](#l3.43)
self.assertEqual(n, 1)[](#l3.44)
bad_int = BadInt2()[](#l3.46)
n = operator.index(bad_int)[](#l3.47)
self.assertEqual(n, 0)[](#l3.48)
+ class SeqTestCase: # This test case isn't run directly. It just defines common tests
--- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -263,32 +263,7 @@ class IntTestCases(unittest.TestCase): def int(self): return 42
class Foo1(object):[](#l4.7)
def __int__(self):[](#l4.8)
return 42[](#l4.9)
class Foo2(int):[](#l4.11)
def __int__(self):[](#l4.12)
return 42[](#l4.13)
class Foo3(int):[](#l4.15)
def __int__(self):[](#l4.16)
return self[](#l4.17)
class Foo4(int):[](#l4.19)
def __int__(self):[](#l4.20)
return 42[](#l4.21)
class Foo5(int):[](#l4.23)
def __int__(self):[](#l4.24)
return 42.[](#l4.25)
- self.assertEqual(int(Foo0()), 42)
self.assertEqual(int(Foo1()), 42)[](#l4.28)
self.assertEqual(int(Foo2()), 42)[](#l4.29)
self.assertEqual(int(Foo3()), 0)[](#l4.30)
self.assertEqual(int(Foo4()), 42)[](#l4.31)
self.assertRaises(TypeError, int, Foo5())[](#l4.32)
class Classic: pass @@ -351,6 +326,57 @@ class IntTestCases(unittest.TestCase): with self.assertRaises(TypeError): int(TruncReturnsBadInt())
- def test_int_subclass_with_int(self):
class MyInt(int):[](#l4.41)
def __int__(self):[](#l4.42)
return 42[](#l4.43)
class BadInt(int):[](#l4.45)
def __int__(self):[](#l4.46)
return 42.0[](#l4.47)
my_int = MyInt(7)[](#l4.49)
self.assertEqual(my_int, 7)[](#l4.50)
self.assertEqual(int(my_int), 42)[](#l4.51)
self.assertRaises(TypeError, int, BadInt())[](#l4.53)
- def test_int_returns_int_subclass(self):
class BadInt:[](#l4.56)
def __int__(self):[](#l4.57)
return True[](#l4.58)
class BadInt2(int):[](#l4.60)
def __int__(self):[](#l4.61)
return True[](#l4.62)
class TruncReturnsBadInt:[](#l4.64)
def __trunc__(self):[](#l4.65)
return BadInt()[](#l4.66)
class TruncReturnsIntSubclass:[](#l4.68)
def __trunc__(self):[](#l4.69)
return True[](#l4.70)
bad_int = BadInt()[](#l4.72)
with self.assertWarns(DeprecationWarning):[](#l4.73)
n = int(bad_int)[](#l4.74)
self.assertEqual(n, 1)[](#l4.75)
bad_int = BadInt2()[](#l4.77)
with self.assertWarns(DeprecationWarning):[](#l4.78)
n = int(bad_int)[](#l4.79)
self.assertEqual(n, 1)[](#l4.80)
bad_int = TruncReturnsBadInt()[](#l4.82)
with self.assertWarns(DeprecationWarning):[](#l4.83)
n = int(bad_int)[](#l4.84)
self.assertEqual(n, 1)[](#l4.85)
good_int = TruncReturnsIntSubclass()[](#l4.87)
n = int(good_int)[](#l4.88)
self.assertEqual(n, 1)[](#l4.89)
+ def test_error_message(self): def check(s, base=None): with self.assertRaises(ValueError,
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.3.4 release candi Core and Builtins ----------------- +- Issue #17576: Deprecation warning emitted now when int() or index()
- Issue #19932: Fix typo in import.h, missing whitespaces in function prototypes.
- Issue #19729: In str.format(), fix recursive expansion in format spec.
--- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1132,7 +1132,7 @@ PyNumber_Absolute(PyObject o) return type_error("bad operand type for abs(): '%.200s'", o); } -/ Return a Python int from the object item +/* Return a Python int from the object item. Raise TypeError if the result is not an int or if the object cannot be interpreted as an index. */ @@ -1146,21 +1146,30 @@ PyNumber_Index(PyObject *item) Py_INCREF(item); return item; }
- if (PyIndex_Check(item)) {
result = item->ob_type->tp_as_number->nb_index(item);[](#l6.17)
if (result && !PyLong_Check(result)) {[](#l6.18)
PyErr_Format(PyExc_TypeError,[](#l6.19)
"__index__ returned non-int "[](#l6.20)
"(type %.200s)",[](#l6.21)
result->ob_type->tp_name);[](#l6.22)
Py_DECREF(result);[](#l6.23)
return NULL;[](#l6.24)
}[](#l6.25)
- }
- else {
- if (!PyIndex_Check(item)) { PyErr_Format(PyExc_TypeError, "'%.200s' object cannot be interpreted " "as an integer", item->ob_type->tp_name);
return NULL;[](#l6.32)
- }
- result = item->ob_type->tp_as_number->nb_index(item);
- if (!result || PyLong_CheckExact(result))
return result;[](#l6.36)
- if (!PyLong_Check(result)) {
PyErr_Format(PyExc_TypeError,[](#l6.38)
"__index__ returned non-int (type %.200s)",[](#l6.39)
result->ob_type->tp_name);[](#l6.40)
Py_DECREF(result);[](#l6.41)
return NULL;[](#l6.42)
- }
- /* Issue #17576: warn if 'result' not of exact type int. */
- if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"__index__ returned non-int (type %.200s). "[](#l6.46)
"The ability to return an instance of a strict subclass of int "[](#l6.47)
"is deprecated, and may be removed in a future version of Python.",[](#l6.48)
result->ob_type->tp_name)) {[](#l6.49)
Py_DECREF(result);[](#l6.50)
} return result; } @@ -1212,34 +1221,6 @@ PyNumber_AsSsize_t(PyObject item, PyObj } -/return NULL;[](#l6.51)
- Returns the Integral instance converted to an int. The instance is expected
- to be an int or have an int method. Steals integral's
- reference. error_format will be used to create the TypeError if integral
- isn't actually an Integral instance. error_format should be a format string
- that can accept a char* naming integral's type. -*/ -static PyObject * -convert_integral_to_int(PyObject *integral, const char *error_format) -{
- PyNumberMethods *nb;
- if (PyLong_Check(integral))
return integral;[](#l6.71)
- nb = Py_TYPE(integral)->tp_as_number;
- if (nb->nb_int) {
PyObject *as_int = nb->nb_int(integral);[](#l6.74)
if (!as_int || PyLong_Check(as_int)) {[](#l6.75)
Py_DECREF(integral);[](#l6.76)
return as_int;[](#l6.77)
}[](#l6.78)
Py_DECREF(as_int);[](#l6.79)
- }
- PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name);
- Py_DECREF(integral);
- return NULL;
-} - - PyObject * PyNumber_Long(PyObject *o) { @@ -1257,29 +1238,28 @@ PyNumber_Long(PyObject o) } m = o->ob_type->tp_as_number; if (m && m->nb_int) { / This should include subclasses of int */
PyObject *res = m->nb_int(o);[](#l6.94)
if (res && !PyLong_Check(res)) {[](#l6.95)
PyErr_Format(PyExc_TypeError,[](#l6.96)
"__int__ returned non-int (type %.200s)",[](#l6.97)
res->ob_type->tp_name);[](#l6.98)
Py_DECREF(res);[](#l6.99)
return NULL;[](#l6.100)
}[](#l6.101)
return res;[](#l6.102)
- if (PyLong_Check(o)) /* An int subclass without nb_int */
trunc_func = PyObject_LookupSpecial(o, &PyId___trunc_); if (trunc_func) { PyObject *truncated = PyEval_CallObject(trunc_func, NULL); PyObject *int_instance; Py_DECREF(trunc_func);return _PyLong_Copy((PyLongObject *)o);[](#l6.106)
if (truncated == NULL)[](#l6.112)
return NULL;[](#l6.113)
if (truncated == NULL || PyLong_Check(truncated))[](#l6.114)
return truncated;[](#l6.115) /* __trunc__ is specified to return an Integral type,[](#l6.116) but int() needs to return a int. */[](#l6.117)
int_instance = convert_integral_to_int(truncated,[](#l6.118)
"__trunc__ returned non-Integral (type %.200s)");[](#l6.119)
m = truncated->ob_type->tp_as_number;[](#l6.120)
if (m == NULL || m->nb_int == NULL) {[](#l6.121)
PyErr_Format([](#l6.122)
PyExc_TypeError,[](#l6.123)
"__trunc__ returned non-Integral (type %.200s)",[](#l6.124)
truncated->ob_type->tp_name);[](#l6.125)
Py_DECREF(truncated);[](#l6.126)
return NULL;[](#l6.127)
}[](#l6.128)
int_instance = (PyObject *)_PyLong_FromNbInt(truncated);[](#l6.129)
} if (PyErr_Occurred())Py_DECREF(truncated);[](#l6.130) return int_instance;[](#l6.131)
--- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -116,6 +116,56 @@ long_normalize(register PyLongObject v) return v; } +/ _PyLong_FromNbInt: Convert the given object to a PyLongObject
- using the nb_int slot, if available. Raise TypeError if either the
- nb_int slot is not available or the result of the call to nb_int
- returns something not of type int. +*/ +PyLongObject * +_PyLong_FromNbInt(PyObject *integral) +{
- PyNumberMethods *nb;
- PyObject *result;
- /* Fast path for the case that we already have an int. */
- if (PyLong_CheckExact(integral)) {
Py_INCREF(integral);[](#l7.20)
return (PyLongObject *)integral;[](#l7.21)
- }
- nb = Py_TYPE(integral)->tp_as_number;
- if (nb == NULL || nb->nb_int == NULL) {
PyErr_Format(PyExc_TypeError,[](#l7.26)
"an integer is required (got type %.200s)",[](#l7.27)
Py_TYPE(integral)->tp_name);[](#l7.28)
return NULL;[](#l7.29)
- }
- /* Convert using the nb_int slot, which should return something
of exact type int. */[](#l7.33)
- result = nb->nb_int(integral);
- if (!result || PyLong_CheckExact(result))
return (PyLongObject *)result;[](#l7.36)
- if (!PyLong_Check(result)) {
PyErr_Format(PyExc_TypeError,[](#l7.38)
"__int__ returned non-int (type %.200s)",[](#l7.39)
result->ob_type->tp_name);[](#l7.40)
Py_DECREF(result);[](#l7.41)
return NULL;[](#l7.42)
- }
- /* Issue #17576: warn if 'result' not of exact type int. */
- if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"__int__ returned non-int (type %.200s). "[](#l7.46)
"The ability to return an instance of a strict subclass of int "[](#l7.47)
"is deprecated, and may be removed in a future version of Python.",[](#l7.48)
result->ob_type->tp_name)) {[](#l7.49)
Py_DECREF(result);[](#l7.50)
return NULL;[](#l7.51)
- }
- return (PyLongObject *)result;
+} + + /* Allocate a new int object with size digits. Return NULL and set exception if we run out of memory. */ @@ -347,28 +397,17 @@ PyLong_AsLongAndOverflow(PyObject *vv, i return -1; }
- if (!PyLong_Check(vv)) {
PyNumberMethods *nb;[](#l7.65)
nb = vv->ob_type->tp_as_number;[](#l7.66)
if (nb == NULL || nb->nb_int == NULL) {[](#l7.67)
PyErr_SetString(PyExc_TypeError,[](#l7.68)
"an integer is required");[](#l7.69)
return -1;[](#l7.70)
}[](#l7.71)
vv = (*nb->nb_int) (vv);[](#l7.72)
if (vv == NULL)[](#l7.73)
- if (PyLong_Check(vv)) {
v = (PyLongObject *)vv;[](#l7.75)
- }
- else {
v = _PyLong_FromNbInt(vv);[](#l7.78)
if (v == NULL)[](#l7.79) return -1;[](#l7.80) do_decref = 1;[](#l7.81)
if (!PyLong_Check(vv)) {[](#l7.82)
Py_DECREF(vv);[](#l7.83)
PyErr_SetString(PyExc_TypeError,[](#l7.84)
"nb_int should return int object");[](#l7.85)
return -1;[](#l7.86)
} res = -1;}[](#l7.87)
- v = (PyLongObject *)vv; i = Py_SIZE(v); switch (i) { @@ -412,7 +451,7 @@ PyLong_AsLongAndOverflow(PyObject *vv, i } exit: if (do_decref) {
Py_DECREF(vv);[](#l7.99)
} return res; } @@ -630,36 +669,25 @@ static unsigned long unsigned long PyLong_AsUnsignedLongMask(register PyObject *op) {Py_DECREF(v);[](#l7.100)
- if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
nb->nb_int == NULL) {[](#l7.122)
PyErr_SetString(PyExc_TypeError, "an integer is required");[](#l7.123)
return (unsigned long)-1;[](#l7.124)
- }
- if (PyLong_Check(lo)) {
val = _PyLong_AsUnsignedLongMask((PyObject *)lo);[](#l7.134)
Py_DECREF(lo);[](#l7.135)
if (PyErr_Occurred())[](#l7.136)
return (unsigned long)-1;[](#l7.137)
return val;[](#l7.138)
- }
- else
- {
Py_DECREF(lo);[](#l7.142)
PyErr_SetString(PyExc_TypeError,[](#l7.143)
"nb_int should return int object");[](#l7.144)
return (unsigned long)-1;[](#l7.145)
- }
} int @@ -1169,40 +1197,41 @@ PyLong_AsLongLong(PyObject *vv) PY_LONG_LONG bytes; int one = 1; int res;
if (vv == NULL) { PyErr_BadInternalCall(); return -1; }
- if (!PyLong_Check(vv)) {
PyNumberMethods *nb;[](#l7.165)
PyObject *io;[](#l7.166)
if ((nb = vv->ob_type->tp_as_number) == NULL ||[](#l7.167)
nb->nb_int == NULL) {[](#l7.168)
PyErr_SetString(PyExc_TypeError, "an integer is required");[](#l7.169)
return -1;[](#l7.170)
}[](#l7.171)
io = (*nb->nb_int) (vv);[](#l7.172)
if (io == NULL)[](#l7.173)
- if (PyLong_Check(vv)) {
v = (PyLongObject *)vv;[](#l7.176)
- }
- else {
v = _PyLong_FromNbInt(vv);[](#l7.179)
if (v == NULL)[](#l7.180) return -1;[](#l7.181)
if (PyLong_Check(io)) {[](#l7.182)
bytes = PyLong_AsLongLong(io);[](#l7.183)
Py_DECREF(io);[](#l7.184)
return bytes;[](#l7.185)
}[](#l7.186)
Py_DECREF(io);[](#l7.187)
PyErr_SetString(PyExc_TypeError, "integer conversion failed");[](#l7.188)
return -1;[](#l7.189)
- }
- case -1: return -(sdigit)v->ob_digit[0];
- case 0: return 0;
- case 1: return v->ob_digit[0];
- }
- res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes,
SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);[](#l7.203)
- case -1:
bytes = -(sdigit)v->ob_digit[0];[](#l7.205)
break;[](#l7.206)
- case 0:
bytes = 0;[](#l7.208)
break;[](#l7.209)
- case 1:
bytes = v->ob_digit[0];[](#l7.211)
break;[](#l7.212)
- default:
res = _PyLong_AsByteArray((PyLongObject *)v, (unsigned char *)&bytes,[](#l7.214)
SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);[](#l7.215)
- }
- if (do_decref) {
Py_DECREF(v);[](#l7.218)
- }
/* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ if (res < 0) @@ -1283,36 +1312,25 @@ static unsigned PY_LONG_LONG unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(register PyObject *op) {
- if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
nb->nb_int == NULL) {[](#l7.241)
PyErr_SetString(PyExc_TypeError, "an integer is required");[](#l7.242)
return (unsigned PY_LONG_LONG)-1;[](#l7.243)
- }
- if (PyLong_Check(lo)) {
val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo);[](#l7.253)
Py_DECREF(lo);[](#l7.254)
if (PyErr_Occurred())[](#l7.255)
return (unsigned PY_LONG_LONG)-1;[](#l7.256)
return val;[](#l7.257)
- }
- else
- {
Py_DECREF(lo);[](#l7.261)
PyErr_SetString(PyExc_TypeError,[](#l7.262)
"nb_int should return int object");[](#l7.263)
return (unsigned PY_LONG_LONG)-1;[](#l7.264)
- }
} #undef IS_LITTLE_ENDIAN @@ -1343,28 +1361,17 @@ PyLong_AsLongLongAndOverflow(PyObject *v return -1; }
- if (!PyLong_Check(vv)) {
PyNumberMethods *nb;[](#l7.278)
nb = vv->ob_type->tp_as_number;[](#l7.279)
if (nb == NULL || nb->nb_int == NULL) {[](#l7.280)
PyErr_SetString(PyExc_TypeError,[](#l7.281)
"an integer is required");[](#l7.282)
return -1;[](#l7.283)
}[](#l7.284)
vv = (*nb->nb_int) (vv);[](#l7.285)
if (vv == NULL)[](#l7.286)
- if (PyLong_Check(vv)) {
v = (PyLongObject *)vv;[](#l7.288)
- }
- else {
v = _PyLong_FromNbInt(vv);[](#l7.291)
if (v == NULL)[](#l7.292) return -1;[](#l7.293) do_decref = 1;[](#l7.294)
if (!PyLong_Check(vv)) {[](#l7.295)
Py_DECREF(vv);[](#l7.296)
PyErr_SetString(PyExc_TypeError,[](#l7.297)
"nb_int should return int object");[](#l7.298)
return -1;[](#l7.299)
} res = -1;}[](#l7.300)
- v = (PyLongObject *)vv; i = Py_SIZE(v); switch (i) { @@ -1408,7 +1415,7 @@ PyLong_AsLongLongAndOverflow(PyObject *v } exit: if (do_decref) {
Py_DECREF(vv);[](#l7.312)