mirror of
https://github.com/python/cpython.git
synced 2025-07-29 22:24:49 +00:00
Backport of PEP 3101, Advanced String Formatting, from py3k.
Highlights: - Adding PyObject_Format. - Adding string.Format class. - Adding __format__ for str, unicode, int, long, float, datetime. - Adding builtin format. - Adding ''.format and u''.format. - str/unicode fixups for formatters. The files in Objects/stringlib that implement PEP 3101 (stringdefs.h, unicodedefs.h, formatter.h, string_format.h) are identical in trunk and py3k. Any changes from here on should be made to trunk, and changes will propogate to py3k).
This commit is contained in:
parent
e139688d34
commit
a9f7d62480
27 changed files with 3873 additions and 23 deletions
|
@ -3210,11 +3210,57 @@ object_reduce_ex(PyObject *self, PyObject *args)
|
|||
return _common_reduce(self, proto);
|
||||
}
|
||||
|
||||
/*
|
||||
from PEP 3101, this code implements:
|
||||
|
||||
class object:
|
||||
def __format__(self, format_spec):
|
||||
if isinstance(format_spec, str):
|
||||
return format(str(self), format_spec)
|
||||
elif isinstance(format_spec, unicode):
|
||||
return format(unicode(self), format_spec)
|
||||
*/
|
||||
static PyObject *
|
||||
object_format(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *format_spec;
|
||||
PyObject *self_as_str = NULL;
|
||||
PyObject *result = NULL;
|
||||
PyObject *format_meth = NULL;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
|
||||
return NULL;
|
||||
if (PyUnicode_Check(format_spec)) {
|
||||
self_as_str = PyObject_Unicode(self);
|
||||
} else if (PyString_Check(format_spec)) {
|
||||
self_as_str = PyObject_Str(self);
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "argument to __format__ must be unicode or str");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (self_as_str != NULL) {
|
||||
/* find the format function */
|
||||
format_meth = PyObject_GetAttrString(self_as_str, "__format__");
|
||||
if (format_meth != NULL) {
|
||||
/* and call it */
|
||||
result = PyObject_CallFunctionObjArgs(format_meth, format_spec, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
Py_XDECREF(self_as_str);
|
||||
Py_XDECREF(format_meth);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static PyMethodDef object_methods[] = {
|
||||
{"__reduce_ex__", object_reduce_ex, METH_VARARGS,
|
||||
PyDoc_STR("helper for pickle")},
|
||||
{"__reduce__", object_reduce, METH_VARARGS,
|
||||
PyDoc_STR("helper for pickle")},
|
||||
{"__format__", object_format, METH_VARARGS,
|
||||
PyDoc_STR("default object formatter")},
|
||||
{0}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue