diff Objects/stringobject.c @ 45381:b7231bf6bfb7 legacy-trunk

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).
author Eric Smith <eric@trueblade.com>
date Sun, 17 Feb 2008 19:46:49 +0000
parents 26384b8180c7
children c03aff6d07f5
line wrap: on
line diff
--- a/Objects/stringobject.c	Sun Feb 17 18:59:29 2008 +0000
+++ b/Objects/stringobject.c	Sun Feb 17 19:46:49 2008 +0000
@@ -4,6 +4,8 @@
 
 #include "Python.h"
 
+#include "formatter_string.h"
+
 #include <ctype.h>
 
 #ifdef COUNT_ALLOCS
@@ -771,15 +773,7 @@
 /* -------------------------------------------------------------------- */
 /* 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 @@
 	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 @@
 	{"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,