Fixed bug in time-to-midnight calculation.
[python.git] / Modules / _testcapimodule.c
blob9a5d885434537cf810d78477667fd2c0468f345b
1 /*
2 * C Extension module to test Python interpreter C APIs.
4 * The 'test_*' functions exported by this module are run as part of the
5 * standard Python regression test, via Lib/test/test_capi.py.
6 */
8 #include "Python.h"
10 #ifdef WITH_THREAD
11 #include "pythread.h"
12 #endif /* WITH_THREAD */
14 static PyObject *TestError; /* set to exception object in init */
16 /* Raise TestError with test_name + ": " + msg, and return NULL. */
18 static PyObject *
19 raiseTestError(const char* test_name, const char* msg)
21 char buf[2048];
23 if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
24 PyErr_SetString(TestError, "internal error msg too large");
25 else {
26 PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
27 PyErr_SetString(TestError, buf);
29 return NULL;
32 /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
34 The ones derived from autoconf on the UNIX-like OSes can be relied
35 upon (in the absence of sloppy cross-compiling), but the Windows
36 platforms have these hardcoded. Better safe than sorry.
38 static PyObject*
39 sizeof_error(const char* fatname, const char* typename,
40 int expected, int got)
42 char buf[1024];
43 PyOS_snprintf(buf, sizeof(buf),
44 "%.200s #define == %d but sizeof(%.200s) == %d",
45 fatname, expected, typename, got);
46 PyErr_SetString(TestError, buf);
47 return (PyObject*)NULL;
50 static PyObject*
51 test_config(PyObject *self)
53 #define CHECK_SIZEOF(FATNAME, TYPE) \
54 if (FATNAME != sizeof(TYPE)) \
55 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
57 CHECK_SIZEOF(SIZEOF_SHORT, short);
58 CHECK_SIZEOF(SIZEOF_INT, int);
59 CHECK_SIZEOF(SIZEOF_LONG, long);
60 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
61 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
62 #ifdef HAVE_LONG_LONG
63 CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
64 #endif
66 #undef CHECK_SIZEOF
68 Py_INCREF(Py_None);
69 return Py_None;
72 static PyObject*
73 test_list_api(PyObject *self)
75 PyObject* list;
76 int i;
78 /* SF bug 132008: PyList_Reverse segfaults */
79 #define NLIST 30
80 list = PyList_New(NLIST);
81 if (list == (PyObject*)NULL)
82 return (PyObject*)NULL;
83 /* list = range(NLIST) */
84 for (i = 0; i < NLIST; ++i) {
85 PyObject* anint = PyInt_FromLong(i);
86 if (anint == (PyObject*)NULL) {
87 Py_DECREF(list);
88 return (PyObject*)NULL;
90 PyList_SET_ITEM(list, i, anint);
92 /* list.reverse(), via PyList_Reverse() */
93 i = PyList_Reverse(list); /* should not blow up! */
94 if (i != 0) {
95 Py_DECREF(list);
96 return (PyObject*)NULL;
98 /* Check that list == range(29, -1, -1) now */
99 for (i = 0; i < NLIST; ++i) {
100 PyObject* anint = PyList_GET_ITEM(list, i);
101 if (PyInt_AS_LONG(anint) != NLIST-1-i) {
102 PyErr_SetString(TestError,
103 "test_list_api: reverse screwed up");
104 Py_DECREF(list);
105 return (PyObject*)NULL;
108 Py_DECREF(list);
109 #undef NLIST
111 Py_INCREF(Py_None);
112 return Py_None;
115 static int
116 test_dict_inner(int count)
118 int pos = 0, iterations = 0, i;
119 PyObject *dict = PyDict_New();
120 PyObject *v, *k;
122 if (dict == NULL)
123 return -1;
125 for (i = 0; i < count; i++) {
126 v = PyInt_FromLong(i);
127 PyDict_SetItem(dict, v, v);
128 Py_DECREF(v);
131 while (PyDict_Next(dict, &pos, &k, &v)) {
132 PyObject *o;
133 iterations++;
135 i = PyInt_AS_LONG(v) + 1;
136 o = PyInt_FromLong(i);
137 if (o == NULL)
138 return -1;
139 if (PyDict_SetItem(dict, k, o) < 0) {
140 Py_DECREF(o);
141 return -1;
143 Py_DECREF(o);
146 Py_DECREF(dict);
148 if (iterations != count) {
149 PyErr_SetString(
150 TestError,
151 "test_dict_iteration: dict iteration went wrong ");
152 return -1;
153 } else {
154 return 0;
158 static PyObject*
159 test_dict_iteration(PyObject* self)
161 int i;
163 for (i = 0; i < 200; i++) {
164 if (test_dict_inner(i) < 0) {
165 return NULL;
169 Py_INCREF(Py_None);
170 return Py_None;
174 /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
175 PyLong_{As, From}{Unsigned,}LongLong().
177 Note that the meat of the test is contained in testcapi_long.h.
178 This is revolting, but delicate code duplication is worse: "almost
179 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
180 dependence on type names makes it impossible to use a parameterized
181 function. A giant macro would be even worse than this. A C++ template
182 would be perfect.
184 The "report an error" functions are deliberately not part of the #include
185 file: if the test fails, you can set a breakpoint in the appropriate
186 error function directly, and crawl back from there in the debugger.
189 #define UNBIND(X) Py_DECREF(X); (X) = NULL
191 static PyObject *
192 raise_test_long_error(const char* msg)
194 return raiseTestError("test_long_api", msg);
197 #define TESTNAME test_long_api_inner
198 #define TYPENAME long
199 #define F_S_TO_PY PyLong_FromLong
200 #define F_PY_TO_S PyLong_AsLong
201 #define F_U_TO_PY PyLong_FromUnsignedLong
202 #define F_PY_TO_U PyLong_AsUnsignedLong
204 #include "testcapi_long.h"
206 static PyObject *
207 test_long_api(PyObject* self)
209 return TESTNAME(raise_test_long_error);
212 #undef TESTNAME
213 #undef TYPENAME
214 #undef F_S_TO_PY
215 #undef F_PY_TO_S
216 #undef F_U_TO_PY
217 #undef F_PY_TO_U
219 #ifdef HAVE_LONG_LONG
221 static PyObject *
222 raise_test_longlong_error(const char* msg)
224 return raiseTestError("test_longlong_api", msg);
227 #define TESTNAME test_longlong_api_inner
228 #define TYPENAME PY_LONG_LONG
229 #define F_S_TO_PY PyLong_FromLongLong
230 #define F_PY_TO_S PyLong_AsLongLong
231 #define F_U_TO_PY PyLong_FromUnsignedLongLong
232 #define F_PY_TO_U PyLong_AsUnsignedLongLong
234 #include "testcapi_long.h"
236 static PyObject *
237 test_longlong_api(PyObject* self)
239 return TESTNAME(raise_test_longlong_error);
242 #undef TESTNAME
243 #undef TYPENAME
244 #undef F_S_TO_PY
245 #undef F_PY_TO_S
246 #undef F_U_TO_PY
247 #undef F_PY_TO_U
249 /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
250 for both long and int arguments. The test may leak a little memory if
251 it fails.
253 static PyObject *
254 test_L_code(PyObject *self)
256 PyObject *tuple, *num;
257 PY_LONG_LONG value;
259 tuple = PyTuple_New(1);
260 if (tuple == NULL)
261 return NULL;
263 num = PyLong_FromLong(42);
264 if (num == NULL)
265 return NULL;
267 PyTuple_SET_ITEM(tuple, 0, num);
269 value = -1;
270 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
271 return NULL;
272 if (value != 42)
273 return raiseTestError("test_L_code",
274 "L code returned wrong value for long 42");
276 Py_DECREF(num);
277 num = PyInt_FromLong(42);
278 if (num == NULL)
279 return NULL;
281 PyTuple_SET_ITEM(tuple, 0, num);
283 value = -1;
284 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
285 return NULL;
286 if (value != 42)
287 return raiseTestError("test_L_code",
288 "L code returned wrong value for int 42");
290 Py_DECREF(tuple);
291 Py_INCREF(Py_None);
292 return Py_None;
295 #endif /* ifdef HAVE_LONG_LONG */
297 /* Functions to call PyArg_ParseTuple with integer format codes,
298 and return the result.
300 static PyObject *
301 getargs_b(PyObject *self, PyObject *args)
303 unsigned char value;
304 if (!PyArg_ParseTuple(args, "b", &value))
305 return NULL;
306 return PyLong_FromUnsignedLong((unsigned long)value);
309 static PyObject *
310 getargs_B(PyObject *self, PyObject *args)
312 unsigned char value;
313 if (!PyArg_ParseTuple(args, "B", &value))
314 return NULL;
315 return PyLong_FromUnsignedLong((unsigned long)value);
318 static PyObject *
319 getargs_H(PyObject *self, PyObject *args)
321 unsigned short value;
322 if (!PyArg_ParseTuple(args, "H", &value))
323 return NULL;
324 return PyLong_FromUnsignedLong((unsigned long)value);
327 static PyObject *
328 getargs_I(PyObject *self, PyObject *args)
330 unsigned int value;
331 if (!PyArg_ParseTuple(args, "I", &value))
332 return NULL;
333 return PyLong_FromUnsignedLong((unsigned long)value);
336 static PyObject *
337 getargs_k(PyObject *self, PyObject *args)
339 unsigned long value;
340 if (!PyArg_ParseTuple(args, "k", &value))
341 return NULL;
342 return PyLong_FromUnsignedLong(value);
345 static PyObject *
346 getargs_i(PyObject *self, PyObject *args)
348 int value;
349 if (!PyArg_ParseTuple(args, "i", &value))
350 return NULL;
351 return PyLong_FromLong((long)value);
354 static PyObject *
355 getargs_l(PyObject *self, PyObject *args)
357 long value;
358 if (!PyArg_ParseTuple(args, "l", &value))
359 return NULL;
360 return PyLong_FromLong(value);
363 #ifdef HAVE_LONG_LONG
364 static PyObject *
365 getargs_L(PyObject *self, PyObject *args)
367 PY_LONG_LONG value;
368 if (!PyArg_ParseTuple(args, "L", &value))
369 return NULL;
370 return PyLong_FromLongLong(value);
373 static PyObject *
374 getargs_K(PyObject *self, PyObject *args)
376 unsigned PY_LONG_LONG value;
377 if (!PyArg_ParseTuple(args, "K", &value))
378 return NULL;
379 return PyLong_FromUnsignedLongLong(value);
381 #endif
383 /* This function not only tests the 'k' getargs code, but also the
384 PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
385 static PyObject *
386 test_k_code(PyObject *self)
388 PyObject *tuple, *num;
389 unsigned long value;
391 tuple = PyTuple_New(1);
392 if (tuple == NULL)
393 return NULL;
395 /* a number larger than ULONG_MAX even on 64-bit platforms */
396 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
397 if (num == NULL)
398 return NULL;
400 value = PyInt_AsUnsignedLongMask(num);
401 if (value != ULONG_MAX)
402 return raiseTestError("test_k_code",
403 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
405 PyTuple_SET_ITEM(tuple, 0, num);
407 value = -1;
408 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
409 return NULL;
410 if (value != ULONG_MAX)
411 return raiseTestError("test_k_code",
412 "k code returned wrong value for long 0xFFF...FFF");
414 Py_DECREF(num);
415 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
416 if (num == NULL)
417 return NULL;
419 value = PyInt_AsUnsignedLongMask(num);
420 if (value != (unsigned long)-0x42)
421 return raiseTestError("test_k_code",
422 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
424 PyTuple_SET_ITEM(tuple, 0, num);
426 value = -1;
427 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
428 return NULL;
429 if (value != (unsigned long)-0x42)
430 return raiseTestError("test_k_code",
431 "k code returned wrong value for long -0xFFF..000042");
433 Py_DECREF(tuple);
434 Py_INCREF(Py_None);
435 return Py_None;
438 #ifdef Py_USING_UNICODE
440 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
441 of an error.
443 static PyObject *
444 test_u_code(PyObject *self)
446 PyObject *tuple, *obj;
447 Py_UNICODE *value;
448 int len;
450 tuple = PyTuple_New(1);
451 if (tuple == NULL)
452 return NULL;
454 obj = PyUnicode_Decode("test", strlen("test"),
455 "ascii", NULL);
456 if (obj == NULL)
457 return NULL;
459 PyTuple_SET_ITEM(tuple, 0, obj);
461 value = 0;
462 if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
463 return NULL;
464 if (value != PyUnicode_AS_UNICODE(obj))
465 return raiseTestError("test_u_code",
466 "u code returned wrong value for u'test'");
467 value = 0;
468 if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
469 return NULL;
470 if (value != PyUnicode_AS_UNICODE(obj) ||
471 len != PyUnicode_GET_SIZE(obj))
472 return raiseTestError("test_u_code",
473 "u# code returned wrong values for u'test'");
475 Py_DECREF(tuple);
476 Py_INCREF(Py_None);
477 return Py_None;
480 #endif
482 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
483 static PyObject *
484 test_long_numbits(PyObject *self)
486 struct triple {
487 long input;
488 size_t nbits;
489 int sign;
490 } testcases[] = {{0, 0, 0},
491 {1L, 1, 1},
492 {-1L, 1, -1},
493 {2L, 2, 1},
494 {-2L, 2, -1},
495 {3L, 2, 1},
496 {-3L, 2, -1},
497 {4L, 3, 1},
498 {-4L, 3, -1},
499 {0x7fffL, 15, 1}, /* one Python long digit */
500 {-0x7fffL, 15, -1},
501 {0xffffL, 16, 1},
502 {-0xffffL, 16, -1},
503 {0xfffffffL, 28, 1},
504 {-0xfffffffL, 28, -1}};
505 int i;
507 for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
508 PyObject *plong = PyLong_FromLong(testcases[i].input);
509 size_t nbits = _PyLong_NumBits(plong);
510 int sign = _PyLong_Sign(plong);
512 Py_DECREF(plong);
513 if (nbits != testcases[i].nbits)
514 return raiseTestError("test_long_numbits",
515 "wrong result for _PyLong_NumBits");
516 if (sign != testcases[i].sign)
517 return raiseTestError("test_long_numbits",
518 "wrong result for _PyLong_Sign");
520 Py_INCREF(Py_None);
521 return Py_None;
524 static PyObject *
525 raise_exception(PyObject *self, PyObject *args)
527 PyObject *exc;
528 PyObject *exc_args, *v;
529 int num_args, i;
531 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
532 &exc, &num_args))
533 return NULL;
535 exc_args = PyTuple_New(num_args);
536 if (exc_args == NULL)
537 return NULL;
538 for (i = 0; i < num_args; ++i) {
539 v = PyInt_FromLong(i);
540 if (v == NULL) {
541 Py_DECREF(exc_args);
542 return NULL;
544 PyTuple_SET_ITEM(exc_args, i, v);
546 PyErr_SetObject(exc, exc_args);
547 Py_DECREF(exc_args);
548 return NULL;
551 #ifdef WITH_THREAD
553 void _make_call(void *callable)
555 PyObject *rc;
556 PyGILState_STATE s = PyGILState_Ensure();
557 rc = PyObject_CallFunction(callable, "");
558 Py_XDECREF(rc);
559 PyGILState_Release(s);
562 static PyObject *
563 test_thread_state(PyObject *self, PyObject *args)
565 PyObject *fn;
566 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
567 return NULL;
568 /* Ensure Python is setup for threading */
569 PyEval_InitThreads();
570 /* Start a new thread for our callback. */
571 PyThread_start_new_thread( _make_call, fn);
572 /* Make the callback with the thread lock held by this thread */
573 _make_call(fn);
574 /* Do it all again, but this time with the thread-lock released */
575 Py_BEGIN_ALLOW_THREADS
576 _make_call(fn);
577 Py_END_ALLOW_THREADS
578 /* And once more with and without a thread
579 XXX - should use a lock and work out exactly what we are trying
580 to test <wink>
582 Py_BEGIN_ALLOW_THREADS
583 PyThread_start_new_thread( _make_call, fn);
584 _make_call(fn);
585 Py_END_ALLOW_THREADS
586 Py_INCREF(Py_None);
587 return Py_None;
589 #endif
591 static PyMethodDef TestMethods[] = {
592 {"raise_exception", raise_exception, METH_VARARGS},
593 {"test_config", (PyCFunction)test_config, METH_NOARGS},
594 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
595 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
596 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
597 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
598 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
600 {"getargs_b", (PyCFunction)getargs_b, METH_VARARGS},
601 {"getargs_B", (PyCFunction)getargs_B, METH_VARARGS},
602 {"getargs_H", (PyCFunction)getargs_H, METH_VARARGS},
603 {"getargs_I", (PyCFunction)getargs_I, METH_VARARGS},
604 {"getargs_k", (PyCFunction)getargs_k, METH_VARARGS},
605 {"getargs_i", (PyCFunction)getargs_i, METH_VARARGS},
606 {"getargs_l", (PyCFunction)getargs_l, METH_VARARGS},
607 #ifdef HAVE_LONG_LONG
608 {"getargs_L", (PyCFunction)getargs_L, METH_VARARGS},
609 {"getargs_K", (PyCFunction)getargs_K, METH_VARARGS},
610 {"test_longlong_api", (PyCFunction)test_longlong_api, METH_NOARGS},
611 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
612 #endif
613 #ifdef Py_USING_UNICODE
614 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
615 #endif
616 #ifdef WITH_THREAD
617 {"_test_thread_state", (PyCFunction)test_thread_state, METH_VARARGS},
618 #endif
619 {NULL, NULL} /* sentinel */
622 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
624 PyMODINIT_FUNC
625 init_testcapi(void)
627 PyObject *m;
629 m = Py_InitModule("_testcapi", TestMethods);
631 PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX));
632 PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX));
633 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
634 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
635 PyModule_AddObject(m, "INT_MIN", PyInt_FromLong(INT_MIN));
636 PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN));
637 PyModule_AddObject(m, "INT_MAX", PyInt_FromLong(INT_MAX));
638 PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX));
640 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
641 Py_INCREF(TestError);
642 PyModule_AddObject(m, "error", TestError);