[Python-checkins] r68556 - in python/branches/release30-maint: Lib/test/test_long.py Misc/NEWS Objects/abstract.c

mark.dickinson python-checkins at python.org
Mon Jan 12 22:53:17 CET 2009


Author: mark.dickinson
Date: Mon Jan 12 22:53:16 2009
New Revision: 68556

Log:
Merged revisions 68553 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r68553 | mark.dickinson | 2009-01-12 20:49:19 +0000 (Mon, 12 Jan 2009) | 8 lines
  
  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.
........


Modified:
   python/branches/release30-maint/   (props changed)
   python/branches/release30-maint/Lib/test/test_long.py
   python/branches/release30-maint/Misc/NEWS
   python/branches/release30-maint/Objects/abstract.c

Modified: python/branches/release30-maint/Lib/test/test_long.py
==============================================================================
--- python/branches/release30-maint/Lib/test/test_long.py	(original)
+++ python/branches/release30-maint/Lib/test/test_long.py	Mon Jan 12 22:53:16 2009
@@ -366,7 +366,7 @@
 
 
     def test_conversion(self):
-        # Test __long__()
+        # Test __int__()
         class ClassicMissingMethods:
             pass
         self.assertRaises(TypeError, int, ClassicMissingMethods())
@@ -409,18 +409,32 @@
         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):

Modified: python/branches/release30-maint/Misc/NEWS
==============================================================================
--- python/branches/release30-maint/Misc/NEWS	(original)
+++ python/branches/release30-maint/Misc/NEWS	Mon Jan 12 22:53:16 2009
@@ -12,6 +12,10 @@
 Core and Builtins
 -----------------
 
+- Issue #4910: Builtin int() function and PyNumber_Long/PyNumber_Int API
+  function no longer attempt to call the __long__ slot to convert an object
+  to an integer.  Only the __int__ and __trunc__ slots are examined.
+
 - Issue #4604: Some objects of the I/O library could still be used after 
   having been closed (for instance, a read() call could return some
   previously buffered data). Patch by Dmitry Vasiliev.

Modified: python/branches/release30-maint/Objects/abstract.c
==============================================================================
--- python/branches/release30-maint/Objects/abstract.c	(original)
+++ python/branches/release30-maint/Objects/abstract.c	Mon Jan 12 22:53:16 2009
@@ -1379,19 +1379,7 @@
 		}
 		return res;
 	}
-	if (m && m->nb_long) { /* This should include subclasses of long */
-		/* Classic classes always take this branch. */
-		PyObject *res = m->nb_long(o);
-		if (res && !PyLong_Check(res)) {
-			PyErr_Format(PyExc_TypeError,
-				     "__long__ returned non-long (type %.200s)",
-				     res->ob_type->tp_name);
-			Py_DECREF(res);
-			return NULL;
-		}
-		return res;
-	}
-	if (PyLong_Check(o)) /* A long subclass without nb_long */
+	if (PyLong_Check(o)) /* An int subclass without nb_int */
 		return _PyLong_Copy((PyLongObject *)o);
 	trunc_func = PyObject_GetAttr(o, trunc_name);
 	if (trunc_func) {


More information about the Python-checkins mailing list