Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12ebefc

Browse files
committed
Closes #12579. Positional fields with str.format_map() now raise a ValueError instead of SystemError.
1 parent b899007 commit 12ebefc

4 files changed

Lines changed: 20 additions & 0 deletions

File tree

Lib/test/test_unicode.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,11 @@ def __format__(self, spec):
736736
self.assertRaises(TypeError, '{a'.format_map)
737737
self.assertRaises(TypeError, '}a'.format_map)
738738

739+
# issue #12579: can't supply positional params to format_map
740+
self.assertRaises(ValueError, '{}'.format_map, {'a' : 2})
741+
self.assertRaises(ValueError, '{}'.format_map, 'a')
742+
self.assertRaises(ValueError, '{a} {}'.format_map, {"a" : 2, "b" : 1})
743+
739744
def test_format_auto_numbering(self):
740745
class C:
741746
def __init__(self, x=100):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Eli Bendersky
7878
Andrew Bennetts
7979
Andy Bensky
8080
Michel Van den Bergh
81+
Julian Berman
8182
Eric Beser
8283
Steven Bethard
8384
Stephen Bevan

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.2.2?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #12579: str.format_map() now raises a ValueError if used on a
14+
format string that contains positional fields. Initial patch by
15+
Julian Berman.
16+
1317
- Issue #11627: Fix segfault when __new__ on a exception returns a non-exception
1418
class.
1519

Objects/stringlib/string_format.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,16 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs,
511511
Py_DECREF(key);
512512
}
513513
else {
514+
/* If args is NULL, we have a format string with a positional field
515+
with only kwargs to retrieve it from. This can only happen when
516+
used with format_map(), where positional arguments are not
517+
allowed. */
518+
if (args == NULL) {
519+
PyErr_SetString(PyExc_ValueError, "Format string contains "
520+
"positional fields");
521+
goto error;
522+
}
523+
514524
/* look up in args */
515525
obj = PySequence_GetItem(args, index);
516526
if (obj == NULL)

0 commit comments

Comments
 (0)