blob: 940605e47653c0ab8f3acfe9cd6441f9205e4797 [file] [log] [blame]
Tim Peters9ea17ac2001-02-02 05:57:15 +00001/*
2 * C Extension module to test Python interpreter C APIs.
3 *
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 */
7
Neal Norwitz8866e0a2007-10-27 04:01:17 +00008#define PY_SSIZE_T_CLEAN
9
Tim Peters9ea17ac2001-02-02 05:57:15 +000010#include "Python.h"
Thomas Wouters89f507f2006-12-13 04:49:30 +000011#include <float.h>
12#include "structmember.h"
Tim Peters9ea17ac2001-02-02 05:57:15 +000013
Mark Hammond8d98d2c2003-04-19 15:41:53 +000014#ifdef WITH_THREAD
15#include "pythread.h"
16#endif /* WITH_THREAD */
Tim Peters9ea17ac2001-02-02 05:57:15 +000017static PyObject *TestError; /* set to exception object in init */
18
Tim Peters91621db2001-06-12 20:10:01 +000019/* Raise TestError with test_name + ": " + msg, and return NULL. */
20
21static PyObject *
22raiseTestError(const char* test_name, const char* msg)
23{
24 char buf[2048];
25
26 if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
27 PyErr_SetString(TestError, "internal error msg too large");
28 else {
Tim Peters885d4572001-11-28 20:27:42 +000029 PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
Tim Peters91621db2001-06-12 20:10:01 +000030 PyErr_SetString(TestError, buf);
31 }
32 return NULL;
33}
34
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000035/* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
Tim Peters9ea17ac2001-02-02 05:57:15 +000036
37 The ones derived from autoconf on the UNIX-like OSes can be relied
38 upon (in the absence of sloppy cross-compiling), but the Windows
39 platforms have these hardcoded. Better safe than sorry.
40*/
41static PyObject*
Thomas Wouters89f507f2006-12-13 04:49:30 +000042sizeof_error(const char* fatname, const char* typname,
Tim Peters9ea17ac2001-02-02 05:57:15 +000043 int expected, int got)
44{
45 char buf[1024];
Tim Petersbaefd9e2003-01-28 20:37:45 +000046 PyOS_snprintf(buf, sizeof(buf),
Tim Peters885d4572001-11-28 20:27:42 +000047 "%.200s #define == %d but sizeof(%.200s) == %d",
Thomas Wouters89f507f2006-12-13 04:49:30 +000048 fatname, expected, typname, got);
Tim Peters9ea17ac2001-02-02 05:57:15 +000049 PyErr_SetString(TestError, buf);
50 return (PyObject*)NULL;
51}
52
53static PyObject*
Fred Drakeacee69f2002-04-01 14:28:58 +000054test_config(PyObject *self)
Tim Peters9ea17ac2001-02-02 05:57:15 +000055{
Tim Peters9ea17ac2001-02-02 05:57:15 +000056#define CHECK_SIZEOF(FATNAME, TYPE) \
57 if (FATNAME != sizeof(TYPE)) \
58 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
59
Tim Peters208efe52001-06-26 22:40:47 +000060 CHECK_SIZEOF(SIZEOF_SHORT, short);
Tim Peters9ea17ac2001-02-02 05:57:15 +000061 CHECK_SIZEOF(SIZEOF_INT, int);
62 CHECK_SIZEOF(SIZEOF_LONG, long);
63 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
64 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
65#ifdef HAVE_LONG_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +000066 CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
Tim Peters9ea17ac2001-02-02 05:57:15 +000067#endif
68
69#undef CHECK_SIZEOF
70
71 Py_INCREF(Py_None);
72 return Py_None;
73}
74
Tim Peters5c4d5bf2001-02-12 22:13:26 +000075static PyObject*
Fred Drakeacee69f2002-04-01 14:28:58 +000076test_list_api(PyObject *self)
Tim Peters5c4d5bf2001-02-12 22:13:26 +000077{
78 PyObject* list;
79 int i;
Tim Peters5c4d5bf2001-02-12 22:13:26 +000080
81 /* SF bug 132008: PyList_Reverse segfaults */
82#define NLIST 30
83 list = PyList_New(NLIST);
84 if (list == (PyObject*)NULL)
85 return (PyObject*)NULL;
86 /* list = range(NLIST) */
87 for (i = 0; i < NLIST; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +000088 PyObject* anint = PyLong_FromLong(i);
Tim Peters5c4d5bf2001-02-12 22:13:26 +000089 if (anint == (PyObject*)NULL) {
90 Py_DECREF(list);
91 return (PyObject*)NULL;
92 }
93 PyList_SET_ITEM(list, i, anint);
94 }
95 /* list.reverse(), via PyList_Reverse() */
96 i = PyList_Reverse(list); /* should not blow up! */
97 if (i != 0) {
98 Py_DECREF(list);
99 return (PyObject*)NULL;
100 }
101 /* Check that list == range(29, -1, -1) now */
102 for (i = 0; i < NLIST; ++i) {
103 PyObject* anint = PyList_GET_ITEM(list, i);
Christian Heimes217cfd12007-12-02 14:31:20 +0000104 if (PyLong_AS_LONG(anint) != NLIST-1-i) {
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000105 PyErr_SetString(TestError,
106 "test_list_api: reverse screwed up");
107 Py_DECREF(list);
108 return (PyObject*)NULL;
109 }
110 }
111 Py_DECREF(list);
112#undef NLIST
113
114 Py_INCREF(Py_None);
115 return Py_None;
116}
117
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000118static int
119test_dict_inner(int count)
120{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000121 Py_ssize_t pos = 0, iterations = 0;
122 int i;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000123 PyObject *dict = PyDict_New();
124 PyObject *v, *k;
125
126 if (dict == NULL)
127 return -1;
128
129 for (i = 0; i < count; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000130 v = PyLong_FromLong(i);
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000131 PyDict_SetItem(dict, v, v);
132 Py_DECREF(v);
133 }
134
135 while (PyDict_Next(dict, &pos, &k, &v)) {
136 PyObject *o;
137 iterations++;
138
Christian Heimes217cfd12007-12-02 14:31:20 +0000139 i = PyLong_AS_LONG(v) + 1;
140 o = PyLong_FromLong(i);
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000141 if (o == NULL)
142 return -1;
143 if (PyDict_SetItem(dict, k, o) < 0) {
144 Py_DECREF(o);
145 return -1;
146 }
147 Py_DECREF(o);
148 }
149
150 Py_DECREF(dict);
151
152 if (iterations != count) {
153 PyErr_SetString(
154 TestError,
155 "test_dict_iteration: dict iteration went wrong ");
156 return -1;
157 } else {
158 return 0;
159 }
160}
161
162static PyObject*
Fred Drakeacee69f2002-04-01 14:28:58 +0000163test_dict_iteration(PyObject* self)
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000164{
165 int i;
166
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000167 for (i = 0; i < 200; i++) {
168 if (test_dict_inner(i) < 0) {
169 return NULL;
170 }
171 }
172
173 Py_INCREF(Py_None);
174 return Py_None;
175}
176
Tim Peters91621db2001-06-12 20:10:01 +0000177
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000178/* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
Tim Petersff70d3c2001-06-14 01:11:03 +0000179 PyLong_{As, From}{Unsigned,}LongLong().
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000180
181 Note that the meat of the test is contained in testcapi_long.h.
182 This is revolting, but delicate code duplication is worse: "almost
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000183 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000184 dependence on type names makes it impossible to use a parameterized
185 function. A giant macro would be even worse than this. A C++ template
186 would be perfect.
187
188 The "report an error" functions are deliberately not part of the #include
189 file: if the test fails, you can set a breakpoint in the appropriate
190 error function directly, and crawl back from there in the debugger.
191*/
192
193#define UNBIND(X) Py_DECREF(X); (X) = NULL
194
195static PyObject *
196raise_test_long_error(const char* msg)
197{
198 return raiseTestError("test_long_api", msg);
199}
200
201#define TESTNAME test_long_api_inner
202#define TYPENAME long
203#define F_S_TO_PY PyLong_FromLong
204#define F_PY_TO_S PyLong_AsLong
205#define F_U_TO_PY PyLong_FromUnsignedLong
206#define F_PY_TO_U PyLong_AsUnsignedLong
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000207
208#include "testcapi_long.h"
209
210static PyObject *
Fred Drakeacee69f2002-04-01 14:28:58 +0000211test_long_api(PyObject* self)
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000212{
Tim Peters83c9edc2001-06-16 08:10:13 +0000213 return TESTNAME(raise_test_long_error);
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000214}
215
216#undef TESTNAME
217#undef TYPENAME
218#undef F_S_TO_PY
219#undef F_PY_TO_S
220#undef F_U_TO_PY
221#undef F_PY_TO_U
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000222
223#ifdef HAVE_LONG_LONG
Tim Peters91621db2001-06-12 20:10:01 +0000224
225static PyObject *
Tim Petersd1a7da62001-06-13 00:35:57 +0000226raise_test_longlong_error(const char* msg)
227{
228 return raiseTestError("test_longlong_api", msg);
229}
230
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000231#define TESTNAME test_longlong_api_inner
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000232#define TYPENAME PY_LONG_LONG
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000233#define F_S_TO_PY PyLong_FromLongLong
234#define F_PY_TO_S PyLong_AsLongLong
235#define F_U_TO_PY PyLong_FromUnsignedLongLong
236#define F_PY_TO_U PyLong_AsUnsignedLongLong
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000237
238#include "testcapi_long.h"
Tim Petersd1a7da62001-06-13 00:35:57 +0000239
240static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000241test_longlong_api(PyObject* self, PyObject *args)
Tim Peters91621db2001-06-12 20:10:01 +0000242{
Tim Peters83c9edc2001-06-16 08:10:13 +0000243 return TESTNAME(raise_test_longlong_error);
Tim Peters91621db2001-06-12 20:10:01 +0000244}
245
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000246#undef TESTNAME
247#undef TYPENAME
248#undef F_S_TO_PY
249#undef F_PY_TO_S
250#undef F_U_TO_PY
251#undef F_PY_TO_U
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000252
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000253/* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
Tim Petersd38b1c72001-09-30 05:09:37 +0000254 for both long and int arguments. The test may leak a little memory if
255 it fails.
256*/
257static PyObject *
Fred Drakeacee69f2002-04-01 14:28:58 +0000258test_L_code(PyObject *self)
Tim Petersd38b1c72001-09-30 05:09:37 +0000259{
260 PyObject *tuple, *num;
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000261 PY_LONG_LONG value;
Tim Petersd38b1c72001-09-30 05:09:37 +0000262
Tim Petersd38b1c72001-09-30 05:09:37 +0000263 tuple = PyTuple_New(1);
264 if (tuple == NULL)
265 return NULL;
266
267 num = PyLong_FromLong(42);
268 if (num == NULL)
269 return NULL;
270
271 PyTuple_SET_ITEM(tuple, 0, num);
272
273 value = -1;
274 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
275 return NULL;
276 if (value != 42)
277 return raiseTestError("test_L_code",
278 "L code returned wrong value for long 42");
279
280 Py_DECREF(num);
Christian Heimes217cfd12007-12-02 14:31:20 +0000281 num = PyLong_FromLong(42);
Tim Petersd38b1c72001-09-30 05:09:37 +0000282 if (num == NULL)
283 return NULL;
284
285 PyTuple_SET_ITEM(tuple, 0, num);
286
287 value = -1;
288 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
289 return NULL;
290 if (value != 42)
291 return raiseTestError("test_L_code",
292 "L code returned wrong value for int 42");
293
294 Py_DECREF(tuple);
295 Py_INCREF(Py_None);
296 return Py_None;
297}
298
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000299#endif /* ifdef HAVE_LONG_LONG */
300
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000301/* Test tuple argument processing */
302static PyObject *
303getargs_tuple(PyObject *self, PyObject *args)
304{
305 int a, b, c;
306 if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
307 return NULL;
308 return Py_BuildValue("iii", a, b, c);
309}
310
Christian Heimes380f7f22008-02-28 11:19:05 +0000311/* test PyArg_ParseTupleAndKeywords */
312static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
313{
314 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
315 static char *fmt="(ii)i|(i(ii))(iii)i";
316 int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
317
318 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
319 &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
320 &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
321 return NULL;
322 return Py_BuildValue("iiiiiiiiii",
323 int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
324 int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
325}
326
Thomas Heller3457e4b2003-04-24 16:14:27 +0000327/* Functions to call PyArg_ParseTuple with integer format codes,
328 and return the result.
329*/
Thomas Hellera4ea6032003-04-17 18:55:45 +0000330static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000331getargs_b(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000332{
Thomas Heller3457e4b2003-04-24 16:14:27 +0000333 unsigned char value;
334 if (!PyArg_ParseTuple(args, "b", &value))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000335 return NULL;
Thomas Heller3457e4b2003-04-24 16:14:27 +0000336 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000337}
338
Thomas Heller3457e4b2003-04-24 16:14:27 +0000339static PyObject *
340getargs_B(PyObject *self, PyObject *args)
341{
342 unsigned char value;
343 if (!PyArg_ParseTuple(args, "B", &value))
344 return NULL;
345 return PyLong_FromUnsignedLong((unsigned long)value);
346}
347
348static PyObject *
349getargs_H(PyObject *self, PyObject *args)
350{
351 unsigned short value;
352 if (!PyArg_ParseTuple(args, "H", &value))
353 return NULL;
354 return PyLong_FromUnsignedLong((unsigned long)value);
355}
356
357static PyObject *
358getargs_I(PyObject *self, PyObject *args)
359{
360 unsigned int value;
361 if (!PyArg_ParseTuple(args, "I", &value))
362 return NULL;
363 return PyLong_FromUnsignedLong((unsigned long)value);
364}
365
366static PyObject *
367getargs_k(PyObject *self, PyObject *args)
368{
369 unsigned long value;
370 if (!PyArg_ParseTuple(args, "k", &value))
371 return NULL;
372 return PyLong_FromUnsignedLong(value);
373}
374
375static PyObject *
376getargs_i(PyObject *self, PyObject *args)
377{
378 int value;
379 if (!PyArg_ParseTuple(args, "i", &value))
380 return NULL;
381 return PyLong_FromLong((long)value);
382}
383
Thomas Hellera4ea6032003-04-17 18:55:45 +0000384static PyObject *
385getargs_l(PyObject *self, PyObject *args)
386{
Thomas Heller3457e4b2003-04-24 16:14:27 +0000387 long value;
388 if (!PyArg_ParseTuple(args, "l", &value))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000389 return NULL;
Thomas Heller3457e4b2003-04-24 16:14:27 +0000390 return PyLong_FromLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000391}
392
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000393static PyObject *
394getargs_n(PyObject *self, PyObject *args)
395{
396 Py_ssize_t value;
397 if (!PyArg_ParseTuple(args, "n", &value))
Neal Norwitz8866e0a2007-10-27 04:01:17 +0000398 return NULL;
399 return PyLong_FromSsize_t(value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400}
401
Thomas Hellera4ea6032003-04-17 18:55:45 +0000402#ifdef HAVE_LONG_LONG
Thomas Hellera4ea6032003-04-17 18:55:45 +0000403static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000404getargs_L(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000405{
Thomas Heller3457e4b2003-04-24 16:14:27 +0000406 PY_LONG_LONG value;
407 if (!PyArg_ParseTuple(args, "L", &value))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000408 return NULL;
Thomas Heller3457e4b2003-04-24 16:14:27 +0000409 return PyLong_FromLongLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000410}
411
Thomas Hellera4ea6032003-04-17 18:55:45 +0000412static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000413getargs_K(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000414{
Thomas Heller3457e4b2003-04-24 16:14:27 +0000415 unsigned PY_LONG_LONG value;
416 if (!PyArg_ParseTuple(args, "K", &value))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000417 return NULL;
Thomas Heller3457e4b2003-04-24 16:14:27 +0000418 return PyLong_FromUnsignedLongLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000419}
420#endif
421
422/* This function not only tests the 'k' getargs code, but also the
Christian Heimes217cfd12007-12-02 14:31:20 +0000423 PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */
Thomas Hellera4ea6032003-04-17 18:55:45 +0000424static PyObject *
425test_k_code(PyObject *self)
426{
427 PyObject *tuple, *num;
428 unsigned long value;
429
430 tuple = PyTuple_New(1);
431 if (tuple == NULL)
432 return NULL;
433
Neal Norwitz699cbb72003-04-22 01:28:57 +0000434 /* a number larger than ULONG_MAX even on 64-bit platforms */
Thomas Hellera4ea6032003-04-17 18:55:45 +0000435 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
436 if (num == NULL)
437 return NULL;
438
Christian Heimes217cfd12007-12-02 14:31:20 +0000439 value = PyLong_AsUnsignedLongMask(num);
Neal Norwitz699cbb72003-04-22 01:28:57 +0000440 if (value != ULONG_MAX)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000441 return raiseTestError("test_k_code",
Christian Heimes217cfd12007-12-02 14:31:20 +0000442 "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
Thomas Hellera4ea6032003-04-17 18:55:45 +0000443
444 PyTuple_SET_ITEM(tuple, 0, num);
445
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000446 value = 0;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000447 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
448 return NULL;
Neal Norwitz699cbb72003-04-22 01:28:57 +0000449 if (value != ULONG_MAX)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000450 return raiseTestError("test_k_code",
451 "k code returned wrong value for long 0xFFF...FFF");
452
453 Py_DECREF(num);
454 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
455 if (num == NULL)
456 return NULL;
457
Christian Heimes217cfd12007-12-02 14:31:20 +0000458 value = PyLong_AsUnsignedLongMask(num);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000459 if (value != (unsigned long)-0x42)
460 return raiseTestError("test_k_code",
Christian Heimes217cfd12007-12-02 14:31:20 +0000461 "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
Thomas Hellera4ea6032003-04-17 18:55:45 +0000462
463 PyTuple_SET_ITEM(tuple, 0, num);
464
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000465 value = 0;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000466 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
467 return NULL;
468 if (value != (unsigned long)-0x42)
469 return raiseTestError("test_k_code",
470 "k code returned wrong value for long -0xFFF..000042");
471
472 Py_DECREF(tuple);
473 Py_INCREF(Py_None);
474 return Py_None;
475}
476
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000477
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +0000478/* Test the s and z codes for PyArg_ParseTuple.
479*/
480static PyObject *
481test_s_code(PyObject *self)
482{
483 /* Unicode strings should be accepted */
484 PyObject *tuple, *obj;
485 char *value;
486
487 tuple = PyTuple_New(1);
488 if (tuple == NULL)
489 return NULL;
490
491 obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"),
492 "latin-1", NULL);
493 if (obj == NULL)
494 return NULL;
495
496 PyTuple_SET_ITEM(tuple, 0, obj);
497
498 /* These two blocks used to raise a TypeError:
499 * "argument must be string without null bytes, not str"
500 */
501 if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0)
502 return NULL;
503
504 if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0)
505 return NULL;
506
Alexandre Vassalottib645bc72008-05-15 22:06:59 +0000507 Py_DECREF(tuple);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +0000508 Py_RETURN_NONE;
509}
510
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000511/* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
512 of an error.
513*/
514static PyObject *
Fred Drakeacee69f2002-04-01 14:28:58 +0000515test_u_code(PyObject *self)
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000516{
517 PyObject *tuple, *obj;
518 Py_UNICODE *value;
Neal Norwitz8866e0a2007-10-27 04:01:17 +0000519 Py_ssize_t len;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000520
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000521 tuple = PyTuple_New(1);
522 if (tuple == NULL)
523 return NULL;
524
525 obj = PyUnicode_Decode("test", strlen("test"),
526 "ascii", NULL);
527 if (obj == NULL)
528 return NULL;
529
530 PyTuple_SET_ITEM(tuple, 0, obj);
531
532 value = 0;
533 if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
534 return NULL;
535 if (value != PyUnicode_AS_UNICODE(obj))
536 return raiseTestError("test_u_code",
537 "u code returned wrong value for u'test'");
538 value = 0;
539 if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
540 return NULL;
541 if (value != PyUnicode_AS_UNICODE(obj) ||
542 len != PyUnicode_GET_SIZE(obj))
543 return raiseTestError("test_u_code",
544 "u# code returned wrong values for u'test'");
Tim Petersbaefd9e2003-01-28 20:37:45 +0000545
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000546 Py_DECREF(tuple);
547 Py_INCREF(Py_None);
548 return Py_None;
549}
550
Guido van Rossumfb67be22007-08-29 18:38:11 +0000551/* Test Z and Z# codes for PyArg_ParseTuple */
552static PyObject *
553test_Z_code(PyObject *self)
554{
555 PyObject *tuple, *obj;
556 Py_UNICODE *value1, *value2;
Neal Norwitz8866e0a2007-10-27 04:01:17 +0000557 Py_ssize_t len1, len2;
Guido van Rossumfb67be22007-08-29 18:38:11 +0000558
559 tuple = PyTuple_New(2);
560 if (tuple == NULL)
561 return NULL;
562
563 obj = PyUnicode_FromString("test");
564 PyTuple_SET_ITEM(tuple, 0, obj);
565 Py_INCREF(Py_None);
566 PyTuple_SET_ITEM(tuple, 1, Py_None);
567
568 /* swap values on purpose */
569 value1 = NULL;
570 value2 = PyUnicode_AS_UNICODE(obj);
571
572 /* Test Z for both values */
573 if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0)
574 return NULL;
575 if (value1 != PyUnicode_AS_UNICODE(obj))
576 return raiseTestError("test_Z_code",
577 "Z code returned wrong value for 'test'");
578 if (value2 != NULL)
579 return raiseTestError("test_Z_code",
580 "Z code returned wrong value for None");
581
582 value1 = NULL;
583 value2 = PyUnicode_AS_UNICODE(obj);
584 len1 = -1;
585 len2 = -1;
586
587 /* Test Z# for both values */
588 if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1,
589 &value2, &len2) < 0)
590 return NULL;
591 if (value1 != PyUnicode_AS_UNICODE(obj) ||
592 len1 != PyUnicode_GET_SIZE(obj))
593 return raiseTestError("test_Z_code",
594 "Z# code returned wrong values for 'test'");
595 if (value2 != NULL ||
596 len2 != 0)
597 return raiseTestError("test_Z_code",
598 "Z# code returned wrong values for None'");
599
600 Py_DECREF(tuple);
601 Py_RETURN_NONE;
602}
603
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604static PyObject *
605codec_incrementalencoder(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000606{
607 const char *encoding, *errors = NULL;
608 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
609 &encoding, &errors))
610 return NULL;
611 return PyCodec_IncrementalEncoder(encoding, errors);
612}
613
Thomas Wouters477c8d52006-05-27 19:21:47 +0000614static PyObject *
615codec_incrementaldecoder(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616{
617 const char *encoding, *errors = NULL;
618 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
619 &encoding, &errors))
620 return NULL;
621 return PyCodec_IncrementalDecoder(encoding, errors);
622}
623
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000624
Tim Peters5b8132f2003-01-31 15:52:05 +0000625/* Simple test of _PyLong_NumBits and _PyLong_Sign. */
Tim Petersbaefd9e2003-01-28 20:37:45 +0000626static PyObject *
627test_long_numbits(PyObject *self)
628{
Tim Peters5b8132f2003-01-31 15:52:05 +0000629 struct triple {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000630 long input;
Tim Peters5b8132f2003-01-31 15:52:05 +0000631 size_t nbits;
632 int sign;
633 } testcases[] = {{0, 0, 0},
634 {1L, 1, 1},
635 {-1L, 1, -1},
636 {2L, 2, 1},
637 {-2L, 2, -1},
638 {3L, 2, 1},
639 {-3L, 2, -1},
640 {4L, 3, 1},
641 {-4L, 3, -1},
642 {0x7fffL, 15, 1}, /* one Python long digit */
643 {-0x7fffL, 15, -1},
644 {0xffffL, 16, 1},
645 {-0xffffL, 16, -1},
646 {0xfffffffL, 28, 1},
647 {-0xfffffffL, 28, -1}};
Tim Petersbaefd9e2003-01-28 20:37:45 +0000648 int i;
649
Tim Peters5b8132f2003-01-31 15:52:05 +0000650 for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
651 PyObject *plong = PyLong_FromLong(testcases[i].input);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000652 size_t nbits = _PyLong_NumBits(plong);
Tim Peters5b8132f2003-01-31 15:52:05 +0000653 int sign = _PyLong_Sign(plong);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000654
655 Py_DECREF(plong);
Tim Peters5b8132f2003-01-31 15:52:05 +0000656 if (nbits != testcases[i].nbits)
Tim Petersbaefd9e2003-01-28 20:37:45 +0000657 return raiseTestError("test_long_numbits",
Tim Peters5b8132f2003-01-31 15:52:05 +0000658 "wrong result for _PyLong_NumBits");
659 if (sign != testcases[i].sign)
660 return raiseTestError("test_long_numbits",
661 "wrong result for _PyLong_Sign");
Tim Petersbaefd9e2003-01-28 20:37:45 +0000662 }
663 Py_INCREF(Py_None);
664 return Py_None;
665}
666
Thomas Heller519a0422007-11-15 20:48:54 +0000667/* Example passing NULLs to PyObject_Str(NULL). */
Thomas Woutersa9773292006-04-21 09:43:23 +0000668
669static PyObject *
670test_null_strings(PyObject *self)
671{
Thomas Heller519a0422007-11-15 20:48:54 +0000672 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL);
Thomas Woutersa9773292006-04-21 09:43:23 +0000673 PyObject *tuple = PyTuple_Pack(2, o1, o2);
674 Py_XDECREF(o1);
675 Py_XDECREF(o2);
676 return tuple;
677}
678
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000679static PyObject *
680raise_exception(PyObject *self, PyObject *args)
681{
682 PyObject *exc;
683 PyObject *exc_args, *v;
684 int num_args, i;
685
686 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
687 &exc, &num_args))
688 return NULL;
689
690 exc_args = PyTuple_New(num_args);
691 if (exc_args == NULL)
692 return NULL;
693 for (i = 0; i < num_args; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000694 v = PyLong_FromLong(i);
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000695 if (v == NULL) {
696 Py_DECREF(exc_args);
697 return NULL;
698 }
699 PyTuple_SET_ITEM(exc_args, i, v);
700 }
701 PyErr_SetObject(exc, exc_args);
Michael W. Hudsonf02bcee2003-08-15 13:03:30 +0000702 Py_DECREF(exc_args);
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000703 return NULL;
704}
Tim Peters91621db2001-06-12 20:10:01 +0000705
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000706#ifdef WITH_THREAD
707
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000708/* test_thread_state spawns a thread of its own, and that thread releases
709 * `thread_done` when it's finished. The driver code has to know when the
710 * thread finishes, because the thread uses a PyObject (the callable) that
711 * may go away when the driver finishes. The former lack of this explicit
712 * synchronization caused rare segfaults, so rare that they were seen only
713 * on a Mac buildbot (although they were possible on any box).
714 */
715static PyThread_type_lock thread_done = NULL;
716
Benjamin Petersona786b022008-08-25 21:05:21 +0000717static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000718_make_call(void *callable)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000719{
720 PyObject *rc;
Benjamin Petersona786b022008-08-25 21:05:21 +0000721 int success;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000722 PyGILState_STATE s = PyGILState_Ensure();
Thomas Wouters89f507f2006-12-13 04:49:30 +0000723 rc = PyObject_CallFunction((PyObject *)callable, "");
Benjamin Petersona786b022008-08-25 21:05:21 +0000724 success = (rc != NULL);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000725 Py_XDECREF(rc);
726 PyGILState_Release(s);
Benjamin Petersona786b022008-08-25 21:05:21 +0000727 return success;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000728}
729
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000730/* Same thing, but releases `thread_done` when it returns. This variant
731 * should be called only from threads spawned by test_thread_state().
732 */
733static void
734_make_call_from_thread(void *callable)
735{
736 _make_call(callable);
737 PyThread_release_lock(thread_done);
738}
739
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000740static PyObject *
741test_thread_state(PyObject *self, PyObject *args)
742{
743 PyObject *fn;
Benjamin Petersona786b022008-08-25 21:05:21 +0000744 int success = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000745
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000746 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
747 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000748
Benjamin Petersona786b022008-08-25 21:05:21 +0000749 if (!PyCallable_Check(fn)) {
750 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
751 fn->ob_type->tp_name);
752 return NULL;
753 }
754
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000755 /* Ensure Python is set up for threading */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000756 PyEval_InitThreads();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000757 thread_done = PyThread_allocate_lock();
758 if (thread_done == NULL)
759 return PyErr_NoMemory();
760 PyThread_acquire_lock(thread_done, 1);
761
762 /* Start a new thread with our callback. */
763 PyThread_start_new_thread(_make_call_from_thread, fn);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000764 /* Make the callback with the thread lock held by this thread */
Benjamin Petersona786b022008-08-25 21:05:21 +0000765 success &= _make_call(fn);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000766 /* Do it all again, but this time with the thread-lock released */
767 Py_BEGIN_ALLOW_THREADS
Benjamin Petersona786b022008-08-25 21:05:21 +0000768 success &= _make_call(fn);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000769 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000770 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000771
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000772 /* And once more with and without a thread
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000773 XXX - should use a lock and work out exactly what we are trying
774 to test <wink>
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000775 */
776 Py_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000777 PyThread_start_new_thread(_make_call_from_thread, fn);
Benjamin Petersona786b022008-08-25 21:05:21 +0000778 success &= _make_call(fn);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000779 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000780 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000781
Thomas Wouters89f507f2006-12-13 04:49:30 +0000782 /* Release lock we acquired above. This is required on HP-UX. */
783 PyThread_release_lock(thread_done);
784
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000785 PyThread_free_lock(thread_done);
Benjamin Petersona786b022008-08-25 21:05:21 +0000786 if (!success)
787 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000788 Py_RETURN_NONE;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000789}
790#endif
791
Neal Norwitzb0d26332007-08-25 00:49:05 +0000792/* Some tests of PyUnicode_FromFormat(). This needs more tests. */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000793static PyObject *
794test_string_from_format(PyObject *self, PyObject *args)
795{
796 PyObject *result;
797 char *msg;
798
799#define CHECK_1_FORMAT(FORMAT, TYPE) \
Neal Norwitzb0d26332007-08-25 00:49:05 +0000800 result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000801 if (result == NULL) \
802 return NULL; \
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000803 if (strcmp(_PyUnicode_AsString(result), "1")) { \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000804 msg = FORMAT " failed at 1"; \
805 goto Fail; \
806 } \
807 Py_DECREF(result)
808
809 CHECK_1_FORMAT("%d", int);
810 CHECK_1_FORMAT("%ld", long);
811 /* The z width modifier was added in Python 2.5. */
812 CHECK_1_FORMAT("%zd", Py_ssize_t);
813
814 /* The u type code was added in Python 2.5. */
815 CHECK_1_FORMAT("%u", unsigned int);
816 CHECK_1_FORMAT("%lu", unsigned long);
817 CHECK_1_FORMAT("%zu", size_t);
818
819 Py_RETURN_NONE;
820
821 Fail:
822 Py_XDECREF(result);
823 return raiseTestError("test_string_from_format", msg);
824
825#undef CHECK_1_FORMAT
826}
827
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000828/* This is here to provide a docstring for test_descr. */
829static PyObject *
830test_with_docstring(PyObject *self)
831{
832 Py_RETURN_NONE;
833}
834
Guido van Rossumddefaf32007-01-14 03:31:43 +0000835#ifdef HAVE_GETTIMEOFDAY
836/* Profiling of integer performance */
Martin v. Löwis1c951552008-06-13 07:48:19 +0000837static void print_delta(int test, struct timeval *s, struct timeval *e)
Guido van Rossumddefaf32007-01-14 03:31:43 +0000838{
839 e->tv_sec -= s->tv_sec;
840 e->tv_usec -= s->tv_usec;
841 if (e->tv_usec < 0) {
842 e->tv_sec -=1;
843 e->tv_usec += 1000000;
844 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000845 printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000846}
847
848static PyObject *
849profile_int(PyObject *self, PyObject* args)
850{
851 int i, k;
852 struct timeval start, stop;
853 PyObject *single, **multiple, *op1, *result;
854
855 /* Test 1: Allocate and immediately deallocate
856 many small integers */
857 gettimeofday(&start, NULL);
858 for(k=0; k < 20000; k++)
859 for(i=0; i < 1000; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000860 single = PyLong_FromLong(i);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000861 Py_DECREF(single);
862 }
863 gettimeofday(&stop, NULL);
864 print_delta(1, &start, &stop);
865
866 /* Test 2: Allocate and immediately deallocate
867 many large integers */
868 gettimeofday(&start, NULL);
869 for(k=0; k < 20000; k++)
870 for(i=0; i < 1000; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000871 single = PyLong_FromLong(i+1000000);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000872 Py_DECREF(single);
873 }
874 gettimeofday(&stop, NULL);
875 print_delta(2, &start, &stop);
876
877 /* Test 3: Allocate a few integers, then release
878 them all simultaneously. */
879 multiple = malloc(sizeof(PyObject*) * 1000);
880 gettimeofday(&start, NULL);
881 for(k=0; k < 20000; k++) {
882 for(i=0; i < 1000; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000883 multiple[i] = PyLong_FromLong(i+1000000);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000884 }
885 for(i=0; i < 1000; i++) {
886 Py_DECREF(multiple[i]);
887 }
888 }
889 gettimeofday(&stop, NULL);
890 print_delta(3, &start, &stop);
891
892 /* Test 4: Allocate many integers, then release
893 them all simultaneously. */
894 multiple = malloc(sizeof(PyObject*) * 1000000);
895 gettimeofday(&start, NULL);
896 for(k=0; k < 20; k++) {
897 for(i=0; i < 1000000; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000898 multiple[i] = PyLong_FromLong(i+1000000);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000899 }
900 for(i=0; i < 1000000; i++) {
901 Py_DECREF(multiple[i]);
902 }
903 }
904 gettimeofday(&stop, NULL);
905 print_delta(4, &start, &stop);
906
907 /* Test 5: Allocate many integers < 32000 */
908 multiple = malloc(sizeof(PyObject*) * 1000000);
909 gettimeofday(&start, NULL);
910 for(k=0; k < 10; k++) {
911 for(i=0; i < 1000000; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000912 multiple[i] = PyLong_FromLong(i+1000);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000913 }
914 for(i=0; i < 1000000; i++) {
915 Py_DECREF(multiple[i]);
916 }
917 }
918 gettimeofday(&stop, NULL);
919 print_delta(5, &start, &stop);
920
921 /* Test 6: Perform small int addition */
Christian Heimes217cfd12007-12-02 14:31:20 +0000922 op1 = PyLong_FromLong(1);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000923 gettimeofday(&start, NULL);
924 for(i=0; i < 10000000; i++) {
925 result = PyNumber_Add(op1, op1);
926 Py_DECREF(result);
927 }
928 gettimeofday(&stop, NULL);
929 Py_DECREF(op1);
930 print_delta(6, &start, &stop);
931
932 /* Test 7: Perform medium int addition */
Christian Heimes217cfd12007-12-02 14:31:20 +0000933 op1 = PyLong_FromLong(1000);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000934 gettimeofday(&start, NULL);
935 for(i=0; i < 10000000; i++) {
936 result = PyNumber_Add(op1, op1);
937 Py_DECREF(result);
938 }
939 gettimeofday(&stop, NULL);
940 Py_DECREF(op1);
941 print_delta(7, &start, &stop);
942
943 Py_INCREF(Py_None);
944 return Py_None;
945}
946#endif
947
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000948/* To test the format of tracebacks as printed out. */
949static PyObject *
950traceback_print(PyObject *self, PyObject *args)
951{
952 PyObject *file;
953 PyObject *traceback;
954 int result;
955
956 if (!PyArg_ParseTuple(args, "OO:traceback_print",
957 &traceback, &file))
958 return NULL;
959
960 result = PyTraceBack_Print(traceback, file);
961 if (result < 0)
962 return NULL;
963 Py_RETURN_NONE;
964}
965
Benjamin Petersone6528212008-07-15 15:32:09 +0000966/* To test the format of exceptions as printed out. */
967static PyObject *
968exception_print(PyObject *self, PyObject *args)
969{
970 PyObject *value;
971 PyObject *tb;
972
973 if (!PyArg_ParseTuple(args, "O:exception_print",
974 &value))
975 return NULL;
Benjamin Peterson6784eb72008-08-23 20:32:27 +0000976 if (!PyExceptionInstance_Check(value)) {
977 PyErr_Format(PyExc_TypeError, "an exception instance is required");
978 return NULL;
979 }
Benjamin Petersone6528212008-07-15 15:32:09 +0000980
981 tb = PyException_GetTraceback(value);
982 PyErr_Display((PyObject *) Py_TYPE(value), value, tb);
983 Py_XDECREF(tb);
984
985 Py_RETURN_NONE;
986}
987
988
989
Benjamin Peterson0067bd62008-08-16 16:11:03 +0000990
991/* reliably raise a MemoryError */
992static PyObject *
993raise_memoryerror(PyObject *self)
994{
995 PyErr_NoMemory();
996 return NULL;
997}
998
999
Tim Peters9ea17ac2001-02-02 05:57:15 +00001000static PyMethodDef TestMethods[] = {
Fred Drakeacee69f2002-04-01 14:28:58 +00001001 {"raise_exception", raise_exception, METH_VARARGS},
Benjamin Peterson0067bd62008-08-16 16:11:03 +00001002 {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
Fred Drakeacee69f2002-04-01 14:28:58 +00001003 {"test_config", (PyCFunction)test_config, METH_NOARGS},
1004 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
1005 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
1006 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
Tim Petersbaefd9e2003-01-28 20:37:45 +00001007 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
Thomas Hellera4ea6032003-04-17 18:55:45 +00001008 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
Thomas Woutersa9773292006-04-21 09:43:23 +00001009 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
Thomas Wouters477c8d52006-05-27 19:21:47 +00001010 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001011 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
1012 PyDoc_STR("This is a pretty normal docstring.")},
Thomas Heller3457e4b2003-04-24 16:14:27 +00001013
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001014 {"getargs_tuple", getargs_tuple, METH_VARARGS},
Christian Heimes380f7f22008-02-28 11:19:05 +00001015 {"getargs_keywords", (PyCFunction)getargs_keywords,
1016 METH_VARARGS|METH_KEYWORDS},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001017 {"getargs_b", getargs_b, METH_VARARGS},
1018 {"getargs_B", getargs_B, METH_VARARGS},
1019 {"getargs_H", getargs_H, METH_VARARGS},
1020 {"getargs_I", getargs_I, METH_VARARGS},
1021 {"getargs_k", getargs_k, METH_VARARGS},
1022 {"getargs_i", getargs_i, METH_VARARGS},
1023 {"getargs_l", getargs_l, METH_VARARGS},
1024 {"getargs_n", getargs_n, METH_VARARGS},
Tim Peters91621db2001-06-12 20:10:01 +00001025#ifdef HAVE_LONG_LONG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001026 {"getargs_L", getargs_L, METH_VARARGS},
1027 {"getargs_K", getargs_K, METH_VARARGS},
1028 {"test_longlong_api", test_longlong_api, METH_NOARGS},
Fred Drakeacee69f2002-04-01 14:28:58 +00001029 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001030 {"codec_incrementalencoder",
1031 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
1032 {"codec_incrementaldecoder",
1033 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
Tim Peters91621db2001-06-12 20:10:01 +00001034#endif
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001035 {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS},
Fred Drakeacee69f2002-04-01 14:28:58 +00001036 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
Guido van Rossumfb67be22007-08-29 18:38:11 +00001037 {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS},
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001038#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001039 {"_test_thread_state", test_thread_state, METH_VARARGS},
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001040#endif
Guido van Rossumddefaf32007-01-14 03:31:43 +00001041#ifdef HAVE_GETTIMEOFDAY
1042 {"profile_int", profile_int, METH_NOARGS},
1043#endif
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001044 {"traceback_print", traceback_print, METH_VARARGS},
Benjamin Petersone6528212008-07-15 15:32:09 +00001045 {"exception_print", exception_print, METH_VARARGS},
Tim Peters9ea17ac2001-02-02 05:57:15 +00001046 {NULL, NULL} /* sentinel */
1047};
1048
Thomas Hellera4ea6032003-04-17 18:55:45 +00001049#define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
1050
Thomas Wouters89f507f2006-12-13 04:49:30 +00001051typedef struct {
Christian Heimes1af737c2008-01-23 08:24:23 +00001052 char bool_member;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001053 char byte_member;
1054 unsigned char ubyte_member;
1055 short short_member;
1056 unsigned short ushort_member;
1057 int int_member;
1058 unsigned int uint_member;
1059 long long_member;
1060 unsigned long ulong_member;
1061 float float_member;
1062 double double_member;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001063#ifdef HAVE_LONG_LONG
1064 PY_LONG_LONG longlong_member;
1065 unsigned PY_LONG_LONG ulonglong_member;
1066#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +00001067} all_structmembers;
1068
1069typedef struct {
1070 PyObject_HEAD
1071 all_structmembers structmembers;
1072} test_structmembers;
1073
1074static struct PyMemberDef test_members[] = {
Christian Heimes1af737c2008-01-23 08:24:23 +00001075 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
Thomas Wouters89f507f2006-12-13 04:49:30 +00001076 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
1077 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
1078 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
1079 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
1080 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
1081 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
1082 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
1083 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
1084 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
1085 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001086#ifdef HAVE_LONG_LONG
1087 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
1088 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
1089#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +00001090 {NULL}
1091};
1092
1093
Christian Heimes1af737c2008-01-23 08:24:23 +00001094static PyObject *
1095test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1096{
1097 static char *keywords[] = {
1098 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
1099 "T_INT", "T_UINT", "T_LONG", "T_ULONG",
1100 "T_FLOAT", "T_DOUBLE",
1101#ifdef HAVE_LONG_LONG
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001102 "T_LONGLONG", "T_ULONGLONG",
Christian Heimes1af737c2008-01-23 08:24:23 +00001103#endif
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001104 NULL};
Christian Heimes1af737c2008-01-23 08:24:23 +00001105 static char *fmt = "|bbBhHiIlkfd"
1106#ifdef HAVE_LONG_LONG
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001107 "LK"
Christian Heimes1af737c2008-01-23 08:24:23 +00001108#endif
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001109 ;
Christian Heimes1af737c2008-01-23 08:24:23 +00001110 test_structmembers *ob;
1111 ob = PyObject_New(test_structmembers, type);
1112 if (ob == NULL)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001113 return NULL;
1114 memset(&ob->structmembers, 0, sizeof(all_structmembers));
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001115 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
Christian Heimes1af737c2008-01-23 08:24:23 +00001116 &ob->structmembers.bool_member,
1117 &ob->structmembers.byte_member,
1118 &ob->structmembers.ubyte_member,
1119 &ob->structmembers.short_member,
1120 &ob->structmembers.ushort_member,
1121 &ob->structmembers.int_member,
1122 &ob->structmembers.uint_member,
1123 &ob->structmembers.long_member,
1124 &ob->structmembers.ulong_member,
1125 &ob->structmembers.float_member,
1126 &ob->structmembers.double_member
1127#ifdef HAVE_LONG_LONG
1128 , &ob->structmembers.longlong_member,
1129 &ob->structmembers.ulonglong_member
1130#endif
1131 )) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001132 Py_DECREF(ob);
1133 return NULL;
Christian Heimes1af737c2008-01-23 08:24:23 +00001134 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001135 return (PyObject *)ob;
1136}
1137
Christian Heimes1af737c2008-01-23 08:24:23 +00001138static void
1139test_structmembers_free(PyObject *ob)
1140{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001141 PyObject_FREE(ob);
1142}
1143
1144static PyTypeObject test_structmembersType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001145 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001146 "test_structmembersType",
1147 sizeof(test_structmembers), /* tp_basicsize */
1148 0, /* tp_itemsize */
1149 test_structmembers_free, /* destructor tp_dealloc */
1150 0, /* tp_print */
1151 0, /* tp_getattr */
1152 0, /* tp_setattr */
1153 0, /* tp_compare */
1154 0, /* tp_repr */
1155 0, /* tp_as_number */
1156 0, /* tp_as_sequence */
1157 0, /* tp_as_mapping */
1158 0, /* tp_hash */
1159 0, /* tp_call */
1160 0, /* tp_str */
Christian Heimes1af737c2008-01-23 08:24:23 +00001161 PyObject_GenericGetAttr, /* tp_getattro */
1162 PyObject_GenericSetAttr, /* tp_setattro */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001163 0, /* tp_as_buffer */
1164 0, /* tp_flags */
1165 "Type containing all structmember types",
1166 0, /* traverseproc tp_traverse */
1167 0, /* tp_clear */
1168 0, /* tp_richcompare */
1169 0, /* tp_weaklistoffset */
1170 0, /* tp_iter */
1171 0, /* tp_iternext */
1172 0, /* tp_methods */
Christian Heimes1af737c2008-01-23 08:24:23 +00001173 test_members, /* tp_members */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001174 0,
1175 0,
1176 0,
1177 0,
1178 0,
1179 0,
1180 0,
1181 0,
Christian Heimes1af737c2008-01-23 08:24:23 +00001182 test_structmembers_new, /* tp_new */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001183};
1184
1185
Martin v. Löwis1a214512008-06-11 05:26:20 +00001186
1187static struct PyModuleDef _testcapimodule = {
1188 PyModuleDef_HEAD_INIT,
1189 "_testcapi",
1190 NULL,
1191 -1,
1192 TestMethods,
1193 NULL,
1194 NULL,
1195 NULL,
1196 NULL
1197};
1198
Mark Hammond62b1ab12002-07-23 06:31:15 +00001199PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001200PyInit__testcapi(void)
Tim Peters9ea17ac2001-02-02 05:57:15 +00001201{
Fred Drakeacee69f2002-04-01 14:28:58 +00001202 PyObject *m;
Tim Peters9ea17ac2001-02-02 05:57:15 +00001203
Martin v. Löwis1a214512008-06-11 05:26:20 +00001204 m = PyModule_Create(&_testcapimodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001205 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001206 return NULL;
Tim Peters9ea17ac2001-02-02 05:57:15 +00001207
Christian Heimes90aa7642007-12-19 02:45:37 +00001208 Py_TYPE(&test_structmembersType)=&PyType_Type;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001209 Py_INCREF(&test_structmembersType);
1210 PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType);
1211
Christian Heimes217cfd12007-12-02 14:31:20 +00001212 PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
1213 PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
1214 PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX));
1215 PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX));
1216 PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN));
1217 PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001218 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
1219 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
Thomas Hellera4ea6032003-04-17 18:55:45 +00001220 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
Christian Heimes217cfd12007-12-02 14:31:20 +00001221 PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX));
1222 PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001223 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
1224 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
1225 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
1226 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
1227 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001228 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
1229 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
1230 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
Christian Heimes217cfd12007-12-02 14:31:20 +00001231 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
1232 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001233 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
Thomas Hellera4ea6032003-04-17 18:55:45 +00001234
Tim Petersd66595f2001-02-04 03:09:53 +00001235 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
Fred Drakeacee69f2002-04-01 14:28:58 +00001236 Py_INCREF(TestError);
1237 PyModule_AddObject(m, "error", TestError);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001238 return m;
Tim Peters9ea17ac2001-02-02 05:57:15 +00001239}