Index: Objects/unicodeobject.c =================================================================== --- Objects/unicodeobject.c (revision 43573) +++ Objects/unicodeobject.c (working copy) @@ -5282,7 +5282,30 @@ return result; } +PyDoc_STRVAR(format__doc__, +"S.format(ARGS | KWARGS) -> str\n\ +\n\ +format the string."); + static PyObject * +unicode_format(PyUnicodeObject *self, PyObject *args, PyObject *kwds) +{ + int a, k; + a = PyObject_Length(args); + k = PyObject_Length(kwds); + if ((a > 0) && (k > 0)) { + PyErr_SetString \ + (PyExc_ValueError, + "format requires positional or keyword args, not both"); + return NULL; + } else if (k > 0) { + return PyUnicode_Format(self, kwds); + } else { + return PyUnicode_Format(self, args); + } +} + +static PyObject * unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) { if (index < 0 || index >= self->length) { @@ -6391,6 +6414,8 @@ {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, + {"format", (PyCFunction) unicode_format, METH_VARARGS | METH_KEYWORDS, + format__doc__}, {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, Index: Objects/stringobject.c =================================================================== --- Objects/stringobject.c (revision 43573) +++ Objects/stringobject.c (working copy) @@ -1806,7 +1806,29 @@ return PyInt_FromSsize_t(result); } +PyDoc_STRVAR(format__doc__, +"S.format(ARGS | KWARGS) -> str\n\ +\n\ +format the string."); +static PyObject * +string_format(PyStringObject *self, PyObject *args, PyObject *kwds) +{ + int a, k; + a = PyObject_Length(args); + k = PyObject_Length(kwds); + if ((a > 0) && (k > 0)) { + PyErr_SetString \ + (PyExc_ValueError, + "format requires positional or keyword args, not both"); + return NULL; + } else if (k > 0) { + return PyString_Format(self, kwds); + } else { + return PyString_Format(self, args); + } +} + PyDoc_STRVAR(index__doc__, "S.index(sub [,start [,end]]) -> int\n\ \n\ @@ -3306,6 +3328,8 @@ {"endswith", (PyCFunction)string_endswith, METH_VARARGS, endswith__doc__}, {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__}, + {"format", (PyCFunction)string_format, METH_VARARGS | METH_KEYWORDS, + format__doc__}, {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__}, {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__}, {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__},