diff options
author | Eric Smith <eric@trueblade.com> | 2010-11-04 17:06:58 (GMT) |
---|---|---|
committer | Eric Smith <eric@trueblade.com> | 2010-11-04 17:06:58 (GMT) |
commit | 27bbca6f79690472c7beff8020ff331b14450b77 (patch) | |
tree | 3b6ea39069caf2f197656092c45e9cba0b384b85 /Objects | |
parent | 2397dd58b70986db898f689fe7a1597cdd51f29f (diff) | |
download | cpython-27bbca6f79690472c7beff8020ff331b14450b77.zip cpython-27bbca6f79690472c7beff8020ff331b14450b77.tar.gz cpython-27bbca6f79690472c7beff8020ff331b14450b77.tar.bz2 |
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.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/stringlib/string_format.h | 11 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 6 |
2 files changed, 16 insertions, 1 deletions
diff --git a/Objects/stringlib/string_format.h b/Objects/stringlib/string_format.h index 126c870..5205aa9 100644 --- a/Objects/stringlib/string_format.h +++ b/Objects/stringlib/string_format.h @@ -499,7 +499,11 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs, 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 @@ do_string_format(PyObject *self, PyObject *args, PyObject *kwargs) 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); +} /************************************************************************/ diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 17dc27e..b67b8f9 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -9028,6 +9028,11 @@ PyDoc_STRVAR(format__doc__, \n\ "); +PyDoc_STRVAR(format_map__doc__, + "S.format_map(mapping) -> str\n\ +\n\ +"); + static PyObject * unicode__format__(PyObject* self, PyObject* args) { @@ -9109,6 +9114,7 @@ static PyMethodDef unicode_methods[] = { {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, + {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS | METH_STATIC, maketrans__doc__}, |