blob: 40eaaad63e76915c7330012940dfd8aff68b7f98 [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
8#include "Python.h"
Martin v. Löwis0347a9a2006-10-27 07:06:52 +00009#include <float.h>
Martin v. Löwisb5bc5372006-10-27 06:16:31 +000010#include "structmember.h"
Tim Peters9ea17ac2001-02-02 05:57:15 +000011
Mark Hammond8d98d2c2003-04-19 15:41:53 +000012#ifdef WITH_THREAD
13#include "pythread.h"
14#endif /* WITH_THREAD */
Tim Peters9ea17ac2001-02-02 05:57:15 +000015static PyObject *TestError; /* set to exception object in init */
16
Tim Peters91621db2001-06-12 20:10:01 +000017/* Raise TestError with test_name + ": " + msg, and return NULL. */
18
19static PyObject *
20raiseTestError(const char* test_name, const char* msg)
21{
22 char buf[2048];
23
24 if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
25 PyErr_SetString(TestError, "internal error msg too large");
26 else {
Tim Peters885d4572001-11-28 20:27:42 +000027 PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
Tim Peters91621db2001-06-12 20:10:01 +000028 PyErr_SetString(TestError, buf);
29 }
30 return NULL;
31}
32
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000033/* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
Tim Peters9ea17ac2001-02-02 05:57:15 +000034
35 The ones derived from autoconf on the UNIX-like OSes can be relied
36 upon (in the absence of sloppy cross-compiling), but the Windows
37 platforms have these hardcoded. Better safe than sorry.
38*/
39static PyObject*
Martin v. Löwisb5bc5372006-10-27 06:16:31 +000040sizeof_error(const char* fatname, const char* typname,
Tim Peters9ea17ac2001-02-02 05:57:15 +000041 int expected, int got)
42{
43 char buf[1024];
Tim Petersbaefd9e2003-01-28 20:37:45 +000044 PyOS_snprintf(buf, sizeof(buf),
Tim Peters885d4572001-11-28 20:27:42 +000045 "%.200s #define == %d but sizeof(%.200s) == %d",
Martin v. Löwisb5bc5372006-10-27 06:16:31 +000046 fatname, expected, typname, got);
Tim Peters9ea17ac2001-02-02 05:57:15 +000047 PyErr_SetString(TestError, buf);
48 return (PyObject*)NULL;
49}
50
51static PyObject*
Fred Drakeacee69f2002-04-01 14:28:58 +000052test_config(PyObject *self)
Tim Peters9ea17ac2001-02-02 05:57:15 +000053{
Tim Peters9ea17ac2001-02-02 05:57:15 +000054#define CHECK_SIZEOF(FATNAME, TYPE) \
55 if (FATNAME != sizeof(TYPE)) \
56 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
57
Tim Peters208efe52001-06-26 22:40:47 +000058 CHECK_SIZEOF(SIZEOF_SHORT, short);
Tim Peters9ea17ac2001-02-02 05:57:15 +000059 CHECK_SIZEOF(SIZEOF_INT, int);
60 CHECK_SIZEOF(SIZEOF_LONG, long);
61 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
62 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
63#ifdef HAVE_LONG_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +000064 CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
Tim Peters9ea17ac2001-02-02 05:57:15 +000065#endif
66
67#undef CHECK_SIZEOF
68
69 Py_INCREF(Py_None);
70 return Py_None;
71}
72
Tim Peters5c4d5bf2001-02-12 22:13:26 +000073static PyObject*
Fred Drakeacee69f2002-04-01 14:28:58 +000074test_list_api(PyObject *self)
Tim Peters5c4d5bf2001-02-12 22:13:26 +000075{
76 PyObject* list;
77 int i;
Tim Peters5c4d5bf2001-02-12 22:13:26 +000078
79 /* SF bug 132008: PyList_Reverse segfaults */
80#define NLIST 30
81 list = PyList_New(NLIST);
82 if (list == (PyObject*)NULL)
83 return (PyObject*)NULL;
84 /* list = range(NLIST) */
85 for (i = 0; i < NLIST; ++i) {
86 PyObject* anint = PyInt_FromLong(i);
87 if (anint == (PyObject*)NULL) {
88 Py_DECREF(list);
89 return (PyObject*)NULL;
90 }
91 PyList_SET_ITEM(list, i, anint);
92 }
93 /* list.reverse(), via PyList_Reverse() */
94 i = PyList_Reverse(list); /* should not blow up! */
95 if (i != 0) {
96 Py_DECREF(list);
97 return (PyObject*)NULL;
98 }
99 /* Check that list == range(29, -1, -1) now */
100 for (i = 0; i < NLIST; ++i) {
101 PyObject* anint = PyList_GET_ITEM(list, i);
102 if (PyInt_AS_LONG(anint) != NLIST-1-i) {
103 PyErr_SetString(TestError,
104 "test_list_api: reverse screwed up");
105 Py_DECREF(list);
106 return (PyObject*)NULL;
107 }
108 }
109 Py_DECREF(list);
110#undef NLIST
111
112 Py_INCREF(Py_None);
113 return Py_None;
114}
115
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000116static int
117test_dict_inner(int count)
118{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000119 Py_ssize_t pos = 0, iterations = 0;
120 int i;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000121 PyObject *dict = PyDict_New();
122 PyObject *v, *k;
123
124 if (dict == NULL)
125 return -1;
126
127 for (i = 0; i < count; i++) {
128 v = PyInt_FromLong(i);
129 PyDict_SetItem(dict, v, v);
130 Py_DECREF(v);
131 }
132
133 while (PyDict_Next(dict, &pos, &k, &v)) {
134 PyObject *o;
135 iterations++;
136
137 i = PyInt_AS_LONG(v) + 1;
138 o = PyInt_FromLong(i);
139 if (o == NULL)
140 return -1;
141 if (PyDict_SetItem(dict, k, o) < 0) {
142 Py_DECREF(o);
143 return -1;
144 }
145 Py_DECREF(o);
146 }
147
148 Py_DECREF(dict);
149
150 if (iterations != count) {
151 PyErr_SetString(
152 TestError,
153 "test_dict_iteration: dict iteration went wrong ");
154 return -1;
155 } else {
156 return 0;
157 }
158}
159
160static PyObject*
Fred Drakeacee69f2002-04-01 14:28:58 +0000161test_dict_iteration(PyObject* self)
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000162{
163 int i;
164
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000165 for (i = 0; i < 200; i++) {
166 if (test_dict_inner(i) < 0) {
167 return NULL;
168 }
169 }
170
171 Py_INCREF(Py_None);
172 return Py_None;
173}
174
Tim Peters91621db2001-06-12 20:10:01 +0000175
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000176/* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
Tim Petersff70d3c2001-06-14 01:11:03 +0000177 PyLong_{As, From}{Unsigned,}LongLong().
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000178
179 Note that the meat of the test is contained in testcapi_long.h.
180 This is revolting, but delicate code duplication is worse: "almost
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000181 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000182 dependence on type names makes it impossible to use a parameterized
183 function. A giant macro would be even worse than this. A C++ template
184 would be perfect.
185
186 The "report an error" functions are deliberately not part of the #include
187 file: if the test fails, you can set a breakpoint in the appropriate
188 error function directly, and crawl back from there in the debugger.
189*/
190
191#define UNBIND(X) Py_DECREF(X); (X) = NULL
192
193static PyObject *
194raise_test_long_error(const char* msg)
195{
196 return raiseTestError("test_long_api", msg);
197}
198
199#define TESTNAME test_long_api_inner
200#define TYPENAME long
201#define F_S_TO_PY PyLong_FromLong
202#define F_PY_TO_S PyLong_AsLong
203#define F_U_TO_PY PyLong_FromUnsignedLong
204#define F_PY_TO_U PyLong_AsUnsignedLong
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000205
206#include "testcapi_long.h"
207
208static PyObject *
Fred Drakeacee69f2002-04-01 14:28:58 +0000209test_long_api(PyObject* self)
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000210{
Tim Peters83c9edc2001-06-16 08:10:13 +0000211 return TESTNAME(raise_test_long_error);
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000212}
213
214#undef TESTNAME
215#undef TYPENAME
216#undef F_S_TO_PY
217#undef F_PY_TO_S
218#undef F_U_TO_PY
219#undef F_PY_TO_U
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000220
221#ifdef HAVE_LONG_LONG
Tim Peters91621db2001-06-12 20:10:01 +0000222
223static PyObject *
Tim Petersd1a7da62001-06-13 00:35:57 +0000224raise_test_longlong_error(const char* msg)
225{
226 return raiseTestError("test_longlong_api", msg);
227}
228
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000229#define TESTNAME test_longlong_api_inner
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000230#define TYPENAME PY_LONG_LONG
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000231#define F_S_TO_PY PyLong_FromLongLong
232#define F_PY_TO_S PyLong_AsLongLong
233#define F_U_TO_PY PyLong_FromUnsignedLongLong
234#define F_PY_TO_U PyLong_AsUnsignedLongLong
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000235
236#include "testcapi_long.h"
Tim Petersd1a7da62001-06-13 00:35:57 +0000237
238static PyObject *
Skip Montanaro9582c142006-04-18 01:01:41 +0000239test_longlong_api(PyObject* self, PyObject *args)
Tim Peters91621db2001-06-12 20:10:01 +0000240{
Tim Peters83c9edc2001-06-16 08:10:13 +0000241 return TESTNAME(raise_test_longlong_error);
Tim Peters91621db2001-06-12 20:10:01 +0000242}
243
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000244#undef TESTNAME
245#undef TYPENAME
246#undef F_S_TO_PY
247#undef F_PY_TO_S
248#undef F_U_TO_PY
249#undef F_PY_TO_U
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000250
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000251/* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
Tim Petersd38b1c72001-09-30 05:09:37 +0000252 for both long and int arguments. The test may leak a little memory if
253 it fails.
254*/
255static PyObject *
Fred Drakeacee69f2002-04-01 14:28:58 +0000256test_L_code(PyObject *self)
Tim Petersd38b1c72001-09-30 05:09:37 +0000257{
258 PyObject *tuple, *num;
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000259 PY_LONG_LONG value;
Tim Petersd38b1c72001-09-30 05:09:37 +0000260
Tim Petersd38b1c72001-09-30 05:09:37 +0000261 tuple = PyTuple_New(1);
262 if (tuple == NULL)
263 return NULL;
264
265 num = PyLong_FromLong(42);
266 if (num == NULL)
267 return NULL;
268
269 PyTuple_SET_ITEM(tuple, 0, num);
270
271 value = -1;
272 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
273 return NULL;
274 if (value != 42)
275 return raiseTestError("test_L_code",
276 "L code returned wrong value for long 42");
277
278 Py_DECREF(num);
279 num = PyInt_FromLong(42);
280 if (num == NULL)
281 return NULL;
282
283 PyTuple_SET_ITEM(tuple, 0, num);
284
285 value = -1;
286 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
287 return NULL;
288 if (value != 42)
289 return raiseTestError("test_L_code",
290 "L code returned wrong value for int 42");
291
292 Py_DECREF(tuple);
293 Py_INCREF(Py_None);
294 return Py_None;
295}
296
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000297#endif /* ifdef HAVE_LONG_LONG */
298
Georg Brandl5f135782006-07-26 08:03:10 +0000299/* Test tuple argument processing */
300static PyObject *
301getargs_tuple(PyObject *self, PyObject *args)
302{
303 int a, b, c;
304 if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
305 return NULL;
306 return Py_BuildValue("iii", a, b, c);
307}
308
Christian Heimesea837932008-02-26 17:23:51 +0000309/* test PyArg_ParseTupleAndKeywords */
310static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
311{
312 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
313 static char *fmt="(ii)i|(i(ii))(iii)i";
314 int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
315
316 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
317 &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
318 &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
319 return NULL;
320 return Py_BuildValue("iiiiiiiiii",
321 int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
322 int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
323}
324
Thomas Heller3457e4b2003-04-24 16:14:27 +0000325/* Functions to call PyArg_ParseTuple with integer format codes,
326 and return the result.
327*/
Thomas Hellera4ea6032003-04-17 18:55:45 +0000328static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000329getargs_b(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000330{
Thomas Heller3457e4b2003-04-24 16:14:27 +0000331 unsigned char value;
332 if (!PyArg_ParseTuple(args, "b", &value))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000333 return NULL;
Thomas Heller3457e4b2003-04-24 16:14:27 +0000334 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000335}
336
Thomas Heller3457e4b2003-04-24 16:14:27 +0000337static PyObject *
338getargs_B(PyObject *self, PyObject *args)
339{
340 unsigned char value;
341 if (!PyArg_ParseTuple(args, "B", &value))
342 return NULL;
343 return PyLong_FromUnsignedLong((unsigned long)value);
344}
345
346static PyObject *
347getargs_H(PyObject *self, PyObject *args)
348{
349 unsigned short value;
350 if (!PyArg_ParseTuple(args, "H", &value))
351 return NULL;
352 return PyLong_FromUnsignedLong((unsigned long)value);
353}
354
355static PyObject *
356getargs_I(PyObject *self, PyObject *args)
357{
358 unsigned int value;
359 if (!PyArg_ParseTuple(args, "I", &value))
360 return NULL;
361 return PyLong_FromUnsignedLong((unsigned long)value);
362}
363
364static PyObject *
365getargs_k(PyObject *self, PyObject *args)
366{
367 unsigned long value;
368 if (!PyArg_ParseTuple(args, "k", &value))
369 return NULL;
370 return PyLong_FromUnsignedLong(value);
371}
372
373static PyObject *
374getargs_i(PyObject *self, PyObject *args)
375{
376 int value;
377 if (!PyArg_ParseTuple(args, "i", &value))
378 return NULL;
379 return PyLong_FromLong((long)value);
380}
381
Thomas Hellera4ea6032003-04-17 18:55:45 +0000382static PyObject *
383getargs_l(PyObject *self, PyObject *args)
384{
Thomas Heller3457e4b2003-04-24 16:14:27 +0000385 long value;
386 if (!PyArg_ParseTuple(args, "l", &value))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000387 return NULL;
Thomas Heller3457e4b2003-04-24 16:14:27 +0000388 return PyLong_FromLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000389}
390
Georg Brandl7f573f72006-04-13 07:59:30 +0000391static PyObject *
392getargs_n(PyObject *self, PyObject *args)
393{
394 Py_ssize_t value;
395 if (!PyArg_ParseTuple(args, "n", &value))
396 return NULL;
397 return PyInt_FromSsize_t(value);
398}
399
Thomas Hellera4ea6032003-04-17 18:55:45 +0000400#ifdef HAVE_LONG_LONG
Thomas Hellera4ea6032003-04-17 18:55:45 +0000401static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000402getargs_L(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000403{
Thomas Heller3457e4b2003-04-24 16:14:27 +0000404 PY_LONG_LONG value;
405 if (!PyArg_ParseTuple(args, "L", &value))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000406 return NULL;
Thomas Heller3457e4b2003-04-24 16:14:27 +0000407 return PyLong_FromLongLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000408}
409
Thomas Hellera4ea6032003-04-17 18:55:45 +0000410static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000411getargs_K(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000412{
Thomas Heller3457e4b2003-04-24 16:14:27 +0000413 unsigned PY_LONG_LONG value;
414 if (!PyArg_ParseTuple(args, "K", &value))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000415 return NULL;
Thomas Heller3457e4b2003-04-24 16:14:27 +0000416 return PyLong_FromUnsignedLongLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000417}
418#endif
419
420/* This function not only tests the 'k' getargs code, but also the
421 PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
422static PyObject *
423test_k_code(PyObject *self)
424{
425 PyObject *tuple, *num;
426 unsigned long value;
427
428 tuple = PyTuple_New(1);
429 if (tuple == NULL)
430 return NULL;
431
Neal Norwitz699cbb72003-04-22 01:28:57 +0000432 /* a number larger than ULONG_MAX even on 64-bit platforms */
Thomas Hellera4ea6032003-04-17 18:55:45 +0000433 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
434 if (num == NULL)
435 return NULL;
436
437 value = PyInt_AsUnsignedLongMask(num);
Neal Norwitz699cbb72003-04-22 01:28:57 +0000438 if (value != ULONG_MAX)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000439 return raiseTestError("test_k_code",
440 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
441
442 PyTuple_SET_ITEM(tuple, 0, num);
443
Neal Norwitzb183a252006-04-10 01:03:32 +0000444 value = 0;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000445 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
446 return NULL;
Neal Norwitz699cbb72003-04-22 01:28:57 +0000447 if (value != ULONG_MAX)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000448 return raiseTestError("test_k_code",
449 "k code returned wrong value for long 0xFFF...FFF");
450
451 Py_DECREF(num);
452 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
453 if (num == NULL)
454 return NULL;
455
456 value = PyInt_AsUnsignedLongMask(num);
457 if (value != (unsigned long)-0x42)
458 return raiseTestError("test_k_code",
459 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
460
461 PyTuple_SET_ITEM(tuple, 0, num);
462
Neal Norwitzb183a252006-04-10 01:03:32 +0000463 value = 0;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000464 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
465 return NULL;
466 if (value != (unsigned long)-0x42)
467 return raiseTestError("test_k_code",
468 "k code returned wrong value for long -0xFFF..000042");
469
470 Py_DECREF(tuple);
471 Py_INCREF(Py_None);
472 return Py_None;
473}
474
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000475#ifdef Py_USING_UNICODE
476
477/* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
478 of an error.
479*/
480static PyObject *
Fred Drakeacee69f2002-04-01 14:28:58 +0000481test_u_code(PyObject *self)
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000482{
483 PyObject *tuple, *obj;
484 Py_UNICODE *value;
485 int len;
486
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000487 tuple = PyTuple_New(1);
488 if (tuple == NULL)
489 return NULL;
490
491 obj = PyUnicode_Decode("test", strlen("test"),
492 "ascii", NULL);
493 if (obj == NULL)
494 return NULL;
495
496 PyTuple_SET_ITEM(tuple, 0, obj);
497
498 value = 0;
499 if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
500 return NULL;
501 if (value != PyUnicode_AS_UNICODE(obj))
502 return raiseTestError("test_u_code",
503 "u code returned wrong value for u'test'");
504 value = 0;
505 if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
506 return NULL;
507 if (value != PyUnicode_AS_UNICODE(obj) ||
508 len != PyUnicode_GET_SIZE(obj))
509 return raiseTestError("test_u_code",
510 "u# code returned wrong values for u'test'");
Tim Petersbaefd9e2003-01-28 20:37:45 +0000511
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000512 Py_DECREF(tuple);
513 Py_INCREF(Py_None);
514 return Py_None;
515}
516
Tim Peters8931ff12006-05-13 23:28:20 +0000517static PyObject *
518codec_incrementalencoder(PyObject *self, PyObject *args)
Walter Dörwald9ae019b2006-03-18 14:22:26 +0000519{
520 const char *encoding, *errors = NULL;
Tim Peters59b96c12006-03-21 03:58:41 +0000521 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
Walter Dörwald9ae019b2006-03-18 14:22:26 +0000522 &encoding, &errors))
523 return NULL;
524 return PyCodec_IncrementalEncoder(encoding, errors);
525}
526
Tim Peters8931ff12006-05-13 23:28:20 +0000527static PyObject *
528codec_incrementaldecoder(PyObject *self, PyObject *args)
Walter Dörwald9ae019b2006-03-18 14:22:26 +0000529{
530 const char *encoding, *errors = NULL;
Tim Peters59b96c12006-03-21 03:58:41 +0000531 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
Walter Dörwald9ae019b2006-03-18 14:22:26 +0000532 &encoding, &errors))
533 return NULL;
534 return PyCodec_IncrementalDecoder(encoding, errors);
535}
536
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000537#endif
538
Tim Peters5b8132f2003-01-31 15:52:05 +0000539/* Simple test of _PyLong_NumBits and _PyLong_Sign. */
Tim Petersbaefd9e2003-01-28 20:37:45 +0000540static PyObject *
541test_long_numbits(PyObject *self)
542{
Tim Peters5b8132f2003-01-31 15:52:05 +0000543 struct triple {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000544 long input;
Tim Peters5b8132f2003-01-31 15:52:05 +0000545 size_t nbits;
546 int sign;
547 } testcases[] = {{0, 0, 0},
548 {1L, 1, 1},
549 {-1L, 1, -1},
550 {2L, 2, 1},
551 {-2L, 2, -1},
552 {3L, 2, 1},
553 {-3L, 2, -1},
554 {4L, 3, 1},
555 {-4L, 3, -1},
556 {0x7fffL, 15, 1}, /* one Python long digit */
557 {-0x7fffL, 15, -1},
558 {0xffffL, 16, 1},
559 {-0xffffL, 16, -1},
560 {0xfffffffL, 28, 1},
561 {-0xfffffffL, 28, -1}};
Tim Petersbaefd9e2003-01-28 20:37:45 +0000562 int i;
563
Tim Peters5b8132f2003-01-31 15:52:05 +0000564 for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
565 PyObject *plong = PyLong_FromLong(testcases[i].input);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000566 size_t nbits = _PyLong_NumBits(plong);
Tim Peters5b8132f2003-01-31 15:52:05 +0000567 int sign = _PyLong_Sign(plong);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000568
569 Py_DECREF(plong);
Tim Peters5b8132f2003-01-31 15:52:05 +0000570 if (nbits != testcases[i].nbits)
Tim Petersbaefd9e2003-01-28 20:37:45 +0000571 return raiseTestError("test_long_numbits",
Tim Peters5b8132f2003-01-31 15:52:05 +0000572 "wrong result for _PyLong_NumBits");
573 if (sign != testcases[i].sign)
574 return raiseTestError("test_long_numbits",
575 "wrong result for _PyLong_Sign");
Tim Petersbaefd9e2003-01-28 20:37:45 +0000576 }
577 Py_INCREF(Py_None);
578 return Py_None;
579}
580
Neal Norwitz4a53dad2006-03-15 05:43:10 +0000581/* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
582
583static PyObject *
584test_null_strings(PyObject *self)
585{
586 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL);
587 PyObject *tuple = PyTuple_Pack(2, o1, o2);
588 Py_XDECREF(o1);
589 Py_XDECREF(o2);
590 return tuple;
591}
592
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000593static PyObject *
594raise_exception(PyObject *self, PyObject *args)
595{
596 PyObject *exc;
597 PyObject *exc_args, *v;
598 int num_args, i;
599
600 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
601 &exc, &num_args))
602 return NULL;
603
604 exc_args = PyTuple_New(num_args);
605 if (exc_args == NULL)
606 return NULL;
607 for (i = 0; i < num_args; ++i) {
608 v = PyInt_FromLong(i);
609 if (v == NULL) {
610 Py_DECREF(exc_args);
611 return NULL;
612 }
613 PyTuple_SET_ITEM(exc_args, i, v);
614 }
615 PyErr_SetObject(exc, exc_args);
Michael W. Hudsonf02bcee2003-08-15 13:03:30 +0000616 Py_DECREF(exc_args);
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000617 return NULL;
618}
Tim Peters91621db2001-06-12 20:10:01 +0000619
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000620#ifdef WITH_THREAD
621
Tim Peters59b96c12006-03-21 03:58:41 +0000622/* test_thread_state spawns a thread of its own, and that thread releases
623 * `thread_done` when it's finished. The driver code has to know when the
624 * thread finishes, because the thread uses a PyObject (the callable) that
625 * may go away when the driver finishes. The former lack of this explicit
626 * synchronization caused rare segfaults, so rare that they were seen only
627 * on a Mac buildbot (although they were possible on any box).
628 */
629static PyThread_type_lock thread_done = NULL;
630
631static void
632_make_call(void *callable)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000633{
634 PyObject *rc;
635 PyGILState_STATE s = PyGILState_Ensure();
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000636 rc = PyObject_CallFunction((PyObject *)callable, "");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000637 Py_XDECREF(rc);
638 PyGILState_Release(s);
639}
640
Tim Peters59b96c12006-03-21 03:58:41 +0000641/* Same thing, but releases `thread_done` when it returns. This variant
642 * should be called only from threads spawned by test_thread_state().
643 */
644static void
645_make_call_from_thread(void *callable)
646{
647 _make_call(callable);
648 PyThread_release_lock(thread_done);
649}
650
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000651static PyObject *
652test_thread_state(PyObject *self, PyObject *args)
653{
654 PyObject *fn;
Tim Peters59b96c12006-03-21 03:58:41 +0000655
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000656 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
657 return NULL;
Tim Peters59b96c12006-03-21 03:58:41 +0000658
659 /* Ensure Python is set up for threading */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000660 PyEval_InitThreads();
Tim Peters59b96c12006-03-21 03:58:41 +0000661 thread_done = PyThread_allocate_lock();
662 if (thread_done == NULL)
663 return PyErr_NoMemory();
664 PyThread_acquire_lock(thread_done, 1);
665
666 /* Start a new thread with our callback. */
667 PyThread_start_new_thread(_make_call_from_thread, fn);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000668 /* Make the callback with the thread lock held by this thread */
669 _make_call(fn);
670 /* Do it all again, but this time with the thread-lock released */
671 Py_BEGIN_ALLOW_THREADS
672 _make_call(fn);
Tim Peters59b96c12006-03-21 03:58:41 +0000673 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000674 Py_END_ALLOW_THREADS
Tim Peters59b96c12006-03-21 03:58:41 +0000675
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000676 /* And once more with and without a thread
Tim Peters59b96c12006-03-21 03:58:41 +0000677 XXX - should use a lock and work out exactly what we are trying
678 to test <wink>
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000679 */
680 Py_BEGIN_ALLOW_THREADS
Tim Peters59b96c12006-03-21 03:58:41 +0000681 PyThread_start_new_thread(_make_call_from_thread, fn);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000682 _make_call(fn);
Tim Peters59b96c12006-03-21 03:58:41 +0000683 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000684 Py_END_ALLOW_THREADS
Tim Peters59b96c12006-03-21 03:58:41 +0000685
Neal Norwitz97a57222006-10-28 21:17:51 +0000686 /* Release lock we acquired above. This is required on HP-UX. */
687 PyThread_release_lock(thread_done);
688
Tim Peters59b96c12006-03-21 03:58:41 +0000689 PyThread_free_lock(thread_done);
690 Py_RETURN_NONE;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000691}
692#endif
693
Tim Peters375f06b2006-05-13 23:33:19 +0000694/* Some tests of PyString_FromFormat(). This needs more tests. */
Tim Peters8931ff12006-05-13 23:28:20 +0000695static PyObject *
696test_string_from_format(PyObject *self, PyObject *args)
697{
698 PyObject *result;
699 char *msg;
700
701#define CHECK_1_FORMAT(FORMAT, TYPE) \
702 result = PyString_FromFormat(FORMAT, (TYPE)1); \
703 if (result == NULL) \
704 return NULL; \
705 if (strcmp(PyString_AsString(result), "1")) { \
706 msg = FORMAT " failed at 1"; \
707 goto Fail; \
708 } \
709 Py_DECREF(result)
710
711 CHECK_1_FORMAT("%d", int);
712 CHECK_1_FORMAT("%ld", long);
713 /* The z width modifier was added in Python 2.5. */
714 CHECK_1_FORMAT("%zd", Py_ssize_t);
715
716 /* The u type code was added in Python 2.5. */
717 CHECK_1_FORMAT("%u", unsigned int);
718 CHECK_1_FORMAT("%lu", unsigned long);
719 CHECK_1_FORMAT("%zu", size_t);
720
721 Py_RETURN_NONE;
722
723 Fail:
724 Py_XDECREF(result);
725 return raiseTestError("test_string_from_format", msg);
726
727#undef CHECK_1_FORMAT
728}
729
Georg Brandle9462c72006-08-04 18:03:37 +0000730/* This is here to provide a docstring for test_descr. */
731static PyObject *
732test_with_docstring(PyObject *self)
733{
734 Py_RETURN_NONE;
735}
736
Tim Peters9ea17ac2001-02-02 05:57:15 +0000737static PyMethodDef TestMethods[] = {
Fred Drakeacee69f2002-04-01 14:28:58 +0000738 {"raise_exception", raise_exception, METH_VARARGS},
739 {"test_config", (PyCFunction)test_config, METH_NOARGS},
740 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
741 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
742 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
Tim Petersbaefd9e2003-01-28 20:37:45 +0000743 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
Thomas Hellera4ea6032003-04-17 18:55:45 +0000744 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
Neal Norwitz4a53dad2006-03-15 05:43:10 +0000745 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
Tim Peters8931ff12006-05-13 23:28:20 +0000746 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
Georg Brandle9462c72006-08-04 18:03:37 +0000747 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
748 PyDoc_STR("This is a pretty normal docstring.")},
Thomas Heller3457e4b2003-04-24 16:14:27 +0000749
Georg Brandl5f135782006-07-26 08:03:10 +0000750 {"getargs_tuple", getargs_tuple, METH_VARARGS},
Christian Heimesea837932008-02-26 17:23:51 +0000751 {"getargs_keywords", (PyCFunction)getargs_keywords,
752 METH_VARARGS|METH_KEYWORDS},
Georg Brandl7f573f72006-04-13 07:59:30 +0000753 {"getargs_b", getargs_b, METH_VARARGS},
754 {"getargs_B", getargs_B, METH_VARARGS},
755 {"getargs_H", getargs_H, METH_VARARGS},
756 {"getargs_I", getargs_I, METH_VARARGS},
757 {"getargs_k", getargs_k, METH_VARARGS},
758 {"getargs_i", getargs_i, METH_VARARGS},
759 {"getargs_l", getargs_l, METH_VARARGS},
760 {"getargs_n", getargs_n, METH_VARARGS},
Tim Peters91621db2001-06-12 20:10:01 +0000761#ifdef HAVE_LONG_LONG
Georg Brandl7f573f72006-04-13 07:59:30 +0000762 {"getargs_L", getargs_L, METH_VARARGS},
763 {"getargs_K", getargs_K, METH_VARARGS},
764 {"test_longlong_api", test_longlong_api, METH_NOARGS},
Fred Drakeacee69f2002-04-01 14:28:58 +0000765 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
Walter Dörwald9ae019b2006-03-18 14:22:26 +0000766 {"codec_incrementalencoder",
767 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
768 {"codec_incrementaldecoder",
769 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
Tim Peters91621db2001-06-12 20:10:01 +0000770#endif
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000771#ifdef Py_USING_UNICODE
Fred Drakeacee69f2002-04-01 14:28:58 +0000772 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000773#endif
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000774#ifdef WITH_THREAD
Georg Brandl7f573f72006-04-13 07:59:30 +0000775 {"_test_thread_state", test_thread_state, METH_VARARGS},
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000776#endif
Tim Peters9ea17ac2001-02-02 05:57:15 +0000777 {NULL, NULL} /* sentinel */
778};
779
Thomas Hellera4ea6032003-04-17 18:55:45 +0000780#define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
781
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000782typedef struct {
Georg Brandl32a3fb52008-01-21 21:23:15 +0000783 char bool_member;
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000784 char byte_member;
785 unsigned char ubyte_member;
786 short short_member;
787 unsigned short ushort_member;
788 int int_member;
789 unsigned int uint_member;
790 long long_member;
791 unsigned long ulong_member;
792 float float_member;
793 double double_member;
Martin v. Löwis6371cd82007-06-09 07:42:52 +0000794#ifdef HAVE_LONG_LONG
795 PY_LONG_LONG longlong_member;
796 unsigned PY_LONG_LONG ulonglong_member;
797#endif
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000798} all_structmembers;
799
800typedef struct {
801 PyObject_HEAD
802 all_structmembers structmembers;
803} test_structmembers;
804
805static struct PyMemberDef test_members[] = {
Georg Brandl32a3fb52008-01-21 21:23:15 +0000806 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000807 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
808 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
809 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
810 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
811 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
812 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
813 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
814 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
815 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
816 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
Martin v. Löwis6371cd82007-06-09 07:42:52 +0000817#ifdef HAVE_LONG_LONG
818 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
819 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
820#endif
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000821 {NULL}
822};
823
824
Georg Brandlc09b94e2008-01-21 21:28:32 +0000825static PyObject *
826test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
827{
828 static char *keywords[] = {
829 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
830 "T_INT", "T_UINT", "T_LONG", "T_ULONG",
831 "T_FLOAT", "T_DOUBLE",
832#ifdef HAVE_LONG_LONG
Martin v. Löwis6371cd82007-06-09 07:42:52 +0000833 "T_LONGLONG", "T_ULONGLONG",
Georg Brandlc09b94e2008-01-21 21:28:32 +0000834#endif
Martin v. Löwis6371cd82007-06-09 07:42:52 +0000835 NULL};
Georg Brandlc09b94e2008-01-21 21:28:32 +0000836 static char *fmt = "|bbBhHiIlkfd"
837#ifdef HAVE_LONG_LONG
Martin v. Löwis6371cd82007-06-09 07:42:52 +0000838 "LK"
Georg Brandlc09b94e2008-01-21 21:28:32 +0000839#endif
Martin v. Löwis6371cd82007-06-09 07:42:52 +0000840 ;
Georg Brandlc09b94e2008-01-21 21:28:32 +0000841 test_structmembers *ob;
842 ob = PyObject_New(test_structmembers, type);
843 if (ob == NULL)
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000844 return NULL;
845 memset(&ob->structmembers, 0, sizeof(all_structmembers));
Martin v. Löwis6371cd82007-06-09 07:42:52 +0000846 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
Georg Brandlc09b94e2008-01-21 21:28:32 +0000847 &ob->structmembers.bool_member,
848 &ob->structmembers.byte_member,
849 &ob->structmembers.ubyte_member,
850 &ob->structmembers.short_member,
851 &ob->structmembers.ushort_member,
852 &ob->structmembers.int_member,
853 &ob->structmembers.uint_member,
854 &ob->structmembers.long_member,
855 &ob->structmembers.ulong_member,
856 &ob->structmembers.float_member,
857 &ob->structmembers.double_member
858#ifdef HAVE_LONG_LONG
859 , &ob->structmembers.longlong_member,
860 &ob->structmembers.ulonglong_member
861#endif
862 )) {
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000863 Py_DECREF(ob);
864 return NULL;
Georg Brandlc09b94e2008-01-21 21:28:32 +0000865 }
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000866 return (PyObject *)ob;
867}
868
Georg Brandlc09b94e2008-01-21 21:28:32 +0000869static void
870test_structmembers_free(PyObject *ob)
871{
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000872 PyObject_FREE(ob);
873}
874
875static PyTypeObject test_structmembersType = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000876 PyVarObject_HEAD_INIT(NULL, 0)
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000877 "test_structmembersType",
878 sizeof(test_structmembers), /* tp_basicsize */
879 0, /* tp_itemsize */
880 test_structmembers_free, /* destructor tp_dealloc */
881 0, /* tp_print */
882 0, /* tp_getattr */
883 0, /* tp_setattr */
884 0, /* tp_compare */
885 0, /* tp_repr */
886 0, /* tp_as_number */
887 0, /* tp_as_sequence */
888 0, /* tp_as_mapping */
889 0, /* tp_hash */
890 0, /* tp_call */
891 0, /* tp_str */
Georg Brandlc09b94e2008-01-21 21:28:32 +0000892 PyObject_GenericGetAttr, /* tp_getattro */
893 PyObject_GenericSetAttr, /* tp_setattro */
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000894 0, /* tp_as_buffer */
895 0, /* tp_flags */
896 "Type containing all structmember types",
897 0, /* traverseproc tp_traverse */
898 0, /* tp_clear */
899 0, /* tp_richcompare */
900 0, /* tp_weaklistoffset */
901 0, /* tp_iter */
902 0, /* tp_iternext */
903 0, /* tp_methods */
Georg Brandlc09b94e2008-01-21 21:28:32 +0000904 test_members, /* tp_members */
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000905 0,
906 0,
907 0,
908 0,
909 0,
910 0,
911 0,
912 0,
Georg Brandlc09b94e2008-01-21 21:28:32 +0000913 test_structmembers_new, /* tp_new */
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000914};
915
916
Mark Hammond62b1ab12002-07-23 06:31:15 +0000917PyMODINIT_FUNC
Tim Petersd66595f2001-02-04 03:09:53 +0000918init_testcapi(void)
Tim Peters9ea17ac2001-02-02 05:57:15 +0000919{
Fred Drakeacee69f2002-04-01 14:28:58 +0000920 PyObject *m;
Tim Peters9ea17ac2001-02-02 05:57:15 +0000921
Tim Petersd66595f2001-02-04 03:09:53 +0000922 m = Py_InitModule("_testcapi", TestMethods);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000923 if (m == NULL)
924 return;
Tim Peters9ea17ac2001-02-02 05:57:15 +0000925
Christian Heimese93237d2007-12-19 02:37:44 +0000926 Py_TYPE(&test_structmembersType)=&PyType_Type;
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000927 Py_INCREF(&test_structmembersType);
928 PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType);
929
930 PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX));
931 PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN));
Thomas Hellera4ea6032003-04-17 18:55:45 +0000932 PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX));
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000933 PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX));
934 PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN));
Thomas Hellera4ea6032003-04-17 18:55:45 +0000935 PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX));
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000936 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
937 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
Thomas Hellera4ea6032003-04-17 18:55:45 +0000938 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
Thomas Hellera4ea6032003-04-17 18:55:45 +0000939 PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX));
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000940 PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN));
941 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
942 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
943 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
944 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
945 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
Martin v. Löwis6371cd82007-06-09 07:42:52 +0000946 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
947 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
948 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
Georg Brandl635af322006-04-13 07:29:18 +0000949 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX));
Martin v. Löwisb5bc5372006-10-27 06:16:31 +0000950 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN));
Thomas Hellera4ea6032003-04-17 18:55:45 +0000951
Tim Petersd66595f2001-02-04 03:09:53 +0000952 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
Fred Drakeacee69f2002-04-01 14:28:58 +0000953 Py_INCREF(TestError);
954 PyModule_AddObject(m, "error", TestError);
Tim Peters9ea17ac2001-02-02 05:57:15 +0000955}