summaryrefslogtreecommitdiff
path: root/Objects/bytesobject.c
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2008-05-30 18:10:04 +0000
committerEric Smith <eric@trueblade.com>2008-05-30 18:10:04 +0000
commitdc13b79a384691ed966f760a58d73a1b835e7d6b (patch)
tree40aa36feea2e0ee23f41801a8959990914e3fc71 /Objects/bytesobject.c
parent30fadc17990baf4005081d2cdcb8d3adc9c45a7a (diff)
downloadcpython-git-dc13b79a384691ed966f760a58d73a1b835e7d6b.tar.gz
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r--Objects/bytesobject.c32
1 files changed, 29 insertions, 3 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 0f4d4c3be6..79c1e4f0d4 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -3,9 +3,6 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
-
-#include "formatter_string.h"
-
#include <ctype.h>
#ifdef COUNT_ALLOCS
@@ -3939,6 +3936,35 @@ PyDoc_STRVAR(format__doc__,
\n\
");
+static PyObject *
+string__format__(PyObject* self, PyObject* args)
+{
+ PyObject *format_spec;
+ PyObject *result = NULL;
+ PyObject *tmp = NULL;
+
+ /* If 2.x, convert format_spec to the same type as value */
+ /* This is to allow things like u''.format('') */
+ if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
+ goto done;
+ if (!(PyBytes_Check(format_spec) || PyUnicode_Check(format_spec))) {
+ PyErr_Format(PyExc_TypeError, "__format__ arg must be str "
+ "or unicode, not %s", Py_TYPE(format_spec)->tp_name);
+ goto done;
+ }
+ tmp = PyObject_Str(format_spec);
+ if (tmp == NULL)
+ goto done;
+ format_spec = tmp;
+
+ result = _PyBytes_FormatAdvanced(self,
+ PyBytes_AS_STRING(format_spec),
+ PyBytes_GET_SIZE(format_spec));
+done:
+ Py_XDECREF(tmp);
+ return result;
+}
+
PyDoc_STRVAR(p_format__doc__,
"S.__format__(format_spec) -> unicode\n\
\n\