blob: f19d0df5944bc853b3da24d953a7a7646f1a3719 [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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 char buf[2048];
Tim Peters91621db2001-06-12 20:10:01 +000026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
28 PyErr_SetString(TestError, "internal error msg too large");
29 else {
30 PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
31 PyErr_SetString(TestError, buf);
32 }
33 return NULL;
Tim Peters91621db2001-06-12 20:10:01 +000034}
35
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000036/* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
Tim Peters9ea17ac2001-02-02 05:57:15 +000037
38 The ones derived from autoconf on the UNIX-like OSes can be relied
39 upon (in the absence of sloppy cross-compiling), but the Windows
40 platforms have these hardcoded. Better safe than sorry.
41*/
42static PyObject*
Thomas Wouters89f507f2006-12-13 04:49:30 +000043sizeof_error(const char* fatname, const char* typname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 int expected, int got)
Tim Peters9ea17ac2001-02-02 05:57:15 +000045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 char buf[1024];
47 PyOS_snprintf(buf, sizeof(buf),
48 "%.200s #define == %d but sizeof(%.200s) == %d",
49 fatname, expected, typname, got);
50 PyErr_SetString(TestError, buf);
51 return (PyObject*)NULL;
Tim Peters9ea17ac2001-02-02 05:57:15 +000052}
53
54static PyObject*
Fred Drakeacee69f2002-04-01 14:28:58 +000055test_config(PyObject *self)
Tim Peters9ea17ac2001-02-02 05:57:15 +000056{
Tim Peters9ea17ac2001-02-02 05:57:15 +000057#define CHECK_SIZEOF(FATNAME, TYPE) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 if (FATNAME != sizeof(TYPE)) \
59 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
Tim Peters9ea17ac2001-02-02 05:57:15 +000060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 CHECK_SIZEOF(SIZEOF_SHORT, short);
62 CHECK_SIZEOF(SIZEOF_INT, int);
63 CHECK_SIZEOF(SIZEOF_LONG, long);
64 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
65 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
Tim Peters9ea17ac2001-02-02 05:57:15 +000066#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
Tim Peters9ea17ac2001-02-02 05:57:15 +000068#endif
69
70#undef CHECK_SIZEOF
71
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 Py_INCREF(Py_None);
73 return Py_None;
Tim Peters9ea17ac2001-02-02 05:57:15 +000074}
75
Tim Peters5c4d5bf2001-02-12 22:13:26 +000076static PyObject*
Fred Drakeacee69f2002-04-01 14:28:58 +000077test_list_api(PyObject *self)
Tim Peters5c4d5bf2001-02-12 22:13:26 +000078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 PyObject* list;
80 int i;
Tim Peters5c4d5bf2001-02-12 22:13:26 +000081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 /* SF bug 132008: PyList_Reverse segfaults */
Tim Peters5c4d5bf2001-02-12 22:13:26 +000083#define NLIST 30
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 list = PyList_New(NLIST);
85 if (list == (PyObject*)NULL)
86 return (PyObject*)NULL;
87 /* list = range(NLIST) */
88 for (i = 0; i < NLIST; ++i) {
89 PyObject* anint = PyLong_FromLong(i);
90 if (anint == (PyObject*)NULL) {
91 Py_DECREF(list);
92 return (PyObject*)NULL;
93 }
94 PyList_SET_ITEM(list, i, anint);
95 }
96 /* list.reverse(), via PyList_Reverse() */
97 i = PyList_Reverse(list); /* should not blow up! */
98 if (i != 0) {
99 Py_DECREF(list);
100 return (PyObject*)NULL;
101 }
102 /* Check that list == range(29, -1, -1) now */
103 for (i = 0; i < NLIST; ++i) {
104 PyObject* anint = PyList_GET_ITEM(list, i);
105 if (PyLong_AS_LONG(anint) != NLIST-1-i) {
106 PyErr_SetString(TestError,
107 "test_list_api: reverse screwed up");
108 Py_DECREF(list);
109 return (PyObject*)NULL;
110 }
111 }
112 Py_DECREF(list);
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000113#undef NLIST
114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 Py_INCREF(Py_None);
116 return Py_None;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000117}
118
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000119static int
120test_dict_inner(int count)
121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 Py_ssize_t pos = 0, iterations = 0;
123 int i;
124 PyObject *dict = PyDict_New();
125 PyObject *v, *k;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 if (dict == NULL)
128 return -1;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 for (i = 0; i < count; i++) {
131 v = PyLong_FromLong(i);
132 PyDict_SetItem(dict, v, v);
133 Py_DECREF(v);
134 }
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 while (PyDict_Next(dict, &pos, &k, &v)) {
137 PyObject *o;
138 iterations++;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 i = PyLong_AS_LONG(v) + 1;
141 o = PyLong_FromLong(i);
142 if (o == NULL)
143 return -1;
144 if (PyDict_SetItem(dict, k, o) < 0) {
145 Py_DECREF(o);
146 return -1;
147 }
148 Py_DECREF(o);
149 }
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 Py_DECREF(dict);
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (iterations != count) {
154 PyErr_SetString(
155 TestError,
156 "test_dict_iteration: dict iteration went wrong ");
157 return -1;
158 } else {
159 return 0;
160 }
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000161}
162
163static PyObject*
Fred Drakeacee69f2002-04-01 14:28:58 +0000164test_dict_iteration(PyObject* self)
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 int i;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 for (i = 0; i < 200; i++) {
169 if (test_dict_inner(i) < 0) {
170 return NULL;
171 }
172 }
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 Py_INCREF(Py_None);
175 return Py_None;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000176}
177
Tim Peters91621db2001-06-12 20:10:01 +0000178
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000179/* Issue #4701: Check that PyObject_Hash implicitly calls
180 * PyType_Ready if it hasn't already been called
181 */
182static PyTypeObject _HashInheritanceTester_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 PyVarObject_HEAD_INIT(NULL, 0)
184 "hashinheritancetester", /* Name of this type */
185 sizeof(PyObject), /* Basic object size */
186 0, /* Item size for varobject */
187 (destructor)PyObject_Del, /* tp_dealloc */
188 0, /* tp_print */
189 0, /* tp_getattr */
190 0, /* tp_setattr */
191 0, /* tp_reserved */
192 0, /* tp_repr */
193 0, /* tp_as_number */
194 0, /* tp_as_sequence */
195 0, /* tp_as_mapping */
196 0, /* tp_hash */
197 0, /* tp_call */
198 0, /* tp_str */
199 PyObject_GenericGetAttr, /* tp_getattro */
200 0, /* tp_setattro */
201 0, /* tp_as_buffer */
202 Py_TPFLAGS_DEFAULT, /* tp_flags */
203 0, /* tp_doc */
204 0, /* tp_traverse */
205 0, /* tp_clear */
206 0, /* tp_richcompare */
207 0, /* tp_weaklistoffset */
208 0, /* tp_iter */
209 0, /* tp_iternext */
210 0, /* tp_methods */
211 0, /* tp_members */
212 0, /* tp_getset */
213 0, /* tp_base */
214 0, /* tp_dict */
215 0, /* tp_descr_get */
216 0, /* tp_descr_set */
217 0, /* tp_dictoffset */
218 0, /* tp_init */
219 0, /* tp_alloc */
220 PyType_GenericNew, /* tp_new */
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000221};
222
223static PyObject*
224test_lazy_hash_inheritance(PyObject* self)
225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 PyTypeObject *type;
227 PyObject *obj;
Antoine Pitrou29aad002010-10-23 19:42:38 +0000228 Py_hash_t hash;
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 type = &_HashInheritanceTester_Type;
Benjamin Petersonf1726372009-05-05 21:11:54 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 if (type->tp_dict != NULL)
233 /* The type has already been initialized. This probably means
234 -R is being used. */
235 Py_RETURN_NONE;
Benjamin Petersonf1726372009-05-05 21:11:54 +0000236
237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 obj = PyObject_New(PyObject, type);
239 if (obj == NULL) {
240 PyErr_Clear();
241 PyErr_SetString(
242 TestError,
243 "test_lazy_hash_inheritance: failed to create object");
244 return NULL;
245 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (type->tp_dict != NULL) {
248 PyErr_SetString(
249 TestError,
250 "test_lazy_hash_inheritance: type initialised too soon");
251 Py_DECREF(obj);
252 return NULL;
253 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 hash = PyObject_Hash(obj);
256 if ((hash == -1) && PyErr_Occurred()) {
257 PyErr_Clear();
258 PyErr_SetString(
259 TestError,
260 "test_lazy_hash_inheritance: could not hash object");
261 Py_DECREF(obj);
262 return NULL;
263 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 if (type->tp_dict == NULL) {
266 PyErr_SetString(
267 TestError,
268 "test_lazy_hash_inheritance: type not initialised by hash()");
269 Py_DECREF(obj);
270 return NULL;
271 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (type->tp_hash != PyType_Type.tp_hash) {
274 PyErr_SetString(
275 TestError,
276 "test_lazy_hash_inheritance: unexpected hash function");
277 Py_DECREF(obj);
278 return NULL;
279 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 Py_DECREF(obj);
Benjamin Petersonf1726372009-05-05 21:11:54 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_RETURN_NONE;
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000284}
285
286
Antoine Pitrou05b7c562010-02-02 22:47:00 +0000287/* Issue #7385: Check that memoryview() does not crash
288 * when bf_getbuffer returns an error
289 */
290
291static int
292broken_buffer_getbuffer(PyObject *self, Py_buffer *view, int flags)
293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 PyErr_SetString(
295 TestError,
296 "test_broken_memoryview: expected error in bf_getbuffer");
297 return -1;
Antoine Pitrou05b7c562010-02-02 22:47:00 +0000298}
299
300static PyBufferProcs memoryviewtester_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 (getbufferproc)broken_buffer_getbuffer, /* bf_getbuffer */
302 0, /* bf_releasebuffer */
Antoine Pitrou05b7c562010-02-02 22:47:00 +0000303};
304
305static PyTypeObject _MemoryViewTester_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 PyVarObject_HEAD_INIT(NULL, 0)
307 "memoryviewtester", /* Name of this type */
308 sizeof(PyObject), /* Basic object size */
309 0, /* Item size for varobject */
310 (destructor)PyObject_Del, /* tp_dealloc */
311 0, /* tp_print */
312 0, /* tp_getattr */
313 0, /* tp_setattr */
314 0, /* tp_compare */
315 0, /* tp_repr */
316 0, /* tp_as_number */
317 0, /* tp_as_sequence */
318 0, /* tp_as_mapping */
319 0, /* tp_hash */
320 0, /* tp_call */
321 0, /* tp_str */
322 PyObject_GenericGetAttr, /* tp_getattro */
323 0, /* tp_setattro */
324 &memoryviewtester_as_buffer, /* tp_as_buffer */
325 Py_TPFLAGS_DEFAULT, /* tp_flags */
326 0, /* tp_doc */
327 0, /* tp_traverse */
328 0, /* tp_clear */
329 0, /* tp_richcompare */
330 0, /* tp_weaklistoffset */
331 0, /* tp_iter */
332 0, /* tp_iternext */
333 0, /* tp_methods */
334 0, /* tp_members */
335 0, /* tp_getset */
336 0, /* tp_base */
337 0, /* tp_dict */
338 0, /* tp_descr_get */
339 0, /* tp_descr_set */
340 0, /* tp_dictoffset */
341 0, /* tp_init */
342 0, /* tp_alloc */
343 PyType_GenericNew, /* tp_new */
Antoine Pitrou05b7c562010-02-02 22:47:00 +0000344};
345
346static PyObject*
347test_broken_memoryview(PyObject* self)
348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 PyObject *obj = PyObject_New(PyObject, &_MemoryViewTester_Type);
350 PyObject *res;
Antoine Pitrou05b7c562010-02-02 22:47:00 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (obj == NULL) {
353 PyErr_Clear();
354 PyErr_SetString(
355 TestError,
356 "test_broken_memoryview: failed to create object");
357 return NULL;
358 }
Antoine Pitrou05b7c562010-02-02 22:47:00 +0000359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 res = PyMemoryView_FromObject(obj);
361 if (res || !PyErr_Occurred()){
362 PyErr_SetString(
363 TestError,
364 "test_broken_memoryview: memoryview() didn't raise an Exception");
365 Py_XDECREF(res);
366 Py_DECREF(obj);
367 return NULL;
368 }
Antoine Pitrou05b7c562010-02-02 22:47:00 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 PyErr_Clear();
371 Py_DECREF(obj);
372 Py_RETURN_NONE;
Antoine Pitrou05b7c562010-02-02 22:47:00 +0000373}
374
375
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000376/* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
Tim Petersff70d3c2001-06-14 01:11:03 +0000377 PyLong_{As, From}{Unsigned,}LongLong().
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000378
379 Note that the meat of the test is contained in testcapi_long.h.
380 This is revolting, but delicate code duplication is worse: "almost
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000381 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000382 dependence on type names makes it impossible to use a parameterized
383 function. A giant macro would be even worse than this. A C++ template
384 would be perfect.
385
386 The "report an error" functions are deliberately not part of the #include
387 file: if the test fails, you can set a breakpoint in the appropriate
388 error function directly, and crawl back from there in the debugger.
389*/
390
391#define UNBIND(X) Py_DECREF(X); (X) = NULL
392
393static PyObject *
394raise_test_long_error(const char* msg)
395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 return raiseTestError("test_long_api", msg);
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000397}
398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399#define TESTNAME test_long_api_inner
400#define TYPENAME long
401#define F_S_TO_PY PyLong_FromLong
402#define F_PY_TO_S PyLong_AsLong
403#define F_U_TO_PY PyLong_FromUnsignedLong
404#define F_PY_TO_U PyLong_AsUnsignedLong
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000405
406#include "testcapi_long.h"
407
408static PyObject *
Fred Drakeacee69f2002-04-01 14:28:58 +0000409test_long_api(PyObject* self)
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 return TESTNAME(raise_test_long_error);
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000412}
413
414#undef TESTNAME
415#undef TYPENAME
416#undef F_S_TO_PY
417#undef F_PY_TO_S
418#undef F_U_TO_PY
419#undef F_PY_TO_U
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000420
421#ifdef HAVE_LONG_LONG
Tim Peters91621db2001-06-12 20:10:01 +0000422
423static PyObject *
Tim Petersd1a7da62001-06-13 00:35:57 +0000424raise_test_longlong_error(const char* msg)
425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 return raiseTestError("test_longlong_api", msg);
Tim Petersd1a7da62001-06-13 00:35:57 +0000427}
428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429#define TESTNAME test_longlong_api_inner
430#define TYPENAME PY_LONG_LONG
431#define F_S_TO_PY PyLong_FromLongLong
432#define F_PY_TO_S PyLong_AsLongLong
433#define F_U_TO_PY PyLong_FromUnsignedLongLong
434#define F_PY_TO_U PyLong_AsUnsignedLongLong
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000435
436#include "testcapi_long.h"
Tim Petersd1a7da62001-06-13 00:35:57 +0000437
438static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000439test_longlong_api(PyObject* self, PyObject *args)
Tim Peters91621db2001-06-12 20:10:01 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 return TESTNAME(raise_test_longlong_error);
Tim Peters91621db2001-06-12 20:10:01 +0000442}
443
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000444#undef TESTNAME
445#undef TYPENAME
446#undef F_S_TO_PY
447#undef F_PY_TO_S
448#undef F_U_TO_PY
449#undef F_PY_TO_U
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000450
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000451/* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG
452 is tested by test_long_api_inner. This test will concentrate on proper
453 handling of overflow.
454*/
455
456static PyObject *
457test_long_and_overflow(PyObject *self)
458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 PyObject *num, *one, *temp;
460 long value;
461 int overflow;
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 /* Test that overflow is set properly for a large value. */
464 /* num is a number larger than LONG_MAX even on 64-bit platforms */
465 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
466 if (num == NULL)
467 return NULL;
468 overflow = 1234;
469 value = PyLong_AsLongAndOverflow(num, &overflow);
470 Py_DECREF(num);
471 if (value == -1 && PyErr_Occurred())
472 return NULL;
473 if (value != -1)
474 return raiseTestError("test_long_and_overflow",
475 "return value was not set to -1");
476 if (overflow != 1)
477 return raiseTestError("test_long_and_overflow",
478 "overflow was not set to 1");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 /* Same again, with num = LONG_MAX + 1 */
481 num = PyLong_FromLong(LONG_MAX);
482 if (num == NULL)
483 return NULL;
484 one = PyLong_FromLong(1L);
485 if (one == NULL) {
486 Py_DECREF(num);
487 return NULL;
488 }
489 temp = PyNumber_Add(num, one);
490 Py_DECREF(one);
491 Py_DECREF(num);
492 num = temp;
493 if (num == NULL)
494 return NULL;
495 overflow = 0;
496 value = PyLong_AsLongAndOverflow(num, &overflow);
497 Py_DECREF(num);
498 if (value == -1 && PyErr_Occurred())
499 return NULL;
500 if (value != -1)
501 return raiseTestError("test_long_and_overflow",
502 "return value was not set to -1");
503 if (overflow != 1)
504 return raiseTestError("test_long_and_overflow",
505 "overflow was not set to 1");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 /* Test that overflow is set properly for a large negative value. */
508 /* num is a number smaller than LONG_MIN even on 64-bit platforms */
509 num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
510 if (num == NULL)
511 return NULL;
512 overflow = 1234;
513 value = PyLong_AsLongAndOverflow(num, &overflow);
514 Py_DECREF(num);
515 if (value == -1 && PyErr_Occurred())
516 return NULL;
517 if (value != -1)
518 return raiseTestError("test_long_and_overflow",
519 "return value was not set to -1");
520 if (overflow != -1)
521 return raiseTestError("test_long_and_overflow",
522 "overflow was not set to -1");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 /* Same again, with num = LONG_MIN - 1 */
525 num = PyLong_FromLong(LONG_MIN);
526 if (num == NULL)
527 return NULL;
528 one = PyLong_FromLong(1L);
529 if (one == NULL) {
530 Py_DECREF(num);
531 return NULL;
532 }
533 temp = PyNumber_Subtract(num, one);
534 Py_DECREF(one);
535 Py_DECREF(num);
536 num = temp;
537 if (num == NULL)
538 return NULL;
539 overflow = 0;
540 value = PyLong_AsLongAndOverflow(num, &overflow);
541 Py_DECREF(num);
542 if (value == -1 && PyErr_Occurred())
543 return NULL;
544 if (value != -1)
545 return raiseTestError("test_long_and_overflow",
546 "return value was not set to -1");
547 if (overflow != -1)
548 return raiseTestError("test_long_and_overflow",
549 "overflow was not set to -1");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 /* Test that overflow is cleared properly for small values. */
552 num = PyLong_FromString("FF", NULL, 16);
553 if (num == NULL)
554 return NULL;
555 overflow = 1234;
556 value = PyLong_AsLongAndOverflow(num, &overflow);
557 Py_DECREF(num);
558 if (value == -1 && PyErr_Occurred())
559 return NULL;
560 if (value != 0xFF)
561 return raiseTestError("test_long_and_overflow",
562 "expected return value 0xFF");
563 if (overflow != 0)
564 return raiseTestError("test_long_and_overflow",
565 "overflow was not cleared");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 num = PyLong_FromString("-FF", NULL, 16);
568 if (num == NULL)
569 return NULL;
570 overflow = 0;
571 value = PyLong_AsLongAndOverflow(num, &overflow);
572 Py_DECREF(num);
573 if (value == -1 && PyErr_Occurred())
574 return NULL;
575 if (value != -0xFF)
576 return raiseTestError("test_long_and_overflow",
577 "expected return value 0xFF");
578 if (overflow != 0)
579 return raiseTestError("test_long_and_overflow",
580 "overflow was set incorrectly");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 num = PyLong_FromLong(LONG_MAX);
583 if (num == NULL)
584 return NULL;
585 overflow = 1234;
586 value = PyLong_AsLongAndOverflow(num, &overflow);
587 Py_DECREF(num);
588 if (value == -1 && PyErr_Occurred())
589 return NULL;
590 if (value != LONG_MAX)
591 return raiseTestError("test_long_and_overflow",
592 "expected return value LONG_MAX");
593 if (overflow != 0)
594 return raiseTestError("test_long_and_overflow",
595 "overflow was not cleared");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 num = PyLong_FromLong(LONG_MIN);
598 if (num == NULL)
599 return NULL;
600 overflow = 0;
601 value = PyLong_AsLongAndOverflow(num, &overflow);
602 Py_DECREF(num);
603 if (value == -1 && PyErr_Occurred())
604 return NULL;
605 if (value != LONG_MIN)
606 return raiseTestError("test_long_and_overflow",
607 "expected return value LONG_MIN");
608 if (overflow != 0)
609 return raiseTestError("test_long_and_overflow",
610 "overflow was not cleared");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 Py_INCREF(Py_None);
613 return Py_None;
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000614}
615
Mark Dickinson93f562c2010-01-30 10:30:15 +0000616/* Test the PyLong_AsLongLongAndOverflow API. General conversion to
617 PY_LONG_LONG is tested by test_long_api_inner. This test will
618 concentrate on proper handling of overflow.
619*/
620
621static PyObject *
622test_long_long_and_overflow(PyObject *self)
623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 PyObject *num, *one, *temp;
625 PY_LONG_LONG value;
626 int overflow;
Mark Dickinson93f562c2010-01-30 10:30:15 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 /* Test that overflow is set properly for a large value. */
629 /* num is a number larger than PY_LLONG_MAX on a typical machine. */
630 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
631 if (num == NULL)
632 return NULL;
633 overflow = 1234;
634 value = PyLong_AsLongLongAndOverflow(num, &overflow);
635 Py_DECREF(num);
636 if (value == -1 && PyErr_Occurred())
637 return NULL;
638 if (value != -1)
639 return raiseTestError("test_long_long_and_overflow",
640 "return value was not set to -1");
641 if (overflow != 1)
642 return raiseTestError("test_long_long_and_overflow",
643 "overflow was not set to 1");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 /* Same again, with num = PY_LLONG_MAX + 1 */
646 num = PyLong_FromLongLong(PY_LLONG_MAX);
647 if (num == NULL)
648 return NULL;
649 one = PyLong_FromLong(1L);
650 if (one == NULL) {
651 Py_DECREF(num);
652 return NULL;
653 }
654 temp = PyNumber_Add(num, one);
655 Py_DECREF(one);
656 Py_DECREF(num);
657 num = temp;
658 if (num == NULL)
659 return NULL;
660 overflow = 0;
661 value = PyLong_AsLongLongAndOverflow(num, &overflow);
662 Py_DECREF(num);
663 if (value == -1 && PyErr_Occurred())
664 return NULL;
665 if (value != -1)
666 return raiseTestError("test_long_long_and_overflow",
667 "return value was not set to -1");
668 if (overflow != 1)
669 return raiseTestError("test_long_long_and_overflow",
670 "overflow was not set to 1");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 /* Test that overflow is set properly for a large negative value. */
673 /* num is a number smaller than PY_LLONG_MIN on a typical platform */
674 num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
675 if (num == NULL)
676 return NULL;
677 overflow = 1234;
678 value = PyLong_AsLongLongAndOverflow(num, &overflow);
679 Py_DECREF(num);
680 if (value == -1 && PyErr_Occurred())
681 return NULL;
682 if (value != -1)
683 return raiseTestError("test_long_long_and_overflow",
684 "return value was not set to -1");
685 if (overflow != -1)
686 return raiseTestError("test_long_long_and_overflow",
687 "overflow was not set to -1");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 /* Same again, with num = PY_LLONG_MIN - 1 */
690 num = PyLong_FromLongLong(PY_LLONG_MIN);
691 if (num == NULL)
692 return NULL;
693 one = PyLong_FromLong(1L);
694 if (one == NULL) {
695 Py_DECREF(num);
696 return NULL;
697 }
698 temp = PyNumber_Subtract(num, one);
699 Py_DECREF(one);
700 Py_DECREF(num);
701 num = temp;
702 if (num == NULL)
703 return NULL;
704 overflow = 0;
705 value = PyLong_AsLongLongAndOverflow(num, &overflow);
706 Py_DECREF(num);
707 if (value == -1 && PyErr_Occurred())
708 return NULL;
709 if (value != -1)
710 return raiseTestError("test_long_long_and_overflow",
711 "return value was not set to -1");
712 if (overflow != -1)
713 return raiseTestError("test_long_long_and_overflow",
714 "overflow was not set to -1");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 /* Test that overflow is cleared properly for small values. */
717 num = PyLong_FromString("FF", NULL, 16);
718 if (num == NULL)
719 return NULL;
720 overflow = 1234;
721 value = PyLong_AsLongLongAndOverflow(num, &overflow);
722 Py_DECREF(num);
723 if (value == -1 && PyErr_Occurred())
724 return NULL;
725 if (value != 0xFF)
726 return raiseTestError("test_long_long_and_overflow",
727 "expected return value 0xFF");
728 if (overflow != 0)
729 return raiseTestError("test_long_long_and_overflow",
730 "overflow was not cleared");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 num = PyLong_FromString("-FF", NULL, 16);
733 if (num == NULL)
734 return NULL;
735 overflow = 0;
736 value = PyLong_AsLongLongAndOverflow(num, &overflow);
737 Py_DECREF(num);
738 if (value == -1 && PyErr_Occurred())
739 return NULL;
740 if (value != -0xFF)
741 return raiseTestError("test_long_long_and_overflow",
742 "expected return value 0xFF");
743 if (overflow != 0)
744 return raiseTestError("test_long_long_and_overflow",
745 "overflow was set incorrectly");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 num = PyLong_FromLongLong(PY_LLONG_MAX);
748 if (num == NULL)
749 return NULL;
750 overflow = 1234;
751 value = PyLong_AsLongLongAndOverflow(num, &overflow);
752 Py_DECREF(num);
753 if (value == -1 && PyErr_Occurred())
754 return NULL;
755 if (value != PY_LLONG_MAX)
756 return raiseTestError("test_long_long_and_overflow",
757 "expected return value PY_LLONG_MAX");
758 if (overflow != 0)
759 return raiseTestError("test_long_long_and_overflow",
760 "overflow was not cleared");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 num = PyLong_FromLongLong(PY_LLONG_MIN);
763 if (num == NULL)
764 return NULL;
765 overflow = 0;
766 value = PyLong_AsLongLongAndOverflow(num, &overflow);
767 Py_DECREF(num);
768 if (value == -1 && PyErr_Occurred())
769 return NULL;
770 if (value != PY_LLONG_MIN)
771 return raiseTestError("test_long_long_and_overflow",
772 "expected return value PY_LLONG_MIN");
773 if (overflow != 0)
774 return raiseTestError("test_long_long_and_overflow",
775 "overflow was not cleared");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 Py_INCREF(Py_None);
778 return Py_None;
Mark Dickinson93f562c2010-01-30 10:30:15 +0000779}
780
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000781/* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
Tim Petersd38b1c72001-09-30 05:09:37 +0000782 for both long and int arguments. The test may leak a little memory if
783 it fails.
784*/
785static PyObject *
Fred Drakeacee69f2002-04-01 14:28:58 +0000786test_L_code(PyObject *self)
Tim Petersd38b1c72001-09-30 05:09:37 +0000787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyObject *tuple, *num;
789 PY_LONG_LONG value;
Tim Petersd38b1c72001-09-30 05:09:37 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 tuple = PyTuple_New(1);
792 if (tuple == NULL)
793 return NULL;
Tim Petersd38b1c72001-09-30 05:09:37 +0000794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 num = PyLong_FromLong(42);
796 if (num == NULL)
797 return NULL;
Tim Petersd38b1c72001-09-30 05:09:37 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 PyTuple_SET_ITEM(tuple, 0, num);
Tim Petersd38b1c72001-09-30 05:09:37 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 value = -1;
802 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
803 return NULL;
804 if (value != 42)
805 return raiseTestError("test_L_code",
806 "L code returned wrong value for long 42");
Tim Petersd38b1c72001-09-30 05:09:37 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 Py_DECREF(num);
809 num = PyLong_FromLong(42);
810 if (num == NULL)
811 return NULL;
Tim Petersd38b1c72001-09-30 05:09:37 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 PyTuple_SET_ITEM(tuple, 0, num);
Tim Petersd38b1c72001-09-30 05:09:37 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 value = -1;
816 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
817 return NULL;
818 if (value != 42)
819 return raiseTestError("test_L_code",
820 "L code returned wrong value for int 42");
Tim Petersd38b1c72001-09-30 05:09:37 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 Py_DECREF(tuple);
823 Py_INCREF(Py_None);
824 return Py_None;
Tim Petersd38b1c72001-09-30 05:09:37 +0000825}
826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827#endif /* ifdef HAVE_LONG_LONG */
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000828
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000829/* Test tuple argument processing */
830static PyObject *
831getargs_tuple(PyObject *self, PyObject *args)
832{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 int a, b, c;
834 if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
835 return NULL;
836 return Py_BuildValue("iii", a, b, c);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000837}
838
Christian Heimes380f7f22008-02-28 11:19:05 +0000839/* test PyArg_ParseTupleAndKeywords */
840static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
843 static char *fmt="(ii)i|(i(ii))(iii)i";
844 int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
Christian Heimes380f7f22008-02-28 11:19:05 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
847 &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
848 &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
849 return NULL;
850 return Py_BuildValue("iiiiiiiiii",
851 int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
852 int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
Christian Heimes380f7f22008-02-28 11:19:05 +0000853}
854
Thomas Heller3457e4b2003-04-24 16:14:27 +0000855/* Functions to call PyArg_ParseTuple with integer format codes,
856 and return the result.
857*/
Thomas Hellera4ea6032003-04-17 18:55:45 +0000858static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000859getargs_b(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 unsigned char value;
862 if (!PyArg_ParseTuple(args, "b", &value))
863 return NULL;
864 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000865}
866
Thomas Heller3457e4b2003-04-24 16:14:27 +0000867static PyObject *
868getargs_B(PyObject *self, PyObject *args)
869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 unsigned char value;
871 if (!PyArg_ParseTuple(args, "B", &value))
872 return NULL;
873 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +0000874}
875
876static PyObject *
Mark Dickinson1554b182009-12-20 16:03:30 +0000877getargs_h(PyObject *self, PyObject *args)
878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 short value;
880 if (!PyArg_ParseTuple(args, "h", &value))
881 return NULL;
882 return PyLong_FromLong((long)value);
Mark Dickinson1554b182009-12-20 16:03:30 +0000883}
884
885static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000886getargs_H(PyObject *self, PyObject *args)
887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 unsigned short value;
889 if (!PyArg_ParseTuple(args, "H", &value))
890 return NULL;
891 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +0000892}
893
894static PyObject *
895getargs_I(PyObject *self, PyObject *args)
896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 unsigned int value;
898 if (!PyArg_ParseTuple(args, "I", &value))
899 return NULL;
900 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +0000901}
902
903static PyObject *
904getargs_k(PyObject *self, PyObject *args)
905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 unsigned long value;
907 if (!PyArg_ParseTuple(args, "k", &value))
908 return NULL;
909 return PyLong_FromUnsignedLong(value);
Thomas Heller3457e4b2003-04-24 16:14:27 +0000910}
911
912static PyObject *
913getargs_i(PyObject *self, PyObject *args)
914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 int value;
916 if (!PyArg_ParseTuple(args, "i", &value))
917 return NULL;
918 return PyLong_FromLong((long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +0000919}
920
Thomas Hellera4ea6032003-04-17 18:55:45 +0000921static PyObject *
922getargs_l(PyObject *self, PyObject *args)
923{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 long value;
925 if (!PyArg_ParseTuple(args, "l", &value))
926 return NULL;
927 return PyLong_FromLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000928}
929
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000930static PyObject *
931getargs_n(PyObject *self, PyObject *args)
932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 Py_ssize_t value;
934 if (!PyArg_ParseTuple(args, "n", &value))
935 return NULL;
936 return PyLong_FromSsize_t(value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000937}
938
Thomas Hellera4ea6032003-04-17 18:55:45 +0000939#ifdef HAVE_LONG_LONG
Thomas Hellera4ea6032003-04-17 18:55:45 +0000940static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000941getargs_L(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 PY_LONG_LONG value;
944 if (!PyArg_ParseTuple(args, "L", &value))
945 return NULL;
946 return PyLong_FromLongLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000947}
948
Thomas Hellera4ea6032003-04-17 18:55:45 +0000949static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +0000950getargs_K(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 unsigned PY_LONG_LONG value;
953 if (!PyArg_ParseTuple(args, "K", &value))
954 return NULL;
955 return PyLong_FromUnsignedLongLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000956}
957#endif
958
959/* This function not only tests the 'k' getargs code, but also the
Christian Heimes217cfd12007-12-02 14:31:20 +0000960 PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */
Thomas Hellera4ea6032003-04-17 18:55:45 +0000961static PyObject *
962test_k_code(PyObject *self)
963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 PyObject *tuple, *num;
965 unsigned long value;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 tuple = PyTuple_New(1);
968 if (tuple == NULL)
969 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 /* a number larger than ULONG_MAX even on 64-bit platforms */
972 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
973 if (num == NULL)
974 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 value = PyLong_AsUnsignedLongMask(num);
977 if (value != ULONG_MAX)
978 return raiseTestError("test_k_code",
979 "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
Thomas Hellera4ea6032003-04-17 18:55:45 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 PyTuple_SET_ITEM(tuple, 0, num);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 value = 0;
984 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
985 return NULL;
986 if (value != ULONG_MAX)
987 return raiseTestError("test_k_code",
988 "k code returned wrong value for long 0xFFF...FFF");
Thomas Hellera4ea6032003-04-17 18:55:45 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 Py_DECREF(num);
991 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
992 if (num == NULL)
993 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 value = PyLong_AsUnsignedLongMask(num);
996 if (value != (unsigned long)-0x42)
997 return raiseTestError("test_k_code",
998 "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
Thomas Hellera4ea6032003-04-17 18:55:45 +0000999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 PyTuple_SET_ITEM(tuple, 0, num);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 value = 0;
1003 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
1004 return NULL;
1005 if (value != (unsigned long)-0x42)
1006 return raiseTestError("test_k_code",
1007 "k code returned wrong value for long -0xFFF..000042");
Thomas Hellera4ea6032003-04-17 18:55:45 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Py_DECREF(tuple);
1010 Py_INCREF(Py_None);
1011 return Py_None;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001012}
1013
Victor Stinner06e49dd2010-06-13 18:21:50 +00001014static PyObject *
1015getargs_s(PyObject *self, PyObject *args)
1016{
1017 char *str;
1018 if (!PyArg_ParseTuple(args, "s", &str))
1019 return NULL;
1020 return PyBytes_FromString(str);
1021}
1022
1023static PyObject *
1024getargs_s_star(PyObject *self, PyObject *args)
1025{
1026 Py_buffer buffer;
1027 PyObject *bytes;
1028 if (!PyArg_ParseTuple(args, "s*", &buffer))
1029 return NULL;
1030 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1031 PyBuffer_Release(&buffer);
1032 return bytes;
1033}
1034
1035static PyObject *
1036getargs_s_hash(PyObject *self, PyObject *args)
1037{
1038 char *str;
1039 Py_ssize_t size;
1040 if (!PyArg_ParseTuple(args, "s#", &str, &size))
1041 return NULL;
1042 return PyBytes_FromStringAndSize(str, size);
1043}
1044
1045static PyObject *
1046getargs_z(PyObject *self, PyObject *args)
1047{
1048 char *str;
1049 if (!PyArg_ParseTuple(args, "z", &str))
1050 return NULL;
1051 if (str != NULL)
1052 return PyBytes_FromString(str);
1053 else
1054 Py_RETURN_NONE;
1055}
1056
1057static PyObject *
1058getargs_z_star(PyObject *self, PyObject *args)
1059{
1060 Py_buffer buffer;
1061 PyObject *bytes;
1062 if (!PyArg_ParseTuple(args, "z*", &buffer))
1063 return NULL;
1064 if (buffer.buf != NULL)
1065 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1066 else {
1067 Py_INCREF(Py_None);
1068 bytes = Py_None;
1069 }
1070 PyBuffer_Release(&buffer);
1071 return bytes;
1072}
1073
1074static PyObject *
1075getargs_z_hash(PyObject *self, PyObject *args)
1076{
1077 char *str;
1078 Py_ssize_t size;
1079 if (!PyArg_ParseTuple(args, "z#", &str, &size))
1080 return NULL;
1081 if (str != NULL)
1082 return PyBytes_FromStringAndSize(str, size);
1083 else
1084 Py_RETURN_NONE;
1085}
1086
1087static PyObject *
1088getargs_y(PyObject *self, PyObject *args)
1089{
1090 char *str;
1091 if (!PyArg_ParseTuple(args, "y", &str))
1092 return NULL;
1093 return PyBytes_FromString(str);
1094}
1095
1096static PyObject *
1097getargs_y_star(PyObject *self, PyObject *args)
1098{
1099 Py_buffer buffer;
1100 PyObject *bytes;
1101 if (!PyArg_ParseTuple(args, "y*", &buffer))
1102 return NULL;
1103 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1104 PyBuffer_Release(&buffer);
1105 return bytes;
1106}
1107
1108static PyObject *
1109getargs_y_hash(PyObject *self, PyObject *args)
1110{
1111 char *str;
1112 Py_ssize_t size;
1113 if (!PyArg_ParseTuple(args, "y#", &str, &size))
1114 return NULL;
1115 return PyBytes_FromStringAndSize(str, size);
1116}
1117
1118static PyObject *
1119getargs_u(PyObject *self, PyObject *args)
1120{
1121 Py_UNICODE *str;
1122 Py_ssize_t size;
1123 if (!PyArg_ParseTuple(args, "u", &str))
1124 return NULL;
1125 size = Py_UNICODE_strlen(str);
1126 return PyUnicode_FromUnicode(str, size);
1127}
1128
1129static PyObject *
1130getargs_u_hash(PyObject *self, PyObject *args)
1131{
1132 Py_UNICODE *str;
1133 Py_ssize_t size;
1134 if (!PyArg_ParseTuple(args, "u#", &str, &size))
1135 return NULL;
1136 return PyUnicode_FromUnicode(str, size);
1137}
1138
1139static PyObject *
1140getargs_Z(PyObject *self, PyObject *args)
1141{
1142 Py_UNICODE *str;
1143 Py_ssize_t size;
1144 if (!PyArg_ParseTuple(args, "Z", &str))
1145 return NULL;
1146 if (str != NULL) {
1147 size = Py_UNICODE_strlen(str);
1148 return PyUnicode_FromUnicode(str, size);
1149 } else
1150 Py_RETURN_NONE;
1151}
1152
1153static PyObject *
1154getargs_Z_hash(PyObject *self, PyObject *args)
1155{
1156 Py_UNICODE *str;
1157 Py_ssize_t size;
1158 if (!PyArg_ParseTuple(args, "Z#", &str, &size))
1159 return NULL;
1160 if (str != NULL)
1161 return PyUnicode_FromUnicode(str, size);
1162 else
1163 Py_RETURN_NONE;
1164}
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001165
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001166/* Test the s and z codes for PyArg_ParseTuple.
1167*/
1168static PyObject *
1169test_s_code(PyObject *self)
1170{
1171 /* Unicode strings should be accepted */
1172 PyObject *tuple, *obj;
1173 char *value;
1174
1175 tuple = PyTuple_New(1);
1176 if (tuple == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 return NULL;
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001178
1179 obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 "latin-1", NULL);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001181 if (obj == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 return NULL;
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001183
1184 PyTuple_SET_ITEM(tuple, 0, obj);
1185
1186 /* These two blocks used to raise a TypeError:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 * "argument must be string without null bytes, not str"
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001188 */
1189 if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 return NULL;
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001191
1192 if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 return NULL;
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001194
Alexandre Vassalottib645bc72008-05-15 22:06:59 +00001195 Py_DECREF(tuple);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001196 Py_RETURN_NONE;
1197}
1198
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001199static PyObject *
1200test_bug_7414(PyObject *self)
1201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 /* Issue #7414: for PyArg_ParseTupleAndKeywords, 'C' code wasn't being
1203 skipped properly in skipitem() */
1204 int a = 0, b = 0, result;
1205 char *kwlist[] = {"a", "b", NULL};
1206 PyObject *tuple = NULL, *dict = NULL, *b_str;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 tuple = PyTuple_New(0);
1209 if (tuple == NULL)
1210 goto failure;
1211 dict = PyDict_New();
1212 if (dict == NULL)
1213 goto failure;
1214 b_str = PyUnicode_FromString("b");
1215 if (b_str == NULL)
1216 goto failure;
1217 result = PyDict_SetItemString(dict, "b", b_str);
1218 Py_DECREF(b_str);
1219 if (result < 0)
1220 goto failure;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 result = PyArg_ParseTupleAndKeywords(tuple, dict, "|CC",
1223 kwlist, &a, &b);
1224 if (!result)
1225 goto failure;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 if (a != 0)
1228 return raiseTestError("test_bug_7414",
1229 "C format code not skipped properly");
1230 if (b != 'b')
1231 return raiseTestError("test_bug_7414",
1232 "C format code returned wrong value");
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 Py_DECREF(dict);
1235 Py_DECREF(tuple);
1236 Py_RETURN_NONE;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001237
1238 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 Py_XDECREF(dict);
1240 Py_XDECREF(tuple);
1241 return NULL;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001242}
1243
1244
Benjamin Peterson92035012008-12-27 16:00:54 +00001245static volatile int x;
1246
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001247/* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
1248 of an error.
1249*/
1250static PyObject *
Fred Drakeacee69f2002-04-01 14:28:58 +00001251test_u_code(PyObject *self)
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 PyObject *tuple, *obj;
1254 Py_UNICODE *value;
1255 Py_ssize_t len;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
1258 /* Just use the macro and check that it compiles */
1259 x = Py_UNICODE_ISSPACE(25);
Benjamin Peterson206e3072008-10-19 14:07:49 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 tuple = PyTuple_New(1);
1262 if (tuple == NULL)
1263 return NULL;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 obj = PyUnicode_Decode("test", strlen("test"),
1266 "ascii", NULL);
1267 if (obj == NULL)
1268 return NULL;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 PyTuple_SET_ITEM(tuple, 0, obj);
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 value = 0;
1273 if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
1274 return NULL;
1275 if (value != PyUnicode_AS_UNICODE(obj))
1276 return raiseTestError("test_u_code",
1277 "u code returned wrong value for u'test'");
1278 value = 0;
1279 if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
1280 return NULL;
1281 if (value != PyUnicode_AS_UNICODE(obj) ||
1282 len != PyUnicode_GET_SIZE(obj))
1283 return raiseTestError("test_u_code",
1284 "u# code returned wrong values for u'test'");
Tim Petersbaefd9e2003-01-28 20:37:45 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 Py_DECREF(tuple);
1287 Py_INCREF(Py_None);
1288 return Py_None;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001289}
1290
Guido van Rossumfb67be22007-08-29 18:38:11 +00001291/* Test Z and Z# codes for PyArg_ParseTuple */
1292static PyObject *
1293test_Z_code(PyObject *self)
1294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 PyObject *tuple, *obj;
1296 Py_UNICODE *value1, *value2;
1297 Py_ssize_t len1, len2;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 tuple = PyTuple_New(2);
1300 if (tuple == NULL)
1301 return NULL;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 obj = PyUnicode_FromString("test");
1304 PyTuple_SET_ITEM(tuple, 0, obj);
1305 Py_INCREF(Py_None);
1306 PyTuple_SET_ITEM(tuple, 1, Py_None);
Guido van Rossumfb67be22007-08-29 18:38:11 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /* swap values on purpose */
1309 value1 = NULL;
1310 value2 = PyUnicode_AS_UNICODE(obj);
Guido van Rossumfb67be22007-08-29 18:38:11 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 /* Test Z for both values */
1313 if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0)
1314 return NULL;
1315 if (value1 != PyUnicode_AS_UNICODE(obj))
1316 return raiseTestError("test_Z_code",
1317 "Z code returned wrong value for 'test'");
1318 if (value2 != NULL)
1319 return raiseTestError("test_Z_code",
1320 "Z code returned wrong value for None");
Guido van Rossumfb67be22007-08-29 18:38:11 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 value1 = NULL;
1323 value2 = PyUnicode_AS_UNICODE(obj);
1324 len1 = -1;
1325 len2 = -1;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 /* Test Z# for both values */
1328 if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1,
1329 &value2, &len2) < 0)
1330 return NULL;
1331 if (value1 != PyUnicode_AS_UNICODE(obj) ||
1332 len1 != PyUnicode_GET_SIZE(obj))
1333 return raiseTestError("test_Z_code",
1334 "Z# code returned wrong values for 'test'");
1335 if (value2 != NULL ||
1336 len2 != 0)
1337 return raiseTestError("test_Z_code",
1338 "Z# code returned wrong values for None'");
Guido van Rossumfb67be22007-08-29 18:38:11 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 Py_DECREF(tuple);
1341 Py_RETURN_NONE;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001342}
1343
Thomas Wouters477c8d52006-05-27 19:21:47 +00001344static PyObject *
Mark Dickinson081dfee2009-03-18 14:47:41 +00001345test_widechar(PyObject *self)
1346{
1347#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
1349 size_t wtextlen = 1;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001350#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
1352 size_t wtextlen = 2;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001353#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 PyObject *wide, *utf8;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 wide = PyUnicode_FromWideChar(wtext, wtextlen);
1357 if (wide == NULL)
1358 return NULL;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d");
1361 if (utf8 == NULL) {
1362 Py_DECREF(wide);
1363 return NULL;
1364 }
Mark Dickinson081dfee2009-03-18 14:47:41 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) {
1367 Py_DECREF(wide);
1368 Py_DECREF(utf8);
1369 return raiseTestError("test_widechar",
1370 "wide string and utf8 string "
1371 "have different length");
1372 }
1373 if (PyUnicode_Compare(wide, utf8)) {
1374 Py_DECREF(wide);
1375 Py_DECREF(utf8);
1376 if (PyErr_Occurred())
1377 return NULL;
1378 return raiseTestError("test_widechar",
1379 "wide string and utf8 string "
1380 "are different");
1381 }
Mark Dickinson081dfee2009-03-18 14:47:41 +00001382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 Py_DECREF(wide);
1384 Py_DECREF(utf8);
1385 Py_RETURN_NONE;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001386}
1387
1388static PyObject *
Victor Stinner46c7b3b2010-10-02 11:49:31 +00001389unicode_aswidechar(PyObject *self, PyObject *args)
Victor Stinner1c24bd02010-10-02 11:03:13 +00001390{
1391 PyObject *unicode, *result;
1392 Py_ssize_t buflen, size;
1393 wchar_t *buffer;
1394
1395 if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen))
1396 return NULL;
1397 buffer = PyMem_Malloc(buflen * sizeof(wchar_t));
1398 if (buffer == NULL)
1399 return PyErr_NoMemory();
1400
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001401 size = PyUnicode_AsWideChar(unicode, buffer, buflen);
Victor Stinner1c24bd02010-10-02 11:03:13 +00001402 if (size == -1) {
1403 PyMem_Free(buffer);
1404 return NULL;
1405 }
1406
1407 if (size < buflen)
1408 buflen = size + 1;
1409 else
1410 buflen = size;
1411 result = PyUnicode_FromWideChar(buffer, buflen);
1412 PyMem_Free(buffer);
1413 if (result == NULL)
1414 return NULL;
1415
1416 return Py_BuildValue("(Nn)", result, size);
1417}
1418
1419static PyObject *
Victor Stinner46c7b3b2010-10-02 11:49:31 +00001420unicode_aswidecharstring(PyObject *self, PyObject *args)
Victor Stinner1c24bd02010-10-02 11:03:13 +00001421{
1422 PyObject *unicode, *result;
1423 Py_ssize_t size;
1424 wchar_t *buffer;
1425
1426 if (!PyArg_ParseTuple(args, "U", &unicode))
1427 return NULL;
1428
Victor Stinnerbeb4135b2010-10-07 01:02:42 +00001429 buffer = PyUnicode_AsWideCharString(unicode, &size);
Victor Stinner1c24bd02010-10-02 11:03:13 +00001430 if (buffer == NULL)
1431 return NULL;
1432
1433 result = PyUnicode_FromWideChar(buffer, size + 1);
1434 PyMem_Free(buffer);
1435 if (result == NULL)
1436 return NULL;
1437 return Py_BuildValue("(Nn)", result, size);
1438}
1439
1440static PyObject *
Victor Stinner25e8ec42010-06-25 00:02:38 +00001441getargs_w_star(PyObject *self, PyObject *args)
1442{
1443 Py_buffer buffer;
1444 PyObject *result;
1445 char *str;
1446
1447 if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer))
1448 return NULL;
1449
1450 if (2 <= buffer.len) {
1451 str = buffer.buf;
1452 str[0] = '[';
1453 str[buffer.len-1] = ']';
1454 }
1455
1456 result = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1457 PyBuffer_Release(&buffer);
1458 return result;
1459}
1460
1461
1462static PyObject *
Benjamin Peterson92035012008-12-27 16:00:54 +00001463test_empty_argparse(PyObject *self)
1464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 /* Test that formats can begin with '|'. See issue #4720. */
1466 PyObject *tuple, *dict = NULL;
1467 static char *kwlist[] = {NULL};
1468 int result;
1469 tuple = PyTuple_New(0);
1470 if (!tuple)
1471 return NULL;
1472 if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0)
1473 goto done;
1474 dict = PyDict_New();
1475 if (!dict)
1476 goto done;
1477 result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
Benjamin Peterson92035012008-12-27 16:00:54 +00001478 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 Py_DECREF(tuple);
1480 Py_XDECREF(dict);
1481 if (result < 0)
1482 return NULL;
1483 else {
1484 Py_RETURN_NONE;
1485 }
Benjamin Peterson92035012008-12-27 16:00:54 +00001486}
1487
1488static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001489codec_incrementalencoder(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 const char *encoding, *errors = NULL;
1492 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
1493 &encoding, &errors))
1494 return NULL;
1495 return PyCodec_IncrementalEncoder(encoding, errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001496}
1497
Thomas Wouters477c8d52006-05-27 19:21:47 +00001498static PyObject *
1499codec_incrementaldecoder(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 const char *encoding, *errors = NULL;
1502 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
1503 &encoding, &errors))
1504 return NULL;
1505 return PyCodec_IncrementalDecoder(encoding, errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001506}
1507
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001508
Tim Peters5b8132f2003-01-31 15:52:05 +00001509/* Simple test of _PyLong_NumBits and _PyLong_Sign. */
Tim Petersbaefd9e2003-01-28 20:37:45 +00001510static PyObject *
1511test_long_numbits(PyObject *self)
1512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 struct triple {
1514 long input;
1515 size_t nbits;
1516 int sign;
1517 } testcases[] = {{0, 0, 0},
1518 {1L, 1, 1},
1519 {-1L, 1, -1},
1520 {2L, 2, 1},
1521 {-2L, 2, -1},
1522 {3L, 2, 1},
1523 {-3L, 2, -1},
1524 {4L, 3, 1},
1525 {-4L, 3, -1},
1526 {0x7fffL, 15, 1}, /* one Python long digit */
1527 {-0x7fffL, 15, -1},
1528 {0xffffL, 16, 1},
1529 {-0xffffL, 16, -1},
1530 {0xfffffffL, 28, 1},
1531 {-0xfffffffL, 28, -1}};
1532 int i;
Tim Petersbaefd9e2003-01-28 20:37:45 +00001533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
1535 PyObject *plong = PyLong_FromLong(testcases[i].input);
1536 size_t nbits = _PyLong_NumBits(plong);
1537 int sign = _PyLong_Sign(plong);
Tim Petersbaefd9e2003-01-28 20:37:45 +00001538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 Py_DECREF(plong);
1540 if (nbits != testcases[i].nbits)
1541 return raiseTestError("test_long_numbits",
1542 "wrong result for _PyLong_NumBits");
1543 if (sign != testcases[i].sign)
1544 return raiseTestError("test_long_numbits",
1545 "wrong result for _PyLong_Sign");
1546 }
1547 Py_INCREF(Py_None);
1548 return Py_None;
Tim Petersbaefd9e2003-01-28 20:37:45 +00001549}
1550
Thomas Heller519a0422007-11-15 20:48:54 +00001551/* Example passing NULLs to PyObject_Str(NULL). */
Thomas Woutersa9773292006-04-21 09:43:23 +00001552
1553static PyObject *
1554test_null_strings(PyObject *self)
1555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL);
1557 PyObject *tuple = PyTuple_Pack(2, o1, o2);
1558 Py_XDECREF(o1);
1559 Py_XDECREF(o2);
1560 return tuple;
Thomas Woutersa9773292006-04-21 09:43:23 +00001561}
1562
Jeremy Hyltonede049b2001-09-26 20:01:13 +00001563static PyObject *
1564raise_exception(PyObject *self, PyObject *args)
1565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 PyObject *exc;
1567 PyObject *exc_args, *v;
1568 int num_args, i;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00001569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
1571 &exc, &num_args))
1572 return NULL;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00001573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 exc_args = PyTuple_New(num_args);
1575 if (exc_args == NULL)
1576 return NULL;
1577 for (i = 0; i < num_args; ++i) {
1578 v = PyLong_FromLong(i);
1579 if (v == NULL) {
1580 Py_DECREF(exc_args);
1581 return NULL;
1582 }
1583 PyTuple_SET_ITEM(exc_args, i, v);
1584 }
1585 PyErr_SetObject(exc, exc_args);
1586 Py_DECREF(exc_args);
1587 return NULL;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00001588}
Tim Peters91621db2001-06-12 20:10:01 +00001589
Benjamin Peterson16323982010-02-03 01:13:41 +00001590
1591static int test_run_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001592
Benjamin Peterson91d58bd2009-12-13 21:30:54 +00001593static PyObject *
1594test_datetime_capi(PyObject *self, PyObject *args) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 if (PyDateTimeAPI) {
1596 if (test_run_counter) {
1597 /* Probably regrtest.py -R */
1598 Py_RETURN_NONE;
1599 }
1600 else {
1601 PyErr_SetString(PyExc_AssertionError,
1602 "PyDateTime_CAPI somehow initialized");
1603 return NULL;
1604 }
1605 }
1606 test_run_counter++;
1607 PyDateTime_IMPORT;
1608 if (PyDateTimeAPI)
1609 Py_RETURN_NONE;
1610 else
1611 return NULL;
Benjamin Peterson91d58bd2009-12-13 21:30:54 +00001612}
1613
Benjamin Peterson16323982010-02-03 01:13:41 +00001614
1615#ifdef WITH_THREAD
1616
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001617/* test_thread_state spawns a thread of its own, and that thread releases
1618 * `thread_done` when it's finished. The driver code has to know when the
1619 * thread finishes, because the thread uses a PyObject (the callable) that
1620 * may go away when the driver finishes. The former lack of this explicit
1621 * synchronization caused rare segfaults, so rare that they were seen only
1622 * on a Mac buildbot (although they were possible on any box).
1623 */
1624static PyThread_type_lock thread_done = NULL;
1625
Benjamin Petersona786b022008-08-25 21:05:21 +00001626static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001627_make_call(void *callable)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 PyObject *rc;
1630 int success;
1631 PyGILState_STATE s = PyGILState_Ensure();
1632 rc = PyObject_CallFunction((PyObject *)callable, "");
1633 success = (rc != NULL);
1634 Py_XDECREF(rc);
1635 PyGILState_Release(s);
1636 return success;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001637}
1638
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001639/* Same thing, but releases `thread_done` when it returns. This variant
1640 * should be called only from threads spawned by test_thread_state().
1641 */
1642static void
1643_make_call_from_thread(void *callable)
1644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 _make_call(callable);
1646 PyThread_release_lock(thread_done);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001647}
1648
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001649static PyObject *
1650test_thread_state(PyObject *self, PyObject *args)
1651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 PyObject *fn;
1653 int success = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
1656 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 if (!PyCallable_Check(fn)) {
1659 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
1660 fn->ob_type->tp_name);
1661 return NULL;
1662 }
Benjamin Petersona786b022008-08-25 21:05:21 +00001663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 /* Ensure Python is set up for threading */
1665 PyEval_InitThreads();
1666 thread_done = PyThread_allocate_lock();
1667 if (thread_done == NULL)
1668 return PyErr_NoMemory();
1669 PyThread_acquire_lock(thread_done, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 /* Start a new thread with our callback. */
1672 PyThread_start_new_thread(_make_call_from_thread, fn);
1673 /* Make the callback with the thread lock held by this thread */
1674 success &= _make_call(fn);
1675 /* Do it all again, but this time with the thread-lock released */
1676 Py_BEGIN_ALLOW_THREADS
1677 success &= _make_call(fn);
1678 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
1679 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 /* And once more with and without a thread
1682 XXX - should use a lock and work out exactly what we are trying
1683 to test <wink>
1684 */
1685 Py_BEGIN_ALLOW_THREADS
1686 PyThread_start_new_thread(_make_call_from_thread, fn);
1687 success &= _make_call(fn);
1688 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
1689 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 /* Release lock we acquired above. This is required on HP-UX. */
1692 PyThread_release_lock(thread_done);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 PyThread_free_lock(thread_done);
1695 if (!success)
1696 return NULL;
1697 Py_RETURN_NONE;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001698}
Benjamin Petersona54c9092009-01-13 02:11:23 +00001699
1700/* test Py_AddPendingCalls using threads */
1701static int _pending_callback(void *arg)
1702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 /* we assume the argument is callable object to which we own a reference */
1704 PyObject *callable = (PyObject *)arg;
1705 PyObject *r = PyObject_CallObject(callable, NULL);
1706 Py_DECREF(callable);
1707 Py_XDECREF(r);
1708 return r != NULL ? 0 : -1;
Benjamin Petersona54c9092009-01-13 02:11:23 +00001709}
1710
1711/* The following requests n callbacks to _pending_callback. It can be
1712 * run from any python thread.
1713 */
1714PyObject *pending_threadfunc(PyObject *self, PyObject *arg)
1715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 PyObject *callable;
1717 int r;
1718 if (PyArg_ParseTuple(arg, "O", &callable) == 0)
1719 return NULL;
Benjamin Petersona54c9092009-01-13 02:11:23 +00001720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 /* create the reference for the callbackwhile we hold the lock */
1722 Py_INCREF(callable);
Benjamin Petersona54c9092009-01-13 02:11:23 +00001723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 Py_BEGIN_ALLOW_THREADS
1725 r = Py_AddPendingCall(&_pending_callback, callable);
1726 Py_END_ALLOW_THREADS
Benjamin Petersona54c9092009-01-13 02:11:23 +00001727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 if (r<0) {
1729 Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
1730 Py_INCREF(Py_False);
1731 return Py_False;
1732 }
1733 Py_INCREF(Py_True);
1734 return Py_True;
Benjamin Petersona54c9092009-01-13 02:11:23 +00001735}
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001736#endif
1737
Neal Norwitzb0d26332007-08-25 00:49:05 +00001738/* Some tests of PyUnicode_FromFormat(). This needs more tests. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001739static PyObject *
1740test_string_from_format(PyObject *self, PyObject *args)
1741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 PyObject *result;
1743 char *msg;
Alexander Belopolskye239d232010-12-08 23:31:48 +00001744 static const Py_UNICODE one[] = {'1', 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00001745
Alexander Belopolskye239d232010-12-08 23:31:48 +00001746#define CHECK_1_FORMAT(FORMAT, TYPE) \
1747 result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \
1748 if (result == NULL) \
1749 return NULL; \
1750 if (Py_UNICODE_strcmp(PyUnicode_AS_UNICODE(result), one)) { \
1751 msg = FORMAT " failed at 1"; \
1752 goto Fail; \
1753 } \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 Py_DECREF(result)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 CHECK_1_FORMAT("%d", int);
1757 CHECK_1_FORMAT("%ld", long);
1758 /* The z width modifier was added in Python 2.5. */
1759 CHECK_1_FORMAT("%zd", Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 /* The u type code was added in Python 2.5. */
1762 CHECK_1_FORMAT("%u", unsigned int);
1763 CHECK_1_FORMAT("%lu", unsigned long);
1764 CHECK_1_FORMAT("%zu", size_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 /* "%lld" and "%llu" support added in Python 2.7. */
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00001767#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG);
1769 CHECK_1_FORMAT("%lld", PY_LONG_LONG);
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00001770#endif
1771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001773
1774 Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 Py_XDECREF(result);
1776 return raiseTestError("test_string_from_format", msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001777
1778#undef CHECK_1_FORMAT
1779}
1780
Benjamin Peterson8667a9b2010-01-09 21:45:28 +00001781
1782static PyObject *
1783test_unicode_compare_with_ascii(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4);
1785 int result;
1786 if (py_s == NULL)
1787 return NULL;
1788 result = PyUnicode_CompareWithASCIIString(py_s, "str");
1789 Py_DECREF(py_s);
1790 if (!result) {
1791 PyErr_SetString(TestError, "Python string ending in NULL "
1792 "should not compare equal to c string.");
1793 return NULL;
1794 }
1795 Py_RETURN_NONE;
Victor Stinner3e2b7172010-11-09 09:32:19 +00001796}
Benjamin Peterson8667a9b2010-01-09 21:45:28 +00001797
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001798/* This is here to provide a docstring for test_descr. */
1799static PyObject *
1800test_with_docstring(PyObject *self)
1801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001803}
1804
Mark Dickinson725bfd82009-05-03 20:33:40 +00001805/* Test PyOS_string_to_double. */
1806static PyObject *
1807test_string_to_double(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 double result;
1809 char *msg;
Mark Dickinson725bfd82009-05-03 20:33:40 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811#define CHECK_STRING(STR, expected) \
1812 result = PyOS_string_to_double(STR, NULL, NULL); \
1813 if (result == -1.0 && PyErr_Occurred()) \
1814 return NULL; \
1815 if (result != expected) { \
1816 msg = "conversion of " STR " to float failed"; \
1817 goto fail; \
1818 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00001819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820#define CHECK_INVALID(STR) \
1821 result = PyOS_string_to_double(STR, NULL, NULL); \
1822 if (result == -1.0 && PyErr_Occurred()) { \
1823 if (PyErr_ExceptionMatches(PyExc_ValueError)) \
1824 PyErr_Clear(); \
1825 else \
1826 return NULL; \
1827 } \
1828 else { \
1829 msg = "conversion of " STR " didn't raise ValueError"; \
1830 goto fail; \
1831 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00001832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 CHECK_STRING("0.1", 0.1);
1834 CHECK_STRING("1.234", 1.234);
1835 CHECK_STRING("-1.35", -1.35);
1836 CHECK_STRING(".1e01", 1.0);
1837 CHECK_STRING("2.e-2", 0.02);
Mark Dickinson725bfd82009-05-03 20:33:40 +00001838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 CHECK_INVALID(" 0.1");
1840 CHECK_INVALID("\t\n-3");
1841 CHECK_INVALID(".123 ");
1842 CHECK_INVALID("3\n");
1843 CHECK_INVALID("123abc");
Mark Dickinson725bfd82009-05-03 20:33:40 +00001844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 Py_RETURN_NONE;
Mark Dickinson725bfd82009-05-03 20:33:40 +00001846 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 return raiseTestError("test_string_to_double", msg);
Mark Dickinson725bfd82009-05-03 20:33:40 +00001848#undef CHECK_STRING
1849#undef CHECK_INVALID
1850}
1851
1852
Benjamin Petersonb173f782009-05-05 22:31:58 +00001853/* Coverage testing of capsule objects. */
1854
1855static const char *capsule_name = "capsule name";
1856static char *capsule_pointer = "capsule pointer";
1857static char *capsule_context = "capsule context";
1858static const char *capsule_error = NULL;
1859static int
1860capsule_destructor_call_count = 0;
1861
1862static void
1863capsule_destructor(PyObject *o) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 capsule_destructor_call_count++;
1865 if (PyCapsule_GetContext(o) != capsule_context) {
1866 capsule_error = "context did not match in destructor!";
1867 } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
1868 capsule_error = "destructor did not match in destructor! (woah!)";
1869 } else if (PyCapsule_GetName(o) != capsule_name) {
1870 capsule_error = "name did not match in destructor!";
1871 } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
1872 capsule_error = "pointer did not match in destructor!";
1873 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00001874}
1875
1876typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 char *name;
1878 char *module;
1879 char *attribute;
Benjamin Petersonb173f782009-05-05 22:31:58 +00001880} known_capsule;
1881
1882static PyObject *
1883test_capsule(PyObject *self, PyObject *args)
1884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 PyObject *object;
1886 const char *error = NULL;
1887 void *pointer;
1888 void *pointer2;
1889 known_capsule known_capsules[] = {
1890 #define KNOWN_CAPSULE(module, name) { module "." name, module, name }
1891 KNOWN_CAPSULE("_socket", "CAPI"),
1892 KNOWN_CAPSULE("_curses", "_C_API"),
1893 KNOWN_CAPSULE("datetime", "datetime_CAPI"),
1894 { NULL, NULL },
1895 };
1896 known_capsule *known = &known_capsules[0];
Benjamin Petersonb173f782009-05-05 22:31:58 +00001897
1898#define FAIL(x) { error = (x); goto exit; }
1899
1900#define CHECK_DESTRUCTOR \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 if (capsule_error) { \
1902 FAIL(capsule_error); \
1903 } \
1904 else if (!capsule_destructor_call_count) { \
1905 FAIL("destructor not called!"); \
1906 } \
1907 capsule_destructor_call_count = 0; \
Benjamin Petersonb173f782009-05-05 22:31:58 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
1910 PyCapsule_SetContext(object, capsule_context);
1911 capsule_destructor(object);
1912 CHECK_DESTRUCTOR;
1913 Py_DECREF(object);
1914 CHECK_DESTRUCTOR;
Benjamin Petersonb173f782009-05-05 22:31:58 +00001915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 object = PyCapsule_New(known, "ignored", NULL);
1917 PyCapsule_SetPointer(object, capsule_pointer);
1918 PyCapsule_SetName(object, capsule_name);
1919 PyCapsule_SetDestructor(object, capsule_destructor);
1920 PyCapsule_SetContext(object, capsule_context);
1921 capsule_destructor(object);
1922 CHECK_DESTRUCTOR;
1923 /* intentionally access using the wrong name */
1924 pointer2 = PyCapsule_GetPointer(object, "the wrong name");
1925 if (!PyErr_Occurred()) {
1926 FAIL("PyCapsule_GetPointer should have failed but did not!");
1927 }
1928 PyErr_Clear();
1929 if (pointer2) {
1930 if (pointer2 == capsule_pointer) {
1931 FAIL("PyCapsule_GetPointer should not have"
1932 " returned the internal pointer!");
1933 } else {
1934 FAIL("PyCapsule_GetPointer should have "
1935 "returned NULL pointer but did not!");
1936 }
1937 }
1938 PyCapsule_SetDestructor(object, NULL);
1939 Py_DECREF(object);
1940 if (capsule_destructor_call_count) {
1941 FAIL("destructor called when it should not have been!");
1942 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00001943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 for (known = &known_capsules[0]; known->module != NULL; known++) {
1945 /* yeah, ordinarily I wouldn't do this either,
1946 but it's fine for this test harness.
1947 */
1948 static char buffer[256];
Benjamin Petersonb173f782009-05-05 22:31:58 +00001949#undef FAIL
1950#define FAIL(x) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 { \
1952 sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
1953 x, known->module, known->attribute); \
1954 error = buffer; \
1955 goto exit; \
1956 } \
Benjamin Petersonb173f782009-05-05 22:31:58 +00001957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 PyObject *module = PyImport_ImportModule(known->module);
1959 if (module) {
1960 pointer = PyCapsule_Import(known->name, 0);
1961 if (!pointer) {
1962 Py_DECREF(module);
1963 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
1964 }
1965 object = PyObject_GetAttrString(module, known->attribute);
1966 if (!object) {
1967 Py_DECREF(module);
1968 return NULL;
1969 }
1970 pointer2 = PyCapsule_GetPointer(object,
1971 "weebles wobble but they don't fall down");
1972 if (!PyErr_Occurred()) {
1973 Py_DECREF(object);
1974 Py_DECREF(module);
1975 FAIL("PyCapsule_GetPointer should have failed but did not!");
1976 }
1977 PyErr_Clear();
1978 if (pointer2) {
1979 Py_DECREF(module);
1980 Py_DECREF(object);
1981 if (pointer2 == pointer) {
1982 FAIL("PyCapsule_GetPointer should not have"
1983 " returned its internal pointer!");
1984 } else {
1985 FAIL("PyCapsule_GetPointer should have"
1986 " returned NULL pointer but did not!");
1987 }
1988 }
1989 Py_DECREF(object);
1990 Py_DECREF(module);
1991 }
1992 else
1993 PyErr_Clear();
1994 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00001995
1996 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 if (error) {
1998 return raiseTestError("test_capsule", error);
1999 }
2000 Py_RETURN_NONE;
Benjamin Petersonb173f782009-05-05 22:31:58 +00002001#undef FAIL
2002}
2003
Guido van Rossumddefaf32007-01-14 03:31:43 +00002004#ifdef HAVE_GETTIMEOFDAY
2005/* Profiling of integer performance */
Martin v. Löwis1c951552008-06-13 07:48:19 +00002006static void print_delta(int test, struct timeval *s, struct timeval *e)
Guido van Rossumddefaf32007-01-14 03:31:43 +00002007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 e->tv_sec -= s->tv_sec;
2009 e->tv_usec -= s->tv_usec;
2010 if (e->tv_usec < 0) {
2011 e->tv_sec -=1;
2012 e->tv_usec += 1000000;
2013 }
2014 printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002015}
2016
2017static PyObject *
2018profile_int(PyObject *self, PyObject* args)
2019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 int i, k;
2021 struct timeval start, stop;
2022 PyObject *single, **multiple, *op1, *result;
Guido van Rossumddefaf32007-01-14 03:31:43 +00002023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 /* Test 1: Allocate and immediately deallocate
2025 many small integers */
2026 gettimeofday(&start, NULL);
2027 for(k=0; k < 20000; k++)
2028 for(i=0; i < 1000; i++) {
2029 single = PyLong_FromLong(i);
2030 Py_DECREF(single);
2031 }
2032 gettimeofday(&stop, NULL);
2033 print_delta(1, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 /* Test 2: Allocate and immediately deallocate
2036 many large integers */
2037 gettimeofday(&start, NULL);
2038 for(k=0; k < 20000; k++)
2039 for(i=0; i < 1000; i++) {
2040 single = PyLong_FromLong(i+1000000);
2041 Py_DECREF(single);
2042 }
2043 gettimeofday(&stop, NULL);
2044 print_delta(2, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 /* Test 3: Allocate a few integers, then release
2047 them all simultaneously. */
2048 multiple = malloc(sizeof(PyObject*) * 1000);
2049 gettimeofday(&start, NULL);
2050 for(k=0; k < 20000; k++) {
2051 for(i=0; i < 1000; i++) {
2052 multiple[i] = PyLong_FromLong(i+1000000);
2053 }
2054 for(i=0; i < 1000; i++) {
2055 Py_DECREF(multiple[i]);
2056 }
2057 }
2058 gettimeofday(&stop, NULL);
2059 print_delta(3, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 /* Test 4: Allocate many integers, then release
2062 them all simultaneously. */
2063 multiple = malloc(sizeof(PyObject*) * 1000000);
2064 gettimeofday(&start, NULL);
2065 for(k=0; k < 20; k++) {
2066 for(i=0; i < 1000000; i++) {
2067 multiple[i] = PyLong_FromLong(i+1000000);
2068 }
2069 for(i=0; i < 1000000; i++) {
2070 Py_DECREF(multiple[i]);
2071 }
2072 }
2073 gettimeofday(&stop, NULL);
2074 print_delta(4, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 /* Test 5: Allocate many integers < 32000 */
2077 multiple = malloc(sizeof(PyObject*) * 1000000);
2078 gettimeofday(&start, NULL);
2079 for(k=0; k < 10; k++) {
2080 for(i=0; i < 1000000; i++) {
2081 multiple[i] = PyLong_FromLong(i+1000);
2082 }
2083 for(i=0; i < 1000000; i++) {
2084 Py_DECREF(multiple[i]);
2085 }
2086 }
2087 gettimeofday(&stop, NULL);
2088 print_delta(5, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 /* Test 6: Perform small int addition */
2091 op1 = PyLong_FromLong(1);
2092 gettimeofday(&start, NULL);
2093 for(i=0; i < 10000000; i++) {
2094 result = PyNumber_Add(op1, op1);
2095 Py_DECREF(result);
2096 }
2097 gettimeofday(&stop, NULL);
2098 Py_DECREF(op1);
2099 print_delta(6, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 /* Test 7: Perform medium int addition */
2102 op1 = PyLong_FromLong(1000);
2103 gettimeofday(&start, NULL);
2104 for(i=0; i < 10000000; i++) {
2105 result = PyNumber_Add(op1, op1);
2106 Py_DECREF(result);
2107 }
2108 gettimeofday(&stop, NULL);
2109 Py_DECREF(op1);
2110 print_delta(7, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 Py_INCREF(Py_None);
2113 return Py_None;
Guido van Rossumddefaf32007-01-14 03:31:43 +00002114}
2115#endif
2116
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002117/* To test the format of tracebacks as printed out. */
2118static PyObject *
2119traceback_print(PyObject *self, PyObject *args)
2120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 PyObject *file;
2122 PyObject *traceback;
2123 int result;
2124
2125 if (!PyArg_ParseTuple(args, "OO:traceback_print",
2126 &traceback, &file))
2127 return NULL;
2128
2129 result = PyTraceBack_Print(traceback, file);
2130 if (result < 0)
2131 return NULL;
2132 Py_RETURN_NONE;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002133}
2134
Benjamin Petersone6528212008-07-15 15:32:09 +00002135/* To test the format of exceptions as printed out. */
2136static PyObject *
2137exception_print(PyObject *self, PyObject *args)
2138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 PyObject *value;
2140 PyObject *tb;
Benjamin Petersone6528212008-07-15 15:32:09 +00002141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 if (!PyArg_ParseTuple(args, "O:exception_print",
2143 &value))
2144 return NULL;
2145 if (!PyExceptionInstance_Check(value)) {
2146 PyErr_Format(PyExc_TypeError, "an exception instance is required");
2147 return NULL;
2148 }
Benjamin Petersone6528212008-07-15 15:32:09 +00002149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 tb = PyException_GetTraceback(value);
2151 PyErr_Display((PyObject *) Py_TYPE(value), value, tb);
2152 Py_XDECREF(tb);
Benjamin Petersone6528212008-07-15 15:32:09 +00002153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 Py_RETURN_NONE;
Benjamin Petersone6528212008-07-15 15:32:09 +00002155}
2156
2157
2158
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002159
2160/* reliably raise a MemoryError */
2161static PyObject *
2162raise_memoryerror(PyObject *self)
2163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 PyErr_NoMemory();
2165 return NULL;
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002166}
2167
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00002168/* Issue 6012 */
2169static PyObject *str1, *str2;
2170static int
2171failing_converter(PyObject *obj, void *arg)
2172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 /* Clone str1, then let the conversion fail. */
2174 assert(str1);
2175 str2 = str1;
2176 Py_INCREF(str2);
2177 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00002178}
2179static PyObject*
2180argparsing(PyObject *o, PyObject *args)
2181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 PyObject *res;
2183 str1 = str2 = NULL;
2184 if (!PyArg_ParseTuple(args, "O&O&",
2185 PyUnicode_FSConverter, &str1,
2186 failing_converter, &str2)) {
2187 if (!str2)
2188 /* argument converter not called? */
2189 return NULL;
2190 /* Should be 1 */
Victor Stinner0fcab4a2011-01-04 12:59:15 +00002191 res = PyLong_FromSsize_t(Py_REFCNT(str2));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 Py_DECREF(str2);
2193 PyErr_Clear();
2194 return res;
2195 }
2196 Py_RETURN_NONE;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00002197}
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002198
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002199/* To test that the result of PyCode_NewEmpty has the right members. */
2200static PyObject *
2201code_newempty(PyObject *self, PyObject *args)
2202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 const char *filename;
2204 const char *funcname;
2205 int firstlineno;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 if (!PyArg_ParseTuple(args, "ssi:code_newempty",
2208 &filename, &funcname, &firstlineno))
2209 return NULL;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002212}
2213
Georg Brandl1e28a272009-12-28 08:41:01 +00002214/* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException).
2215 Run via Lib/test/test_exceptions.py */
2216static PyObject *
2217make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs)
2218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 const char *name;
2220 const char *doc = NULL;
2221 PyObject *base = NULL;
2222 PyObject *dict = NULL;
Georg Brandl1e28a272009-12-28 08:41:01 +00002223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 static char *kwlist[] = {"name", "doc", "base", "dict", NULL};
Georg Brandl1e28a272009-12-28 08:41:01 +00002225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
2227 "s|sOO:make_exception_with_doc", kwlist,
2228 &name, &doc, &base, &dict))
2229 return NULL;
Georg Brandl1e28a272009-12-28 08:41:01 +00002230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 return PyErr_NewExceptionWithDoc(name, doc, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +00002232}
2233
Antoine Pitrou915605c2011-02-24 20:53:48 +00002234static PyObject *
2235make_memoryview_from_NULL_pointer(PyObject *self)
2236{
2237 Py_buffer info;
2238 if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0)
2239 return NULL;
2240 return PyMemoryView_FromBuffer(&info);
2241}
2242
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00002243/* Test that the fatal error from not having a current thread doesn't
2244 cause an infinite loop. Run via Lib/test/test_capi.py */
2245static PyObject *
2246crash_no_current_thread(PyObject *self)
2247{
2248 Py_BEGIN_ALLOW_THREADS
Jeffrey Yasskinea7b7482010-05-17 16:59:23 +00002249 /* Using PyThreadState_Get() directly allows the test to pass in
2250 !pydebug mode. However, the test only actually tests anything
2251 in pydebug mode, since that's where the infinite loop was in
2252 the first place. */
2253 PyThreadState_Get();
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00002254 Py_END_ALLOW_THREADS
2255 return NULL;
2256}
2257
Tim Peters9ea17ac2001-02-02 05:57:15 +00002258static PyMethodDef TestMethods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 {"raise_exception", raise_exception, METH_VARARGS},
2260 {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
2261 {"test_config", (PyCFunction)test_config, METH_NOARGS},
2262 {"test_datetime_capi", test_datetime_capi, METH_NOARGS},
2263 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
2264 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
2265 {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS},
2266 {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS},
2267 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
2268 {"test_long_and_overflow", (PyCFunction)test_long_and_overflow,
2269 METH_NOARGS},
2270 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
2271 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
2272 {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
2273 {"test_bug_7414", (PyCFunction)test_bug_7414, METH_NOARGS},
2274 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
2275 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
2276 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
2277 PyDoc_STR("This is a pretty normal docstring.")},
2278 {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS},
2279 {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS},
2280 {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
2281 {"getargs_tuple", getargs_tuple, METH_VARARGS},
2282 {"getargs_keywords", (PyCFunction)getargs_keywords,
2283 METH_VARARGS|METH_KEYWORDS},
2284 {"getargs_b", getargs_b, METH_VARARGS},
2285 {"getargs_B", getargs_B, METH_VARARGS},
2286 {"getargs_h", getargs_h, METH_VARARGS},
2287 {"getargs_H", getargs_H, METH_VARARGS},
2288 {"getargs_I", getargs_I, METH_VARARGS},
2289 {"getargs_k", getargs_k, METH_VARARGS},
2290 {"getargs_i", getargs_i, METH_VARARGS},
2291 {"getargs_l", getargs_l, METH_VARARGS},
2292 {"getargs_n", getargs_n, METH_VARARGS},
Tim Peters91621db2001-06-12 20:10:01 +00002293#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 {"getargs_L", getargs_L, METH_VARARGS},
2295 {"getargs_K", getargs_K, METH_VARARGS},
2296 {"test_longlong_api", test_longlong_api, METH_NOARGS},
2297 {"test_long_long_and_overflow",
2298 (PyCFunction)test_long_long_and_overflow, METH_NOARGS},
2299 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
Victor Stinner06e49dd2010-06-13 18:21:50 +00002300#endif
2301 {"getargs_s", getargs_s, METH_VARARGS},
2302 {"getargs_s_star", getargs_s_star, METH_VARARGS},
2303 {"getargs_s_hash", getargs_s_hash, METH_VARARGS},
2304 {"getargs_z", getargs_z, METH_VARARGS},
2305 {"getargs_z_star", getargs_z_star, METH_VARARGS},
2306 {"getargs_z_hash", getargs_z_hash, METH_VARARGS},
2307 {"getargs_y", getargs_y, METH_VARARGS},
2308 {"getargs_y_star", getargs_y_star, METH_VARARGS},
2309 {"getargs_y_hash", getargs_y_hash, METH_VARARGS},
2310 {"getargs_u", getargs_u, METH_VARARGS},
2311 {"getargs_u_hash", getargs_u_hash, METH_VARARGS},
2312 {"getargs_Z", getargs_Z, METH_VARARGS},
2313 {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS},
Victor Stinner25e8ec42010-06-25 00:02:38 +00002314 {"getargs_w_star", getargs_w_star, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 {"codec_incrementalencoder",
Victor Stinner1c24bd02010-10-02 11:03:13 +00002316 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 {"codec_incrementaldecoder",
Victor Stinner1c24bd02010-10-02 11:03:13 +00002318 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS},
2320 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
2321 {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS},
2322 {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS},
Victor Stinner46c7b3b2010-10-02 11:49:31 +00002323 {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS},
2324 {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS},
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002325#ifdef WITH_THREAD
Victor Stinner1c24bd02010-10-02 11:03:13 +00002326 {"_test_thread_state", test_thread_state, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 {"_pending_threadfunc", pending_threadfunc, METH_VARARGS},
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002328#endif
Guido van Rossumddefaf32007-01-14 03:31:43 +00002329#ifdef HAVE_GETTIMEOFDAY
Victor Stinner1c24bd02010-10-02 11:03:13 +00002330 {"profile_int", profile_int, METH_NOARGS},
Guido van Rossumddefaf32007-01-14 03:31:43 +00002331#endif
Victor Stinner1c24bd02010-10-02 11:03:13 +00002332 {"traceback_print", traceback_print, METH_VARARGS},
2333 {"exception_print", exception_print, METH_VARARGS},
2334 {"argparsing", argparsing, METH_VARARGS},
2335 {"code_newempty", code_newempty, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 {"make_exception_with_doc", (PyCFunction)make_exception_with_doc,
2337 METH_VARARGS | METH_KEYWORDS},
Antoine Pitrou915605c2011-02-24 20:53:48 +00002338 {"make_memoryview_from_NULL_pointer", (PyCFunction)make_memoryview_from_NULL_pointer,
2339 METH_NOARGS},
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00002340 {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 {NULL, NULL} /* sentinel */
Tim Peters9ea17ac2001-02-02 05:57:15 +00002342};
2343
Thomas Hellera4ea6032003-04-17 18:55:45 +00002344#define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
2345
Thomas Wouters89f507f2006-12-13 04:49:30 +00002346typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 char bool_member;
2348 char byte_member;
2349 unsigned char ubyte_member;
2350 short short_member;
2351 unsigned short ushort_member;
2352 int int_member;
2353 unsigned int uint_member;
2354 long long_member;
2355 unsigned long ulong_member;
2356 Py_ssize_t pyssizet_member;
2357 float float_member;
2358 double double_member;
2359 char inplace_member[6];
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002360#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 PY_LONG_LONG longlong_member;
2362 unsigned PY_LONG_LONG ulonglong_member;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002363#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +00002364} all_structmembers;
2365
2366typedef struct {
2367 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 all_structmembers structmembers;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002369} test_structmembers;
2370
2371static struct PyMemberDef test_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
2373 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
2374 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
2375 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
2376 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
2377 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
2378 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
2379 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
2380 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
2381 {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL},
2382 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
2383 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
2384 {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002385#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
2387 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002388#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 {NULL}
Thomas Wouters89f507f2006-12-13 04:49:30 +00002390};
2391
2392
Christian Heimes1af737c2008-01-23 08:24:23 +00002393static PyObject *
2394test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 static char *keywords[] = {
2397 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
2398 "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET",
2399 "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
Mark Dickinsond59b4162010-03-13 11:34:40 +00002400#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 "T_LONGLONG", "T_ULONGLONG",
Christian Heimes1af737c2008-01-23 08:24:23 +00002402#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 NULL};
2404 static char *fmt = "|bbBhHiIlknfds#"
Christian Heimes1af737c2008-01-23 08:24:23 +00002405#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 "LK"
Christian Heimes1af737c2008-01-23 08:24:23 +00002407#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 ;
2409 test_structmembers *ob;
2410 const char *s = NULL;
2411 Py_ssize_t string_len = 0;
2412 ob = PyObject_New(test_structmembers, type);
2413 if (ob == NULL)
2414 return NULL;
2415 memset(&ob->structmembers, 0, sizeof(all_structmembers));
2416 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
2417 &ob->structmembers.bool_member,
2418 &ob->structmembers.byte_member,
2419 &ob->structmembers.ubyte_member,
2420 &ob->structmembers.short_member,
2421 &ob->structmembers.ushort_member,
2422 &ob->structmembers.int_member,
2423 &ob->structmembers.uint_member,
2424 &ob->structmembers.long_member,
2425 &ob->structmembers.ulong_member,
2426 &ob->structmembers.pyssizet_member,
2427 &ob->structmembers.float_member,
2428 &ob->structmembers.double_member,
2429 &s, &string_len
Christian Heimes1af737c2008-01-23 08:24:23 +00002430#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 , &ob->structmembers.longlong_member,
2432 &ob->structmembers.ulonglong_member
Christian Heimes1af737c2008-01-23 08:24:23 +00002433#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 )) {
2435 Py_DECREF(ob);
2436 return NULL;
2437 }
2438 if (s != NULL) {
2439 if (string_len > 5) {
2440 Py_DECREF(ob);
2441 PyErr_SetString(PyExc_ValueError, "string too long");
2442 return NULL;
2443 }
2444 strcpy(ob->structmembers.inplace_member, s);
2445 }
2446 else {
2447 strcpy(ob->structmembers.inplace_member, "");
2448 }
2449 return (PyObject *)ob;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002450}
2451
Christian Heimes1af737c2008-01-23 08:24:23 +00002452static void
2453test_structmembers_free(PyObject *ob)
2454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 PyObject_FREE(ob);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002456}
2457
2458static PyTypeObject test_structmembersType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002459 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 "test_structmembersType",
2461 sizeof(test_structmembers), /* tp_basicsize */
2462 0, /* tp_itemsize */
2463 test_structmembers_free, /* destructor tp_dealloc */
2464 0, /* tp_print */
2465 0, /* tp_getattr */
2466 0, /* tp_setattr */
2467 0, /* tp_reserved */
2468 0, /* tp_repr */
2469 0, /* tp_as_number */
2470 0, /* tp_as_sequence */
2471 0, /* tp_as_mapping */
2472 0, /* tp_hash */
2473 0, /* tp_call */
2474 0, /* tp_str */
2475 PyObject_GenericGetAttr, /* tp_getattro */
2476 PyObject_GenericSetAttr, /* tp_setattro */
2477 0, /* tp_as_buffer */
2478 0, /* tp_flags */
2479 "Type containing all structmember types",
2480 0, /* traverseproc tp_traverse */
2481 0, /* tp_clear */
2482 0, /* tp_richcompare */
2483 0, /* tp_weaklistoffset */
2484 0, /* tp_iter */
2485 0, /* tp_iternext */
2486 0, /* tp_methods */
2487 test_members, /* tp_members */
2488 0,
2489 0,
2490 0,
2491 0,
2492 0,
2493 0,
2494 0,
2495 0,
2496 test_structmembers_new, /* tp_new */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002497};
2498
2499
Martin v. Löwis1a214512008-06-11 05:26:20 +00002500
2501static struct PyModuleDef _testcapimodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 PyModuleDef_HEAD_INIT,
2503 "_testcapi",
2504 NULL,
2505 -1,
2506 TestMethods,
2507 NULL,
2508 NULL,
2509 NULL,
2510 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002511};
2512
Mark Hammond62b1ab12002-07-23 06:31:15 +00002513PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002514PyInit__testcapi(void)
Tim Peters9ea17ac2001-02-02 05:57:15 +00002515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 PyObject *m;
Tim Peters9ea17ac2001-02-02 05:57:15 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 m = PyModule_Create(&_testcapimodule);
2519 if (m == NULL)
2520 return NULL;
Tim Peters9ea17ac2001-02-02 05:57:15 +00002521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
2523 Py_TYPE(&_MemoryViewTester_Type)=&PyType_Type;
Hirokazu Yamamoto8ebab5d2008-12-31 06:05:46 +00002524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 Py_TYPE(&test_structmembersType)=&PyType_Type;
2526 Py_INCREF(&test_structmembersType);
2527 /* don't use a name starting with "test", since we don't want
2528 test_capi to automatically call this */
2529 PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
2532 PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
2533 PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX));
2534 PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX));
2535 PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN));
2536 PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX));
2537 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
2538 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
2539 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
2540 PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX));
2541 PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN));
2542 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
2543 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
2544 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
2545 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
2546 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
2547 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
2548 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
2549 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
2550 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
2551 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
2552 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
2553 Py_INCREF(&PyInstanceMethod_Type);
2554 PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
Thomas Hellera4ea6032003-04-17 18:55:45 +00002555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
2557 Py_INCREF(TestError);
2558 PyModule_AddObject(m, "error", TestError);
2559 return m;
Tim Peters9ea17ac2001-02-02 05:57:15 +00002560}