diff options
author | Eric Smith <eric@trueblade.com> | 2008-02-17 19:46:49 (GMT) |
---|---|---|
committer | Eric Smith <eric@trueblade.com> | 2008-02-17 19:46:49 (GMT) |
commit | a9f7d6248032c9572b4d2024a1be8bd2823af09f (patch) | |
tree | 5465a1051312055678248db0076d314924ee4ebc /Objects/stringobject.c | |
parent | e139688d34cc12b23d3a310f10d4f440f75f7d08 (diff) | |
download | cpython-a9f7d6248032c9572b4d2024a1be8bd2823af09f.zip cpython-a9f7d6248032c9572b4d2024a1be8bd2823af09f.tar.gz cpython-a9f7d6248032c9572b4d2024a1be8bd2823af09f.tar.bz2 |
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).
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r-- | Objects/stringobject.c | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 21f59ac..392da93 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -4,6 +4,8 @@ #include "Python.h" +#include "formatter_string.h" + #include <ctype.h> #ifdef COUNT_ALLOCS @@ -771,15 +773,7 @@ PyString_AsStringAndSize(register PyObject *obj, /* -------------------------------------------------------------------- */ /* Methods */ -#define STRINGLIB_CHAR char - -#define STRINGLIB_CMP memcmp -#define STRINGLIB_LEN PyString_GET_SIZE -#define STRINGLIB_NEW PyString_FromStringAndSize -#define STRINGLIB_STR PyString_AS_STRING - -#define STRINGLIB_EMPTY nullstring - +#include "stringlib/stringdefs.h" #include "stringlib/fastsearch.h" #include "stringlib/count.h" @@ -3910,6 +3904,19 @@ string_getnewargs(PyStringObject *v) return Py_BuildValue("(s#)", v->ob_sval, Py_SIZE(v)); } + +#include "stringlib/string_format.h" + +PyDoc_STRVAR(format__doc__, +"S.format(*args, **kwargs) -> unicode\n\ +\n\ +"); + +PyDoc_STRVAR(p_format__doc__, +"S.__format__(format_spec) -> unicode\n\ +\n\ +"); + static PyMethodDef string_methods[] = { @@ -3954,6 +3961,10 @@ string_methods[] = { {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__}, {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__}, {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__}, + {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, + {"__format__", (PyCFunction) string__format__, METH_VARARGS, p_format__doc__}, + {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, + {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, {"encode", (PyCFunction)string_encode, METH_VARARGS, encode__doc__}, {"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__}, {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, |