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

/* _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 BadInt:

+ +class BadInt2:

+ +class BadInt3(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.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(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(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(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(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.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.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.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.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.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(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):

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)

+

+

+

+

+

+ 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

-

-

-

-

- self.assertEqual(int(Foo0()), 42)

class Classic: pass @@ -351,6 +326,57 @@ class IntTestCases(unittest.TestCase): with self.assertRaises(TypeError): int(TruncReturnsBadInt())

+

+

+

+

+

+

+

+

+

+

+

+ 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()

--- 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; }

-} - - 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 */

--- 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

+

+

+

+} + + /* 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; }

+

-

-

+

+

} 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; }

+

-

+

/* 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) {

+

-

-

+

+

} #undef IS_LITTLE_ENDIAN @@ -1343,28 +1361,17 @@ PyLong_AsLongLongAndOverflow(PyObject *v return -1; }