[Python-3000-checkins] r62269 - in python/branches/py3k: Lib/test/test_getargs2.py Objects/abstract.c Python/getargs.c

trent.nelson python-3000-checkins at python.org
Thu Apr 10 18:25:37 CEST 2008


Author: trent.nelson
Date: Thu Apr 10 18:25:37 2008
New Revision: 62269

Modified:
   python/branches/py3k/Lib/test/test_getargs2.py
   python/branches/py3k/Objects/abstract.c
   python/branches/py3k/Python/getargs.c
Log:
Issue 2440: fix the handling of %n in Python/getargs.c's convertsimple(), extend Objects/abstract.c's PyNumber_Index() to accept PyObjects that have nb_int slots, and update test_getargs2 to test that an exception is thrown when __int__() returns a non-int object.

Modified: python/branches/py3k/Lib/test/test_getargs2.py
==============================================================================
--- python/branches/py3k/Lib/test/test_getargs2.py	(original)
+++ python/branches/py3k/Lib/test/test_getargs2.py	Thu Apr 10 18:25:37 2008
@@ -63,6 +63,10 @@
     def __int__(self):
         return 99
 
+class InvalidLongAsString:
+    def __int__(self):
+        return 'foobar'
+
 class Unsigned_TestCase(unittest.TestCase):
     def test_b(self):
         from _testcapi import getargs_b
@@ -199,6 +203,7 @@
         self.failUnlessEqual(42, getargs_n(42))
         self.assertRaises(OverflowError, getargs_n, VERY_LARGE)
 
+        self.assertRaises(TypeError, getargs_n, InvalidLongAsString())
 
 class LongLong_TestCase(unittest.TestCase):
     def test_L(self):

Modified: python/branches/py3k/Objects/abstract.c
==============================================================================
--- python/branches/py3k/Objects/abstract.c	(original)
+++ python/branches/py3k/Objects/abstract.c	Thu Apr 10 18:25:37 2008
@@ -1220,6 +1220,7 @@
 PyObject *
 PyNumber_Index(PyObject *item)
 {
+	PyNumberMethods *m;
 	PyObject *result = NULL;
 	if (item == NULL)
 		return null_error();
@@ -1227,8 +1228,9 @@
 		Py_INCREF(item);
 		return item;
 	}
+	m = item->ob_type->tp_as_number;
 	if (PyIndex_Check(item)) {
-		result = item->ob_type->tp_as_number->nb_index(item);
+		result = m->nb_index(item);
 		if (result && !PyLong_Check(result)) {
 			PyErr_Format(PyExc_TypeError,
 				     "__index__ returned non-int "
@@ -1238,7 +1240,17 @@
 			return NULL;
 		}
 	}
-	else {
+	else if (m && m->nb_int != NULL) {
+		result = m->nb_int(item);
+		if (result && !PyLong_Check(result)) {
+			PyErr_Format(PyExc_TypeError,
+				     "__int__ returned non-int "
+				     "(type %.200s)",
+				     result->ob_type->tp_name);
+			Py_DECREF(result);
+			return NULL;
+		}
+	} else {
 		PyErr_Format(PyExc_TypeError,
 			     "'%.200s' object cannot be interpreted "
 			     "as an integer", item->ob_type->tp_name);

Modified: python/branches/py3k/Python/getargs.c
==============================================================================
--- python/branches/py3k/Python/getargs.c	(original)
+++ python/branches/py3k/Python/getargs.c	Thu Apr 10 18:25:37 2008
@@ -663,7 +663,6 @@
 	}
 
 	case 'n': /* Py_ssize_t */
-#if SIZEOF_SIZE_T != SIZEOF_LONG
 	{
 		PyObject *iobj;
 		Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
@@ -672,14 +671,13 @@
 			return converterr("integer<n>", arg, msgbuf, bufsize);
 		iobj = PyNumber_Index(arg);
 		if (iobj != NULL)
-			ival = PyLong_AsSsize_t(arg);
+			ival = PyLong_AsSsize_t(iobj);
 		if (ival == -1 && PyErr_Occurred())
 			return converterr("integer<n>", arg, msgbuf, bufsize);
 		*p = ival;
 		break;
 	}
-#endif
-	/* Fall through from 'n' to 'l' if Py_ssize_t is int */
+
 	case 'l': {/* long int */
 		long *p = va_arg(*p_va, long *);
 		long ival;


More information about the Python-3000-checkins mailing list