summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2008-02-17 19:48:00 (GMT)
committerEric Smith <eric@trueblade.com>2008-02-17 19:48:00 (GMT)
commit8fd3eba0501a77eec463179f529f56025ff4b40b (patch)
treed459220bd52a30ce3de89b89ed1db077e7b5d4a4 /Python
parent18c66898b0a14761786161c07d89d65c8f088601 (diff)
downloadcpython-8fd3eba0501a77eec463179f529f56025ff4b40b.zip
cpython-8fd3eba0501a77eec463179f529f56025ff4b40b.tar.gz
cpython-8fd3eba0501a77eec463179f529f56025ff4b40b.tar.bz2
Fixes for shared 2.6 code that implements PEP 3101, advanced string
formatting. Includes: - Modifying tests for basic types to use __format__ methods, instead of builtin "format". - Adding PyObject_Format. - General str/unicode cleanup discovered when backporting to 2.6. - Removing datetimemodule.c's time_format, since it was identical to date_format. 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 'Python')
-rw-r--r--Python/bltinmodule.c57
1 files changed, 6 insertions, 51 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 964fb32..70237bf 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -304,58 +304,13 @@ If the predicate is None, 'lambda x: bool(x)' is assumed.\n\
static PyObject *
builtin_format(PyObject *self, PyObject *args)
{
- static PyObject * format_str = NULL;
PyObject *value;
- PyObject *spec = NULL;
- PyObject *meth;
- PyObject *empty = NULL;
- PyObject *result = NULL;
-
- /* Initialize cached value */
- if (format_str == NULL) {
- /* Initialize static variable needed by _PyType_Lookup */
- format_str = PyUnicode_FromString("__format__");
- if (format_str == NULL)
- goto done;
- }
-
- if (!PyArg_ParseTuple(args, "O|U:format", &value, &spec))
- goto done;
-
- /* initialize the default value */
- if (spec == NULL) {
- empty = PyUnicode_FromUnicode(NULL, 0);
- spec = empty;
- }
-
- /* Make sure the type is initialized. float gets initialized late */
- if (Py_TYPE(value)->tp_dict == NULL)
- if (PyType_Ready(Py_TYPE(value)) < 0)
- goto done;
-
- /* Find the (unbound!) __format__ method (a borrowed reference) */
- meth = _PyType_Lookup(Py_TYPE(value), format_str);
- if (meth == NULL) {
- PyErr_Format(PyExc_TypeError,
- "Type %.100s doesn't define __format__",
- Py_TYPE(value)->tp_name);
- goto done;
- }
-
- /* And call it, binding it to the value */
- result = PyObject_CallFunctionObjArgs(meth, value, spec, NULL);
-
- if (result && !PyUnicode_Check(result)) {
- PyErr_SetString(PyExc_TypeError,
- "__format__ method did not return string");
- Py_DECREF(result);
- result = NULL;
- goto done;
- }
-
-done:
- Py_XDECREF(empty);
- return result;
+ PyObject *format_spec = NULL;
+
+ if (!PyArg_ParseTuple(args, "O|U:format", &value, &format_spec))
+ return NULL;
+
+ return PyObject_Format(value, format_spec);
}
PyDoc_STRVAR(format_doc,