[Python-checkins] r69809 - in python/branches/release30-maint: Misc/NEWS Objects/stringlib/formatter.h

eric.smith python-checkins at python.org
Fri Feb 20 15:30:15 CET 2009


Author: eric.smith
Date: Fri Feb 20 15:30:15 2009
New Revision: 69809

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

................
  r69808 | eric.smith | 2009-02-20 09:25:03 -0500 (Fri, 20 Feb 2009) | 9 lines
  
  Merged revisions 69806 via svnmerge from 
  svn+ssh://pythondev@svn.python.org/python/trunk
  
  ........
    r69806 | eric.smith | 2009-02-20 09:02:36 -0500 (Fri, 20 Feb 2009) | 1 line
    
    Issue #5247: Improve error message when unknown format codes are used when using str.format() with str, int, and float arguments.
  ........
................


Modified:
   python/branches/release30-maint/   (props changed)
   python/branches/release30-maint/Misc/NEWS
   python/branches/release30-maint/Objects/stringlib/formatter.h

Modified: python/branches/release30-maint/Misc/NEWS
==============================================================================
--- python/branches/release30-maint/Misc/NEWS	(original)
+++ python/branches/release30-maint/Misc/NEWS	Fri Feb 20 15:30:15 2009
@@ -12,6 +12,10 @@
 Core and Builtins
 -----------------
 
+=======
+- Issue #5247: Improve error message when unknown format codes are
+  used when using str.format() with str, int, and float arguments.
+
 - Issue #5249: time.strftime returned malformed string when format string
   contained non ascii character on windows.
 

Modified: python/branches/release30-maint/Objects/stringlib/formatter.h
==============================================================================
--- python/branches/release30-maint/Objects/stringlib/formatter.h	(original)
+++ python/branches/release30-maint/Objects/stringlib/formatter.h	Fri Feb 20 15:30:15 2009
@@ -15,6 +15,34 @@
 
 #define ALLOW_PARENS_FOR_SIGN 0
 
+/* Raises an exception about an unknown presentation type for this
+ * type. */
+
+static void
+unknown_presentation_type(STRINGLIB_CHAR presentation_type,
+                          const char* type_name)
+{
+#if STRINGLIB_IS_UNICODE
+    /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,
+       hence the two cases. If it is char, gcc complains that the
+       condition below is always true, hence the ifdef. */
+    if (presentation_type > 32 && presentation_type < 128)
+#endif
+        PyErr_Format(PyExc_ValueError,
+                     "Unknown format code '%c' "
+                     "for object of type '%.200s'",
+                     presentation_type,
+                     type_name);
+#if STRINGLIB_IS_UNICODE
+    else
+        PyErr_Format(PyExc_ValueError,
+                     "Unknown format code '\\x%x' "
+                     "for object of type '%.200s'",
+                     (unsigned int)presentation_type,
+                     type_name);
+#endif
+}
+
 /*
     get_integer consumes 0 or more decimal digit characters from an
     input string, updates *result with the corresponding positive
@@ -854,19 +882,7 @@
         break;
     default:
         /* unknown */
-	#if STRINGLIB_IS_UNICODE
-	/* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,
-	   hence the two cases. If it is char, gcc complains that the
-	   condition below is always true, hence the ifdef. */
-        if (format.type > 32 && format.type <128)
-	#endif
-            PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
-                         (char)format.type);
-	#if STRINGLIB_IS_UNICODE
-	else
-            PyErr_Format(PyExc_ValueError, "Unknown conversion type '\\x%x'",
-                         (unsigned int)format.type);
-	#endif
+        unknown_presentation_type(format.type, obj->ob_type->tp_name);
         goto done;
     }
 
@@ -928,8 +944,7 @@
 
     default:
         /* unknown */
-        PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
-                     format.type);
+        unknown_presentation_type(format.type, obj->ob_type->tp_name);
         goto done;
     }
 
@@ -1031,8 +1046,7 @@
 
     default:
         /* unknown */
-        PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
-                     format.type);
+        unknown_presentation_type(format.type, obj->ob_type->tp_name);
         goto done;
     }
 


More information about the Python-checkins mailing list