diff options
author | Eric Smith <eric@trueblade.com> | 2007-08-25 02:26:07 (GMT) |
---|---|---|
committer | Eric Smith <eric@trueblade.com> | 2007-08-25 02:26:07 (GMT) |
commit | 8c6632636807c35bee40210ed8483c1eca82664f (patch) | |
tree | 50f386d98ce14116eaf9d83085b82ff11bdb3e69 /Python/sysmodule.c | |
parent | e4dc32488446240942123cf4e9e7296ad97e20bf (diff) | |
download | cpython-8c6632636807c35bee40210ed8483c1eca82664f.zip cpython-8c6632636807c35bee40210ed8483c1eca82664f.tar.gz cpython-8c6632636807c35bee40210ed8483c1eca82664f.tar.bz2 |
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.
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r-- | Python/sysmodule.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 6ccd3e9..10a7427 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -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}, |