blob: b3ed0474b0a11f41838d93e8b2df125bc6aa0781 [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"
Benjamin Peterson91d58bd2009-12-13 21:30:54 +000013#include "datetime.h"
Tim Peters9ea17ac2001-02-02 05:57:15 +000014
Mark Hammond8d98d2c2003-04-19 15:41:53 +000015#ifdef WITH_THREAD
16#include "pythread.h"
17#endif /* WITH_THREAD */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018static PyObject *TestError; /* set to exception object in init */
Tim Peters9ea17ac2001-02-02 05:57:15 +000019
Tim Peters91621db2001-06-12 20:10:01 +000020/* Raise TestError with test_name + ": " + msg, and return NULL. */
21
22static PyObject *
23raiseTestError(const char* test_name, const char* msg)
24{
Victor Stinner6ced7c42011-03-21 18:15:42 +010025 PyErr_Format(TestError, "%s: %s", test_name, msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 return NULL;
Tim Peters91621db2001-06-12 20:10:01 +000027}
28
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000029/* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
Tim Peters9ea17ac2001-02-02 05:57:15 +000030
31 The ones derived from autoconf on the UNIX-like OSes can be relied
32 upon (in the absence of sloppy cross-compiling), but the Windows
33 platforms have these hardcoded. Better safe than sorry.
34*/
35static PyObject*
Thomas Wouters89f507f2006-12-13 04:49:30 +000036sizeof_error(const char* fatname, const char* typname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 int expected, int got)
Tim Peters9ea17ac2001-02-02 05:57:15 +000038{
Victor Stinner499dfcf2011-03-21 13:26:24 +010039 PyErr_Format(TestError,
40 "%s #define == %d but sizeof(%s) == %d",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 fatname, expected, typname, got);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 return (PyObject*)NULL;
Tim Peters9ea17ac2001-02-02 05:57:15 +000043}
44
45static PyObject*
Fred Drakeacee69f2002-04-01 14:28:58 +000046test_config(PyObject *self)
Tim Peters9ea17ac2001-02-02 05:57:15 +000047{
Tim Peters9ea17ac2001-02-02 05:57:15 +000048#define CHECK_SIZEOF(FATNAME, TYPE) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 if (FATNAME != sizeof(TYPE)) \
50 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
Tim Peters9ea17ac2001-02-02 05:57:15 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 CHECK_SIZEOF(SIZEOF_SHORT, short);
53 CHECK_SIZEOF(SIZEOF_INT, int);
54 CHECK_SIZEOF(SIZEOF_LONG, long);
55 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
56 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
Tim Peters9ea17ac2001-02-02 05:57:15 +000057#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
Tim Peters9ea17ac2001-02-02 05:57:15 +000059#endif
60
61#undef CHECK_SIZEOF
62
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 Py_INCREF(Py_None);
64 return Py_None;
Tim Peters9ea17ac2001-02-02 05:57:15 +000065}
66
Tim Peters5c4d5bf2001-02-12 22:13:26 +000067static PyObject*
Fred Drakeacee69f2002-04-01 14:28:58 +000068test_list_api(PyObject *self)
Tim Peters5c4d5bf2001-02-12 22:13:26 +000069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 PyObject* list;
71 int i;
Tim Peters5c4d5bf2001-02-12 22:13:26 +000072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 /* SF bug 132008: PyList_Reverse segfaults */
Tim Peters5c4d5bf2001-02-12 22:13:26 +000074#define NLIST 30
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 list = PyList_New(NLIST);
76 if (list == (PyObject*)NULL)
77 return (PyObject*)NULL;
78 /* list = range(NLIST) */
79 for (i = 0; i < NLIST; ++i) {
80 PyObject* anint = PyLong_FromLong(i);
81 if (anint == (PyObject*)NULL) {
82 Py_DECREF(list);
83 return (PyObject*)NULL;
84 }
85 PyList_SET_ITEM(list, i, anint);
86 }
87 /* list.reverse(), via PyList_Reverse() */
88 i = PyList_Reverse(list); /* should not blow up! */
89 if (i != 0) {
90 Py_DECREF(list);
91 return (PyObject*)NULL;
92 }
93 /* Check that list == range(29, -1, -1) now */
94 for (i = 0; i < NLIST; ++i) {
95 PyObject* anint = PyList_GET_ITEM(list, i);
96 if (PyLong_AS_LONG(anint) != NLIST-1-i) {
97 PyErr_SetString(TestError,
98 "test_list_api: reverse screwed up");
99 Py_DECREF(list);
100 return (PyObject*)NULL;
101 }
102 }
103 Py_DECREF(list);
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000104#undef NLIST
105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 Py_INCREF(Py_None);
107 return Py_None;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000108}
109
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000110static int
111test_dict_inner(int count)
112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 Py_ssize_t pos = 0, iterations = 0;
114 int i;
115 PyObject *dict = PyDict_New();
116 PyObject *v, *k;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 if (dict == NULL)
119 return -1;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 for (i = 0; i < count; i++) {
122 v = PyLong_FromLong(i);
123 PyDict_SetItem(dict, v, v);
124 Py_DECREF(v);
125 }
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 while (PyDict_Next(dict, &pos, &k, &v)) {
128 PyObject *o;
129 iterations++;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 i = PyLong_AS_LONG(v) + 1;
132 o = PyLong_FromLong(i);
133 if (o == NULL)
134 return -1;
135 if (PyDict_SetItem(dict, k, o) < 0) {
136 Py_DECREF(o);
137 return -1;
138 }
139 Py_DECREF(o);
140 }
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 Py_DECREF(dict);
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 if (iterations != count) {
145 PyErr_SetString(
146 TestError,
147 "test_dict_iteration: dict iteration went wrong ");
148 return -1;
149 } else {
150 return 0;
151 }
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000152}
153
154static PyObject*
Fred Drakeacee69f2002-04-01 14:28:58 +0000155test_dict_iteration(PyObject* self)
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 int i;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 for (i = 0; i < 200; i++) {
160 if (test_dict_inner(i) < 0) {
161 return NULL;
162 }
163 }
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 Py_INCREF(Py_None);
166 return Py_None;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000167}
168
Tim Peters91621db2001-06-12 20:10:01 +0000169
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000170/* Issue #4701: Check that PyObject_Hash implicitly calls
171 * PyType_Ready if it hasn't already been called
172 */
173static PyTypeObject _HashInheritanceTester_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 PyVarObject_HEAD_INIT(NULL, 0)
175 "hashinheritancetester", /* Name of this type */
176 sizeof(PyObject), /* Basic object size */
177 0, /* Item size for varobject */
178 (destructor)PyObject_Del, /* tp_dealloc */
179 0, /* tp_print */
180 0, /* tp_getattr */
181 0, /* tp_setattr */
182 0, /* tp_reserved */
183 0, /* tp_repr */
184 0, /* tp_as_number */
185 0, /* tp_as_sequence */
186 0, /* tp_as_mapping */
187 0, /* tp_hash */
188 0, /* tp_call */
189 0, /* tp_str */
190 PyObject_GenericGetAttr, /* tp_getattro */
191 0, /* tp_setattro */
192 0, /* tp_as_buffer */
193 Py_TPFLAGS_DEFAULT, /* tp_flags */
194 0, /* tp_doc */
195 0, /* tp_traverse */
196 0, /* tp_clear */
197 0, /* tp_richcompare */
198 0, /* tp_weaklistoffset */
199 0, /* tp_iter */
200 0, /* tp_iternext */
201 0, /* tp_methods */
202 0, /* tp_members */
203 0, /* tp_getset */
204 0, /* tp_base */
205 0, /* tp_dict */
206 0, /* tp_descr_get */
207 0, /* tp_descr_set */
208 0, /* tp_dictoffset */
209 0, /* tp_init */
210 0, /* tp_alloc */
211 PyType_GenericNew, /* tp_new */
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000212};
213
214static PyObject*
215test_lazy_hash_inheritance(PyObject* self)
216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 PyTypeObject *type;
218 PyObject *obj;
Antoine Pitrou29aad002010-10-23 19:42:38 +0000219 Py_hash_t hash;
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 type = &_HashInheritanceTester_Type;
Benjamin Petersonf1726372009-05-05 21:11:54 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 if (type->tp_dict != NULL)
224 /* The type has already been initialized. This probably means
225 -R is being used. */
226 Py_RETURN_NONE;
Benjamin Petersonf1726372009-05-05 21:11:54 +0000227
228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 obj = PyObject_New(PyObject, type);
230 if (obj == NULL) {
231 PyErr_Clear();
232 PyErr_SetString(
233 TestError,
234 "test_lazy_hash_inheritance: failed to create object");
235 return NULL;
236 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (type->tp_dict != NULL) {
239 PyErr_SetString(
240 TestError,
241 "test_lazy_hash_inheritance: type initialised too soon");
242 Py_DECREF(obj);
243 return NULL;
244 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 hash = PyObject_Hash(obj);
247 if ((hash == -1) && PyErr_Occurred()) {
248 PyErr_Clear();
249 PyErr_SetString(
250 TestError,
251 "test_lazy_hash_inheritance: could not hash object");
252 Py_DECREF(obj);
253 return NULL;
254 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 if (type->tp_dict == NULL) {
257 PyErr_SetString(
258 TestError,
259 "test_lazy_hash_inheritance: type not initialised by hash()");
260 Py_DECREF(obj);
261 return NULL;
262 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 if (type->tp_hash != PyType_Type.tp_hash) {
265 PyErr_SetString(
266 TestError,
267 "test_lazy_hash_inheritance: unexpected hash function");
268 Py_DECREF(obj);
269 return NULL;
270 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 Py_DECREF(obj);
Benjamin Petersonf1726372009-05-05 21:11:54 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 Py_RETURN_NONE;
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000275}
276
277
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000278/* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
Tim Petersff70d3c2001-06-14 01:11:03 +0000279 PyLong_{As, From}{Unsigned,}LongLong().
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000280
281 Note that the meat of the test is contained in testcapi_long.h.
282 This is revolting, but delicate code duplication is worse: "almost
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000283 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000284 dependence on type names makes it impossible to use a parameterized
285 function. A giant macro would be even worse than this. A C++ template
286 would be perfect.
287
288 The "report an error" functions are deliberately not part of the #include
289 file: if the test fails, you can set a breakpoint in the appropriate
290 error function directly, and crawl back from there in the debugger.
291*/
292
293#define UNBIND(X) Py_DECREF(X); (X) = NULL
294
295static PyObject *
296raise_test_long_error(const char* msg)
297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 return raiseTestError("test_long_api", msg);
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000299}
300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301#define TESTNAME test_long_api_inner
302#define TYPENAME long
303#define F_S_TO_PY PyLong_FromLong
304#define F_PY_TO_S PyLong_AsLong
305#define F_U_TO_PY PyLong_FromUnsignedLong
306#define F_PY_TO_U PyLong_AsUnsignedLong
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000307
308#include "testcapi_long.h"
309
310static PyObject *
Fred Drakeacee69f2002-04-01 14:28:58 +0000311test_long_api(PyObject* self)
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 return TESTNAME(raise_test_long_error);
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000314}
315
316#undef TESTNAME
317#undef TYPENAME
318#undef F_S_TO_PY
319#undef F_PY_TO_S
320#undef F_U_TO_PY
321#undef F_PY_TO_U
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000322
323#ifdef HAVE_LONG_LONG
Tim Peters91621db2001-06-12 20:10:01 +0000324
325static PyObject *
Tim Petersd1a7da62001-06-13 00:35:57 +0000326raise_test_longlong_error(const char* msg)
327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 return raiseTestError("test_longlong_api", msg);
Tim Petersd1a7da62001-06-13 00:35:57 +0000329}
330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331#define TESTNAME test_longlong_api_inner
332#define TYPENAME PY_LONG_LONG
333#define F_S_TO_PY PyLong_FromLongLong
334#define F_PY_TO_S PyLong_AsLongLong
335#define F_U_TO_PY PyLong_FromUnsignedLongLong
336#define F_PY_TO_U PyLong_AsUnsignedLongLong
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000337
338#include "testcapi_long.h"
Tim Petersd1a7da62001-06-13 00:35:57 +0000339
340static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000341test_longlong_api(PyObject* self, PyObject *args)
Tim Peters91621db2001-06-12 20:10:01 +0000342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 return TESTNAME(raise_test_longlong_error);
Tim Peters91621db2001-06-12 20:10:01 +0000344}
345
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000346#undef TESTNAME
347#undef TYPENAME
348#undef F_S_TO_PY
349#undef F_PY_TO_S
350#undef F_U_TO_PY
351#undef F_PY_TO_U
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000352
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000353/* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG
354 is tested by test_long_api_inner. This test will concentrate on proper
355 handling of overflow.
356*/
357
358static PyObject *
359test_long_and_overflow(PyObject *self)
360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 PyObject *num, *one, *temp;
362 long value;
363 int overflow;
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 /* Test that overflow is set properly for a large value. */
366 /* num is a number larger than LONG_MAX even on 64-bit platforms */
367 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
368 if (num == NULL)
369 return NULL;
370 overflow = 1234;
371 value = PyLong_AsLongAndOverflow(num, &overflow);
372 Py_DECREF(num);
373 if (value == -1 && PyErr_Occurred())
374 return NULL;
375 if (value != -1)
376 return raiseTestError("test_long_and_overflow",
377 "return value was not set to -1");
378 if (overflow != 1)
379 return raiseTestError("test_long_and_overflow",
380 "overflow was not set to 1");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 /* Same again, with num = LONG_MAX + 1 */
383 num = PyLong_FromLong(LONG_MAX);
384 if (num == NULL)
385 return NULL;
386 one = PyLong_FromLong(1L);
387 if (one == NULL) {
388 Py_DECREF(num);
389 return NULL;
390 }
391 temp = PyNumber_Add(num, one);
392 Py_DECREF(one);
393 Py_DECREF(num);
394 num = temp;
395 if (num == NULL)
396 return NULL;
397 overflow = 0;
398 value = PyLong_AsLongAndOverflow(num, &overflow);
399 Py_DECREF(num);
400 if (value == -1 && PyErr_Occurred())
401 return NULL;
402 if (value != -1)
403 return raiseTestError("test_long_and_overflow",
404 "return value was not set to -1");
405 if (overflow != 1)
406 return raiseTestError("test_long_and_overflow",
407 "overflow was not set to 1");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 /* Test that overflow is set properly for a large negative value. */
410 /* num is a number smaller than LONG_MIN even on 64-bit platforms */
411 num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
412 if (num == NULL)
413 return NULL;
414 overflow = 1234;
415 value = PyLong_AsLongAndOverflow(num, &overflow);
416 Py_DECREF(num);
417 if (value == -1 && PyErr_Occurred())
418 return NULL;
419 if (value != -1)
420 return raiseTestError("test_long_and_overflow",
421 "return value was not set to -1");
422 if (overflow != -1)
423 return raiseTestError("test_long_and_overflow",
424 "overflow was not set to -1");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 /* Same again, with num = LONG_MIN - 1 */
427 num = PyLong_FromLong(LONG_MIN);
428 if (num == NULL)
429 return NULL;
430 one = PyLong_FromLong(1L);
431 if (one == NULL) {
432 Py_DECREF(num);
433 return NULL;
434 }
435 temp = PyNumber_Subtract(num, one);
436 Py_DECREF(one);
437 Py_DECREF(num);
438 num = temp;
439 if (num == NULL)
440 return NULL;
441 overflow = 0;
442 value = PyLong_AsLongAndOverflow(num, &overflow);
443 Py_DECREF(num);
444 if (value == -1 && PyErr_Occurred())
445 return NULL;
446 if (value != -1)
447 return raiseTestError("test_long_and_overflow",
448 "return value was not set to -1");
449 if (overflow != -1)
450 return raiseTestError("test_long_and_overflow",
451 "overflow was not set to -1");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 /* Test that overflow is cleared properly for small values. */
454 num = PyLong_FromString("FF", NULL, 16);
455 if (num == NULL)
456 return NULL;
457 overflow = 1234;
458 value = PyLong_AsLongAndOverflow(num, &overflow);
459 Py_DECREF(num);
460 if (value == -1 && PyErr_Occurred())
461 return NULL;
462 if (value != 0xFF)
463 return raiseTestError("test_long_and_overflow",
464 "expected return value 0xFF");
465 if (overflow != 0)
466 return raiseTestError("test_long_and_overflow",
467 "overflow was not cleared");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 num = PyLong_FromString("-FF", NULL, 16);
470 if (num == NULL)
471 return NULL;
472 overflow = 0;
473 value = PyLong_AsLongAndOverflow(num, &overflow);
474 Py_DECREF(num);
475 if (value == -1 && PyErr_Occurred())
476 return NULL;
477 if (value != -0xFF)
478 return raiseTestError("test_long_and_overflow",
479 "expected return value 0xFF");
480 if (overflow != 0)
481 return raiseTestError("test_long_and_overflow",
482 "overflow was set incorrectly");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 num = PyLong_FromLong(LONG_MAX);
485 if (num == NULL)
486 return NULL;
487 overflow = 1234;
488 value = PyLong_AsLongAndOverflow(num, &overflow);
489 Py_DECREF(num);
490 if (value == -1 && PyErr_Occurred())
491 return NULL;
492 if (value != LONG_MAX)
493 return raiseTestError("test_long_and_overflow",
494 "expected return value LONG_MAX");
495 if (overflow != 0)
496 return raiseTestError("test_long_and_overflow",
497 "overflow was not cleared");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 num = PyLong_FromLong(LONG_MIN);
500 if (num == NULL)
501 return NULL;
502 overflow = 0;
503 value = PyLong_AsLongAndOverflow(num, &overflow);
504 Py_DECREF(num);
505 if (value == -1 && PyErr_Occurred())
506 return NULL;
507 if (value != LONG_MIN)
508 return raiseTestError("test_long_and_overflow",
509 "expected return value LONG_MIN");
510 if (overflow != 0)
511 return raiseTestError("test_long_and_overflow",
512 "overflow was not cleared");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 Py_INCREF(Py_None);
515 return Py_None;
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000516}
517
Mark Dickinson93f562c2010-01-30 10:30:15 +0000518/* Test the PyLong_AsLongLongAndOverflow API. General conversion to
519 PY_LONG_LONG is tested by test_long_api_inner. This test will
520 concentrate on proper handling of overflow.
521*/
522
523static PyObject *
524test_long_long_and_overflow(PyObject *self)
525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 PyObject *num, *one, *temp;
527 PY_LONG_LONG value;
528 int overflow;
Mark Dickinson93f562c2010-01-30 10:30:15 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 /* Test that overflow is set properly for a large value. */
531 /* num is a number larger than PY_LLONG_MAX on a typical machine. */
532 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
533 if (num == NULL)
534 return NULL;
535 overflow = 1234;
536 value = PyLong_AsLongLongAndOverflow(num, &overflow);
537 Py_DECREF(num);
538 if (value == -1 && PyErr_Occurred())
539 return NULL;
540 if (value != -1)
541 return raiseTestError("test_long_long_and_overflow",
542 "return value was not set to -1");
543 if (overflow != 1)
544 return raiseTestError("test_long_long_and_overflow",
545 "overflow was not set to 1");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 /* Same again, with num = PY_LLONG_MAX + 1 */
548 num = PyLong_FromLongLong(PY_LLONG_MAX);
549 if (num == NULL)
550 return NULL;
551 one = PyLong_FromLong(1L);
552 if (one == NULL) {
553 Py_DECREF(num);
554 return NULL;
555 }
556 temp = PyNumber_Add(num, one);
557 Py_DECREF(one);
558 Py_DECREF(num);
559 num = temp;
560 if (num == NULL)
561 return NULL;
562 overflow = 0;
563 value = PyLong_AsLongLongAndOverflow(num, &overflow);
564 Py_DECREF(num);
565 if (value == -1 && PyErr_Occurred())
566 return NULL;
567 if (value != -1)
568 return raiseTestError("test_long_long_and_overflow",
569 "return value was not set to -1");
570 if (overflow != 1)
571 return raiseTestError("test_long_long_and_overflow",
572 "overflow was not set to 1");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 /* Test that overflow is set properly for a large negative value. */
575 /* num is a number smaller than PY_LLONG_MIN on a typical platform */
576 num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
577 if (num == NULL)
578 return NULL;
579 overflow = 1234;
580 value = PyLong_AsLongLongAndOverflow(num, &overflow);
581 Py_DECREF(num);
582 if (value == -1 && PyErr_Occurred())
583 return NULL;
584 if (value != -1)
585 return raiseTestError("test_long_long_and_overflow",
586 "return value was not set to -1");
587 if (overflow != -1)
588 return raiseTestError("test_long_long_and_overflow",
589 "overflow was not set to -1");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 /* Same again, with num = PY_LLONG_MIN - 1 */
592 num = PyLong_FromLongLong(PY_LLONG_MIN);
593 if (num == NULL)
594 return NULL;
595 one = PyLong_FromLong(1L);
596 if (one == NULL) {
597 Py_DECREF(num);
598 return NULL;
599 }
600 temp = PyNumber_Subtract(num, one);
601 Py_DECREF(one);
602 Py_DECREF(num);
603 num = temp;
604 if (num == NULL)
605 return NULL;
606 overflow = 0;
607 value = PyLong_AsLongLongAndOverflow(num, &overflow);
608 Py_DECREF(num);
609 if (value == -1 && PyErr_Occurred())
610 return NULL;
611 if (value != -1)
612 return raiseTestError("test_long_long_and_overflow",
613 "return value was not set to -1");
614 if (overflow != -1)
615 return raiseTestError("test_long_long_and_overflow",
616 "overflow was not set to -1");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 /* Test that overflow is cleared properly for small values. */
619 num = PyLong_FromString("FF", NULL, 16);
620 if (num == NULL)
621 return NULL;
622 overflow = 1234;
623 value = PyLong_AsLongLongAndOverflow(num, &overflow);
624 Py_DECREF(num);
625 if (value == -1 && PyErr_Occurred())
626 return NULL;
627 if (value != 0xFF)
628 return raiseTestError("test_long_long_and_overflow",
629 "expected return value 0xFF");
630 if (overflow != 0)
631 return raiseTestError("test_long_long_and_overflow",
632 "overflow was not cleared");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 num = PyLong_FromString("-FF", NULL, 16);
635 if (num == NULL)
636 return NULL;
637 overflow = 0;
638 value = PyLong_AsLongLongAndOverflow(num, &overflow);
639 Py_DECREF(num);
640 if (value == -1 && PyErr_Occurred())
641 return NULL;
642 if (value != -0xFF)
643 return raiseTestError("test_long_long_and_overflow",
644 "expected return value 0xFF");
645 if (overflow != 0)
646 return raiseTestError("test_long_long_and_overflow",
647 "overflow was set incorrectly");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 num = PyLong_FromLongLong(PY_LLONG_MAX);
650 if (num == NULL)
651 return NULL;
652 overflow = 1234;
653 value = PyLong_AsLongLongAndOverflow(num, &overflow);
654 Py_DECREF(num);
655 if (value == -1 && PyErr_Occurred())
656 return NULL;
657 if (value != PY_LLONG_MAX)
658 return raiseTestError("test_long_long_and_overflow",
659 "expected return value PY_LLONG_MAX");
660 if (overflow != 0)
661 return raiseTestError("test_long_long_and_overflow",
662 "overflow was not cleared");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 num = PyLong_FromLongLong(PY_LLONG_MIN);
665 if (num == NULL)
666 return NULL;
667 overflow = 0;
668 value = PyLong_AsLongLongAndOverflow(num, &overflow);
669 Py_DECREF(num);
670 if (value == -1 && PyErr_Occurred())
671 return NULL;
672 if (value != PY_LLONG_MIN)
673 return raiseTestError("test_long_long_and_overflow",
674 "expected return value PY_LLONG_MIN");
675 if (overflow != 0)
676 return raiseTestError("test_long_long_and_overflow",
677 "overflow was not cleared");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 Py_INCREF(Py_None);
680 return Py_None;
Mark Dickinson93f562c2010-01-30 10:30:15 +0000681}
682
Nadeem Vawda3d5881e2011-09-07 21:40:26 +0200683/* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that
684 non-integer arguments are handled correctly. It should be extended to
685 test overflow handling.
686 */
687
688static PyObject *
689test_long_as_size_t(PyObject *self)
690{
691 size_t out_u;
692 Py_ssize_t out_s;
693
694 Py_INCREF(Py_None);
695
696 out_u = PyLong_AsSize_t(Py_None);
697 if (out_u != (size_t)-1 || !PyErr_Occurred())
698 return raiseTestError("test_long_as_size_t",
699 "PyLong_AsSize_t(None) didn't complain");
700 if (!PyErr_ExceptionMatches(PyExc_TypeError))
701 return raiseTestError("test_long_as_size_t",
702 "PyLong_AsSize_t(None) raised "
703 "something other than TypeError");
704 PyErr_Clear();
705
706 out_s = PyLong_AsSsize_t(Py_None);
707 if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred())
708 return raiseTestError("test_long_as_size_t",
709 "PyLong_AsSsize_t(None) didn't complain");
710 if (!PyErr_ExceptionMatches(PyExc_TypeError))
711 return raiseTestError("test_long_as_size_t",
712 "PyLong_AsSsize_t(None) raised "
713 "something other than TypeError");
714 PyErr_Clear();
715
716 /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
717 return Py_None;
718}
719
720/* Test the PyLong_AsDouble API. At present this just tests that
721 non-integer arguments are handled correctly.
722 */
723
724static PyObject *
725test_long_as_double(PyObject *self)
726{
727 double out;
728
729 Py_INCREF(Py_None);
730
731 out = PyLong_AsDouble(Py_None);
732 if (out != -1.0 || !PyErr_Occurred())
733 return raiseTestError("test_long_as_double",
734 "PyLong_AsDouble(None) didn't complain");
735 if (!PyErr_ExceptionMatches(PyExc_TypeError))
736 return raiseTestError("test_long_as_double",
737 "PyLong_AsDouble(None) raised "
738 "something other than TypeError");
739 PyErr_Clear();
740
741 /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
742 return Py_None;
743}
744
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000745/* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
Tim Petersd38b1c72001-09-30 05:09:37 +0000746 for both long and int arguments. The test may leak a little memory if
747 it fails.
748*/
749static PyObject *
Fred Drakeacee69f2002-04-01 14:28:58 +0000750test_L_code(PyObject *self)
Tim Petersd38b1c72001-09-30 05:09:37 +0000751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 PyObject *tuple, *num;
753 PY_LONG_LONG value;
Tim Petersd38b1c72001-09-30 05:09:37 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 tuple = PyTuple_New(1);
756 if (tuple == NULL)
757 return NULL;
Tim Petersd38b1c72001-09-30 05:09:37 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 num = PyLong_FromLong(42);
760 if (num == NULL)
761 return NULL;
Tim Petersd38b1c72001-09-30 05:09:37 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 PyTuple_SET_ITEM(tuple, 0, num);
Tim Petersd38b1c72001-09-30 05:09:37 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 value = -1;
766 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
767 return NULL;
768 if (value != 42)
769 return raiseTestError("test_L_code",
770 "L code returned wrong value for long 42");
Tim Petersd38b1c72001-09-30 05:09:37 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 Py_DECREF(num);
773 num = PyLong_FromLong(42);
774 if (num == NULL)
775 return NULL;
Tim Petersd38b1c72001-09-30 05:09:37 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 PyTuple_SET_ITEM(tuple, 0, num);
Tim Petersd38b1c72001-09-30 05:09:37 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 value = -1;
780 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
781 return NULL;
782 if (value != 42)
783 return raiseTestError("test_L_code",
784 "L code returned wrong value for int 42");
Tim Petersd38b1c72001-09-30 05:09:37 +0000785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 Py_DECREF(tuple);
787 Py_INCREF(Py_None);
788 return Py_None;
Tim Petersd38b1c72001-09-30 05:09:37 +0000789}
790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791#endif /* ifdef HAVE_LONG_LONG */
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000792
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000793/* Test tuple argument processing */
794static PyObject *
795getargs_tuple(PyObject *self, PyObject *args)
796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 int a, b, c;
798 if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
799 return NULL;
800 return Py_BuildValue("iii", a, b, c);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000801}
802
Christian Heimes380f7f22008-02-28 11:19:05 +0000803/* test PyArg_ParseTupleAndKeywords */
Larry Hastings83a9f482012-03-20 20:06:16 +0000804static PyObject *
805getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
Christian Heimes380f7f22008-02-28 11:19:05 +0000806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
808 static char *fmt="(ii)i|(i(ii))(iii)i";
809 int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
Christian Heimes380f7f22008-02-28 11:19:05 +0000810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
812 &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
813 &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
814 return NULL;
815 return Py_BuildValue("iiiiiiiiii",
816 int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
817 int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
Christian Heimes380f7f22008-02-28 11:19:05 +0000818}
819
Larry Hastings83a9f482012-03-20 20:06:16 +0000820/* test PyArg_ParseTupleAndKeywords keyword-only arguments */
821static PyObject *
822getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs)
823{
824 static char *keywords[] = {"required", "optional", "keyword_only", NULL};
825 int required = -1;
826 int optional = -1;
827 int keyword_only = -1;
828
829 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords,
830 &required, &optional, &keyword_only))
831 return NULL;
832 return Py_BuildValue("iii", required, optional, keyword_only);
833}
834
Thomas Heller3457e4b2003-04-24 16:14:27 +0000835/* Functions to call PyArg_ParseTuple with integer format codes,
836 and return the result.
837*/
Thomas Hellera4ea6032003-04-17 18:55:45 +0000838static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000839getargs_b(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 unsigned char value;
842 if (!PyArg_ParseTuple(args, "b", &value))
843 return NULL;
844 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000845}
846
Thomas Heller3457e4b2003-04-24 16:14:27 +0000847static PyObject *
848getargs_B(PyObject *self, PyObject *args)
849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 unsigned char value;
851 if (!PyArg_ParseTuple(args, "B", &value))
852 return NULL;
853 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +0000854}
855
856static PyObject *
Mark Dickinson1554b182009-12-20 16:03:30 +0000857getargs_h(PyObject *self, PyObject *args)
858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 short value;
860 if (!PyArg_ParseTuple(args, "h", &value))
861 return NULL;
862 return PyLong_FromLong((long)value);
Mark Dickinson1554b182009-12-20 16:03:30 +0000863}
864
865static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000866getargs_H(PyObject *self, PyObject *args)
867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 unsigned short value;
869 if (!PyArg_ParseTuple(args, "H", &value))
870 return NULL;
871 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +0000872}
873
874static PyObject *
875getargs_I(PyObject *self, PyObject *args)
876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 unsigned int value;
878 if (!PyArg_ParseTuple(args, "I", &value))
879 return NULL;
880 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +0000881}
882
883static PyObject *
884getargs_k(PyObject *self, PyObject *args)
885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 unsigned long value;
887 if (!PyArg_ParseTuple(args, "k", &value))
888 return NULL;
889 return PyLong_FromUnsignedLong(value);
Thomas Heller3457e4b2003-04-24 16:14:27 +0000890}
891
892static PyObject *
893getargs_i(PyObject *self, PyObject *args)
894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 int value;
896 if (!PyArg_ParseTuple(args, "i", &value))
897 return NULL;
898 return PyLong_FromLong((long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +0000899}
900
Thomas Hellera4ea6032003-04-17 18:55:45 +0000901static PyObject *
902getargs_l(PyObject *self, PyObject *args)
903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 long value;
905 if (!PyArg_ParseTuple(args, "l", &value))
906 return NULL;
907 return PyLong_FromLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000908}
909
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000910static PyObject *
911getargs_n(PyObject *self, PyObject *args)
912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 Py_ssize_t value;
914 if (!PyArg_ParseTuple(args, "n", &value))
915 return NULL;
916 return PyLong_FromSsize_t(value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000917}
918
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700919static PyObject *
920getargs_p(PyObject *self, PyObject *args)
921{
922 int value;
923 if (!PyArg_ParseTuple(args, "p", &value))
924 return NULL;
925 return PyLong_FromLong(value);
926}
927
Thomas Hellera4ea6032003-04-17 18:55:45 +0000928#ifdef HAVE_LONG_LONG
Thomas Hellera4ea6032003-04-17 18:55:45 +0000929static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000930getargs_L(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 PY_LONG_LONG value;
933 if (!PyArg_ParseTuple(args, "L", &value))
934 return NULL;
935 return PyLong_FromLongLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000936}
937
Thomas Hellera4ea6032003-04-17 18:55:45 +0000938static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000939getargs_K(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 unsigned PY_LONG_LONG value;
942 if (!PyArg_ParseTuple(args, "K", &value))
943 return NULL;
944 return PyLong_FromUnsignedLongLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000945}
946#endif
947
948/* This function not only tests the 'k' getargs code, but also the
Christian Heimes217cfd12007-12-02 14:31:20 +0000949 PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */
Thomas Hellera4ea6032003-04-17 18:55:45 +0000950static PyObject *
951test_k_code(PyObject *self)
952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 PyObject *tuple, *num;
954 unsigned long value;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 tuple = PyTuple_New(1);
957 if (tuple == NULL)
958 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 /* a number larger than ULONG_MAX even on 64-bit platforms */
961 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
962 if (num == NULL)
963 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 value = PyLong_AsUnsignedLongMask(num);
966 if (value != ULONG_MAX)
967 return raiseTestError("test_k_code",
968 "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
Thomas Hellera4ea6032003-04-17 18:55:45 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 PyTuple_SET_ITEM(tuple, 0, num);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 value = 0;
973 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
974 return NULL;
975 if (value != ULONG_MAX)
976 return raiseTestError("test_k_code",
977 "k code returned wrong value for long 0xFFF...FFF");
Thomas Hellera4ea6032003-04-17 18:55:45 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 Py_DECREF(num);
980 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
981 if (num == NULL)
982 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 value = PyLong_AsUnsignedLongMask(num);
985 if (value != (unsigned long)-0x42)
986 return raiseTestError("test_k_code",
987 "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
Thomas Hellera4ea6032003-04-17 18:55:45 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 PyTuple_SET_ITEM(tuple, 0, num);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 value = 0;
992 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
993 return NULL;
994 if (value != (unsigned long)-0x42)
995 return raiseTestError("test_k_code",
996 "k code returned wrong value for long -0xFFF..000042");
Thomas Hellera4ea6032003-04-17 18:55:45 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 Py_DECREF(tuple);
999 Py_INCREF(Py_None);
1000 return Py_None;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001001}
1002
Victor Stinner06e49dd2010-06-13 18:21:50 +00001003static PyObject *
Eli Bendersky906b88f2011-07-29 07:05:08 +03001004getargs_c(PyObject *self, PyObject *args)
1005{
1006 char c;
1007 if (!PyArg_ParseTuple(args, "c", &c))
1008 return NULL;
1009 return PyBytes_FromStringAndSize(&c, 1);
1010}
1011
1012static PyObject *
Victor Stinner06e49dd2010-06-13 18:21:50 +00001013getargs_s(PyObject *self, PyObject *args)
1014{
1015 char *str;
1016 if (!PyArg_ParseTuple(args, "s", &str))
1017 return NULL;
1018 return PyBytes_FromString(str);
1019}
1020
1021static PyObject *
1022getargs_s_star(PyObject *self, PyObject *args)
1023{
1024 Py_buffer buffer;
1025 PyObject *bytes;
1026 if (!PyArg_ParseTuple(args, "s*", &buffer))
1027 return NULL;
1028 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1029 PyBuffer_Release(&buffer);
1030 return bytes;
1031}
1032
1033static PyObject *
1034getargs_s_hash(PyObject *self, PyObject *args)
1035{
1036 char *str;
1037 Py_ssize_t size;
1038 if (!PyArg_ParseTuple(args, "s#", &str, &size))
1039 return NULL;
1040 return PyBytes_FromStringAndSize(str, size);
1041}
1042
1043static PyObject *
1044getargs_z(PyObject *self, PyObject *args)
1045{
1046 char *str;
1047 if (!PyArg_ParseTuple(args, "z", &str))
1048 return NULL;
1049 if (str != NULL)
1050 return PyBytes_FromString(str);
1051 else
1052 Py_RETURN_NONE;
1053}
1054
1055static PyObject *
1056getargs_z_star(PyObject *self, PyObject *args)
1057{
1058 Py_buffer buffer;
1059 PyObject *bytes;
1060 if (!PyArg_ParseTuple(args, "z*", &buffer))
1061 return NULL;
1062 if (buffer.buf != NULL)
1063 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1064 else {
1065 Py_INCREF(Py_None);
1066 bytes = Py_None;
1067 }
1068 PyBuffer_Release(&buffer);
1069 return bytes;
1070}
1071
1072static PyObject *
1073getargs_z_hash(PyObject *self, PyObject *args)
1074{
1075 char *str;
1076 Py_ssize_t size;
1077 if (!PyArg_ParseTuple(args, "z#", &str, &size))
1078 return NULL;
1079 if (str != NULL)
1080 return PyBytes_FromStringAndSize(str, size);
1081 else
1082 Py_RETURN_NONE;
1083}
1084
1085static PyObject *
1086getargs_y(PyObject *self, PyObject *args)
1087{
1088 char *str;
1089 if (!PyArg_ParseTuple(args, "y", &str))
1090 return NULL;
1091 return PyBytes_FromString(str);
1092}
1093
1094static PyObject *
1095getargs_y_star(PyObject *self, PyObject *args)
1096{
1097 Py_buffer buffer;
1098 PyObject *bytes;
1099 if (!PyArg_ParseTuple(args, "y*", &buffer))
1100 return NULL;
1101 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1102 PyBuffer_Release(&buffer);
1103 return bytes;
1104}
1105
1106static PyObject *
1107getargs_y_hash(PyObject *self, PyObject *args)
1108{
1109 char *str;
1110 Py_ssize_t size;
1111 if (!PyArg_ParseTuple(args, "y#", &str, &size))
1112 return NULL;
1113 return PyBytes_FromStringAndSize(str, size);
1114}
1115
1116static PyObject *
1117getargs_u(PyObject *self, PyObject *args)
1118{
1119 Py_UNICODE *str;
1120 Py_ssize_t size;
1121 if (!PyArg_ParseTuple(args, "u", &str))
1122 return NULL;
1123 size = Py_UNICODE_strlen(str);
1124 return PyUnicode_FromUnicode(str, size);
1125}
1126
1127static PyObject *
1128getargs_u_hash(PyObject *self, PyObject *args)
1129{
1130 Py_UNICODE *str;
1131 Py_ssize_t size;
1132 if (!PyArg_ParseTuple(args, "u#", &str, &size))
1133 return NULL;
1134 return PyUnicode_FromUnicode(str, size);
1135}
1136
1137static PyObject *
1138getargs_Z(PyObject *self, PyObject *args)
1139{
1140 Py_UNICODE *str;
1141 Py_ssize_t size;
1142 if (!PyArg_ParseTuple(args, "Z", &str))
1143 return NULL;
1144 if (str != NULL) {
1145 size = Py_UNICODE_strlen(str);
1146 return PyUnicode_FromUnicode(str, size);
1147 } else
1148 Py_RETURN_NONE;
1149}
1150
1151static PyObject *
1152getargs_Z_hash(PyObject *self, PyObject *args)
1153{
1154 Py_UNICODE *str;
1155 Py_ssize_t size;
1156 if (!PyArg_ParseTuple(args, "Z#", &str, &size))
1157 return NULL;
1158 if (str != NULL)
1159 return PyUnicode_FromUnicode(str, size);
1160 else
1161 Py_RETURN_NONE;
1162}
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001163
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001164/* Test the s and z codes for PyArg_ParseTuple.
1165*/
1166static PyObject *
1167test_s_code(PyObject *self)
1168{
1169 /* Unicode strings should be accepted */
1170 PyObject *tuple, *obj;
1171 char *value;
1172
1173 tuple = PyTuple_New(1);
1174 if (tuple == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 return NULL;
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001176
1177 obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 "latin-1", NULL);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001179 if (obj == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 return NULL;
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001181
1182 PyTuple_SET_ITEM(tuple, 0, obj);
1183
1184 /* These two blocks used to raise a TypeError:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 * "argument must be string without null bytes, not str"
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001186 */
1187 if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 return NULL;
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001189
1190 if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 return NULL;
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001192
Alexandre Vassalottib645bc72008-05-15 22:06:59 +00001193 Py_DECREF(tuple);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001194 Py_RETURN_NONE;
1195}
1196
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001197static PyObject *
Larry Hastings8f904da2012-06-22 03:56:29 -07001198parse_tuple_and_keywords(PyObject *self, PyObject *args)
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001199{
Larry Hastings8f904da2012-06-22 03:56:29 -07001200 PyObject *sub_args;
1201 PyObject *sub_kwargs;
1202 char *sub_format;
1203 PyObject *sub_keywords;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001204
Larry Hastings8f904da2012-06-22 03:56:29 -07001205 Py_ssize_t i, size;
1206 char *keywords[8 + 1]; /* space for NULL at end */
1207 PyObject *o;
1208 PyObject *converted[8];
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001209
Larry Hastings8f904da2012-06-22 03:56:29 -07001210 int result;
1211 PyObject *return_value = NULL;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001212
Larry Hastings22701e82012-08-08 14:52:22 -07001213 double buffers[8][4]; /* double ensures alignment where necessary */
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001214
Larry Hastings8f904da2012-06-22 03:56:29 -07001215 if (!PyArg_ParseTuple(args, "OOyO:parse_tuple_and_keywords",
1216 &sub_args, &sub_kwargs,
1217 &sub_format, &sub_keywords))
1218 return NULL;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001219
Larry Hastings8f904da2012-06-22 03:56:29 -07001220 if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) {
1221 PyErr_SetString(PyExc_ValueError,
1222 "parse_tuple_and_keywords: sub_keywords must be either list or tuple");
1223 return NULL;
1224 }
1225
1226 memset(buffers, 0, sizeof(buffers));
1227 memset(converted, 0, sizeof(converted));
1228 memset(keywords, 0, sizeof(keywords));
1229
1230 size = PySequence_Fast_GET_SIZE(sub_keywords);
1231 if (size > 8) {
1232 PyErr_SetString(PyExc_ValueError,
1233 "parse_tuple_and_keywords: too many keywords in sub_keywords");
1234 goto exit;
1235 }
1236
1237 for (i = 0; i < size; i++) {
1238 o = PySequence_Fast_GET_ITEM(sub_keywords, i);
1239 if (!PyUnicode_FSConverter(o, (void *)(converted + i))) {
1240 PyErr_Format(PyExc_ValueError,
Jesus Cea6e1d2b62012-10-04 16:06:30 +02001241 "parse_tuple_and_keywords: could not convert keywords[%zd] to narrow string", i);
Larry Hastings8f904da2012-06-22 03:56:29 -07001242 goto exit;
1243 }
1244 keywords[i] = PyBytes_AS_STRING(converted[i]);
1245 }
1246
1247 result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs,
1248 sub_format, keywords,
1249 buffers + 0, buffers + 1, buffers + 2, buffers + 3,
1250 buffers + 4, buffers + 5, buffers + 6, buffers + 7);
1251
1252 if (result) {
1253 return_value = Py_None;
1254 Py_INCREF(Py_None);
1255 }
1256
1257exit:
1258 size = sizeof(converted) / sizeof(converted[0]);
1259 for (i = 0; i < size; i++) {
1260 Py_XDECREF(converted[i]);
1261 }
1262 return return_value;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001263}
1264
Benjamin Peterson92035012008-12-27 16:00:54 +00001265static volatile int x;
1266
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001267/* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
1268 of an error.
1269*/
1270static PyObject *
Fred Drakeacee69f2002-04-01 14:28:58 +00001271test_u_code(PyObject *self)
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 PyObject *tuple, *obj;
1274 Py_UNICODE *value;
1275 Py_ssize_t len;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
1278 /* Just use the macro and check that it compiles */
1279 x = Py_UNICODE_ISSPACE(25);
Benjamin Peterson206e3072008-10-19 14:07:49 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 tuple = PyTuple_New(1);
1282 if (tuple == NULL)
1283 return NULL;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 obj = PyUnicode_Decode("test", strlen("test"),
1286 "ascii", NULL);
1287 if (obj == NULL)
1288 return NULL;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 PyTuple_SET_ITEM(tuple, 0, obj);
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 value = 0;
1293 if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
1294 return NULL;
1295 if (value != PyUnicode_AS_UNICODE(obj))
1296 return raiseTestError("test_u_code",
1297 "u code returned wrong value for u'test'");
1298 value = 0;
1299 if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
1300 return NULL;
1301 if (value != PyUnicode_AS_UNICODE(obj) ||
1302 len != PyUnicode_GET_SIZE(obj))
1303 return raiseTestError("test_u_code",
1304 "u# code returned wrong values for u'test'");
Tim Petersbaefd9e2003-01-28 20:37:45 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 Py_DECREF(tuple);
1307 Py_INCREF(Py_None);
1308 return Py_None;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001309}
1310
Guido van Rossumfb67be22007-08-29 18:38:11 +00001311/* Test Z and Z# codes for PyArg_ParseTuple */
1312static PyObject *
1313test_Z_code(PyObject *self)
1314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 PyObject *tuple, *obj;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001316 const Py_UNICODE *value1, *value2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 Py_ssize_t len1, len2;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 tuple = PyTuple_New(2);
1320 if (tuple == NULL)
1321 return NULL;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 obj = PyUnicode_FromString("test");
1324 PyTuple_SET_ITEM(tuple, 0, obj);
1325 Py_INCREF(Py_None);
1326 PyTuple_SET_ITEM(tuple, 1, Py_None);
Guido van Rossumfb67be22007-08-29 18:38:11 +00001327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 /* swap values on purpose */
1329 value1 = NULL;
1330 value2 = PyUnicode_AS_UNICODE(obj);
Guido van Rossumfb67be22007-08-29 18:38:11 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 /* Test Z for both values */
1333 if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0)
1334 return NULL;
1335 if (value1 != PyUnicode_AS_UNICODE(obj))
1336 return raiseTestError("test_Z_code",
1337 "Z code returned wrong value for 'test'");
1338 if (value2 != NULL)
1339 return raiseTestError("test_Z_code",
1340 "Z code returned wrong value for None");
Guido van Rossumfb67be22007-08-29 18:38:11 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 value1 = NULL;
1343 value2 = PyUnicode_AS_UNICODE(obj);
1344 len1 = -1;
1345 len2 = -1;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 /* Test Z# for both values */
1348 if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1,
1349 &value2, &len2) < 0)
1350 return NULL;
1351 if (value1 != PyUnicode_AS_UNICODE(obj) ||
1352 len1 != PyUnicode_GET_SIZE(obj))
1353 return raiseTestError("test_Z_code",
1354 "Z# code returned wrong values for 'test'");
1355 if (value2 != NULL ||
1356 len2 != 0)
1357 return raiseTestError("test_Z_code",
1358 "Z# code returned wrong values for None'");
Guido van Rossumfb67be22007-08-29 18:38:11 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 Py_DECREF(tuple);
1361 Py_RETURN_NONE;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001362}
1363
Thomas Wouters477c8d52006-05-27 19:21:47 +00001364static PyObject *
Mark Dickinson081dfee2009-03-18 14:47:41 +00001365test_widechar(PyObject *self)
1366{
1367#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
1369 size_t wtextlen = 1;
Victor Stinnere3b47152011-12-09 20:49:49 +01001370 const wchar_t invalid[1] = {(wchar_t)0x110000u};
Mark Dickinson081dfee2009-03-18 14:47:41 +00001371#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
1373 size_t wtextlen = 2;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001374#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 PyObject *wide, *utf8;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 wide = PyUnicode_FromWideChar(wtext, wtextlen);
1378 if (wide == NULL)
1379 return NULL;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d");
1382 if (utf8 == NULL) {
1383 Py_DECREF(wide);
1384 return NULL;
1385 }
Mark Dickinson081dfee2009-03-18 14:47:41 +00001386
Victor Stinner8ef18872011-11-21 02:06:57 +01001387 if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 Py_DECREF(wide);
1389 Py_DECREF(utf8);
1390 return raiseTestError("test_widechar",
1391 "wide string and utf8 string "
1392 "have different length");
1393 }
1394 if (PyUnicode_Compare(wide, utf8)) {
1395 Py_DECREF(wide);
1396 Py_DECREF(utf8);
1397 if (PyErr_Occurred())
1398 return NULL;
1399 return raiseTestError("test_widechar",
1400 "wide string and utf8 string "
1401 "are different");
1402 }
Mark Dickinson081dfee2009-03-18 14:47:41 +00001403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 Py_DECREF(wide);
1405 Py_DECREF(utf8);
Victor Stinnere3b47152011-12-09 20:49:49 +01001406
1407#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
1408 wide = PyUnicode_FromWideChar(invalid, 1);
1409 if (wide == NULL)
1410 PyErr_Clear();
1411 else
1412 return raiseTestError("test_widechar",
1413 "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail");
1414
1415 wide = PyUnicode_FromUnicode(invalid, 1);
1416 if (wide == NULL)
1417 PyErr_Clear();
1418 else
1419 return raiseTestError("test_widechar",
1420 "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail");
Victor Stinnere5c05332013-03-06 00:39:03 +01001421
1422 wide = PyUnicode_FromUnicode(NULL, 1);
1423 if (wide == NULL)
1424 return NULL;
1425 PyUnicode_AS_UNICODE(wide)[0] = invalid[0];
Ezio Melotti03e667d2013-03-07 21:18:45 +02001426 if (_PyUnicode_Ready(wide) < 0) {
1427 Py_DECREF(wide);
Victor Stinnere5c05332013-03-06 00:39:03 +01001428 PyErr_Clear();
Ezio Melotti03e667d2013-03-07 21:18:45 +02001429 }
1430 else {
1431 Py_DECREF(wide);
Victor Stinnere5c05332013-03-06 00:39:03 +01001432 return raiseTestError("test_widechar",
1433 "PyUnicode_Ready() didn't fail");
Ezio Melotti03e667d2013-03-07 21:18:45 +02001434 }
Victor Stinnere3b47152011-12-09 20:49:49 +01001435#endif
1436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 Py_RETURN_NONE;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001438}
1439
1440static PyObject *
Victor Stinner46c7b3b2010-10-02 11:49:31 +00001441unicode_aswidechar(PyObject *self, PyObject *args)
Victor Stinner1c24bd02010-10-02 11:03:13 +00001442{
1443 PyObject *unicode, *result;
1444 Py_ssize_t buflen, size;
1445 wchar_t *buffer;
1446
1447 if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen))
1448 return NULL;
1449 buffer = PyMem_Malloc(buflen * sizeof(wchar_t));
1450 if (buffer == NULL)
1451 return PyErr_NoMemory();
1452
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001453 size = PyUnicode_AsWideChar(unicode, buffer, buflen);
Victor Stinner1c24bd02010-10-02 11:03:13 +00001454 if (size == -1) {
1455 PyMem_Free(buffer);
1456 return NULL;
1457 }
1458
1459 if (size < buflen)
1460 buflen = size + 1;
1461 else
1462 buflen = size;
1463 result = PyUnicode_FromWideChar(buffer, buflen);
1464 PyMem_Free(buffer);
1465 if (result == NULL)
1466 return NULL;
1467
1468 return Py_BuildValue("(Nn)", result, size);
1469}
1470
1471static PyObject *
Victor Stinner46c7b3b2010-10-02 11:49:31 +00001472unicode_aswidecharstring(PyObject *self, PyObject *args)
Victor Stinner1c24bd02010-10-02 11:03:13 +00001473{
1474 PyObject *unicode, *result;
1475 Py_ssize_t size;
1476 wchar_t *buffer;
1477
1478 if (!PyArg_ParseTuple(args, "U", &unicode))
1479 return NULL;
1480
Victor Stinnerbeb4135b2010-10-07 01:02:42 +00001481 buffer = PyUnicode_AsWideCharString(unicode, &size);
Victor Stinner1c24bd02010-10-02 11:03:13 +00001482 if (buffer == NULL)
1483 return NULL;
1484
1485 result = PyUnicode_FromWideChar(buffer, size + 1);
1486 PyMem_Free(buffer);
1487 if (result == NULL)
1488 return NULL;
1489 return Py_BuildValue("(Nn)", result, size);
1490}
1491
1492static PyObject *
Victor Stinner42bf7752011-11-21 22:52:58 +01001493unicode_encodedecimal(PyObject *self, PyObject *args)
1494{
1495 Py_UNICODE *unicode;
1496 Py_ssize_t length;
1497 char *errors = NULL;
1498 PyObject *decimal;
1499 Py_ssize_t decimal_length, new_length;
1500 int res;
1501
1502 if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors))
1503 return NULL;
1504
1505 decimal_length = length * 7; /* len('&#8364;') */
1506 decimal = PyBytes_FromStringAndSize(NULL, decimal_length);
1507 if (decimal == NULL)
1508 return NULL;
1509
1510 res = PyUnicode_EncodeDecimal(unicode, length,
1511 PyBytes_AS_STRING(decimal),
1512 errors);
1513 if (res < 0) {
1514 Py_DECREF(decimal);
1515 return NULL;
1516 }
1517
1518 new_length = strlen(PyBytes_AS_STRING(decimal));
1519 assert(new_length <= decimal_length);
1520 res = _PyBytes_Resize(&decimal, new_length);
1521 if (res < 0)
1522 return NULL;
1523
1524 return decimal;
1525}
1526
1527static PyObject *
1528unicode_transformdecimaltoascii(PyObject *self, PyObject *args)
1529{
1530 Py_UNICODE *unicode;
1531 Py_ssize_t length;
1532 if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length))
1533 return NULL;
1534 return PyUnicode_TransformDecimalToASCII(unicode, length);
1535}
1536
1537static PyObject *
Stefan Krahe6996ed2012-11-02 14:44:20 +01001538unicode_legacy_string(PyObject *self, PyObject *args)
1539{
1540 Py_UNICODE *data;
1541 Py_ssize_t len;
1542 PyObject *u;
1543
1544 if (!PyArg_ParseTuple(args, "u#", &data, &len))
1545 return NULL;
1546
1547 u = PyUnicode_FromUnicode(NULL, len);
1548 if (u == NULL)
1549 return NULL;
1550
1551 memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE));
1552
1553 if (len > 0) { /* The empty string is always ready. */
1554 assert(!PyUnicode_IS_READY(u));
1555 }
1556
1557 return u;
1558}
1559
1560static PyObject *
Victor Stinner25e8ec42010-06-25 00:02:38 +00001561getargs_w_star(PyObject *self, PyObject *args)
1562{
1563 Py_buffer buffer;
1564 PyObject *result;
1565 char *str;
1566
1567 if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer))
1568 return NULL;
1569
1570 if (2 <= buffer.len) {
1571 str = buffer.buf;
1572 str[0] = '[';
1573 str[buffer.len-1] = ']';
1574 }
1575
1576 result = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1577 PyBuffer_Release(&buffer);
1578 return result;
1579}
1580
1581
1582static PyObject *
Benjamin Peterson92035012008-12-27 16:00:54 +00001583test_empty_argparse(PyObject *self)
1584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 /* Test that formats can begin with '|'. See issue #4720. */
1586 PyObject *tuple, *dict = NULL;
1587 static char *kwlist[] = {NULL};
1588 int result;
1589 tuple = PyTuple_New(0);
1590 if (!tuple)
1591 return NULL;
1592 if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0)
1593 goto done;
1594 dict = PyDict_New();
1595 if (!dict)
1596 goto done;
1597 result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
Benjamin Peterson92035012008-12-27 16:00:54 +00001598 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 Py_DECREF(tuple);
1600 Py_XDECREF(dict);
1601 if (result < 0)
1602 return NULL;
1603 else {
1604 Py_RETURN_NONE;
1605 }
Benjamin Peterson92035012008-12-27 16:00:54 +00001606}
1607
1608static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001609codec_incrementalencoder(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 const char *encoding, *errors = NULL;
1612 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
1613 &encoding, &errors))
1614 return NULL;
1615 return PyCodec_IncrementalEncoder(encoding, errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001616}
1617
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618static PyObject *
1619codec_incrementaldecoder(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 const char *encoding, *errors = NULL;
1622 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
1623 &encoding, &errors))
1624 return NULL;
1625 return PyCodec_IncrementalDecoder(encoding, errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001626}
1627
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001628
Tim Peters5b8132f2003-01-31 15:52:05 +00001629/* Simple test of _PyLong_NumBits and _PyLong_Sign. */
Tim Petersbaefd9e2003-01-28 20:37:45 +00001630static PyObject *
1631test_long_numbits(PyObject *self)
1632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 struct triple {
1634 long input;
1635 size_t nbits;
1636 int sign;
1637 } testcases[] = {{0, 0, 0},
1638 {1L, 1, 1},
1639 {-1L, 1, -1},
1640 {2L, 2, 1},
1641 {-2L, 2, -1},
1642 {3L, 2, 1},
1643 {-3L, 2, -1},
1644 {4L, 3, 1},
1645 {-4L, 3, -1},
1646 {0x7fffL, 15, 1}, /* one Python long digit */
1647 {-0x7fffL, 15, -1},
1648 {0xffffL, 16, 1},
1649 {-0xffffL, 16, -1},
1650 {0xfffffffL, 28, 1},
1651 {-0xfffffffL, 28, -1}};
1652 int i;
Tim Petersbaefd9e2003-01-28 20:37:45 +00001653
Victor Stinner63941882011-09-29 00:42:28 +02001654 for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 PyObject *plong = PyLong_FromLong(testcases[i].input);
1656 size_t nbits = _PyLong_NumBits(plong);
1657 int sign = _PyLong_Sign(plong);
Tim Petersbaefd9e2003-01-28 20:37:45 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 Py_DECREF(plong);
1660 if (nbits != testcases[i].nbits)
1661 return raiseTestError("test_long_numbits",
1662 "wrong result for _PyLong_NumBits");
1663 if (sign != testcases[i].sign)
1664 return raiseTestError("test_long_numbits",
1665 "wrong result for _PyLong_Sign");
1666 }
1667 Py_INCREF(Py_None);
1668 return Py_None;
Tim Petersbaefd9e2003-01-28 20:37:45 +00001669}
1670
Thomas Heller519a0422007-11-15 20:48:54 +00001671/* Example passing NULLs to PyObject_Str(NULL). */
Thomas Woutersa9773292006-04-21 09:43:23 +00001672
1673static PyObject *
1674test_null_strings(PyObject *self)
1675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL);
1677 PyObject *tuple = PyTuple_Pack(2, o1, o2);
1678 Py_XDECREF(o1);
1679 Py_XDECREF(o2);
1680 return tuple;
Thomas Woutersa9773292006-04-21 09:43:23 +00001681}
1682
Jeremy Hyltonede049b2001-09-26 20:01:13 +00001683static PyObject *
1684raise_exception(PyObject *self, PyObject *args)
1685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 PyObject *exc;
1687 PyObject *exc_args, *v;
1688 int num_args, i;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00001689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
1691 &exc, &num_args))
1692 return NULL;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00001693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 exc_args = PyTuple_New(num_args);
1695 if (exc_args == NULL)
1696 return NULL;
1697 for (i = 0; i < num_args; ++i) {
1698 v = PyLong_FromLong(i);
1699 if (v == NULL) {
1700 Py_DECREF(exc_args);
1701 return NULL;
1702 }
1703 PyTuple_SET_ITEM(exc_args, i, v);
1704 }
1705 PyErr_SetObject(exc, exc_args);
1706 Py_DECREF(exc_args);
1707 return NULL;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00001708}
Tim Peters91621db2001-06-12 20:10:01 +00001709
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +02001710static PyObject *
1711test_set_exc_info(PyObject *self, PyObject *args)
1712{
1713 PyObject *orig_exc;
1714 PyObject *new_type, *new_value, *new_tb;
1715 PyObject *type, *value, *tb;
1716 if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info",
1717 &new_type, &new_value, &new_tb))
1718 return NULL;
1719
1720 PyErr_GetExcInfo(&type, &value, &tb);
1721
1722 Py_INCREF(new_type);
1723 Py_INCREF(new_value);
1724 Py_INCREF(new_tb);
1725 PyErr_SetExcInfo(new_type, new_value, new_tb);
1726
1727 orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None);
1728 Py_XDECREF(type);
1729 Py_XDECREF(value);
1730 Py_XDECREF(tb);
1731 return orig_exc;
1732}
Benjamin Peterson16323982010-02-03 01:13:41 +00001733
1734static int test_run_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001735
Benjamin Peterson91d58bd2009-12-13 21:30:54 +00001736static PyObject *
1737test_datetime_capi(PyObject *self, PyObject *args) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 if (PyDateTimeAPI) {
1739 if (test_run_counter) {
1740 /* Probably regrtest.py -R */
1741 Py_RETURN_NONE;
1742 }
1743 else {
1744 PyErr_SetString(PyExc_AssertionError,
1745 "PyDateTime_CAPI somehow initialized");
1746 return NULL;
1747 }
1748 }
1749 test_run_counter++;
1750 PyDateTime_IMPORT;
1751 if (PyDateTimeAPI)
1752 Py_RETURN_NONE;
1753 else
1754 return NULL;
Benjamin Peterson91d58bd2009-12-13 21:30:54 +00001755}
1756
Benjamin Peterson16323982010-02-03 01:13:41 +00001757
1758#ifdef WITH_THREAD
1759
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001760/* test_thread_state spawns a thread of its own, and that thread releases
1761 * `thread_done` when it's finished. The driver code has to know when the
1762 * thread finishes, because the thread uses a PyObject (the callable) that
1763 * may go away when the driver finishes. The former lack of this explicit
1764 * synchronization caused rare segfaults, so rare that they were seen only
1765 * on a Mac buildbot (although they were possible on any box).
1766 */
1767static PyThread_type_lock thread_done = NULL;
1768
Benjamin Petersona786b022008-08-25 21:05:21 +00001769static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001770_make_call(void *callable)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 PyObject *rc;
1773 int success;
1774 PyGILState_STATE s = PyGILState_Ensure();
1775 rc = PyObject_CallFunction((PyObject *)callable, "");
1776 success = (rc != NULL);
1777 Py_XDECREF(rc);
1778 PyGILState_Release(s);
1779 return success;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001780}
1781
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001782/* Same thing, but releases `thread_done` when it returns. This variant
1783 * should be called only from threads spawned by test_thread_state().
1784 */
1785static void
1786_make_call_from_thread(void *callable)
1787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 _make_call(callable);
1789 PyThread_release_lock(thread_done);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001790}
1791
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001792static PyObject *
1793test_thread_state(PyObject *self, PyObject *args)
1794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 PyObject *fn;
1796 int success = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
1799 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 if (!PyCallable_Check(fn)) {
1802 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
1803 fn->ob_type->tp_name);
1804 return NULL;
1805 }
Benjamin Petersona786b022008-08-25 21:05:21 +00001806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 /* Ensure Python is set up for threading */
1808 PyEval_InitThreads();
1809 thread_done = PyThread_allocate_lock();
1810 if (thread_done == NULL)
1811 return PyErr_NoMemory();
1812 PyThread_acquire_lock(thread_done, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 /* Start a new thread with our callback. */
1815 PyThread_start_new_thread(_make_call_from_thread, fn);
1816 /* Make the callback with the thread lock held by this thread */
1817 success &= _make_call(fn);
1818 /* Do it all again, but this time with the thread-lock released */
1819 Py_BEGIN_ALLOW_THREADS
1820 success &= _make_call(fn);
1821 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
1822 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 /* And once more with and without a thread
1825 XXX - should use a lock and work out exactly what we are trying
1826 to test <wink>
1827 */
1828 Py_BEGIN_ALLOW_THREADS
1829 PyThread_start_new_thread(_make_call_from_thread, fn);
1830 success &= _make_call(fn);
1831 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
1832 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 /* Release lock we acquired above. This is required on HP-UX. */
1835 PyThread_release_lock(thread_done);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 PyThread_free_lock(thread_done);
1838 if (!success)
1839 return NULL;
1840 Py_RETURN_NONE;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001841}
Benjamin Petersona54c9092009-01-13 02:11:23 +00001842
1843/* test Py_AddPendingCalls using threads */
1844static int _pending_callback(void *arg)
1845{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 /* we assume the argument is callable object to which we own a reference */
1847 PyObject *callable = (PyObject *)arg;
1848 PyObject *r = PyObject_CallObject(callable, NULL);
1849 Py_DECREF(callable);
1850 Py_XDECREF(r);
1851 return r != NULL ? 0 : -1;
Benjamin Petersona54c9092009-01-13 02:11:23 +00001852}
1853
1854/* The following requests n callbacks to _pending_callback. It can be
1855 * run from any python thread.
1856 */
1857PyObject *pending_threadfunc(PyObject *self, PyObject *arg)
1858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 PyObject *callable;
1860 int r;
1861 if (PyArg_ParseTuple(arg, "O", &callable) == 0)
1862 return NULL;
Benjamin Petersona54c9092009-01-13 02:11:23 +00001863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 /* create the reference for the callbackwhile we hold the lock */
1865 Py_INCREF(callable);
Benjamin Petersona54c9092009-01-13 02:11:23 +00001866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 Py_BEGIN_ALLOW_THREADS
1868 r = Py_AddPendingCall(&_pending_callback, callable);
1869 Py_END_ALLOW_THREADS
Benjamin Petersona54c9092009-01-13 02:11:23 +00001870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 if (r<0) {
1872 Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
1873 Py_INCREF(Py_False);
1874 return Py_False;
1875 }
1876 Py_INCREF(Py_True);
1877 return Py_True;
Benjamin Petersona54c9092009-01-13 02:11:23 +00001878}
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001879#endif
1880
Neal Norwitzb0d26332007-08-25 00:49:05 +00001881/* Some tests of PyUnicode_FromFormat(). This needs more tests. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001882static PyObject *
1883test_string_from_format(PyObject *self, PyObject *args)
1884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 PyObject *result;
1886 char *msg;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001887
Alexander Belopolskye239d232010-12-08 23:31:48 +00001888#define CHECK_1_FORMAT(FORMAT, TYPE) \
1889 result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \
1890 if (result == NULL) \
1891 return NULL; \
Victor Stinner2b979bf2011-11-20 19:32:09 +01001892 if (PyUnicode_CompareWithASCIIString(result, "1")) { \
Alexander Belopolskye239d232010-12-08 23:31:48 +00001893 msg = FORMAT " failed at 1"; \
1894 goto Fail; \
1895 } \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 Py_DECREF(result)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 CHECK_1_FORMAT("%d", int);
1899 CHECK_1_FORMAT("%ld", long);
1900 /* The z width modifier was added in Python 2.5. */
1901 CHECK_1_FORMAT("%zd", Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 /* The u type code was added in Python 2.5. */
1904 CHECK_1_FORMAT("%u", unsigned int);
1905 CHECK_1_FORMAT("%lu", unsigned long);
1906 CHECK_1_FORMAT("%zu", size_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 /* "%lld" and "%llu" support added in Python 2.7. */
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00001909#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG);
1911 CHECK_1_FORMAT("%lld", PY_LONG_LONG);
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00001912#endif
1913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001915
1916 Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 Py_XDECREF(result);
1918 return raiseTestError("test_string_from_format", msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001919
1920#undef CHECK_1_FORMAT
1921}
1922
Benjamin Peterson8667a9b2010-01-09 21:45:28 +00001923
1924static PyObject *
1925test_unicode_compare_with_ascii(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4);
1927 int result;
1928 if (py_s == NULL)
1929 return NULL;
1930 result = PyUnicode_CompareWithASCIIString(py_s, "str");
1931 Py_DECREF(py_s);
1932 if (!result) {
1933 PyErr_SetString(TestError, "Python string ending in NULL "
1934 "should not compare equal to c string.");
1935 return NULL;
1936 }
1937 Py_RETURN_NONE;
Victor Stinner3e2b7172010-11-09 09:32:19 +00001938}
Benjamin Peterson8667a9b2010-01-09 21:45:28 +00001939
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001940/* This is here to provide a docstring for test_descr. */
1941static PyObject *
1942test_with_docstring(PyObject *self)
1943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001945}
1946
Mark Dickinson725bfd82009-05-03 20:33:40 +00001947/* Test PyOS_string_to_double. */
1948static PyObject *
1949test_string_to_double(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 double result;
1951 char *msg;
Mark Dickinson725bfd82009-05-03 20:33:40 +00001952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953#define CHECK_STRING(STR, expected) \
1954 result = PyOS_string_to_double(STR, NULL, NULL); \
1955 if (result == -1.0 && PyErr_Occurred()) \
1956 return NULL; \
1957 if (result != expected) { \
1958 msg = "conversion of " STR " to float failed"; \
1959 goto fail; \
1960 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00001961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962#define CHECK_INVALID(STR) \
1963 result = PyOS_string_to_double(STR, NULL, NULL); \
1964 if (result == -1.0 && PyErr_Occurred()) { \
1965 if (PyErr_ExceptionMatches(PyExc_ValueError)) \
1966 PyErr_Clear(); \
1967 else \
1968 return NULL; \
1969 } \
1970 else { \
1971 msg = "conversion of " STR " didn't raise ValueError"; \
1972 goto fail; \
1973 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 CHECK_STRING("0.1", 0.1);
1976 CHECK_STRING("1.234", 1.234);
1977 CHECK_STRING("-1.35", -1.35);
1978 CHECK_STRING(".1e01", 1.0);
1979 CHECK_STRING("2.e-2", 0.02);
Mark Dickinson725bfd82009-05-03 20:33:40 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 CHECK_INVALID(" 0.1");
1982 CHECK_INVALID("\t\n-3");
1983 CHECK_INVALID(".123 ");
1984 CHECK_INVALID("3\n");
1985 CHECK_INVALID("123abc");
Mark Dickinson725bfd82009-05-03 20:33:40 +00001986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 Py_RETURN_NONE;
Mark Dickinson725bfd82009-05-03 20:33:40 +00001988 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 return raiseTestError("test_string_to_double", msg);
Mark Dickinson725bfd82009-05-03 20:33:40 +00001990#undef CHECK_STRING
1991#undef CHECK_INVALID
1992}
1993
1994
Benjamin Petersonb173f782009-05-05 22:31:58 +00001995/* Coverage testing of capsule objects. */
1996
1997static const char *capsule_name = "capsule name";
1998static char *capsule_pointer = "capsule pointer";
1999static char *capsule_context = "capsule context";
2000static const char *capsule_error = NULL;
2001static int
2002capsule_destructor_call_count = 0;
2003
2004static void
2005capsule_destructor(PyObject *o) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 capsule_destructor_call_count++;
2007 if (PyCapsule_GetContext(o) != capsule_context) {
2008 capsule_error = "context did not match in destructor!";
2009 } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
2010 capsule_error = "destructor did not match in destructor! (woah!)";
2011 } else if (PyCapsule_GetName(o) != capsule_name) {
2012 capsule_error = "name did not match in destructor!";
2013 } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
2014 capsule_error = "pointer did not match in destructor!";
2015 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00002016}
2017
2018typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 char *name;
2020 char *module;
2021 char *attribute;
Benjamin Petersonb173f782009-05-05 22:31:58 +00002022} known_capsule;
2023
2024static PyObject *
2025test_capsule(PyObject *self, PyObject *args)
2026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 PyObject *object;
2028 const char *error = NULL;
2029 void *pointer;
2030 void *pointer2;
2031 known_capsule known_capsules[] = {
2032 #define KNOWN_CAPSULE(module, name) { module "." name, module, name }
2033 KNOWN_CAPSULE("_socket", "CAPI"),
2034 KNOWN_CAPSULE("_curses", "_C_API"),
2035 KNOWN_CAPSULE("datetime", "datetime_CAPI"),
2036 { NULL, NULL },
2037 };
2038 known_capsule *known = &known_capsules[0];
Benjamin Petersonb173f782009-05-05 22:31:58 +00002039
2040#define FAIL(x) { error = (x); goto exit; }
2041
2042#define CHECK_DESTRUCTOR \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 if (capsule_error) { \
2044 FAIL(capsule_error); \
2045 } \
2046 else if (!capsule_destructor_call_count) { \
2047 FAIL("destructor not called!"); \
2048 } \
2049 capsule_destructor_call_count = 0; \
Benjamin Petersonb173f782009-05-05 22:31:58 +00002050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
2052 PyCapsule_SetContext(object, capsule_context);
2053 capsule_destructor(object);
2054 CHECK_DESTRUCTOR;
2055 Py_DECREF(object);
2056 CHECK_DESTRUCTOR;
Benjamin Petersonb173f782009-05-05 22:31:58 +00002057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 object = PyCapsule_New(known, "ignored", NULL);
2059 PyCapsule_SetPointer(object, capsule_pointer);
2060 PyCapsule_SetName(object, capsule_name);
2061 PyCapsule_SetDestructor(object, capsule_destructor);
2062 PyCapsule_SetContext(object, capsule_context);
2063 capsule_destructor(object);
2064 CHECK_DESTRUCTOR;
2065 /* intentionally access using the wrong name */
2066 pointer2 = PyCapsule_GetPointer(object, "the wrong name");
2067 if (!PyErr_Occurred()) {
2068 FAIL("PyCapsule_GetPointer should have failed but did not!");
2069 }
2070 PyErr_Clear();
2071 if (pointer2) {
2072 if (pointer2 == capsule_pointer) {
2073 FAIL("PyCapsule_GetPointer should not have"
2074 " returned the internal pointer!");
2075 } else {
2076 FAIL("PyCapsule_GetPointer should have "
2077 "returned NULL pointer but did not!");
2078 }
2079 }
2080 PyCapsule_SetDestructor(object, NULL);
2081 Py_DECREF(object);
2082 if (capsule_destructor_call_count) {
2083 FAIL("destructor called when it should not have been!");
2084 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00002085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 for (known = &known_capsules[0]; known->module != NULL; known++) {
2087 /* yeah, ordinarily I wouldn't do this either,
2088 but it's fine for this test harness.
2089 */
2090 static char buffer[256];
Benjamin Petersonb173f782009-05-05 22:31:58 +00002091#undef FAIL
2092#define FAIL(x) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 { \
2094 sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
2095 x, known->module, known->attribute); \
2096 error = buffer; \
2097 goto exit; \
2098 } \
Benjamin Petersonb173f782009-05-05 22:31:58 +00002099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 PyObject *module = PyImport_ImportModule(known->module);
2101 if (module) {
2102 pointer = PyCapsule_Import(known->name, 0);
2103 if (!pointer) {
2104 Py_DECREF(module);
2105 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
2106 }
2107 object = PyObject_GetAttrString(module, known->attribute);
2108 if (!object) {
2109 Py_DECREF(module);
2110 return NULL;
2111 }
2112 pointer2 = PyCapsule_GetPointer(object,
2113 "weebles wobble but they don't fall down");
2114 if (!PyErr_Occurred()) {
2115 Py_DECREF(object);
2116 Py_DECREF(module);
2117 FAIL("PyCapsule_GetPointer should have failed but did not!");
2118 }
2119 PyErr_Clear();
2120 if (pointer2) {
2121 Py_DECREF(module);
2122 Py_DECREF(object);
2123 if (pointer2 == pointer) {
2124 FAIL("PyCapsule_GetPointer should not have"
2125 " returned its internal pointer!");
2126 } else {
2127 FAIL("PyCapsule_GetPointer should have"
2128 " returned NULL pointer but did not!");
2129 }
2130 }
2131 Py_DECREF(object);
2132 Py_DECREF(module);
2133 }
2134 else
2135 PyErr_Clear();
2136 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00002137
2138 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 if (error) {
2140 return raiseTestError("test_capsule", error);
2141 }
2142 Py_RETURN_NONE;
Benjamin Petersonb173f782009-05-05 22:31:58 +00002143#undef FAIL
2144}
2145
Guido van Rossumddefaf32007-01-14 03:31:43 +00002146#ifdef HAVE_GETTIMEOFDAY
2147/* Profiling of integer performance */
Martin v. Löwis1c951552008-06-13 07:48:19 +00002148static void print_delta(int test, struct timeval *s, struct timeval *e)
Guido van Rossumddefaf32007-01-14 03:31:43 +00002149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 e->tv_sec -= s->tv_sec;
2151 e->tv_usec -= s->tv_usec;
2152 if (e->tv_usec < 0) {
2153 e->tv_sec -=1;
2154 e->tv_usec += 1000000;
2155 }
2156 printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002157}
2158
2159static PyObject *
2160profile_int(PyObject *self, PyObject* args)
2161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 int i, k;
2163 struct timeval start, stop;
2164 PyObject *single, **multiple, *op1, *result;
Guido van Rossumddefaf32007-01-14 03:31:43 +00002165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 /* Test 1: Allocate and immediately deallocate
2167 many small integers */
2168 gettimeofday(&start, NULL);
2169 for(k=0; k < 20000; k++)
2170 for(i=0; i < 1000; i++) {
2171 single = PyLong_FromLong(i);
2172 Py_DECREF(single);
2173 }
2174 gettimeofday(&stop, NULL);
2175 print_delta(1, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 /* Test 2: Allocate and immediately deallocate
2178 many large integers */
2179 gettimeofday(&start, NULL);
2180 for(k=0; k < 20000; k++)
2181 for(i=0; i < 1000; i++) {
2182 single = PyLong_FromLong(i+1000000);
2183 Py_DECREF(single);
2184 }
2185 gettimeofday(&stop, NULL);
2186 print_delta(2, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 /* Test 3: Allocate a few integers, then release
2189 them all simultaneously. */
2190 multiple = malloc(sizeof(PyObject*) * 1000);
2191 gettimeofday(&start, NULL);
2192 for(k=0; k < 20000; k++) {
2193 for(i=0; i < 1000; i++) {
2194 multiple[i] = PyLong_FromLong(i+1000000);
2195 }
2196 for(i=0; i < 1000; i++) {
2197 Py_DECREF(multiple[i]);
2198 }
2199 }
2200 gettimeofday(&stop, NULL);
2201 print_delta(3, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 /* Test 4: Allocate many integers, then release
2204 them all simultaneously. */
2205 multiple = malloc(sizeof(PyObject*) * 1000000);
2206 gettimeofday(&start, NULL);
2207 for(k=0; k < 20; k++) {
2208 for(i=0; i < 1000000; i++) {
2209 multiple[i] = PyLong_FromLong(i+1000000);
2210 }
2211 for(i=0; i < 1000000; i++) {
2212 Py_DECREF(multiple[i]);
2213 }
2214 }
2215 gettimeofday(&stop, NULL);
2216 print_delta(4, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 /* Test 5: Allocate many integers < 32000 */
2219 multiple = malloc(sizeof(PyObject*) * 1000000);
2220 gettimeofday(&start, NULL);
2221 for(k=0; k < 10; k++) {
2222 for(i=0; i < 1000000; i++) {
2223 multiple[i] = PyLong_FromLong(i+1000);
2224 }
2225 for(i=0; i < 1000000; i++) {
2226 Py_DECREF(multiple[i]);
2227 }
2228 }
2229 gettimeofday(&stop, NULL);
2230 print_delta(5, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 /* Test 6: Perform small int addition */
2233 op1 = PyLong_FromLong(1);
2234 gettimeofday(&start, NULL);
2235 for(i=0; i < 10000000; i++) {
2236 result = PyNumber_Add(op1, op1);
2237 Py_DECREF(result);
2238 }
2239 gettimeofday(&stop, NULL);
2240 Py_DECREF(op1);
2241 print_delta(6, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 /* Test 7: Perform medium int addition */
2244 op1 = PyLong_FromLong(1000);
2245 gettimeofday(&start, NULL);
2246 for(i=0; i < 10000000; i++) {
2247 result = PyNumber_Add(op1, op1);
2248 Py_DECREF(result);
2249 }
2250 gettimeofday(&stop, NULL);
2251 Py_DECREF(op1);
2252 print_delta(7, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 Py_INCREF(Py_None);
2255 return Py_None;
Guido van Rossumddefaf32007-01-14 03:31:43 +00002256}
2257#endif
2258
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002259/* To test the format of tracebacks as printed out. */
2260static PyObject *
2261traceback_print(PyObject *self, PyObject *args)
2262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 PyObject *file;
2264 PyObject *traceback;
2265 int result;
2266
2267 if (!PyArg_ParseTuple(args, "OO:traceback_print",
2268 &traceback, &file))
2269 return NULL;
2270
2271 result = PyTraceBack_Print(traceback, file);
2272 if (result < 0)
2273 return NULL;
2274 Py_RETURN_NONE;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002275}
2276
Benjamin Petersone6528212008-07-15 15:32:09 +00002277/* To test the format of exceptions as printed out. */
2278static PyObject *
2279exception_print(PyObject *self, PyObject *args)
2280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 PyObject *value;
2282 PyObject *tb;
Benjamin Petersone6528212008-07-15 15:32:09 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 if (!PyArg_ParseTuple(args, "O:exception_print",
2285 &value))
2286 return NULL;
2287 if (!PyExceptionInstance_Check(value)) {
2288 PyErr_Format(PyExc_TypeError, "an exception instance is required");
2289 return NULL;
2290 }
Benjamin Petersone6528212008-07-15 15:32:09 +00002291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 tb = PyException_GetTraceback(value);
2293 PyErr_Display((PyObject *) Py_TYPE(value), value, tb);
2294 Py_XDECREF(tb);
Benjamin Petersone6528212008-07-15 15:32:09 +00002295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 Py_RETURN_NONE;
Benjamin Petersone6528212008-07-15 15:32:09 +00002297}
2298
2299
2300
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002301
2302/* reliably raise a MemoryError */
2303static PyObject *
2304raise_memoryerror(PyObject *self)
2305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 PyErr_NoMemory();
2307 return NULL;
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002308}
2309
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00002310/* Issue 6012 */
2311static PyObject *str1, *str2;
2312static int
2313failing_converter(PyObject *obj, void *arg)
2314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 /* Clone str1, then let the conversion fail. */
2316 assert(str1);
2317 str2 = str1;
2318 Py_INCREF(str2);
2319 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00002320}
2321static PyObject*
2322argparsing(PyObject *o, PyObject *args)
2323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 PyObject *res;
2325 str1 = str2 = NULL;
2326 if (!PyArg_ParseTuple(args, "O&O&",
2327 PyUnicode_FSConverter, &str1,
2328 failing_converter, &str2)) {
2329 if (!str2)
2330 /* argument converter not called? */
2331 return NULL;
2332 /* Should be 1 */
Victor Stinner0fcab4a2011-01-04 12:59:15 +00002333 res = PyLong_FromSsize_t(Py_REFCNT(str2));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 Py_DECREF(str2);
2335 PyErr_Clear();
2336 return res;
2337 }
2338 Py_RETURN_NONE;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00002339}
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002340
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002341/* To test that the result of PyCode_NewEmpty has the right members. */
2342static PyObject *
2343code_newempty(PyObject *self, PyObject *args)
2344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 const char *filename;
2346 const char *funcname;
2347 int firstlineno;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 if (!PyArg_ParseTuple(args, "ssi:code_newempty",
2350 &filename, &funcname, &firstlineno))
2351 return NULL;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002354}
2355
Georg Brandl1e28a272009-12-28 08:41:01 +00002356/* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException).
2357 Run via Lib/test/test_exceptions.py */
2358static PyObject *
2359make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs)
2360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 const char *name;
2362 const char *doc = NULL;
2363 PyObject *base = NULL;
2364 PyObject *dict = NULL;
Georg Brandl1e28a272009-12-28 08:41:01 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 static char *kwlist[] = {"name", "doc", "base", "dict", NULL};
Georg Brandl1e28a272009-12-28 08:41:01 +00002367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
2369 "s|sOO:make_exception_with_doc", kwlist,
2370 &name, &doc, &base, &dict))
2371 return NULL;
Georg Brandl1e28a272009-12-28 08:41:01 +00002372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 return PyErr_NewExceptionWithDoc(name, doc, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +00002374}
2375
Antoine Pitrou5bffa792011-02-24 20:50:49 +00002376static PyObject *
2377make_memoryview_from_NULL_pointer(PyObject *self)
2378{
2379 Py_buffer info;
2380 if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0)
2381 return NULL;
2382 return PyMemoryView_FromBuffer(&info);
2383}
2384
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00002385/* Test that the fatal error from not having a current thread doesn't
2386 cause an infinite loop. Run via Lib/test/test_capi.py */
2387static PyObject *
2388crash_no_current_thread(PyObject *self)
2389{
2390 Py_BEGIN_ALLOW_THREADS
Jeffrey Yasskinea7b7482010-05-17 16:59:23 +00002391 /* Using PyThreadState_Get() directly allows the test to pass in
2392 !pydebug mode. However, the test only actually tests anything
2393 in pydebug mode, since that's where the infinite loop was in
2394 the first place. */
2395 PyThreadState_Get();
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00002396 Py_END_ALLOW_THREADS
2397 return NULL;
2398}
2399
Antoine Pitrou2f828f22012-01-18 00:21:11 +01002400/* To run some code in a sub-interpreter. */
2401static PyObject *
2402run_in_subinterp(PyObject *self, PyObject *args)
2403{
2404 const char *code;
2405 int r;
2406 PyThreadState *substate, *mainstate;
2407
2408 if (!PyArg_ParseTuple(args, "s:run_in_subinterp",
2409 &code))
2410 return NULL;
2411
2412 mainstate = PyThreadState_Get();
2413
2414 PyThreadState_Swap(NULL);
2415
2416 substate = Py_NewInterpreter();
Brett Cannonb6855682012-02-03 12:08:03 -05002417 if (substate == NULL) {
2418 /* Since no new thread state was created, there is no exception to
2419 propagate; raise a fresh one after swapping in the old thread
2420 state. */
2421 PyThreadState_Swap(mainstate);
2422 PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed");
2423 return NULL;
2424 }
Antoine Pitrou2f828f22012-01-18 00:21:11 +01002425 r = PyRun_SimpleString(code);
2426 Py_EndInterpreter(substate);
2427
2428 PyThreadState_Swap(mainstate);
2429
2430 return PyLong_FromLong(r);
2431}
2432
Victor Stinner5d272cc2012-03-13 13:35:55 +01002433static PyObject *
2434test_pytime_object_to_time_t(PyObject *self, PyObject *args)
2435{
2436 PyObject *obj;
2437 time_t sec;
2438 if (!PyArg_ParseTuple(args, "O:pytime_object_to_time_t", &obj))
2439 return NULL;
2440 if (_PyTime_ObjectToTime_t(obj, &sec) == -1)
2441 return NULL;
2442 return _PyLong_FromTime_t(sec);
2443}
2444
2445static PyObject *
2446test_pytime_object_to_timeval(PyObject *self, PyObject *args)
2447{
2448 PyObject *obj;
2449 time_t sec;
2450 long usec;
2451 if (!PyArg_ParseTuple(args, "O:pytime_object_to_timeval", &obj))
2452 return NULL;
2453 if (_PyTime_ObjectToTimeval(obj, &sec, &usec) == -1)
2454 return NULL;
2455 return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec);
2456}
2457
Victor Stinner643cd682012-03-02 22:54:03 +01002458static PyObject *
2459test_pytime_object_to_timespec(PyObject *self, PyObject *args)
2460{
2461 PyObject *obj;
2462 time_t sec;
2463 long nsec;
2464 if (!PyArg_ParseTuple(args, "O:pytime_object_to_timespec", &obj))
2465 return NULL;
2466 if (_PyTime_ObjectToTimespec(obj, &sec, &nsec) == -1)
2467 return NULL;
Victor Stinner5d272cc2012-03-13 13:35:55 +01002468 return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec);
Victor Stinner643cd682012-03-02 22:54:03 +01002469}
2470
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -07002471static PyObject *
2472_test_incref(PyObject *ob)
2473{
2474 Py_INCREF(ob);
2475 return ob;
2476}
2477
2478static PyObject *
2479test_xincref_doesnt_leak(PyObject *ob)
2480{
2481 PyObject *obj = PyLong_FromLong(0);
2482 Py_XINCREF(_test_incref(obj));
2483 Py_DECREF(obj);
2484 Py_DECREF(obj);
2485 Py_DECREF(obj);
2486 Py_RETURN_NONE;
2487}
2488
2489static PyObject *
2490test_incref_doesnt_leak(PyObject *ob)
2491{
2492 PyObject *obj = PyLong_FromLong(0);
2493 Py_INCREF(_test_incref(obj));
2494 Py_DECREF(obj);
2495 Py_DECREF(obj);
2496 Py_DECREF(obj);
2497 Py_RETURN_NONE;
2498}
2499
2500static PyObject *
2501test_xdecref_doesnt_leak(PyObject *ob)
2502{
2503 Py_XDECREF(PyLong_FromLong(0));
2504 Py_RETURN_NONE;
2505}
2506
2507static PyObject *
2508test_decref_doesnt_leak(PyObject *ob)
2509{
2510 Py_DECREF(PyLong_FromLong(0));
2511 Py_RETURN_NONE;
2512}
Antoine Pitrou2f828f22012-01-18 00:21:11 +01002513
Tim Peters9ea17ac2001-02-02 05:57:15 +00002514static PyMethodDef TestMethods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 {"raise_exception", raise_exception, METH_VARARGS},
2516 {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
2517 {"test_config", (PyCFunction)test_config, METH_NOARGS},
2518 {"test_datetime_capi", test_datetime_capi, METH_NOARGS},
2519 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
2520 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
2521 {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -07002523 {"test_xincref_doesnt_leak",(PyCFunction)test_xincref_doesnt_leak, METH_NOARGS},
2524 {"test_incref_doesnt_leak", (PyCFunction)test_incref_doesnt_leak, METH_NOARGS},
2525 {"test_xdecref_doesnt_leak",(PyCFunction)test_xdecref_doesnt_leak, METH_NOARGS},
2526 {"test_decref_doesnt_leak", (PyCFunction)test_decref_doesnt_leak, METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 {"test_long_and_overflow", (PyCFunction)test_long_and_overflow,
2528 METH_NOARGS},
Nadeem Vawda3d5881e2011-09-07 21:40:26 +02002529 {"test_long_as_double", (PyCFunction)test_long_as_double,METH_NOARGS},
2530 {"test_long_as_size_t", (PyCFunction)test_long_as_size_t,METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
2532 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
2533 {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
Larry Hastings8f904da2012-06-22 03:56:29 -07002534 {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
2536 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
2537 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
2538 PyDoc_STR("This is a pretty normal docstring.")},
2539 {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS},
2540 {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS},
2541 {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
2542 {"getargs_tuple", getargs_tuple, METH_VARARGS},
2543 {"getargs_keywords", (PyCFunction)getargs_keywords,
2544 METH_VARARGS|METH_KEYWORDS},
Larry Hastings83a9f482012-03-20 20:06:16 +00002545 {"getargs_keyword_only", (PyCFunction)getargs_keyword_only,
2546 METH_VARARGS|METH_KEYWORDS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 {"getargs_b", getargs_b, METH_VARARGS},
2548 {"getargs_B", getargs_B, METH_VARARGS},
2549 {"getargs_h", getargs_h, METH_VARARGS},
2550 {"getargs_H", getargs_H, METH_VARARGS},
2551 {"getargs_I", getargs_I, METH_VARARGS},
2552 {"getargs_k", getargs_k, METH_VARARGS},
2553 {"getargs_i", getargs_i, METH_VARARGS},
2554 {"getargs_l", getargs_l, METH_VARARGS},
2555 {"getargs_n", getargs_n, METH_VARARGS},
Larry Hastingsfaf91e72012-05-05 16:54:29 -07002556 {"getargs_p", getargs_p, METH_VARARGS},
Tim Peters91621db2001-06-12 20:10:01 +00002557#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 {"getargs_L", getargs_L, METH_VARARGS},
2559 {"getargs_K", getargs_K, METH_VARARGS},
2560 {"test_longlong_api", test_longlong_api, METH_NOARGS},
2561 {"test_long_long_and_overflow",
2562 (PyCFunction)test_long_long_and_overflow, METH_NOARGS},
2563 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
Victor Stinner06e49dd2010-06-13 18:21:50 +00002564#endif
Eli Bendersky906b88f2011-07-29 07:05:08 +03002565 {"getargs_c", getargs_c, METH_VARARGS},
Victor Stinner06e49dd2010-06-13 18:21:50 +00002566 {"getargs_s", getargs_s, METH_VARARGS},
2567 {"getargs_s_star", getargs_s_star, METH_VARARGS},
2568 {"getargs_s_hash", getargs_s_hash, METH_VARARGS},
2569 {"getargs_z", getargs_z, METH_VARARGS},
2570 {"getargs_z_star", getargs_z_star, METH_VARARGS},
2571 {"getargs_z_hash", getargs_z_hash, METH_VARARGS},
2572 {"getargs_y", getargs_y, METH_VARARGS},
2573 {"getargs_y_star", getargs_y_star, METH_VARARGS},
2574 {"getargs_y_hash", getargs_y_hash, METH_VARARGS},
2575 {"getargs_u", getargs_u, METH_VARARGS},
2576 {"getargs_u_hash", getargs_u_hash, METH_VARARGS},
2577 {"getargs_Z", getargs_Z, METH_VARARGS},
2578 {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS},
Victor Stinner25e8ec42010-06-25 00:02:38 +00002579 {"getargs_w_star", getargs_w_star, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 {"codec_incrementalencoder",
Victor Stinner1c24bd02010-10-02 11:03:13 +00002581 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 {"codec_incrementaldecoder",
Victor Stinner1c24bd02010-10-02 11:03:13 +00002583 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS},
2585 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
2586 {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS},
2587 {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS},
Victor Stinner42bf7752011-11-21 22:52:58 +01002588 {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS},
2589 {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS},
2590 {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS},
2591 {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS},
Stefan Krahe6996ed2012-11-02 14:44:20 +01002592 {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS},
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002593#ifdef WITH_THREAD
Victor Stinner1c24bd02010-10-02 11:03:13 +00002594 {"_test_thread_state", test_thread_state, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 {"_pending_threadfunc", pending_threadfunc, METH_VARARGS},
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002596#endif
Guido van Rossumddefaf32007-01-14 03:31:43 +00002597#ifdef HAVE_GETTIMEOFDAY
Victor Stinner1c24bd02010-10-02 11:03:13 +00002598 {"profile_int", profile_int, METH_NOARGS},
Guido van Rossumddefaf32007-01-14 03:31:43 +00002599#endif
Victor Stinner1c24bd02010-10-02 11:03:13 +00002600 {"traceback_print", traceback_print, METH_VARARGS},
2601 {"exception_print", exception_print, METH_VARARGS},
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +02002602 {"set_exc_info", test_set_exc_info, METH_VARARGS},
Victor Stinner1c24bd02010-10-02 11:03:13 +00002603 {"argparsing", argparsing, METH_VARARGS},
2604 {"code_newempty", code_newempty, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 {"make_exception_with_doc", (PyCFunction)make_exception_with_doc,
2606 METH_VARARGS | METH_KEYWORDS},
Antoine Pitrou5bffa792011-02-24 20:50:49 +00002607 {"make_memoryview_from_NULL_pointer", (PyCFunction)make_memoryview_from_NULL_pointer,
2608 METH_NOARGS},
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00002609 {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS},
Antoine Pitrou2f828f22012-01-18 00:21:11 +01002610 {"run_in_subinterp", run_in_subinterp, METH_VARARGS},
Victor Stinner5d272cc2012-03-13 13:35:55 +01002611 {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS},
2612 {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS},
Victor Stinner643cd682012-03-02 22:54:03 +01002613 {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 {NULL, NULL} /* sentinel */
Tim Peters9ea17ac2001-02-02 05:57:15 +00002615};
2616
Thomas Hellera4ea6032003-04-17 18:55:45 +00002617#define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
2618
Thomas Wouters89f507f2006-12-13 04:49:30 +00002619typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002620 char bool_member;
2621 char byte_member;
2622 unsigned char ubyte_member;
2623 short short_member;
2624 unsigned short ushort_member;
2625 int int_member;
2626 unsigned int uint_member;
2627 long long_member;
2628 unsigned long ulong_member;
2629 Py_ssize_t pyssizet_member;
2630 float float_member;
2631 double double_member;
2632 char inplace_member[6];
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002633#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 PY_LONG_LONG longlong_member;
2635 unsigned PY_LONG_LONG ulonglong_member;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002636#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +00002637} all_structmembers;
2638
2639typedef struct {
2640 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 all_structmembers structmembers;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002642} test_structmembers;
2643
2644static struct PyMemberDef test_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
2646 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
2647 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
2648 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
2649 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
2650 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
2651 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
2652 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
2653 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
2654 {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL},
2655 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
2656 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
2657 {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002658#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
2660 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002661#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 {NULL}
Thomas Wouters89f507f2006-12-13 04:49:30 +00002663};
2664
2665
Christian Heimes1af737c2008-01-23 08:24:23 +00002666static PyObject *
2667test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 static char *keywords[] = {
2670 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
2671 "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET",
2672 "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
Mark Dickinsond59b4162010-03-13 11:34:40 +00002673#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 "T_LONGLONG", "T_ULONGLONG",
Christian Heimes1af737c2008-01-23 08:24:23 +00002675#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 NULL};
2677 static char *fmt = "|bbBhHiIlknfds#"
Christian Heimes1af737c2008-01-23 08:24:23 +00002678#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 "LK"
Christian Heimes1af737c2008-01-23 08:24:23 +00002680#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 ;
2682 test_structmembers *ob;
2683 const char *s = NULL;
2684 Py_ssize_t string_len = 0;
2685 ob = PyObject_New(test_structmembers, type);
2686 if (ob == NULL)
2687 return NULL;
2688 memset(&ob->structmembers, 0, sizeof(all_structmembers));
2689 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
2690 &ob->structmembers.bool_member,
2691 &ob->structmembers.byte_member,
2692 &ob->structmembers.ubyte_member,
2693 &ob->structmembers.short_member,
2694 &ob->structmembers.ushort_member,
2695 &ob->structmembers.int_member,
2696 &ob->structmembers.uint_member,
2697 &ob->structmembers.long_member,
2698 &ob->structmembers.ulong_member,
2699 &ob->structmembers.pyssizet_member,
2700 &ob->structmembers.float_member,
2701 &ob->structmembers.double_member,
2702 &s, &string_len
Christian Heimes1af737c2008-01-23 08:24:23 +00002703#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 , &ob->structmembers.longlong_member,
2705 &ob->structmembers.ulonglong_member
Christian Heimes1af737c2008-01-23 08:24:23 +00002706#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 )) {
2708 Py_DECREF(ob);
2709 return NULL;
2710 }
2711 if (s != NULL) {
2712 if (string_len > 5) {
2713 Py_DECREF(ob);
2714 PyErr_SetString(PyExc_ValueError, "string too long");
2715 return NULL;
2716 }
2717 strcpy(ob->structmembers.inplace_member, s);
2718 }
2719 else {
2720 strcpy(ob->structmembers.inplace_member, "");
2721 }
2722 return (PyObject *)ob;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002723}
2724
Christian Heimes1af737c2008-01-23 08:24:23 +00002725static void
2726test_structmembers_free(PyObject *ob)
2727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 PyObject_FREE(ob);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002729}
2730
2731static PyTypeObject test_structmembersType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002732 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 "test_structmembersType",
2734 sizeof(test_structmembers), /* tp_basicsize */
2735 0, /* tp_itemsize */
2736 test_structmembers_free, /* destructor tp_dealloc */
2737 0, /* tp_print */
2738 0, /* tp_getattr */
2739 0, /* tp_setattr */
2740 0, /* tp_reserved */
2741 0, /* tp_repr */
2742 0, /* tp_as_number */
2743 0, /* tp_as_sequence */
2744 0, /* tp_as_mapping */
2745 0, /* tp_hash */
2746 0, /* tp_call */
2747 0, /* tp_str */
2748 PyObject_GenericGetAttr, /* tp_getattro */
2749 PyObject_GenericSetAttr, /* tp_setattro */
2750 0, /* tp_as_buffer */
2751 0, /* tp_flags */
2752 "Type containing all structmember types",
2753 0, /* traverseproc tp_traverse */
2754 0, /* tp_clear */
2755 0, /* tp_richcompare */
2756 0, /* tp_weaklistoffset */
2757 0, /* tp_iter */
2758 0, /* tp_iternext */
2759 0, /* tp_methods */
2760 test_members, /* tp_members */
2761 0,
2762 0,
2763 0,
2764 0,
2765 0,
2766 0,
2767 0,
2768 0,
2769 test_structmembers_new, /* tp_new */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002770};
2771
2772
Martin v. Löwis1a214512008-06-11 05:26:20 +00002773
2774static struct PyModuleDef _testcapimodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 PyModuleDef_HEAD_INIT,
2776 "_testcapi",
2777 NULL,
2778 -1,
2779 TestMethods,
2780 NULL,
2781 NULL,
2782 NULL,
2783 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002784};
2785
Mark Hammond62b1ab12002-07-23 06:31:15 +00002786PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002787PyInit__testcapi(void)
Tim Peters9ea17ac2001-02-02 05:57:15 +00002788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 PyObject *m;
Tim Peters9ea17ac2001-02-02 05:57:15 +00002790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 m = PyModule_Create(&_testcapimodule);
2792 if (m == NULL)
2793 return NULL;
Tim Peters9ea17ac2001-02-02 05:57:15 +00002794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
Hirokazu Yamamoto8ebab5d2008-12-31 06:05:46 +00002796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 Py_TYPE(&test_structmembersType)=&PyType_Type;
2798 Py_INCREF(&test_structmembersType);
2799 /* don't use a name starting with "test", since we don't want
2800 test_capi to automatically call this */
2801 PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
2804 PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
2805 PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX));
2806 PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX));
2807 PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN));
2808 PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX));
2809 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
2810 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
2811 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
2812 PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX));
2813 PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN));
2814 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
2815 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
2816 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
2817 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
2818 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
2819 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
2820 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
2821 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
2822 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
2823 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
2824 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
2825 Py_INCREF(&PyInstanceMethod_Type);
2826 PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
Thomas Hellera4ea6032003-04-17 18:55:45 +00002827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
2829 Py_INCREF(TestError);
2830 PyModule_AddObject(m, "error", TestError);
2831 return m;
Tim Peters9ea17ac2001-02-02 05:57:15 +00002832}