Implementation of PEP 3101, Advanced String Formatting.

Known issues:

The string.Formatter class, as discussed in the PEP, is incomplete.

Error handling needs to conform to the PEP.

Need to fix this warning that I introduced in Python/formatter_unicode.c:
Objects/stringlib/unicodedefs.h:26: warning: `STRINGLIB_CMP' defined but not used

Need to make sure sign formatting is correct, more tests needed.

Need to remove '()' sign formatting, left over from an earlier version of the PEP.
This commit is contained in:
Eric Smith 2007-08-25 02:26:07 +00:00
parent e4dc324884
commit 8c66326368
22 changed files with 2669 additions and 18 deletions

View file

@ -660,6 +660,54 @@ sys_current_frames(PyObject *self, PyObject *noargs)
return _PyThread_CurrentFrames();
}
/* sys_formatter_iterator is used to implement
string.Formatter.vformat. it parses a string and returns tuples
describing the parsed elements. see unicodeobject.c's
_unicodeformatter_iterator for details */
static PyObject *
sys_formatter_iterator(PyObject *self, PyObject *args)
{
/* in 2.6, check type and dispatch to unicode or string
accordingly */
PyObject *str;
if (!PyArg_ParseTuple(args, "O:_formatter_iterator", &str))
return NULL;
if (!PyUnicode_Check(str)) {
PyErr_SetString(PyExc_TypeError,
"_formatter_iterator expects unicode object");
return NULL;
}
return _unicodeformatter_iterator(str);
}
/* sys_formatter_lookup is used to implement string.Formatter.vformat.
it takes an PEP 3101 "field name", args, and kwargs, and returns a
tuple (index, name, object). see unicodeobject.c's
_unicodeformatter_lookup for details */
static PyObject *
sys_formatter_lookup(PyObject *self, PyObject *args)
{
PyObject *field_name;
PyObject *arg_args;
PyObject *kwargs;
if (!PyArg_ParseTuple(args, "OOO:_formatter_lookup", &field_name,
&arg_args, &kwargs))
return NULL;
if (!PyUnicode_Check(field_name)) {
PyErr_SetString(PyExc_TypeError,
"_formatter_lookup expects unicode object");
return NULL;
}
return _unicodeformatter_lookup(field_name, arg_args, kwargs);
}
PyDoc_STRVAR(call_tracing_doc,
"call_tracing(func, args) -> object\n\
\n\
@ -724,6 +772,8 @@ static PyMethodDef sys_methods[] = {
callstats_doc},
{"_current_frames", sys_current_frames, METH_NOARGS,
current_frames_doc},
{"_formatter_parser", sys_formatter_iterator, METH_VARARGS},
{"_formatter_lookup", sys_formatter_lookup, METH_VARARGS},
{"displayhook", sys_displayhook, METH_O, displayhook_doc},
{"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
{"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},