[Python-checkins] r52306 - in python/branches/release25-maint: Lib/test/test_builtin.py Misc/NEWS Objects/intobject.c

georg.brandl python-checkins at python.org
Thu Oct 12 13:28:05 CEST 2006


Author: georg.brandl
Date: Thu Oct 12 13:28:04 2006
New Revision: 52306

Modified:
   python/branches/release25-maint/Lib/test/test_builtin.py
   python/branches/release25-maint/Misc/NEWS
   python/branches/release25-maint/Objects/intobject.c
Log:
Bug #1545497: when given an explicit base, int() did ignore NULs
embedded in the string to convert.
 (backport from rev. 52305)

Modified: python/branches/release25-maint/Lib/test/test_builtin.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_builtin.py	(original)
+++ python/branches/release25-maint/Lib/test/test_builtin.py	Thu Oct 12 13:28:04 2006
@@ -729,6 +729,11 @@
         self.assertRaises(ValueError, int, '123\0')
         self.assertRaises(ValueError, int, '53', 40)
 
+        # SF bug 1545497: embedded NULs were not detected with
+        # explicit base
+        self.assertRaises(ValueError, int, '123\0', 10)
+        self.assertRaises(ValueError, int, '123\x00 245', 20)
+
         x = int('1' * 600)
         self.assert_(isinstance(x, long))
 

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Thu Oct 12 13:28:04 2006
@@ -12,6 +12,9 @@
 Core and builtins
 -----------------
 
+- Bug #1545497: when given an explicit base, int() did ignore NULs
+  embedded in the string to convert.
+
 - Bug #1569998: break inside a try statement (outside a loop) is now
   recognized and rejected.
 

Modified: python/branches/release25-maint/Objects/intobject.c
==============================================================================
--- python/branches/release25-maint/Objects/intobject.c	(original)
+++ python/branches/release25-maint/Objects/intobject.c	Thu Oct 12 13:28:04 2006
@@ -987,8 +987,25 @@
 		return PyInt_FromLong(0L);
 	if (base == -909)
 		return PyNumber_Int(x);
-	if (PyString_Check(x))
-		return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
+	if (PyString_Check(x)) {
+		/* Since PyInt_FromString doesn't have a length parameter,
+		 * check here for possible NULs in the string. */
+		char *string = PyString_AS_STRING(x);
+		if (strlen(string) != PyString_Size(x)) {
+			/* create a repr() of the input string,
+			 * just like PyInt_FromString does */
+			PyObject *srepr;
+			srepr = PyObject_Repr(x);
+			if (srepr == NULL)
+				return NULL;
+			PyErr_Format(PyExc_ValueError,
+			     "invalid literal for int() with base %d: %s",
+			     base, PyString_AS_STRING(srepr));
+			Py_DECREF(srepr);
+			return NULL;
+		}
+		return PyInt_FromString(string, NULL, base);
+	}
 #ifdef Py_USING_UNICODE
 	if (PyUnicode_Check(x))
 		return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),


More information about the Python-checkins mailing list