summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-11-22 00:45:37 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-11-22 00:45:37 (GMT)
commitab1d16b4561390022efc0a45fae1ce6cbb119a58 (patch)
treee7307b10973f0167102e0baf7d0363815af0fe25 /Modules
parent58fcf9f801d590b999401533154b63f4eb26ce6c (diff)
downloadcpython-ab1d16b4561390022efc0a45fae1ce6cbb119a58.zip
cpython-ab1d16b4561390022efc0a45fae1ce6cbb119a58.tar.gz
cpython-ab1d16b4561390022efc0a45fae1ce6cbb119a58.tar.bz2
Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
* Add tests for PyUnicode_EncodeDecimal() and PyUnicode_TransformDecimalToASCII() * Remove the unused "e" variable in replace()
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testcapimodule.c51
1 files changed, 49 insertions, 2 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index f19d0df..6c61f7d 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -1438,6 +1438,51 @@ unicode_aswidecharstring(PyObject *self, PyObject *args)
}
static PyObject *
+unicode_encodedecimal(PyObject *self, PyObject *args)
+{
+ Py_UNICODE *unicode;
+ Py_ssize_t length;
+ char *errors = NULL;
+ PyObject *decimal;
+ Py_ssize_t decimal_length, new_length;
+ int res;
+
+ if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors))
+ return NULL;
+
+ decimal_length = length * 7; /* len('&#8364;') */
+ decimal = PyBytes_FromStringAndSize(NULL, decimal_length);
+ if (decimal == NULL)
+ return NULL;
+
+ res = PyUnicode_EncodeDecimal(unicode, length,
+ PyBytes_AS_STRING(decimal),
+ errors);
+ if (res < 0) {
+ Py_DECREF(decimal);
+ return NULL;
+ }
+
+ new_length = strlen(PyBytes_AS_STRING(decimal));
+ assert(new_length <= decimal_length);
+ res = _PyBytes_Resize(&decimal, new_length);
+ if (res < 0)
+ return NULL;
+
+ return decimal;
+}
+
+static PyObject *
+unicode_transformdecimaltoascii(PyObject *self, PyObject *args)
+{
+ Py_UNICODE *unicode;
+ Py_ssize_t length;
+ if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length))
+ return NULL;
+ return PyUnicode_TransformDecimalToASCII(unicode, length);
+}
+
+static PyObject *
getargs_w_star(PyObject *self, PyObject *args)
{
Py_buffer buffer;
@@ -2320,8 +2365,10 @@ static PyMethodDef TestMethods[] = {
{"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
{"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS},
{"test_widechar", (PyCFunction)test_widechar, METH_NOARGS},
- {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS},
- {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS},
+ {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS},
+ {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS},
+ {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS},
+ {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS},
#ifdef WITH_THREAD
{"_test_thread_state", test_thread_state, METH_VARARGS},
{"_pending_threadfunc", pending_threadfunc, METH_VARARGS},