[Python-checkins] cpython (3.2): Closes #12579. Positional fields with str.format_map() now raise a ValueError

eric.smith python-checkins at python.org
Mon Jul 18 20:03:52 CEST 2011


http://hg.python.org/cpython/rev/f6d074a7bbd4
changeset:   71409:f6d074a7bbd4
branch:      3.2
parent:      71405:4d5a546b6186
user:        Eric V. Smith <eric at trueblade.com>
date:        Mon Jul 18 14:03:41 2011 -0400
summary:
  Closes #12579. Positional fields with str.format_map() now raise a ValueError instead of SystemError.

files:
  Lib/test/test_unicode.py          |   5 +++++
  Misc/ACKS                         |   1 +
  Misc/NEWS                         |   4 ++++
  Objects/stringlib/string_format.h |  10 ++++++++++
  4 files changed, 20 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -736,6 +736,11 @@
         self.assertRaises(TypeError, '{a'.format_map)
         self.assertRaises(TypeError, '}a'.format_map)
 
+        # issue #12579: can't supply positional params to format_map
+        self.assertRaises(ValueError, '{}'.format_map, {'a' : 2})
+        self.assertRaises(ValueError, '{}'.format_map, 'a')
+        self.assertRaises(ValueError, '{a} {}'.format_map, {"a" : 2, "b" : 1})
+
     def test_format_auto_numbering(self):
         class C:
             def __init__(self, x=100):
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -78,6 +78,7 @@
 Andrew Bennetts
 Andy Bensky
 Michel Van den Bergh
+Julian Berman
 Eric Beser
 Steven Bethard
 Stephen Bevan
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@
 Core and Builtins
 -----------------
 
+- Issue #12579: str.format_map() now raises a ValueError if used on a
+  format string that contains positional fields. Initial patch by
+  Julian Berman.
+
 - Issue #11627: Fix segfault when __new__ on a exception returns a non-exception
   class.
 
diff --git a/Objects/stringlib/string_format.h b/Objects/stringlib/string_format.h
--- a/Objects/stringlib/string_format.h
+++ b/Objects/stringlib/string_format.h
@@ -511,6 +511,16 @@
         Py_DECREF(key);
     }
     else {
+        /* If args is NULL, we have a format string with a positional field
+           with only kwargs to retrieve it from. This can only happen when
+           used with format_map(), where positional arguments are not
+           allowed. */
+        if (args == NULL) {
+            PyErr_SetString(PyExc_ValueError, "Format string contains "
+                            "positional fields");
+            goto error;
+        }
+
         /* look up in args */
         obj = PySequence_GetItem(args, index);
         if (obj == NULL)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list