blob: 94b6e6793a57d47350975c2971ef4f3b0299a142 [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 Stinnerab1d16b2011-11-22 01:45:37 +01001441unicode_encodedecimal(PyObject *self, PyObject *args)
1442{
1443 Py_UNICODE *unicode;
1444 Py_ssize_t length;
1445 char *errors = NULL;
1446 PyObject *decimal;
1447 Py_ssize_t decimal_length, new_length;
1448 int res;
1449
1450 if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors))
1451 return NULL;
1452
1453 decimal_length = length * 7; /* len('&#8364;') */
1454 decimal = PyBytes_FromStringAndSize(NULL, decimal_length);
1455 if (decimal == NULL)
1456 return NULL;
1457
1458 res = PyUnicode_EncodeDecimal(unicode, length,
1459 PyBytes_AS_STRING(decimal),
1460 errors);
1461 if (res < 0) {
1462 Py_DECREF(decimal);
1463 return NULL;
1464 }
1465
1466 new_length = strlen(PyBytes_AS_STRING(decimal));
1467 assert(new_length <= decimal_length);
1468 res = _PyBytes_Resize(&decimal, new_length);
1469 if (res < 0)
1470 return NULL;
1471
1472 return decimal;
1473}
1474
1475static PyObject *
1476unicode_transformdecimaltoascii(PyObject *self, PyObject *args)
1477{
1478 Py_UNICODE *unicode;
1479 Py_ssize_t length;
1480 if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length))
1481 return NULL;
1482 return PyUnicode_TransformDecimalToASCII(unicode, length);
1483}
1484
1485static PyObject *
Victor Stinner25e8ec42010-06-25 00:02:38 +00001486getargs_w_star(PyObject *self, PyObject *args)
1487{
1488 Py_buffer buffer;
1489 PyObject *result;
1490 char *str;
1491
1492 if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer))
1493 return NULL;
1494
1495 if (2 <= buffer.len) {
1496 str = buffer.buf;
1497 str[0] = '[';
1498 str[buffer.len-1] = ']';
1499 }
1500
1501 result = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1502 PyBuffer_Release(&buffer);
1503 return result;
1504}
1505
1506
1507static PyObject *
Benjamin Peterson92035012008-12-27 16:00:54 +00001508test_empty_argparse(PyObject *self)
1509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 /* Test that formats can begin with '|'. See issue #4720. */
1511 PyObject *tuple, *dict = NULL;
1512 static char *kwlist[] = {NULL};
1513 int result;
1514 tuple = PyTuple_New(0);
1515 if (!tuple)
1516 return NULL;
1517 if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0)
1518 goto done;
1519 dict = PyDict_New();
1520 if (!dict)
1521 goto done;
1522 result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
Benjamin Peterson92035012008-12-27 16:00:54 +00001523 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 Py_DECREF(tuple);
1525 Py_XDECREF(dict);
1526 if (result < 0)
1527 return NULL;
1528 else {
1529 Py_RETURN_NONE;
1530 }
Benjamin Peterson92035012008-12-27 16:00:54 +00001531}
1532
1533static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001534codec_incrementalencoder(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 const char *encoding, *errors = NULL;
1537 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
1538 &encoding, &errors))
1539 return NULL;
1540 return PyCodec_IncrementalEncoder(encoding, errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001541}
1542
Thomas Wouters477c8d52006-05-27 19:21:47 +00001543static PyObject *
1544codec_incrementaldecoder(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 const char *encoding, *errors = NULL;
1547 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
1548 &encoding, &errors))
1549 return NULL;
1550 return PyCodec_IncrementalDecoder(encoding, errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001551}
1552
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001553
Tim Peters5b8132f2003-01-31 15:52:05 +00001554/* Simple test of _PyLong_NumBits and _PyLong_Sign. */
Tim Petersbaefd9e2003-01-28 20:37:45 +00001555static PyObject *
1556test_long_numbits(PyObject *self)
1557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 struct triple {
1559 long input;
1560 size_t nbits;
1561 int sign;
1562 } testcases[] = {{0, 0, 0},
1563 {1L, 1, 1},
1564 {-1L, 1, -1},
1565 {2L, 2, 1},
1566 {-2L, 2, -1},
1567 {3L, 2, 1},
1568 {-3L, 2, -1},
1569 {4L, 3, 1},
1570 {-4L, 3, -1},
1571 {0x7fffL, 15, 1}, /* one Python long digit */
1572 {-0x7fffL, 15, -1},
1573 {0xffffL, 16, 1},
1574 {-0xffffL, 16, -1},
1575 {0xfffffffL, 28, 1},
1576 {-0xfffffffL, 28, -1}};
1577 int i;
Tim Petersbaefd9e2003-01-28 20:37:45 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
1580 PyObject *plong = PyLong_FromLong(testcases[i].input);
1581 size_t nbits = _PyLong_NumBits(plong);
1582 int sign = _PyLong_Sign(plong);
Tim Petersbaefd9e2003-01-28 20:37:45 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 Py_DECREF(plong);
1585 if (nbits != testcases[i].nbits)
1586 return raiseTestError("test_long_numbits",
1587 "wrong result for _PyLong_NumBits");
1588 if (sign != testcases[i].sign)
1589 return raiseTestError("test_long_numbits",
1590 "wrong result for _PyLong_Sign");
1591 }
1592 Py_INCREF(Py_None);
1593 return Py_None;
Tim Petersbaefd9e2003-01-28 20:37:45 +00001594}
1595
Thomas Heller519a0422007-11-15 20:48:54 +00001596/* Example passing NULLs to PyObject_Str(NULL). */
Thomas Woutersa9773292006-04-21 09:43:23 +00001597
1598static PyObject *
1599test_null_strings(PyObject *self)
1600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL);
1602 PyObject *tuple = PyTuple_Pack(2, o1, o2);
1603 Py_XDECREF(o1);
1604 Py_XDECREF(o2);
1605 return tuple;
Thomas Woutersa9773292006-04-21 09:43:23 +00001606}
1607
Jeremy Hyltonede049b2001-09-26 20:01:13 +00001608static PyObject *
1609raise_exception(PyObject *self, PyObject *args)
1610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 PyObject *exc;
1612 PyObject *exc_args, *v;
1613 int num_args, i;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00001614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
1616 &exc, &num_args))
1617 return NULL;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00001618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 exc_args = PyTuple_New(num_args);
1620 if (exc_args == NULL)
1621 return NULL;
1622 for (i = 0; i < num_args; ++i) {
1623 v = PyLong_FromLong(i);
1624 if (v == NULL) {
1625 Py_DECREF(exc_args);
1626 return NULL;
1627 }
1628 PyTuple_SET_ITEM(exc_args, i, v);
1629 }
1630 PyErr_SetObject(exc, exc_args);
1631 Py_DECREF(exc_args);
1632 return NULL;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00001633}
Tim Peters91621db2001-06-12 20:10:01 +00001634
Benjamin Peterson16323982010-02-03 01:13:41 +00001635
1636static int test_run_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001637
Benjamin Peterson91d58bd2009-12-13 21:30:54 +00001638static PyObject *
1639test_datetime_capi(PyObject *self, PyObject *args) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (PyDateTimeAPI) {
1641 if (test_run_counter) {
1642 /* Probably regrtest.py -R */
1643 Py_RETURN_NONE;
1644 }
1645 else {
1646 PyErr_SetString(PyExc_AssertionError,
1647 "PyDateTime_CAPI somehow initialized");
1648 return NULL;
1649 }
1650 }
1651 test_run_counter++;
1652 PyDateTime_IMPORT;
1653 if (PyDateTimeAPI)
1654 Py_RETURN_NONE;
1655 else
1656 return NULL;
Benjamin Peterson91d58bd2009-12-13 21:30:54 +00001657}
1658
Benjamin Peterson16323982010-02-03 01:13:41 +00001659
1660#ifdef WITH_THREAD
1661
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001662/* test_thread_state spawns a thread of its own, and that thread releases
1663 * `thread_done` when it's finished. The driver code has to know when the
1664 * thread finishes, because the thread uses a PyObject (the callable) that
1665 * may go away when the driver finishes. The former lack of this explicit
1666 * synchronization caused rare segfaults, so rare that they were seen only
1667 * on a Mac buildbot (although they were possible on any box).
1668 */
1669static PyThread_type_lock thread_done = NULL;
1670
Benjamin Petersona786b022008-08-25 21:05:21 +00001671static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001672_make_call(void *callable)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 PyObject *rc;
1675 int success;
1676 PyGILState_STATE s = PyGILState_Ensure();
1677 rc = PyObject_CallFunction((PyObject *)callable, "");
1678 success = (rc != NULL);
1679 Py_XDECREF(rc);
1680 PyGILState_Release(s);
1681 return success;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001682}
1683
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001684/* Same thing, but releases `thread_done` when it returns. This variant
1685 * should be called only from threads spawned by test_thread_state().
1686 */
1687static void
1688_make_call_from_thread(void *callable)
1689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 _make_call(callable);
1691 PyThread_release_lock(thread_done);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001692}
1693
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001694static PyObject *
1695test_thread_state(PyObject *self, PyObject *args)
1696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 PyObject *fn;
1698 int success = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
1701 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 if (!PyCallable_Check(fn)) {
1704 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
1705 fn->ob_type->tp_name);
1706 return NULL;
1707 }
Benjamin Petersona786b022008-08-25 21:05:21 +00001708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 /* Ensure Python is set up for threading */
1710 PyEval_InitThreads();
1711 thread_done = PyThread_allocate_lock();
1712 if (thread_done == NULL)
1713 return PyErr_NoMemory();
1714 PyThread_acquire_lock(thread_done, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 /* Start a new thread with our callback. */
1717 PyThread_start_new_thread(_make_call_from_thread, fn);
1718 /* Make the callback with the thread lock held by this thread */
1719 success &= _make_call(fn);
1720 /* Do it all again, but this time with the thread-lock released */
1721 Py_BEGIN_ALLOW_THREADS
1722 success &= _make_call(fn);
1723 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
1724 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 /* And once more with and without a thread
1727 XXX - should use a lock and work out exactly what we are trying
1728 to test <wink>
1729 */
1730 Py_BEGIN_ALLOW_THREADS
1731 PyThread_start_new_thread(_make_call_from_thread, fn);
1732 success &= _make_call(fn);
1733 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
1734 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 /* Release lock we acquired above. This is required on HP-UX. */
1737 PyThread_release_lock(thread_done);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 PyThread_free_lock(thread_done);
1740 if (!success)
1741 return NULL;
1742 Py_RETURN_NONE;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001743}
Benjamin Petersona54c9092009-01-13 02:11:23 +00001744
1745/* test Py_AddPendingCalls using threads */
1746static int _pending_callback(void *arg)
1747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 /* we assume the argument is callable object to which we own a reference */
1749 PyObject *callable = (PyObject *)arg;
1750 PyObject *r = PyObject_CallObject(callable, NULL);
1751 Py_DECREF(callable);
1752 Py_XDECREF(r);
1753 return r != NULL ? 0 : -1;
Benjamin Petersona54c9092009-01-13 02:11:23 +00001754}
1755
1756/* The following requests n callbacks to _pending_callback. It can be
1757 * run from any python thread.
1758 */
1759PyObject *pending_threadfunc(PyObject *self, PyObject *arg)
1760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 PyObject *callable;
1762 int r;
1763 if (PyArg_ParseTuple(arg, "O", &callable) == 0)
1764 return NULL;
Benjamin Petersona54c9092009-01-13 02:11:23 +00001765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 /* create the reference for the callbackwhile we hold the lock */
1767 Py_INCREF(callable);
Benjamin Petersona54c9092009-01-13 02:11:23 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 Py_BEGIN_ALLOW_THREADS
1770 r = Py_AddPendingCall(&_pending_callback, callable);
1771 Py_END_ALLOW_THREADS
Benjamin Petersona54c9092009-01-13 02:11:23 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 if (r<0) {
1774 Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
1775 Py_INCREF(Py_False);
1776 return Py_False;
1777 }
1778 Py_INCREF(Py_True);
1779 return Py_True;
Benjamin Petersona54c9092009-01-13 02:11:23 +00001780}
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001781#endif
1782
Neal Norwitzb0d26332007-08-25 00:49:05 +00001783/* Some tests of PyUnicode_FromFormat(). This needs more tests. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001784static PyObject *
1785test_string_from_format(PyObject *self, PyObject *args)
1786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 PyObject *result;
1788 char *msg;
Alexander Belopolskye239d232010-12-08 23:31:48 +00001789 static const Py_UNICODE one[] = {'1', 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00001790
Alexander Belopolskye239d232010-12-08 23:31:48 +00001791#define CHECK_1_FORMAT(FORMAT, TYPE) \
1792 result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \
1793 if (result == NULL) \
1794 return NULL; \
1795 if (Py_UNICODE_strcmp(PyUnicode_AS_UNICODE(result), one)) { \
1796 msg = FORMAT " failed at 1"; \
1797 goto Fail; \
1798 } \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 Py_DECREF(result)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 CHECK_1_FORMAT("%d", int);
1802 CHECK_1_FORMAT("%ld", long);
1803 /* The z width modifier was added in Python 2.5. */
1804 CHECK_1_FORMAT("%zd", Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 /* The u type code was added in Python 2.5. */
1807 CHECK_1_FORMAT("%u", unsigned int);
1808 CHECK_1_FORMAT("%lu", unsigned long);
1809 CHECK_1_FORMAT("%zu", size_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 /* "%lld" and "%llu" support added in Python 2.7. */
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00001812#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG);
1814 CHECK_1_FORMAT("%lld", PY_LONG_LONG);
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00001815#endif
1816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001818
1819 Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 Py_XDECREF(result);
1821 return raiseTestError("test_string_from_format", msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001822
1823#undef CHECK_1_FORMAT
1824}
1825
Benjamin Peterson8667a9b2010-01-09 21:45:28 +00001826
1827static PyObject *
1828test_unicode_compare_with_ascii(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4);
1830 int result;
1831 if (py_s == NULL)
1832 return NULL;
1833 result = PyUnicode_CompareWithASCIIString(py_s, "str");
1834 Py_DECREF(py_s);
1835 if (!result) {
1836 PyErr_SetString(TestError, "Python string ending in NULL "
1837 "should not compare equal to c string.");
1838 return NULL;
1839 }
1840 Py_RETURN_NONE;
Victor Stinner3e2b7172010-11-09 09:32:19 +00001841}
Benjamin Peterson8667a9b2010-01-09 21:45:28 +00001842
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001843/* This is here to provide a docstring for test_descr. */
1844static PyObject *
1845test_with_docstring(PyObject *self)
1846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001848}
1849
Mark Dickinson725bfd82009-05-03 20:33:40 +00001850/* Test PyOS_string_to_double. */
1851static PyObject *
1852test_string_to_double(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 double result;
1854 char *msg;
Mark Dickinson725bfd82009-05-03 20:33:40 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856#define CHECK_STRING(STR, expected) \
1857 result = PyOS_string_to_double(STR, NULL, NULL); \
1858 if (result == -1.0 && PyErr_Occurred()) \
1859 return NULL; \
1860 if (result != expected) { \
1861 msg = "conversion of " STR " to float failed"; \
1862 goto fail; \
1863 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865#define CHECK_INVALID(STR) \
1866 result = PyOS_string_to_double(STR, NULL, NULL); \
1867 if (result == -1.0 && PyErr_Occurred()) { \
1868 if (PyErr_ExceptionMatches(PyExc_ValueError)) \
1869 PyErr_Clear(); \
1870 else \
1871 return NULL; \
1872 } \
1873 else { \
1874 msg = "conversion of " STR " didn't raise ValueError"; \
1875 goto fail; \
1876 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00001877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 CHECK_STRING("0.1", 0.1);
1879 CHECK_STRING("1.234", 1.234);
1880 CHECK_STRING("-1.35", -1.35);
1881 CHECK_STRING(".1e01", 1.0);
1882 CHECK_STRING("2.e-2", 0.02);
Mark Dickinson725bfd82009-05-03 20:33:40 +00001883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 CHECK_INVALID(" 0.1");
1885 CHECK_INVALID("\t\n-3");
1886 CHECK_INVALID(".123 ");
1887 CHECK_INVALID("3\n");
1888 CHECK_INVALID("123abc");
Mark Dickinson725bfd82009-05-03 20:33:40 +00001889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 Py_RETURN_NONE;
Mark Dickinson725bfd82009-05-03 20:33:40 +00001891 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 return raiseTestError("test_string_to_double", msg);
Mark Dickinson725bfd82009-05-03 20:33:40 +00001893#undef CHECK_STRING
1894#undef CHECK_INVALID
1895}
1896
1897
Benjamin Petersonb173f782009-05-05 22:31:58 +00001898/* Coverage testing of capsule objects. */
1899
1900static const char *capsule_name = "capsule name";
1901static char *capsule_pointer = "capsule pointer";
1902static char *capsule_context = "capsule context";
1903static const char *capsule_error = NULL;
1904static int
1905capsule_destructor_call_count = 0;
1906
1907static void
1908capsule_destructor(PyObject *o) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 capsule_destructor_call_count++;
1910 if (PyCapsule_GetContext(o) != capsule_context) {
1911 capsule_error = "context did not match in destructor!";
1912 } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
1913 capsule_error = "destructor did not match in destructor! (woah!)";
1914 } else if (PyCapsule_GetName(o) != capsule_name) {
1915 capsule_error = "name did not match in destructor!";
1916 } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
1917 capsule_error = "pointer did not match in destructor!";
1918 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00001919}
1920
1921typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 char *name;
1923 char *module;
1924 char *attribute;
Benjamin Petersonb173f782009-05-05 22:31:58 +00001925} known_capsule;
1926
1927static PyObject *
1928test_capsule(PyObject *self, PyObject *args)
1929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 PyObject *object;
1931 const char *error = NULL;
1932 void *pointer;
1933 void *pointer2;
1934 known_capsule known_capsules[] = {
1935 #define KNOWN_CAPSULE(module, name) { module "." name, module, name }
1936 KNOWN_CAPSULE("_socket", "CAPI"),
1937 KNOWN_CAPSULE("_curses", "_C_API"),
1938 KNOWN_CAPSULE("datetime", "datetime_CAPI"),
1939 { NULL, NULL },
1940 };
1941 known_capsule *known = &known_capsules[0];
Benjamin Petersonb173f782009-05-05 22:31:58 +00001942
1943#define FAIL(x) { error = (x); goto exit; }
1944
1945#define CHECK_DESTRUCTOR \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 if (capsule_error) { \
1947 FAIL(capsule_error); \
1948 } \
1949 else if (!capsule_destructor_call_count) { \
1950 FAIL("destructor not called!"); \
1951 } \
1952 capsule_destructor_call_count = 0; \
Benjamin Petersonb173f782009-05-05 22:31:58 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
1955 PyCapsule_SetContext(object, capsule_context);
1956 capsule_destructor(object);
1957 CHECK_DESTRUCTOR;
1958 Py_DECREF(object);
1959 CHECK_DESTRUCTOR;
Benjamin Petersonb173f782009-05-05 22:31:58 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 object = PyCapsule_New(known, "ignored", NULL);
1962 PyCapsule_SetPointer(object, capsule_pointer);
1963 PyCapsule_SetName(object, capsule_name);
1964 PyCapsule_SetDestructor(object, capsule_destructor);
1965 PyCapsule_SetContext(object, capsule_context);
1966 capsule_destructor(object);
1967 CHECK_DESTRUCTOR;
1968 /* intentionally access using the wrong name */
1969 pointer2 = PyCapsule_GetPointer(object, "the wrong name");
1970 if (!PyErr_Occurred()) {
1971 FAIL("PyCapsule_GetPointer should have failed but did not!");
1972 }
1973 PyErr_Clear();
1974 if (pointer2) {
1975 if (pointer2 == capsule_pointer) {
1976 FAIL("PyCapsule_GetPointer should not have"
1977 " returned the internal pointer!");
1978 } else {
1979 FAIL("PyCapsule_GetPointer should have "
1980 "returned NULL pointer but did not!");
1981 }
1982 }
1983 PyCapsule_SetDestructor(object, NULL);
1984 Py_DECREF(object);
1985 if (capsule_destructor_call_count) {
1986 FAIL("destructor called when it should not have been!");
1987 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00001988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 for (known = &known_capsules[0]; known->module != NULL; known++) {
1990 /* yeah, ordinarily I wouldn't do this either,
1991 but it's fine for this test harness.
1992 */
1993 static char buffer[256];
Benjamin Petersonb173f782009-05-05 22:31:58 +00001994#undef FAIL
1995#define FAIL(x) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 { \
1997 sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
1998 x, known->module, known->attribute); \
1999 error = buffer; \
2000 goto exit; \
2001 } \
Benjamin Petersonb173f782009-05-05 22:31:58 +00002002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 PyObject *module = PyImport_ImportModule(known->module);
2004 if (module) {
2005 pointer = PyCapsule_Import(known->name, 0);
2006 if (!pointer) {
2007 Py_DECREF(module);
2008 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
2009 }
2010 object = PyObject_GetAttrString(module, known->attribute);
2011 if (!object) {
2012 Py_DECREF(module);
2013 return NULL;
2014 }
2015 pointer2 = PyCapsule_GetPointer(object,
2016 "weebles wobble but they don't fall down");
2017 if (!PyErr_Occurred()) {
2018 Py_DECREF(object);
2019 Py_DECREF(module);
2020 FAIL("PyCapsule_GetPointer should have failed but did not!");
2021 }
2022 PyErr_Clear();
2023 if (pointer2) {
2024 Py_DECREF(module);
2025 Py_DECREF(object);
2026 if (pointer2 == pointer) {
2027 FAIL("PyCapsule_GetPointer should not have"
2028 " returned its internal pointer!");
2029 } else {
2030 FAIL("PyCapsule_GetPointer should have"
2031 " returned NULL pointer but did not!");
2032 }
2033 }
2034 Py_DECREF(object);
2035 Py_DECREF(module);
2036 }
2037 else
2038 PyErr_Clear();
2039 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00002040
2041 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 if (error) {
2043 return raiseTestError("test_capsule", error);
2044 }
2045 Py_RETURN_NONE;
Benjamin Petersonb173f782009-05-05 22:31:58 +00002046#undef FAIL
2047}
2048
Guido van Rossumddefaf32007-01-14 03:31:43 +00002049#ifdef HAVE_GETTIMEOFDAY
2050/* Profiling of integer performance */
Martin v. Löwis1c951552008-06-13 07:48:19 +00002051static void print_delta(int test, struct timeval *s, struct timeval *e)
Guido van Rossumddefaf32007-01-14 03:31:43 +00002052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 e->tv_sec -= s->tv_sec;
2054 e->tv_usec -= s->tv_usec;
2055 if (e->tv_usec < 0) {
2056 e->tv_sec -=1;
2057 e->tv_usec += 1000000;
2058 }
2059 printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002060}
2061
2062static PyObject *
2063profile_int(PyObject *self, PyObject* args)
2064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 int i, k;
2066 struct timeval start, stop;
2067 PyObject *single, **multiple, *op1, *result;
Guido van Rossumddefaf32007-01-14 03:31:43 +00002068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 /* Test 1: Allocate and immediately deallocate
2070 many small integers */
2071 gettimeofday(&start, NULL);
2072 for(k=0; k < 20000; k++)
2073 for(i=0; i < 1000; i++) {
2074 single = PyLong_FromLong(i);
2075 Py_DECREF(single);
2076 }
2077 gettimeofday(&stop, NULL);
2078 print_delta(1, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 /* Test 2: Allocate and immediately deallocate
2081 many large integers */
2082 gettimeofday(&start, NULL);
2083 for(k=0; k < 20000; k++)
2084 for(i=0; i < 1000; i++) {
2085 single = PyLong_FromLong(i+1000000);
2086 Py_DECREF(single);
2087 }
2088 gettimeofday(&stop, NULL);
2089 print_delta(2, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 /* Test 3: Allocate a few integers, then release
2092 them all simultaneously. */
2093 multiple = malloc(sizeof(PyObject*) * 1000);
2094 gettimeofday(&start, NULL);
2095 for(k=0; k < 20000; k++) {
2096 for(i=0; i < 1000; i++) {
2097 multiple[i] = PyLong_FromLong(i+1000000);
2098 }
2099 for(i=0; i < 1000; i++) {
2100 Py_DECREF(multiple[i]);
2101 }
2102 }
2103 gettimeofday(&stop, NULL);
2104 print_delta(3, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 /* Test 4: Allocate many integers, then release
2107 them all simultaneously. */
2108 multiple = malloc(sizeof(PyObject*) * 1000000);
2109 gettimeofday(&start, NULL);
2110 for(k=0; k < 20; k++) {
2111 for(i=0; i < 1000000; i++) {
2112 multiple[i] = PyLong_FromLong(i+1000000);
2113 }
2114 for(i=0; i < 1000000; i++) {
2115 Py_DECREF(multiple[i]);
2116 }
2117 }
2118 gettimeofday(&stop, NULL);
2119 print_delta(4, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 /* Test 5: Allocate many integers < 32000 */
2122 multiple = malloc(sizeof(PyObject*) * 1000000);
2123 gettimeofday(&start, NULL);
2124 for(k=0; k < 10; k++) {
2125 for(i=0; i < 1000000; i++) {
2126 multiple[i] = PyLong_FromLong(i+1000);
2127 }
2128 for(i=0; i < 1000000; i++) {
2129 Py_DECREF(multiple[i]);
2130 }
2131 }
2132 gettimeofday(&stop, NULL);
2133 print_delta(5, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 /* Test 6: Perform small int addition */
2136 op1 = PyLong_FromLong(1);
2137 gettimeofday(&start, NULL);
2138 for(i=0; i < 10000000; i++) {
2139 result = PyNumber_Add(op1, op1);
2140 Py_DECREF(result);
2141 }
2142 gettimeofday(&stop, NULL);
2143 Py_DECREF(op1);
2144 print_delta(6, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 /* Test 7: Perform medium int addition */
2147 op1 = PyLong_FromLong(1000);
2148 gettimeofday(&start, NULL);
2149 for(i=0; i < 10000000; i++) {
2150 result = PyNumber_Add(op1, op1);
2151 Py_DECREF(result);
2152 }
2153 gettimeofday(&stop, NULL);
2154 Py_DECREF(op1);
2155 print_delta(7, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 Py_INCREF(Py_None);
2158 return Py_None;
Guido van Rossumddefaf32007-01-14 03:31:43 +00002159}
2160#endif
2161
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002162/* To test the format of tracebacks as printed out. */
2163static PyObject *
2164traceback_print(PyObject *self, PyObject *args)
2165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 PyObject *file;
2167 PyObject *traceback;
2168 int result;
2169
2170 if (!PyArg_ParseTuple(args, "OO:traceback_print",
2171 &traceback, &file))
2172 return NULL;
2173
2174 result = PyTraceBack_Print(traceback, file);
2175 if (result < 0)
2176 return NULL;
2177 Py_RETURN_NONE;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002178}
2179
Benjamin Petersone6528212008-07-15 15:32:09 +00002180/* To test the format of exceptions as printed out. */
2181static PyObject *
2182exception_print(PyObject *self, PyObject *args)
2183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 PyObject *value;
2185 PyObject *tb;
Benjamin Petersone6528212008-07-15 15:32:09 +00002186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 if (!PyArg_ParseTuple(args, "O:exception_print",
2188 &value))
2189 return NULL;
2190 if (!PyExceptionInstance_Check(value)) {
2191 PyErr_Format(PyExc_TypeError, "an exception instance is required");
2192 return NULL;
2193 }
Benjamin Petersone6528212008-07-15 15:32:09 +00002194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 tb = PyException_GetTraceback(value);
2196 PyErr_Display((PyObject *) Py_TYPE(value), value, tb);
2197 Py_XDECREF(tb);
Benjamin Petersone6528212008-07-15 15:32:09 +00002198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 Py_RETURN_NONE;
Benjamin Petersone6528212008-07-15 15:32:09 +00002200}
2201
2202
2203
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002204
2205/* reliably raise a MemoryError */
2206static PyObject *
2207raise_memoryerror(PyObject *self)
2208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 PyErr_NoMemory();
2210 return NULL;
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002211}
2212
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00002213/* Issue 6012 */
2214static PyObject *str1, *str2;
2215static int
2216failing_converter(PyObject *obj, void *arg)
2217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 /* Clone str1, then let the conversion fail. */
2219 assert(str1);
2220 str2 = str1;
2221 Py_INCREF(str2);
2222 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00002223}
2224static PyObject*
2225argparsing(PyObject *o, PyObject *args)
2226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 PyObject *res;
2228 str1 = str2 = NULL;
2229 if (!PyArg_ParseTuple(args, "O&O&",
2230 PyUnicode_FSConverter, &str1,
2231 failing_converter, &str2)) {
2232 if (!str2)
2233 /* argument converter not called? */
2234 return NULL;
2235 /* Should be 1 */
Victor Stinner0fcab4a2011-01-04 12:59:15 +00002236 res = PyLong_FromSsize_t(Py_REFCNT(str2));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 Py_DECREF(str2);
2238 PyErr_Clear();
2239 return res;
2240 }
2241 Py_RETURN_NONE;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00002242}
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002243
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002244/* To test that the result of PyCode_NewEmpty has the right members. */
2245static PyObject *
2246code_newempty(PyObject *self, PyObject *args)
2247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 const char *filename;
2249 const char *funcname;
2250 int firstlineno;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 if (!PyArg_ParseTuple(args, "ssi:code_newempty",
2253 &filename, &funcname, &firstlineno))
2254 return NULL;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002257}
2258
Georg Brandl1e28a272009-12-28 08:41:01 +00002259/* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException).
2260 Run via Lib/test/test_exceptions.py */
2261static PyObject *
2262make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs)
2263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 const char *name;
2265 const char *doc = NULL;
2266 PyObject *base = NULL;
2267 PyObject *dict = NULL;
Georg Brandl1e28a272009-12-28 08:41:01 +00002268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 static char *kwlist[] = {"name", "doc", "base", "dict", NULL};
Georg Brandl1e28a272009-12-28 08:41:01 +00002270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
2272 "s|sOO:make_exception_with_doc", kwlist,
2273 &name, &doc, &base, &dict))
2274 return NULL;
Georg Brandl1e28a272009-12-28 08:41:01 +00002275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 return PyErr_NewExceptionWithDoc(name, doc, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +00002277}
2278
Antoine Pitrou915605c2011-02-24 20:53:48 +00002279static PyObject *
2280make_memoryview_from_NULL_pointer(PyObject *self)
2281{
2282 Py_buffer info;
2283 if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0)
2284 return NULL;
2285 return PyMemoryView_FromBuffer(&info);
2286}
2287
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00002288/* Test that the fatal error from not having a current thread doesn't
2289 cause an infinite loop. Run via Lib/test/test_capi.py */
2290static PyObject *
2291crash_no_current_thread(PyObject *self)
2292{
2293 Py_BEGIN_ALLOW_THREADS
Jeffrey Yasskinea7b7482010-05-17 16:59:23 +00002294 /* Using PyThreadState_Get() directly allows the test to pass in
2295 !pydebug mode. However, the test only actually tests anything
2296 in pydebug mode, since that's where the infinite loop was in
2297 the first place. */
2298 PyThreadState_Get();
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00002299 Py_END_ALLOW_THREADS
2300 return NULL;
2301}
2302
Antoine Pitrou2f828f22012-01-18 00:21:11 +01002303/* To run some code in a sub-interpreter. */
2304static PyObject *
2305run_in_subinterp(PyObject *self, PyObject *args)
2306{
2307 const char *code;
2308 int r;
2309 PyThreadState *substate, *mainstate;
2310
2311 if (!PyArg_ParseTuple(args, "s:run_in_subinterp",
2312 &code))
2313 return NULL;
2314
2315 mainstate = PyThreadState_Get();
2316
2317 PyThreadState_Swap(NULL);
2318
2319 substate = Py_NewInterpreter();
2320 r = PyRun_SimpleString(code);
2321 Py_EndInterpreter(substate);
2322
2323 PyThreadState_Swap(mainstate);
2324
2325 return PyLong_FromLong(r);
2326}
2327
2328
Tim Peters9ea17ac2001-02-02 05:57:15 +00002329static PyMethodDef TestMethods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 {"raise_exception", raise_exception, METH_VARARGS},
2331 {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
2332 {"test_config", (PyCFunction)test_config, METH_NOARGS},
2333 {"test_datetime_capi", test_datetime_capi, METH_NOARGS},
2334 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
2335 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
2336 {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS},
2337 {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS},
2338 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
2339 {"test_long_and_overflow", (PyCFunction)test_long_and_overflow,
2340 METH_NOARGS},
2341 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
2342 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
2343 {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
2344 {"test_bug_7414", (PyCFunction)test_bug_7414, METH_NOARGS},
2345 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
2346 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
2347 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
2348 PyDoc_STR("This is a pretty normal docstring.")},
2349 {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS},
2350 {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS},
2351 {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
2352 {"getargs_tuple", getargs_tuple, METH_VARARGS},
2353 {"getargs_keywords", (PyCFunction)getargs_keywords,
2354 METH_VARARGS|METH_KEYWORDS},
2355 {"getargs_b", getargs_b, METH_VARARGS},
2356 {"getargs_B", getargs_B, METH_VARARGS},
2357 {"getargs_h", getargs_h, METH_VARARGS},
2358 {"getargs_H", getargs_H, METH_VARARGS},
2359 {"getargs_I", getargs_I, METH_VARARGS},
2360 {"getargs_k", getargs_k, METH_VARARGS},
2361 {"getargs_i", getargs_i, METH_VARARGS},
2362 {"getargs_l", getargs_l, METH_VARARGS},
2363 {"getargs_n", getargs_n, METH_VARARGS},
Tim Peters91621db2001-06-12 20:10:01 +00002364#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 {"getargs_L", getargs_L, METH_VARARGS},
2366 {"getargs_K", getargs_K, METH_VARARGS},
2367 {"test_longlong_api", test_longlong_api, METH_NOARGS},
2368 {"test_long_long_and_overflow",
2369 (PyCFunction)test_long_long_and_overflow, METH_NOARGS},
2370 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
Victor Stinner06e49dd2010-06-13 18:21:50 +00002371#endif
2372 {"getargs_s", getargs_s, METH_VARARGS},
2373 {"getargs_s_star", getargs_s_star, METH_VARARGS},
2374 {"getargs_s_hash", getargs_s_hash, METH_VARARGS},
2375 {"getargs_z", getargs_z, METH_VARARGS},
2376 {"getargs_z_star", getargs_z_star, METH_VARARGS},
2377 {"getargs_z_hash", getargs_z_hash, METH_VARARGS},
2378 {"getargs_y", getargs_y, METH_VARARGS},
2379 {"getargs_y_star", getargs_y_star, METH_VARARGS},
2380 {"getargs_y_hash", getargs_y_hash, METH_VARARGS},
2381 {"getargs_u", getargs_u, METH_VARARGS},
2382 {"getargs_u_hash", getargs_u_hash, METH_VARARGS},
2383 {"getargs_Z", getargs_Z, METH_VARARGS},
2384 {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS},
Victor Stinner25e8ec42010-06-25 00:02:38 +00002385 {"getargs_w_star", getargs_w_star, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 {"codec_incrementalencoder",
Victor Stinner1c24bd02010-10-02 11:03:13 +00002387 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 {"codec_incrementaldecoder",
Victor Stinner1c24bd02010-10-02 11:03:13 +00002389 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS},
2391 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
2392 {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS},
2393 {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS},
Victor Stinnerab1d16b2011-11-22 01:45:37 +01002394 {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS},
2395 {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS},
2396 {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS},
2397 {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS},
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002398#ifdef WITH_THREAD
Victor Stinner1c24bd02010-10-02 11:03:13 +00002399 {"_test_thread_state", test_thread_state, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 {"_pending_threadfunc", pending_threadfunc, METH_VARARGS},
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002401#endif
Guido van Rossumddefaf32007-01-14 03:31:43 +00002402#ifdef HAVE_GETTIMEOFDAY
Victor Stinner1c24bd02010-10-02 11:03:13 +00002403 {"profile_int", profile_int, METH_NOARGS},
Guido van Rossumddefaf32007-01-14 03:31:43 +00002404#endif
Victor Stinner1c24bd02010-10-02 11:03:13 +00002405 {"traceback_print", traceback_print, METH_VARARGS},
2406 {"exception_print", exception_print, METH_VARARGS},
2407 {"argparsing", argparsing, METH_VARARGS},
2408 {"code_newempty", code_newempty, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 {"make_exception_with_doc", (PyCFunction)make_exception_with_doc,
2410 METH_VARARGS | METH_KEYWORDS},
Antoine Pitrou915605c2011-02-24 20:53:48 +00002411 {"make_memoryview_from_NULL_pointer", (PyCFunction)make_memoryview_from_NULL_pointer,
2412 METH_NOARGS},
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00002413 {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS},
Antoine Pitrou2f828f22012-01-18 00:21:11 +01002414 {"run_in_subinterp", run_in_subinterp, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 {NULL, NULL} /* sentinel */
Tim Peters9ea17ac2001-02-02 05:57:15 +00002416};
2417
Thomas Hellera4ea6032003-04-17 18:55:45 +00002418#define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
2419
Thomas Wouters89f507f2006-12-13 04:49:30 +00002420typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 char bool_member;
2422 char byte_member;
2423 unsigned char ubyte_member;
2424 short short_member;
2425 unsigned short ushort_member;
2426 int int_member;
2427 unsigned int uint_member;
2428 long long_member;
2429 unsigned long ulong_member;
2430 Py_ssize_t pyssizet_member;
2431 float float_member;
2432 double double_member;
2433 char inplace_member[6];
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002434#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 PY_LONG_LONG longlong_member;
2436 unsigned PY_LONG_LONG ulonglong_member;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002437#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +00002438} all_structmembers;
2439
2440typedef struct {
2441 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 all_structmembers structmembers;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002443} test_structmembers;
2444
2445static struct PyMemberDef test_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
2447 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
2448 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
2449 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
2450 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
2451 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
2452 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
2453 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
2454 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
2455 {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL},
2456 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
2457 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
2458 {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002459#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
2461 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002462#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 {NULL}
Thomas Wouters89f507f2006-12-13 04:49:30 +00002464};
2465
2466
Christian Heimes1af737c2008-01-23 08:24:23 +00002467static PyObject *
2468test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 static char *keywords[] = {
2471 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
2472 "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET",
2473 "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
Mark Dickinsond59b4162010-03-13 11:34:40 +00002474#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 "T_LONGLONG", "T_ULONGLONG",
Christian Heimes1af737c2008-01-23 08:24:23 +00002476#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 NULL};
2478 static char *fmt = "|bbBhHiIlknfds#"
Christian Heimes1af737c2008-01-23 08:24:23 +00002479#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 "LK"
Christian Heimes1af737c2008-01-23 08:24:23 +00002481#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 ;
2483 test_structmembers *ob;
2484 const char *s = NULL;
2485 Py_ssize_t string_len = 0;
2486 ob = PyObject_New(test_structmembers, type);
2487 if (ob == NULL)
2488 return NULL;
2489 memset(&ob->structmembers, 0, sizeof(all_structmembers));
2490 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
2491 &ob->structmembers.bool_member,
2492 &ob->structmembers.byte_member,
2493 &ob->structmembers.ubyte_member,
2494 &ob->structmembers.short_member,
2495 &ob->structmembers.ushort_member,
2496 &ob->structmembers.int_member,
2497 &ob->structmembers.uint_member,
2498 &ob->structmembers.long_member,
2499 &ob->structmembers.ulong_member,
2500 &ob->structmembers.pyssizet_member,
2501 &ob->structmembers.float_member,
2502 &ob->structmembers.double_member,
2503 &s, &string_len
Christian Heimes1af737c2008-01-23 08:24:23 +00002504#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 , &ob->structmembers.longlong_member,
2506 &ob->structmembers.ulonglong_member
Christian Heimes1af737c2008-01-23 08:24:23 +00002507#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 )) {
2509 Py_DECREF(ob);
2510 return NULL;
2511 }
2512 if (s != NULL) {
2513 if (string_len > 5) {
2514 Py_DECREF(ob);
2515 PyErr_SetString(PyExc_ValueError, "string too long");
2516 return NULL;
2517 }
2518 strcpy(ob->structmembers.inplace_member, s);
2519 }
2520 else {
2521 strcpy(ob->structmembers.inplace_member, "");
2522 }
2523 return (PyObject *)ob;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002524}
2525
Christian Heimes1af737c2008-01-23 08:24:23 +00002526static void
2527test_structmembers_free(PyObject *ob)
2528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 PyObject_FREE(ob);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002530}
2531
2532static PyTypeObject test_structmembersType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002533 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 "test_structmembersType",
2535 sizeof(test_structmembers), /* tp_basicsize */
2536 0, /* tp_itemsize */
2537 test_structmembers_free, /* destructor tp_dealloc */
2538 0, /* tp_print */
2539 0, /* tp_getattr */
2540 0, /* tp_setattr */
2541 0, /* tp_reserved */
2542 0, /* tp_repr */
2543 0, /* tp_as_number */
2544 0, /* tp_as_sequence */
2545 0, /* tp_as_mapping */
2546 0, /* tp_hash */
2547 0, /* tp_call */
2548 0, /* tp_str */
2549 PyObject_GenericGetAttr, /* tp_getattro */
2550 PyObject_GenericSetAttr, /* tp_setattro */
2551 0, /* tp_as_buffer */
2552 0, /* tp_flags */
2553 "Type containing all structmember types",
2554 0, /* traverseproc tp_traverse */
2555 0, /* tp_clear */
2556 0, /* tp_richcompare */
2557 0, /* tp_weaklistoffset */
2558 0, /* tp_iter */
2559 0, /* tp_iternext */
2560 0, /* tp_methods */
2561 test_members, /* tp_members */
2562 0,
2563 0,
2564 0,
2565 0,
2566 0,
2567 0,
2568 0,
2569 0,
2570 test_structmembers_new, /* tp_new */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002571};
2572
2573
Martin v. Löwis1a214512008-06-11 05:26:20 +00002574
2575static struct PyModuleDef _testcapimodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 PyModuleDef_HEAD_INIT,
2577 "_testcapi",
2578 NULL,
2579 -1,
2580 TestMethods,
2581 NULL,
2582 NULL,
2583 NULL,
2584 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002585};
2586
Mark Hammond62b1ab12002-07-23 06:31:15 +00002587PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002588PyInit__testcapi(void)
Tim Peters9ea17ac2001-02-02 05:57:15 +00002589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 PyObject *m;
Tim Peters9ea17ac2001-02-02 05:57:15 +00002591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 m = PyModule_Create(&_testcapimodule);
2593 if (m == NULL)
2594 return NULL;
Tim Peters9ea17ac2001-02-02 05:57:15 +00002595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
2597 Py_TYPE(&_MemoryViewTester_Type)=&PyType_Type;
Hirokazu Yamamoto8ebab5d2008-12-31 06:05:46 +00002598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 Py_TYPE(&test_structmembersType)=&PyType_Type;
2600 Py_INCREF(&test_structmembersType);
2601 /* don't use a name starting with "test", since we don't want
2602 test_capi to automatically call this */
2603 PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
2606 PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
2607 PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX));
2608 PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX));
2609 PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN));
2610 PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX));
2611 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
2612 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
2613 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
2614 PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX));
2615 PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN));
2616 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
2617 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
2618 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
2619 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
2620 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
2621 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
2622 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
2623 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
2624 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
2625 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
2626 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
2627 Py_INCREF(&PyInstanceMethod_Type);
2628 PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
Thomas Hellera4ea6032003-04-17 18:55:45 +00002629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
2631 Py_INCREF(TestError);
2632 PyModule_AddObject(m, "error", TestError);
2633 return m;
Tim Peters9ea17ac2001-02-02 05:57:15 +00002634}