diff Objects/stringlib/string_format.h @ 65940:05eed9f0ce01

Issue #6081: Add str.format_map. str.format_map(mapping) is similar to str.format(**mapping), except mapping does not get converted to a dict.
author Eric Smith <eric@trueblade.com>
date Thu, 04 Nov 2010 17:06:58 +0000
parents fd044cda97d8
children 4c1c48b3af66
line wrap: on
line diff
--- a/Objects/stringlib/string_format.h	Thu Nov 04 16:51:32 2010 +0000
+++ b/Objects/stringlib/string_format.h	Thu Nov 04 17:06:58 2010 +0000
@@ -499,7 +499,11 @@
         PyObject *key = SubString_new_object(&first);
         if (key == NULL)
             goto error;
-        if ((kwargs == NULL) || (obj = PyDict_GetItem(kwargs, key)) == NULL) {
+
+        /* Use PyObject_GetItem instead of PyDict_GetItem because this
+           code is no longer just used with kwargs. It might be passed
+           a non-dict when called through format_map. */
+        if ((kwargs == NULL) || (obj = PyObject_GetItem(kwargs, key)) == NULL) {
             PyErr_SetObject(PyExc_KeyError, key);
             Py_DECREF(key);
             goto error;
@@ -1039,6 +1043,11 @@
     return build_string(&input, args, kwargs, recursion_depth, &auto_number);
 }
 
+static PyObject *
+do_string_format_map(PyObject *self, PyObject *obj)
+{
+    return do_string_format(self, NULL, obj);
+}
 
 
 /************************************************************************/