Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 91621db

Browse files
committed
The merest start of a test for the PyLong_{As,From}{Unsigned,}LongLong()
functions. I intend to replace their guts with calls to the new _PyLong_{As,From}ByteArray() functions, but AFAICT there's no tests for them at all now; I also suspect PyLong_AsLongLong() isn't catching all overflow cases, but without a std test to demonstrate that why should you believe me <wink>. Also added a raiseTestError() utility function.
1 parent 8bc84b4 commit 91621db

1 file changed

Lines changed: 57 additions & 4 deletions

File tree

Modules/_testcapimodule.c

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@
99

1010
static PyObject *TestError; /* set to exception object in init */
1111

12+
/* Raise TestError with test_name + ": " + msg, and return NULL. */
13+
14+
static PyObject *
15+
raiseTestError(const char* test_name, const char* msg)
16+
{
17+
char buf[2048];
18+
19+
if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
20+
PyErr_SetString(TestError, "internal error msg too large");
21+
else {
22+
sprintf(buf, "%s: %s", test_name, msg);
23+
PyErr_SetString(TestError, buf);
24+
}
25+
return NULL;
26+
}
27+
1228
/* Test #defines from config.h (particularly the SIZEOF_* defines).
1329
1430
The ones derived from autoconf on the UNIX-like OSes can be relied
@@ -145,7 +161,7 @@ test_dict_iteration(PyObject* self, PyObject* args)
145161

146162
if (!PyArg_ParseTuple(args, ":test_dict_iteration"))
147163
return NULL;
148-
164+
149165
for (i = 0; i < 200; i++) {
150166
if (test_dict_inner(i) < 0) {
151167
return NULL;
@@ -156,10 +172,47 @@ test_dict_iteration(PyObject* self, PyObject* args)
156172
return Py_None;
157173
}
158174

175+
#ifdef HAVE_LONG_LONG
176+
177+
/* Basic sanity checks for PyLong_{As, From}{Unsigned,}LongLong(). */
178+
179+
static PyObject *
180+
test_longlong_api(PyObject* self, PyObject* args)
181+
{
182+
/* unsigned LONG_LONG uinput, uoutput; */
183+
LONG_LONG input, output;
184+
PyObject *pyresult;
185+
186+
if (!PyArg_ParseTuple(args, ":test_longlong_api"))
187+
return NULL;
188+
189+
input = 0;
190+
pyresult = PyLong_FromLongLong(input);
191+
if (pyresult == NULL)
192+
return raiseTestError("test_longlong_api",
193+
"unexpected null result");
194+
output = PyLong_AsLongLong(pyresult);
195+
if (output == (LONG_LONG)-1 && PyErr_Occurred())
196+
return raiseTestError("test_longlong_api",
197+
"unexpected -1 result");
198+
if (output != input)
199+
return raiseTestError("test_longlong_api",
200+
"output != input");
201+
Py_DECREF(pyresult);
202+
203+
Py_INCREF(Py_None);
204+
return Py_None;
205+
}
206+
207+
#endif
208+
159209
static PyMethodDef TestMethods[] = {
160-
{"test_config", test_config, METH_VARARGS},
161-
{"test_list_api", test_list_api, METH_VARARGS},
162-
{"test_dict_iteration", test_dict_iteration, METH_VARARGS},
210+
{"test_config", test_config, METH_VARARGS},
211+
{"test_list_api", test_list_api, METH_VARARGS},
212+
{"test_dict_iteration", test_dict_iteration, METH_VARARGS},
213+
#ifdef HAVE_LONG_LONG
214+
{"test_longlong_api", test_longlong_api, METH_VARARGS},
215+
#endif
163216
{NULL, NULL} /* sentinel */
164217
};
165218

0 commit comments

Comments
 (0)