summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2006-05-13 23:28:20 (GMT)
committerTim Peters <tim.peters@gmail.com>2006-05-13 23:28:20 (GMT)
commit8931ff1f670c3588f3cb60d2f56a1e9cae964b40 (patch)
treea71837228bcf79f13f6a2752b3e6edde560a9015 /Modules
parent822f34a848de4d19f9d7cca5bb99424e5dd34303 (diff)
downloadcpython-8931ff1f670c3588f3cb60d2f56a1e9cae964b40.zip
cpython-8931ff1f670c3588f3cb60d2f56a1e9cae964b40.tar.gz
cpython-8931ff1f670c3588f3cb60d2f56a1e9cae964b40.tar.bz2
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
about "%u", "%lu" and "%zu" formats. Since PyString_FromFormat and PyErr_Format have exactly the same rules (both inherited from PyString_FromFormatV), it would be good if someone with more LaTeX Fu changed one of them to just point to the other. Their docs were way out of synch before this patch, and I just did a mass copy+paste to repair that. Not a backport candidate (this is a new feature).
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testcapimodule.c47
1 files changed, 43 insertions, 4 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index e8881dc..a74e761 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -486,8 +486,8 @@ test_u_code(PyObject *self)
return Py_None;
}
-static
-PyObject *codec_incrementalencoder(PyObject *self, PyObject *args)
+static PyObject *
+codec_incrementalencoder(PyObject *self, PyObject *args)
{
const char *encoding, *errors = NULL;
if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
@@ -496,8 +496,8 @@ PyObject *codec_incrementalencoder(PyObject *self, PyObject *args)
return PyCodec_IncrementalEncoder(encoding, errors);
}
-static
-PyObject *codec_incrementaldecoder(PyObject *self, PyObject *args)
+static PyObject *
+codec_incrementaldecoder(PyObject *self, PyObject *args)
{
const char *encoding, *errors = NULL;
if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
@@ -660,6 +660,44 @@ test_thread_state(PyObject *self, PyObject *args)
}
#endif
+/* Some tests of PyString_FromFormat(). This needs more tests.
+ * PyString_FromFormat() also needs docs.
+ */
+static PyObject *
+test_string_from_format(PyObject *self, PyObject *args)
+{
+ PyObject *result;
+ char *msg;
+
+#define CHECK_1_FORMAT(FORMAT, TYPE) \
+ result = PyString_FromFormat(FORMAT, (TYPE)1); \
+ if (result == NULL) \
+ return NULL; \
+ if (strcmp(PyString_AsString(result), "1")) { \
+ msg = FORMAT " failed at 1"; \
+ goto Fail; \
+ } \
+ Py_DECREF(result)
+
+ CHECK_1_FORMAT("%d", int);
+ CHECK_1_FORMAT("%ld", long);
+ /* The z width modifier was added in Python 2.5. */
+ CHECK_1_FORMAT("%zd", Py_ssize_t);
+
+ /* The u type code was added in Python 2.5. */
+ CHECK_1_FORMAT("%u", unsigned int);
+ CHECK_1_FORMAT("%lu", unsigned long);
+ CHECK_1_FORMAT("%zu", size_t);
+
+ Py_RETURN_NONE;
+
+ Fail:
+ Py_XDECREF(result);
+ return raiseTestError("test_string_from_format", msg);
+
+#undef CHECK_1_FORMAT
+}
+
static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
{"test_config", (PyCFunction)test_config, METH_NOARGS},
@@ -669,6 +707,7 @@ static PyMethodDef TestMethods[] = {
{"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
{"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
{"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
+ {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
{"getargs_b", getargs_b, METH_VARARGS},
{"getargs_B", getargs_B, METH_VARARGS},