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

Skip to content

Commit e5e298f

Browse files
committed
Issue #4910 (1st patch of a series): fix int() and the corresponding
PyNumber_Int/PyNumber_Long API function so that it no longer attempts to call the __long__ method for conversion. Only the __int__ and __trunc__ methods are used. (This removes a major remaining use of the nb_long slot from the Python 3.x core.) Thanks Benjamin for review.
1 parent 7c2b66c commit e5e298f

3 files changed

Lines changed: 23 additions & 17 deletions

File tree

Lib/test/test_long.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def test_long(self):
367367

368368

369369
def test_conversion(self):
370-
# Test __long__()
370+
# Test __int__()
371371
class ClassicMissingMethods:
372372
pass
373373
self.assertRaises(TypeError, int, ClassicMissingMethods())
@@ -410,18 +410,32 @@ def __int__(self):
410410
class Classic:
411411
pass
412412
for base in (object, Classic):
413-
class LongOverridesTrunc(base):
414-
def __long__(self):
413+
class IntOverridesTrunc(base):
414+
def __int__(self):
415415
return 42
416416
def __trunc__(self):
417417
return -12
418-
self.assertEqual(int(LongOverridesTrunc()), 42)
418+
self.assertEqual(int(IntOverridesTrunc()), 42)
419419

420420
class JustTrunc(base):
421421
def __trunc__(self):
422422
return 42
423423
self.assertEqual(int(JustTrunc()), 42)
424424

425+
class JustLong(base):
426+
# test that __long__ no longer used in 3.x
427+
def __long__(self):
428+
return 42
429+
self.assertRaises(TypeError, int, JustLong())
430+
431+
class LongTrunc(base):
432+
# __long__ should be ignored in 3.x
433+
def __long__(self):
434+
return 42
435+
def __trunc__(self):
436+
return 1729
437+
self.assertEqual(int(LongTrunc()), 1729)
438+
425439
for trunc_result_base in (object, Classic):
426440
class Integral(trunc_result_base):
427441
def __int__(self):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 3.1 alpha 0
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #4910: Builtin int() function and PyNumber_Long/PyNumber_Int API
16+
function no longer attempt to call the __long__ slot to convert an object
17+
to an integer. Only the __int__ and __trunc__ slots are examined.
18+
1519
- Issue #4893: Use NT threading on CE.
1620

1721
- Issue #4915: Port sysmodule to Windows CE.

Objects/abstract.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,19 +1379,7 @@ PyNumber_Long(PyObject *o)
13791379
}
13801380
return res;
13811381
}
1382-
if (m && m->nb_long) { /* This should include subclasses of long */
1383-
/* Classic classes always take this branch. */
1384-
PyObject *res = m->nb_long(o);
1385-
if (res && !PyLong_Check(res)) {
1386-
PyErr_Format(PyExc_TypeError,
1387-
"__long__ returned non-long (type %.200s)",
1388-
res->ob_type->tp_name);
1389-
Py_DECREF(res);
1390-
return NULL;
1391-
}
1392-
return res;
1393-
}
1394-
if (PyLong_Check(o)) /* A long subclass without nb_long */
1382+
if (PyLong_Check(o)) /* An int subclass without nb_int */
13951383
return _PyLong_Copy((PyLongObject *)o);
13961384
trunc_func = PyObject_GetAttr(o, trunc_name);
13971385
if (trunc_func) {

0 commit comments

Comments
 (0)