1 | /*
|
---|
2 | * C Extension module to test Python interpreter C APIs.
|
---|
3 | *
|
---|
4 | * The 'test_*' functions exported by this module are run as part of the
|
---|
5 | * standard Python regression test, via Lib/test/test_capi.py.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "Python.h"
|
---|
9 |
|
---|
10 | #ifdef WITH_THREAD
|
---|
11 | #include "pythread.h"
|
---|
12 | #endif /* WITH_THREAD */
|
---|
13 | static PyObject *TestError; /* set to exception object in init */
|
---|
14 |
|
---|
15 | /* Raise TestError with test_name + ": " + msg, and return NULL. */
|
---|
16 |
|
---|
17 | static PyObject *
|
---|
18 | raiseTestError(const char* test_name, const char* msg)
|
---|
19 | {
|
---|
20 | char buf[2048];
|
---|
21 |
|
---|
22 | if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
|
---|
23 | PyErr_SetString(TestError, "internal error msg too large");
|
---|
24 | else {
|
---|
25 | PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
|
---|
26 | PyErr_SetString(TestError, buf);
|
---|
27 | }
|
---|
28 | return NULL;
|
---|
29 | }
|
---|
30 |
|
---|
31 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
|
---|
32 |
|
---|
33 | The ones derived from autoconf on the UNIX-like OSes can be relied
|
---|
34 | upon (in the absence of sloppy cross-compiling), but the Windows
|
---|
35 | platforms have these hardcoded. Better safe than sorry.
|
---|
36 | */
|
---|
37 | static PyObject*
|
---|
38 | sizeof_error(const char* fatname, const char* typename,
|
---|
39 | int expected, int got)
|
---|
40 | {
|
---|
41 | char buf[1024];
|
---|
42 | PyOS_snprintf(buf, sizeof(buf),
|
---|
43 | "%.200s #define == %d but sizeof(%.200s) == %d",
|
---|
44 | fatname, expected, typename, got);
|
---|
45 | PyErr_SetString(TestError, buf);
|
---|
46 | return (PyObject*)NULL;
|
---|
47 | }
|
---|
48 |
|
---|
49 | static PyObject*
|
---|
50 | test_config(PyObject *self)
|
---|
51 | {
|
---|
52 | #define CHECK_SIZEOF(FATNAME, TYPE) \
|
---|
53 | if (FATNAME != sizeof(TYPE)) \
|
---|
54 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
|
---|
55 |
|
---|
56 | CHECK_SIZEOF(SIZEOF_SHORT, short);
|
---|
57 | CHECK_SIZEOF(SIZEOF_INT, int);
|
---|
58 | CHECK_SIZEOF(SIZEOF_LONG, long);
|
---|
59 | CHECK_SIZEOF(SIZEOF_VOID_P, void*);
|
---|
60 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
|
---|
61 | #ifdef HAVE_LONG_LONG
|
---|
62 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | #undef CHECK_SIZEOF
|
---|
66 |
|
---|
67 | Py_INCREF(Py_None);
|
---|
68 | return Py_None;
|
---|
69 | }
|
---|
70 |
|
---|
71 | static PyObject*
|
---|
72 | test_list_api(PyObject *self)
|
---|
73 | {
|
---|
74 | PyObject* list;
|
---|
75 | int i;
|
---|
76 |
|
---|
77 | /* SF bug 132008: PyList_Reverse segfaults */
|
---|
78 | #define NLIST 30
|
---|
79 | list = PyList_New(NLIST);
|
---|
80 | if (list == (PyObject*)NULL)
|
---|
81 | return (PyObject*)NULL;
|
---|
82 | /* list = range(NLIST) */
|
---|
83 | for (i = 0; i < NLIST; ++i) {
|
---|
84 | PyObject* anint = PyInt_FromLong(i);
|
---|
85 | if (anint == (PyObject*)NULL) {
|
---|
86 | Py_DECREF(list);
|
---|
87 | return (PyObject*)NULL;
|
---|
88 | }
|
---|
89 | PyList_SET_ITEM(list, i, anint);
|
---|
90 | }
|
---|
91 | /* list.reverse(), via PyList_Reverse() */
|
---|
92 | i = PyList_Reverse(list); /* should not blow up! */
|
---|
93 | if (i != 0) {
|
---|
94 | Py_DECREF(list);
|
---|
95 | return (PyObject*)NULL;
|
---|
96 | }
|
---|
97 | /* Check that list == range(29, -1, -1) now */
|
---|
98 | for (i = 0; i < NLIST; ++i) {
|
---|
99 | PyObject* anint = PyList_GET_ITEM(list, i);
|
---|
100 | if (PyInt_AS_LONG(anint) != NLIST-1-i) {
|
---|
101 | PyErr_SetString(TestError,
|
---|
102 | "test_list_api: reverse screwed up");
|
---|
103 | Py_DECREF(list);
|
---|
104 | return (PyObject*)NULL;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | Py_DECREF(list);
|
---|
108 | #undef NLIST
|
---|
109 |
|
---|
110 | Py_INCREF(Py_None);
|
---|
111 | return Py_None;
|
---|
112 | }
|
---|
113 |
|
---|
114 | static int
|
---|
115 | test_dict_inner(int count)
|
---|
116 | {
|
---|
117 | Py_ssize_t pos = 0, iterations = 0;
|
---|
118 | int i;
|
---|
119 | PyObject *dict = PyDict_New();
|
---|
120 | PyObject *v, *k;
|
---|
121 |
|
---|
122 | if (dict == NULL)
|
---|
123 | return -1;
|
---|
124 |
|
---|
125 | for (i = 0; i < count; i++) {
|
---|
126 | v = PyInt_FromLong(i);
|
---|
127 | PyDict_SetItem(dict, v, v);
|
---|
128 | Py_DECREF(v);
|
---|
129 | }
|
---|
130 |
|
---|
131 | while (PyDict_Next(dict, &pos, &k, &v)) {
|
---|
132 | PyObject *o;
|
---|
133 | iterations++;
|
---|
134 |
|
---|
135 | i = PyInt_AS_LONG(v) + 1;
|
---|
136 | o = PyInt_FromLong(i);
|
---|
137 | if (o == NULL)
|
---|
138 | return -1;
|
---|
139 | if (PyDict_SetItem(dict, k, o) < 0) {
|
---|
140 | Py_DECREF(o);
|
---|
141 | return -1;
|
---|
142 | }
|
---|
143 | Py_DECREF(o);
|
---|
144 | }
|
---|
145 |
|
---|
146 | Py_DECREF(dict);
|
---|
147 |
|
---|
148 | if (iterations != count) {
|
---|
149 | PyErr_SetString(
|
---|
150 | TestError,
|
---|
151 | "test_dict_iteration: dict iteration went wrong ");
|
---|
152 | return -1;
|
---|
153 | } else {
|
---|
154 | return 0;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | static PyObject*
|
---|
159 | test_dict_iteration(PyObject* self)
|
---|
160 | {
|
---|
161 | int i;
|
---|
162 |
|
---|
163 | for (i = 0; i < 200; i++) {
|
---|
164 | if (test_dict_inner(i) < 0) {
|
---|
165 | return NULL;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | Py_INCREF(Py_None);
|
---|
170 | return Py_None;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
|
---|
175 | PyLong_{As, From}{Unsigned,}LongLong().
|
---|
176 |
|
---|
177 | Note that the meat of the test is contained in testcapi_long.h.
|
---|
178 | This is revolting, but delicate code duplication is worse: "almost
|
---|
179 | exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
|
---|
180 | dependence on type names makes it impossible to use a parameterized
|
---|
181 | function. A giant macro would be even worse than this. A C++ template
|
---|
182 | would be perfect.
|
---|
183 |
|
---|
184 | The "report an error" functions are deliberately not part of the #include
|
---|
185 | file: if the test fails, you can set a breakpoint in the appropriate
|
---|
186 | error function directly, and crawl back from there in the debugger.
|
---|
187 | */
|
---|
188 |
|
---|
189 | #define UNBIND(X) Py_DECREF(X); (X) = NULL
|
---|
190 |
|
---|
191 | static PyObject *
|
---|
192 | raise_test_long_error(const char* msg)
|
---|
193 | {
|
---|
194 | return raiseTestError("test_long_api", msg);
|
---|
195 | }
|
---|
196 |
|
---|
197 | #define TESTNAME test_long_api_inner
|
---|
198 | #define TYPENAME long
|
---|
199 | #define F_S_TO_PY PyLong_FromLong
|
---|
200 | #define F_PY_TO_S PyLong_AsLong
|
---|
201 | #define F_U_TO_PY PyLong_FromUnsignedLong
|
---|
202 | #define F_PY_TO_U PyLong_AsUnsignedLong
|
---|
203 |
|
---|
204 | #include "testcapi_long.h"
|
---|
205 |
|
---|
206 | static PyObject *
|
---|
207 | test_long_api(PyObject* self)
|
---|
208 | {
|
---|
209 | return TESTNAME(raise_test_long_error);
|
---|
210 | }
|
---|
211 |
|
---|
212 | #undef TESTNAME
|
---|
213 | #undef TYPENAME
|
---|
214 | #undef F_S_TO_PY
|
---|
215 | #undef F_PY_TO_S
|
---|
216 | #undef F_U_TO_PY
|
---|
217 | #undef F_PY_TO_U
|
---|
218 |
|
---|
219 | #ifdef HAVE_LONG_LONG
|
---|
220 |
|
---|
221 | static PyObject *
|
---|
222 | raise_test_longlong_error(const char* msg)
|
---|
223 | {
|
---|
224 | return raiseTestError("test_longlong_api", msg);
|
---|
225 | }
|
---|
226 |
|
---|
227 | #define TESTNAME test_longlong_api_inner
|
---|
228 | #define TYPENAME PY_LONG_LONG
|
---|
229 | #define F_S_TO_PY PyLong_FromLongLong
|
---|
230 | #define F_PY_TO_S PyLong_AsLongLong
|
---|
231 | #define F_U_TO_PY PyLong_FromUnsignedLongLong
|
---|
232 | #define F_PY_TO_U PyLong_AsUnsignedLongLong
|
---|
233 |
|
---|
234 | #include "testcapi_long.h"
|
---|
235 |
|
---|
236 | static PyObject *
|
---|
237 | test_longlong_api(PyObject* self, PyObject *args)
|
---|
238 | {
|
---|
239 | return TESTNAME(raise_test_longlong_error);
|
---|
240 | }
|
---|
241 |
|
---|
242 | #undef TESTNAME
|
---|
243 | #undef TYPENAME
|
---|
244 | #undef F_S_TO_PY
|
---|
245 | #undef F_PY_TO_S
|
---|
246 | #undef F_U_TO_PY
|
---|
247 | #undef F_PY_TO_U
|
---|
248 |
|
---|
249 | /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
|
---|
250 | for both long and int arguments. The test may leak a little memory if
|
---|
251 | it fails.
|
---|
252 | */
|
---|
253 | static PyObject *
|
---|
254 | test_L_code(PyObject *self)
|
---|
255 | {
|
---|
256 | PyObject *tuple, *num;
|
---|
257 | PY_LONG_LONG value;
|
---|
258 |
|
---|
259 | tuple = PyTuple_New(1);
|
---|
260 | if (tuple == NULL)
|
---|
261 | return NULL;
|
---|
262 |
|
---|
263 | num = PyLong_FromLong(42);
|
---|
264 | if (num == NULL)
|
---|
265 | return NULL;
|
---|
266 |
|
---|
267 | PyTuple_SET_ITEM(tuple, 0, num);
|
---|
268 |
|
---|
269 | value = -1;
|
---|
270 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
|
---|
271 | return NULL;
|
---|
272 | if (value != 42)
|
---|
273 | return raiseTestError("test_L_code",
|
---|
274 | "L code returned wrong value for long 42");
|
---|
275 |
|
---|
276 | Py_DECREF(num);
|
---|
277 | num = PyInt_FromLong(42);
|
---|
278 | if (num == NULL)
|
---|
279 | return NULL;
|
---|
280 |
|
---|
281 | PyTuple_SET_ITEM(tuple, 0, num);
|
---|
282 |
|
---|
283 | value = -1;
|
---|
284 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
|
---|
285 | return NULL;
|
---|
286 | if (value != 42)
|
---|
287 | return raiseTestError("test_L_code",
|
---|
288 | "L code returned wrong value for int 42");
|
---|
289 |
|
---|
290 | Py_DECREF(tuple);
|
---|
291 | Py_INCREF(Py_None);
|
---|
292 | return Py_None;
|
---|
293 | }
|
---|
294 |
|
---|
295 | #endif /* ifdef HAVE_LONG_LONG */
|
---|
296 |
|
---|
297 | /* Test tuple argument processing */
|
---|
298 | static PyObject *
|
---|
299 | getargs_tuple(PyObject *self, PyObject *args)
|
---|
300 | {
|
---|
301 | int a, b, c;
|
---|
302 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
|
---|
303 | return NULL;
|
---|
304 | return Py_BuildValue("iii", a, b, c);
|
---|
305 | }
|
---|
306 |
|
---|
307 | /* Functions to call PyArg_ParseTuple with integer format codes,
|
---|
308 | and return the result.
|
---|
309 | */
|
---|
310 | static PyObject *
|
---|
311 | getargs_b(PyObject *self, PyObject *args)
|
---|
312 | {
|
---|
313 | unsigned char value;
|
---|
314 | if (!PyArg_ParseTuple(args, "b", &value))
|
---|
315 | return NULL;
|
---|
316 | return PyLong_FromUnsignedLong((unsigned long)value);
|
---|
317 | }
|
---|
318 |
|
---|
319 | static PyObject *
|
---|
320 | getargs_B(PyObject *self, PyObject *args)
|
---|
321 | {
|
---|
322 | unsigned char value;
|
---|
323 | if (!PyArg_ParseTuple(args, "B", &value))
|
---|
324 | return NULL;
|
---|
325 | return PyLong_FromUnsignedLong((unsigned long)value);
|
---|
326 | }
|
---|
327 |
|
---|
328 | static PyObject *
|
---|
329 | getargs_H(PyObject *self, PyObject *args)
|
---|
330 | {
|
---|
331 | unsigned short value;
|
---|
332 | if (!PyArg_ParseTuple(args, "H", &value))
|
---|
333 | return NULL;
|
---|
334 | return PyLong_FromUnsignedLong((unsigned long)value);
|
---|
335 | }
|
---|
336 |
|
---|
337 | static PyObject *
|
---|
338 | getargs_I(PyObject *self, PyObject *args)
|
---|
339 | {
|
---|
340 | unsigned int value;
|
---|
341 | if (!PyArg_ParseTuple(args, "I", &value))
|
---|
342 | return NULL;
|
---|
343 | return PyLong_FromUnsignedLong((unsigned long)value);
|
---|
344 | }
|
---|
345 |
|
---|
346 | static PyObject *
|
---|
347 | getargs_k(PyObject *self, PyObject *args)
|
---|
348 | {
|
---|
349 | unsigned long value;
|
---|
350 | if (!PyArg_ParseTuple(args, "k", &value))
|
---|
351 | return NULL;
|
---|
352 | return PyLong_FromUnsignedLong(value);
|
---|
353 | }
|
---|
354 |
|
---|
355 | static PyObject *
|
---|
356 | getargs_i(PyObject *self, PyObject *args)
|
---|
357 | {
|
---|
358 | int value;
|
---|
359 | if (!PyArg_ParseTuple(args, "i", &value))
|
---|
360 | return NULL;
|
---|
361 | return PyLong_FromLong((long)value);
|
---|
362 | }
|
---|
363 |
|
---|
364 | static PyObject *
|
---|
365 | getargs_l(PyObject *self, PyObject *args)
|
---|
366 | {
|
---|
367 | long value;
|
---|
368 | if (!PyArg_ParseTuple(args, "l", &value))
|
---|
369 | return NULL;
|
---|
370 | return PyLong_FromLong(value);
|
---|
371 | }
|
---|
372 |
|
---|
373 | static PyObject *
|
---|
374 | getargs_n(PyObject *self, PyObject *args)
|
---|
375 | {
|
---|
376 | Py_ssize_t value;
|
---|
377 | if (!PyArg_ParseTuple(args, "n", &value))
|
---|
378 | return NULL;
|
---|
379 | return PyInt_FromSsize_t(value);
|
---|
380 | }
|
---|
381 |
|
---|
382 | #ifdef HAVE_LONG_LONG
|
---|
383 | static PyObject *
|
---|
384 | getargs_L(PyObject *self, PyObject *args)
|
---|
385 | {
|
---|
386 | PY_LONG_LONG value;
|
---|
387 | if (!PyArg_ParseTuple(args, "L", &value))
|
---|
388 | return NULL;
|
---|
389 | return PyLong_FromLongLong(value);
|
---|
390 | }
|
---|
391 |
|
---|
392 | static PyObject *
|
---|
393 | getargs_K(PyObject *self, PyObject *args)
|
---|
394 | {
|
---|
395 | unsigned PY_LONG_LONG value;
|
---|
396 | if (!PyArg_ParseTuple(args, "K", &value))
|
---|
397 | return NULL;
|
---|
398 | return PyLong_FromUnsignedLongLong(value);
|
---|
399 | }
|
---|
400 | #endif
|
---|
401 |
|
---|
402 | /* This function not only tests the 'k' getargs code, but also the
|
---|
403 | PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
|
---|
404 | static PyObject *
|
---|
405 | test_k_code(PyObject *self)
|
---|
406 | {
|
---|
407 | PyObject *tuple, *num;
|
---|
408 | unsigned long value;
|
---|
409 |
|
---|
410 | tuple = PyTuple_New(1);
|
---|
411 | if (tuple == NULL)
|
---|
412 | return NULL;
|
---|
413 |
|
---|
414 | /* a number larger than ULONG_MAX even on 64-bit platforms */
|
---|
415 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
|
---|
416 | if (num == NULL)
|
---|
417 | return NULL;
|
---|
418 |
|
---|
419 | value = PyInt_AsUnsignedLongMask(num);
|
---|
420 | if (value != ULONG_MAX)
|
---|
421 | return raiseTestError("test_k_code",
|
---|
422 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
|
---|
423 |
|
---|
424 | PyTuple_SET_ITEM(tuple, 0, num);
|
---|
425 |
|
---|
426 | value = 0;
|
---|
427 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
|
---|
428 | return NULL;
|
---|
429 | if (value != ULONG_MAX)
|
---|
430 | return raiseTestError("test_k_code",
|
---|
431 | "k code returned wrong value for long 0xFFF...FFF");
|
---|
432 |
|
---|
433 | Py_DECREF(num);
|
---|
434 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
|
---|
435 | if (num == NULL)
|
---|
436 | return NULL;
|
---|
437 |
|
---|
438 | value = PyInt_AsUnsignedLongMask(num);
|
---|
439 | if (value != (unsigned long)-0x42)
|
---|
440 | return raiseTestError("test_k_code",
|
---|
441 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
|
---|
442 |
|
---|
443 | PyTuple_SET_ITEM(tuple, 0, num);
|
---|
444 |
|
---|
445 | value = 0;
|
---|
446 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
|
---|
447 | return NULL;
|
---|
448 | if (value != (unsigned long)-0x42)
|
---|
449 | return raiseTestError("test_k_code",
|
---|
450 | "k code returned wrong value for long -0xFFF..000042");
|
---|
451 |
|
---|
452 | Py_DECREF(tuple);
|
---|
453 | Py_INCREF(Py_None);
|
---|
454 | return Py_None;
|
---|
455 | }
|
---|
456 |
|
---|
457 | #ifdef Py_USING_UNICODE
|
---|
458 |
|
---|
459 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
|
---|
460 | of an error.
|
---|
461 | */
|
---|
462 | static PyObject *
|
---|
463 | test_u_code(PyObject *self)
|
---|
464 | {
|
---|
465 | PyObject *tuple, *obj;
|
---|
466 | Py_UNICODE *value;
|
---|
467 | int len;
|
---|
468 |
|
---|
469 | tuple = PyTuple_New(1);
|
---|
470 | if (tuple == NULL)
|
---|
471 | return NULL;
|
---|
472 |
|
---|
473 | obj = PyUnicode_Decode("test", strlen("test"),
|
---|
474 | "ascii", NULL);
|
---|
475 | if (obj == NULL)
|
---|
476 | return NULL;
|
---|
477 |
|
---|
478 | PyTuple_SET_ITEM(tuple, 0, obj);
|
---|
479 |
|
---|
480 | value = 0;
|
---|
481 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
|
---|
482 | return NULL;
|
---|
483 | if (value != PyUnicode_AS_UNICODE(obj))
|
---|
484 | return raiseTestError("test_u_code",
|
---|
485 | "u code returned wrong value for u'test'");
|
---|
486 | value = 0;
|
---|
487 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
|
---|
488 | return NULL;
|
---|
489 | if (value != PyUnicode_AS_UNICODE(obj) ||
|
---|
490 | len != PyUnicode_GET_SIZE(obj))
|
---|
491 | return raiseTestError("test_u_code",
|
---|
492 | "u# code returned wrong values for u'test'");
|
---|
493 |
|
---|
494 | Py_DECREF(tuple);
|
---|
495 | Py_INCREF(Py_None);
|
---|
496 | return Py_None;
|
---|
497 | }
|
---|
498 |
|
---|
499 | static PyObject *
|
---|
500 | codec_incrementalencoder(PyObject *self, PyObject *args)
|
---|
501 | {
|
---|
502 | const char *encoding, *errors = NULL;
|
---|
503 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
|
---|
504 | &encoding, &errors))
|
---|
505 | return NULL;
|
---|
506 | return PyCodec_IncrementalEncoder(encoding, errors);
|
---|
507 | }
|
---|
508 |
|
---|
509 | static PyObject *
|
---|
510 | codec_incrementaldecoder(PyObject *self, PyObject *args)
|
---|
511 | {
|
---|
512 | const char *encoding, *errors = NULL;
|
---|
513 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
|
---|
514 | &encoding, &errors))
|
---|
515 | return NULL;
|
---|
516 | return PyCodec_IncrementalDecoder(encoding, errors);
|
---|
517 | }
|
---|
518 |
|
---|
519 | #endif
|
---|
520 |
|
---|
521 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
|
---|
522 | static PyObject *
|
---|
523 | test_long_numbits(PyObject *self)
|
---|
524 | {
|
---|
525 | struct triple {
|
---|
526 | long input;
|
---|
527 | size_t nbits;
|
---|
528 | int sign;
|
---|
529 | } testcases[] = {{0, 0, 0},
|
---|
530 | {1L, 1, 1},
|
---|
531 | {-1L, 1, -1},
|
---|
532 | {2L, 2, 1},
|
---|
533 | {-2L, 2, -1},
|
---|
534 | {3L, 2, 1},
|
---|
535 | {-3L, 2, -1},
|
---|
536 | {4L, 3, 1},
|
---|
537 | {-4L, 3, -1},
|
---|
538 | {0x7fffL, 15, 1}, /* one Python long digit */
|
---|
539 | {-0x7fffL, 15, -1},
|
---|
540 | {0xffffL, 16, 1},
|
---|
541 | {-0xffffL, 16, -1},
|
---|
542 | {0xfffffffL, 28, 1},
|
---|
543 | {-0xfffffffL, 28, -1}};
|
---|
544 | int i;
|
---|
545 |
|
---|
546 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
|
---|
547 | PyObject *plong = PyLong_FromLong(testcases[i].input);
|
---|
548 | size_t nbits = _PyLong_NumBits(plong);
|
---|
549 | int sign = _PyLong_Sign(plong);
|
---|
550 |
|
---|
551 | Py_DECREF(plong);
|
---|
552 | if (nbits != testcases[i].nbits)
|
---|
553 | return raiseTestError("test_long_numbits",
|
---|
554 | "wrong result for _PyLong_NumBits");
|
---|
555 | if (sign != testcases[i].sign)
|
---|
556 | return raiseTestError("test_long_numbits",
|
---|
557 | "wrong result for _PyLong_Sign");
|
---|
558 | }
|
---|
559 | Py_INCREF(Py_None);
|
---|
560 | return Py_None;
|
---|
561 | }
|
---|
562 |
|
---|
563 | /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
|
---|
564 |
|
---|
565 | static PyObject *
|
---|
566 | test_null_strings(PyObject *self)
|
---|
567 | {
|
---|
568 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL);
|
---|
569 | PyObject *tuple = PyTuple_Pack(2, o1, o2);
|
---|
570 | Py_XDECREF(o1);
|
---|
571 | Py_XDECREF(o2);
|
---|
572 | return tuple;
|
---|
573 | }
|
---|
574 |
|
---|
575 | static PyObject *
|
---|
576 | raise_exception(PyObject *self, PyObject *args)
|
---|
577 | {
|
---|
578 | PyObject *exc;
|
---|
579 | PyObject *exc_args, *v;
|
---|
580 | int num_args, i;
|
---|
581 |
|
---|
582 | if (!PyArg_ParseTuple(args, "Oi:raise_exception",
|
---|
583 | &exc, &num_args))
|
---|
584 | return NULL;
|
---|
585 |
|
---|
586 | exc_args = PyTuple_New(num_args);
|
---|
587 | if (exc_args == NULL)
|
---|
588 | return NULL;
|
---|
589 | for (i = 0; i < num_args; ++i) {
|
---|
590 | v = PyInt_FromLong(i);
|
---|
591 | if (v == NULL) {
|
---|
592 | Py_DECREF(exc_args);
|
---|
593 | return NULL;
|
---|
594 | }
|
---|
595 | PyTuple_SET_ITEM(exc_args, i, v);
|
---|
596 | }
|
---|
597 | PyErr_SetObject(exc, exc_args);
|
---|
598 | Py_DECREF(exc_args);
|
---|
599 | return NULL;
|
---|
600 | }
|
---|
601 |
|
---|
602 | #ifdef WITH_THREAD
|
---|
603 |
|
---|
604 | /* test_thread_state spawns a thread of its own, and that thread releases
|
---|
605 | * `thread_done` when it's finished. The driver code has to know when the
|
---|
606 | * thread finishes, because the thread uses a PyObject (the callable) that
|
---|
607 | * may go away when the driver finishes. The former lack of this explicit
|
---|
608 | * synchronization caused rare segfaults, so rare that they were seen only
|
---|
609 | * on a Mac buildbot (although they were possible on any box).
|
---|
610 | */
|
---|
611 | static PyThread_type_lock thread_done = NULL;
|
---|
612 |
|
---|
613 | static void
|
---|
614 | _make_call(void *callable)
|
---|
615 | {
|
---|
616 | PyObject *rc;
|
---|
617 | PyGILState_STATE s = PyGILState_Ensure();
|
---|
618 | rc = PyObject_CallFunction(callable, "");
|
---|
619 | Py_XDECREF(rc);
|
---|
620 | PyGILState_Release(s);
|
---|
621 | }
|
---|
622 |
|
---|
623 | /* Same thing, but releases `thread_done` when it returns. This variant
|
---|
624 | * should be called only from threads spawned by test_thread_state().
|
---|
625 | */
|
---|
626 | static void
|
---|
627 | _make_call_from_thread(void *callable)
|
---|
628 | {
|
---|
629 | _make_call(callable);
|
---|
630 | PyThread_release_lock(thread_done);
|
---|
631 | }
|
---|
632 |
|
---|
633 | static PyObject *
|
---|
634 | test_thread_state(PyObject *self, PyObject *args)
|
---|
635 | {
|
---|
636 | PyObject *fn;
|
---|
637 |
|
---|
638 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
|
---|
639 | return NULL;
|
---|
640 |
|
---|
641 | /* Ensure Python is set up for threading */
|
---|
642 | PyEval_InitThreads();
|
---|
643 | thread_done = PyThread_allocate_lock();
|
---|
644 | if (thread_done == NULL)
|
---|
645 | return PyErr_NoMemory();
|
---|
646 | PyThread_acquire_lock(thread_done, 1);
|
---|
647 |
|
---|
648 | /* Start a new thread with our callback. */
|
---|
649 | PyThread_start_new_thread(_make_call_from_thread, fn);
|
---|
650 | /* Make the callback with the thread lock held by this thread */
|
---|
651 | _make_call(fn);
|
---|
652 | /* Do it all again, but this time with the thread-lock released */
|
---|
653 | Py_BEGIN_ALLOW_THREADS
|
---|
654 | _make_call(fn);
|
---|
655 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
|
---|
656 | Py_END_ALLOW_THREADS
|
---|
657 |
|
---|
658 | /* And once more with and without a thread
|
---|
659 | XXX - should use a lock and work out exactly what we are trying
|
---|
660 | to test <wink>
|
---|
661 | */
|
---|
662 | Py_BEGIN_ALLOW_THREADS
|
---|
663 | PyThread_start_new_thread(_make_call_from_thread, fn);
|
---|
664 | _make_call(fn);
|
---|
665 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
|
---|
666 | Py_END_ALLOW_THREADS
|
---|
667 |
|
---|
668 | PyThread_free_lock(thread_done);
|
---|
669 | Py_RETURN_NONE;
|
---|
670 | }
|
---|
671 | #endif
|
---|
672 |
|
---|
673 | /* Some tests of PyString_FromFormat(). This needs more tests. */
|
---|
674 | static PyObject *
|
---|
675 | test_string_from_format(PyObject *self, PyObject *args)
|
---|
676 | {
|
---|
677 | PyObject *result;
|
---|
678 | char *msg;
|
---|
679 |
|
---|
680 | #define CHECK_1_FORMAT(FORMAT, TYPE) \
|
---|
681 | result = PyString_FromFormat(FORMAT, (TYPE)1); \
|
---|
682 | if (result == NULL) \
|
---|
683 | return NULL; \
|
---|
684 | if (strcmp(PyString_AsString(result), "1")) { \
|
---|
685 | msg = FORMAT " failed at 1"; \
|
---|
686 | goto Fail; \
|
---|
687 | } \
|
---|
688 | Py_DECREF(result)
|
---|
689 |
|
---|
690 | CHECK_1_FORMAT("%d", int);
|
---|
691 | CHECK_1_FORMAT("%ld", long);
|
---|
692 | /* The z width modifier was added in Python 2.5. */
|
---|
693 | CHECK_1_FORMAT("%zd", Py_ssize_t);
|
---|
694 |
|
---|
695 | /* The u type code was added in Python 2.5. */
|
---|
696 | CHECK_1_FORMAT("%u", unsigned int);
|
---|
697 | CHECK_1_FORMAT("%lu", unsigned long);
|
---|
698 | CHECK_1_FORMAT("%zu", size_t);
|
---|
699 |
|
---|
700 | Py_RETURN_NONE;
|
---|
701 |
|
---|
702 | Fail:
|
---|
703 | Py_XDECREF(result);
|
---|
704 | return raiseTestError("test_string_from_format", msg);
|
---|
705 |
|
---|
706 | #undef CHECK_1_FORMAT
|
---|
707 | }
|
---|
708 |
|
---|
709 | /* This is here to provide a docstring for test_descr. */
|
---|
710 | static PyObject *
|
---|
711 | test_with_docstring(PyObject *self)
|
---|
712 | {
|
---|
713 | Py_RETURN_NONE;
|
---|
714 | }
|
---|
715 |
|
---|
716 | static PyMethodDef TestMethods[] = {
|
---|
717 | {"raise_exception", raise_exception, METH_VARARGS},
|
---|
718 | {"test_config", (PyCFunction)test_config, METH_NOARGS},
|
---|
719 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
|
---|
720 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
|
---|
721 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
|
---|
722 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
|
---|
723 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
|
---|
724 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
|
---|
725 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
|
---|
726 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
|
---|
727 | PyDoc_STR("This is a pretty normal docstring.")},
|
---|
728 |
|
---|
729 | {"getargs_tuple", getargs_tuple, METH_VARARGS},
|
---|
730 | {"getargs_b", getargs_b, METH_VARARGS},
|
---|
731 | {"getargs_B", getargs_B, METH_VARARGS},
|
---|
732 | {"getargs_H", getargs_H, METH_VARARGS},
|
---|
733 | {"getargs_I", getargs_I, METH_VARARGS},
|
---|
734 | {"getargs_k", getargs_k, METH_VARARGS},
|
---|
735 | {"getargs_i", getargs_i, METH_VARARGS},
|
---|
736 | {"getargs_l", getargs_l, METH_VARARGS},
|
---|
737 | {"getargs_n", getargs_n, METH_VARARGS},
|
---|
738 | #ifdef HAVE_LONG_LONG
|
---|
739 | {"getargs_L", getargs_L, METH_VARARGS},
|
---|
740 | {"getargs_K", getargs_K, METH_VARARGS},
|
---|
741 | {"test_longlong_api", test_longlong_api, METH_NOARGS},
|
---|
742 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
|
---|
743 | {"codec_incrementalencoder",
|
---|
744 | (PyCFunction)codec_incrementalencoder, METH_VARARGS},
|
---|
745 | {"codec_incrementaldecoder",
|
---|
746 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
|
---|
747 | #endif
|
---|
748 | #ifdef Py_USING_UNICODE
|
---|
749 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
|
---|
750 | #endif
|
---|
751 | #ifdef WITH_THREAD
|
---|
752 | {"_test_thread_state", test_thread_state, METH_VARARGS},
|
---|
753 | #endif
|
---|
754 | {NULL, NULL} /* sentinel */
|
---|
755 | };
|
---|
756 |
|
---|
757 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
|
---|
758 |
|
---|
759 | PyMODINIT_FUNC
|
---|
760 | init_testcapi(void)
|
---|
761 | {
|
---|
762 | PyObject *m;
|
---|
763 |
|
---|
764 | m = Py_InitModule("_testcapi", TestMethods);
|
---|
765 | if (m == NULL)
|
---|
766 | return;
|
---|
767 |
|
---|
768 | PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX));
|
---|
769 | PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX));
|
---|
770 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
|
---|
771 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
|
---|
772 | PyModule_AddObject(m, "INT_MIN", PyInt_FromLong(INT_MIN));
|
---|
773 | PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN));
|
---|
774 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN));
|
---|
775 | PyModule_AddObject(m, "INT_MAX", PyInt_FromLong(INT_MAX));
|
---|
776 | PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX));
|
---|
777 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX));
|
---|
778 |
|
---|
779 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
|
---|
780 | Py_INCREF(TestError);
|
---|
781 | PyModule_AddObject(m, "error", TestError);
|
---|
782 | }
|
---|