mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
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.
This commit is contained in:
parent
7c2b66cc02
commit
e5e298f875
3 changed files with 23 additions and 17 deletions
|
@ -367,7 +367,7 @@ class LongTest(unittest.TestCase):
|
|||
|
||||
|
||||
def test_conversion(self):
|
||||
# Test __long__()
|
||||
# Test __int__()
|
||||
class ClassicMissingMethods:
|
||||
pass
|
||||
self.assertRaises(TypeError, int, ClassicMissingMethods())
|
||||
|
@ -410,18 +410,32 @@ class LongTest(unittest.TestCase):
|
|||
class Classic:
|
||||
pass
|
||||
for base in (object, Classic):
|
||||
class LongOverridesTrunc(base):
|
||||
def __long__(self):
|
||||
class IntOverridesTrunc(base):
|
||||
def __int__(self):
|
||||
return 42
|
||||
def __trunc__(self):
|
||||
return -12
|
||||
self.assertEqual(int(LongOverridesTrunc()), 42)
|
||||
self.assertEqual(int(IntOverridesTrunc()), 42)
|
||||
|
||||
class JustTrunc(base):
|
||||
def __trunc__(self):
|
||||
return 42
|
||||
self.assertEqual(int(JustTrunc()), 42)
|
||||
|
||||
class JustLong(base):
|
||||
# test that __long__ no longer used in 3.x
|
||||
def __long__(self):
|
||||
return 42
|
||||
self.assertRaises(TypeError, int, JustLong())
|
||||
|
||||
class LongTrunc(base):
|
||||
# __long__ should be ignored in 3.x
|
||||
def __long__(self):
|
||||
return 42
|
||||
def __trunc__(self):
|
||||
return 1729
|
||||
self.assertEqual(int(LongTrunc()), 1729)
|
||||
|
||||
for trunc_result_base in (object, Classic):
|
||||
class Integral(trunc_result_base):
|
||||
def __int__(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue