summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapimodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r--Modules/_testcapimodule.c124
1 files changed, 107 insertions, 17 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 94b6e67..bcb3a0f 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -22,14 +22,7 @@ static PyObject *TestError; /* set to exception object in init */
static PyObject *
raiseTestError(const char* test_name, const char* msg)
{
- char buf[2048];
-
- if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
- PyErr_SetString(TestError, "internal error msg too large");
- else {
- PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
- PyErr_SetString(TestError, buf);
- }
+ PyErr_Format(TestError, "%s: %s", test_name, msg);
return NULL;
}
@@ -43,11 +36,9 @@ static PyObject*
sizeof_error(const char* fatname, const char* typname,
int expected, int got)
{
- char buf[1024];
- PyOS_snprintf(buf, sizeof(buf),
- "%.200s #define == %d but sizeof(%.200s) == %d",
+ PyErr_Format(TestError,
+ "%s #define == %d but sizeof(%s) == %d",
fatname, expected, typname, got);
- PyErr_SetString(TestError, buf);
return (PyObject*)NULL;
}
@@ -778,6 +769,68 @@ test_long_long_and_overflow(PyObject *self)
return Py_None;
}
+/* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that
+ non-integer arguments are handled correctly. It should be extended to
+ test overflow handling.
+ */
+
+static PyObject *
+test_long_as_size_t(PyObject *self)
+{
+ size_t out_u;
+ Py_ssize_t out_s;
+
+ Py_INCREF(Py_None);
+
+ out_u = PyLong_AsSize_t(Py_None);
+ if (out_u != (size_t)-1 || !PyErr_Occurred())
+ return raiseTestError("test_long_as_size_t",
+ "PyLong_AsSize_t(None) didn't complain");
+ if (!PyErr_ExceptionMatches(PyExc_TypeError))
+ return raiseTestError("test_long_as_size_t",
+ "PyLong_AsSize_t(None) raised "
+ "something other than TypeError");
+ PyErr_Clear();
+
+ out_s = PyLong_AsSsize_t(Py_None);
+ if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred())
+ return raiseTestError("test_long_as_size_t",
+ "PyLong_AsSsize_t(None) didn't complain");
+ if (!PyErr_ExceptionMatches(PyExc_TypeError))
+ return raiseTestError("test_long_as_size_t",
+ "PyLong_AsSsize_t(None) raised "
+ "something other than TypeError");
+ PyErr_Clear();
+
+ /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
+ return Py_None;
+}
+
+/* Test the PyLong_AsDouble API. At present this just tests that
+ non-integer arguments are handled correctly.
+ */
+
+static PyObject *
+test_long_as_double(PyObject *self)
+{
+ double out;
+
+ Py_INCREF(Py_None);
+
+ out = PyLong_AsDouble(Py_None);
+ if (out != -1.0 || !PyErr_Occurred())
+ return raiseTestError("test_long_as_double",
+ "PyLong_AsDouble(None) didn't complain");
+ if (!PyErr_ExceptionMatches(PyExc_TypeError))
+ return raiseTestError("test_long_as_double",
+ "PyLong_AsDouble(None) raised "
+ "something other than TypeError");
+ PyErr_Clear();
+
+ /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
+ return Py_None;
+}
+
/* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
for both long and int arguments. The test may leak a little memory if
it fails.
@@ -1012,6 +1065,15 @@ test_k_code(PyObject *self)
}
static PyObject *
+getargs_c(PyObject *self, PyObject *args)
+{
+ char c;
+ if (!PyArg_ParseTuple(args, "c", &c))
+ return NULL;
+ return PyBytes_FromStringAndSize(&c, 1);
+}
+
+static PyObject *
getargs_s(PyObject *self, PyObject *args)
{
char *str;
@@ -1293,7 +1355,7 @@ static PyObject *
test_Z_code(PyObject *self)
{
PyObject *tuple, *obj;
- Py_UNICODE *value1, *value2;
+ const Py_UNICODE *value1, *value2;
Py_ssize_t len1, len2;
tuple = PyTuple_New(2);
@@ -1347,6 +1409,7 @@ test_widechar(PyObject *self)
#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
size_t wtextlen = 1;
+ const wchar_t invalid[1] = {(wchar_t)0x110000u};
#else
const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
size_t wtextlen = 2;
@@ -1363,7 +1426,7 @@ test_widechar(PyObject *self)
return NULL;
}
- if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) {
+ if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) {
Py_DECREF(wide);
Py_DECREF(utf8);
return raiseTestError("test_widechar",
@@ -1382,6 +1445,23 @@ test_widechar(PyObject *self)
Py_DECREF(wide);
Py_DECREF(utf8);
+
+#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
+ wide = PyUnicode_FromWideChar(invalid, 1);
+ if (wide == NULL)
+ PyErr_Clear();
+ else
+ return raiseTestError("test_widechar",
+ "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail");
+
+ wide = PyUnicode_FromUnicode(invalid, 1);
+ if (wide == NULL)
+ PyErr_Clear();
+ else
+ return raiseTestError("test_widechar",
+ "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail");
+#endif
+
Py_RETURN_NONE;
}
@@ -1576,7 +1656,7 @@ test_long_numbits(PyObject *self)
{-0xfffffffL, 28, -1}};
int i;
- for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
+ for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) {
PyObject *plong = PyLong_FromLong(testcases[i].input);
size_t nbits = _PyLong_NumBits(plong);
int sign = _PyLong_Sign(plong);
@@ -1786,13 +1866,12 @@ test_string_from_format(PyObject *self, PyObject *args)
{
PyObject *result;
char *msg;
- static const Py_UNICODE one[] = {'1', 0};
#define CHECK_1_FORMAT(FORMAT, TYPE) \
result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \
if (result == NULL) \
return NULL; \
- if (Py_UNICODE_strcmp(PyUnicode_AS_UNICODE(result), one)) { \
+ if (PyUnicode_CompareWithASCIIString(result, "1")) { \
msg = FORMAT " failed at 1"; \
goto Fail; \
} \
@@ -2317,6 +2396,14 @@ run_in_subinterp(PyObject *self, PyObject *args)
PyThreadState_Swap(NULL);
substate = Py_NewInterpreter();
+ if (substate == NULL) {
+ /* Since no new thread state was created, there is no exception to
+ propagate; raise a fresh one after swapping in the old thread
+ state. */
+ PyThreadState_Swap(mainstate);
+ PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed");
+ return NULL;
+ }
r = PyRun_SimpleString(code);
Py_EndInterpreter(substate);
@@ -2338,6 +2425,8 @@ static PyMethodDef TestMethods[] = {
{"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
{"test_long_and_overflow", (PyCFunction)test_long_and_overflow,
METH_NOARGS},
+ {"test_long_as_double", (PyCFunction)test_long_as_double,METH_NOARGS},
+ {"test_long_as_size_t", (PyCFunction)test_long_as_size_t,METH_NOARGS},
{"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
{"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
{"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
@@ -2369,6 +2458,7 @@ static PyMethodDef TestMethods[] = {
(PyCFunction)test_long_long_and_overflow, METH_NOARGS},
{"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
#endif
+ {"getargs_c", getargs_c, METH_VARARGS},
{"getargs_s", getargs_s, METH_VARARGS},
{"getargs_s_star", getargs_s_star, METH_VARARGS},
{"getargs_s_hash", getargs_s_hash, METH_VARARGS},