summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2009-02-20 14:07:55 (GMT)
committerEric Smith <eric@trueblade.com>2009-02-20 14:07:55 (GMT)
commit1718b47328b7d228edbc395ef44fbb380b8c8ed8 (patch)
tree7f1f605ba023f0e116763957cdc1714c9f5113d3
parentb2d412117cc075328603f4f8a3b901cf286a9280 (diff)
downloadcpython-1718b47328b7d228edbc395ef44fbb380b8c8ed8.zip
cpython-1718b47328b7d228edbc395ef44fbb380b8c8ed8.tar.gz
cpython-1718b47328b7d228edbc395ef44fbb380b8c8ed8.tar.bz2
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, unicode, long, int, and float arguments. ........
-rw-r--r--Misc/NEWS4
-rw-r--r--Objects/stringlib/formatter.h48
2 files changed, 35 insertions, 17 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index f95da43..97688f2 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 2.6.2
Core and Builtins
-----------------
+- Issue #5247: Improve error message when unknown format codes are
+ used when using str.format() with str, unicode, long, int, and
+ float arguments.
+
- Running Python with the -3 option now also warns about classic division
for ints and longs.
diff --git a/Objects/stringlib/formatter.h b/Objects/stringlib/formatter.h
index aa99123..2e3e5a6 100644
--- a/Objects/stringlib/formatter.h
+++ b/Objects/stringlib/formatter.h
@@ -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 @@ FORMAT_STRING(PyObject *obj,
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 @@ format_int_or_long(PyObject* obj,
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 @@ FORMAT_FLOAT(PyObject *obj,
default:
/* unknown */
- PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
- format.type);
+ unknown_presentation_type(format.type, obj->ob_type->tp_name);
goto done;
}