Bug Summary

File:Modules/_testcapimodule.c
Location:line 1944, column 10
Description:Assigned value is always the same as the existing value

Annotated Source Code

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#define PY_SSIZE_T_CLEAN
9
10#include "Python.h"
11#include <float.h>
12#include "structmember.h"
13#include "datetime.h"
14
15#ifdef WITH_THREAD1
16#include "pythread.h"
17#endif /* WITH_THREAD */
18static PyObject *TestError; /* set to exception object in init */
19
20/* Raise TestError with test_name + ": " + msg, and return NULL. */
21
22static PyObject *
23raiseTestError(const char* test_name, const char* msg)
24{
25 char buf[2048];
26
27 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((void*)0);
34}
35
36/* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
37
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*
43sizeof_error(const char* fatname, const char* typname,
44 int expected, int got)
45{
46 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((void*)0);
52}
53
54static PyObject*
55test_config(PyObject *self)
56{
57#define CHECK_SIZEOF(FATNAME, TYPE) \
58 if (FATNAME != sizeof(TYPE)) \
59 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
60
61 CHECK_SIZEOF(SIZEOF_SHORT2, short);
62 CHECK_SIZEOF(SIZEOF_INT4, int);
63 CHECK_SIZEOF(SIZEOF_LONG8, long);
64 CHECK_SIZEOF(SIZEOF_VOID_P8, void*);
65 CHECK_SIZEOF(SIZEOF_TIME_T8, time_t);
66#ifdef HAVE_LONG_LONG1
67 CHECK_SIZEOF(SIZEOF_LONG_LONG8, PY_LONG_LONGlong long);
68#endif
69
70#undef CHECK_SIZEOF
71
72 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
73 return Py_None(&_Py_NoneStruct);
74}
75
76static PyObject*
77test_list_api(PyObject *self)
78{
79 PyObject* list;
80 int i;
81
82 /* SF bug 132008: PyList_Reverse segfaults */
83#define NLIST 30
84 list = PyList_New(NLIST);
85 if (list == (PyObject*)NULL((void*)0))
86 return (PyObject*)NULL((void*)0);
87 /* list = range(NLIST) */
88 for (i = 0; i < NLIST; ++i) {
89 PyObject* anint = PyLong_FromLong(i);
90 if (anint == (PyObject*)NULL((void*)0)) {
91 Py_DECREF(list)do { if (_Py_RefTotal-- , --((PyObject*)(list))->ob_refcnt
!= 0) { if (((PyObject*)list)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 91, (PyObject *)(list)); } else _Py_Dealloc((PyObject *)(list
)); } while (0)
;
92 return (PyObject*)NULL((void*)0);
93 }
94 PyList_SET_ITEM(list, i, anint)(((PyListObject *)(list))->ob_item[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)do { if (_Py_RefTotal-- , --((PyObject*)(list))->ob_refcnt
!= 0) { if (((PyObject*)list)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 99, (PyObject *)(list)); } else _Py_Dealloc((PyObject *)(list
)); } while (0)
;
100 return (PyObject*)NULL((void*)0);
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)(((PyListObject *)(list))->ob_item[i]);
105 if (PyLong_AS_LONG(anint)PyLong_AsLong(anint) != NLIST-1-i) {
106 PyErr_SetString(TestError,
107 "test_list_api: reverse screwed up");
108 Py_DECREF(list)do { if (_Py_RefTotal-- , --((PyObject*)(list))->ob_refcnt
!= 0) { if (((PyObject*)list)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 108, (PyObject *)(list)); } else _Py_Dealloc((PyObject *)(list
)); } while (0)
;
109 return (PyObject*)NULL((void*)0);
110 }
111 }
112 Py_DECREF(list)do { if (_Py_RefTotal-- , --((PyObject*)(list))->ob_refcnt
!= 0) { if (((PyObject*)list)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 112, (PyObject *)(list)); } else _Py_Dealloc((PyObject *)(list
)); } while (0)
;
113#undef NLIST
114
115 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
116 return Py_None(&_Py_NoneStruct);
117}
118
119static int
120test_dict_inner(int count)
121{
122 Py_ssize_t pos = 0, iterations = 0;
123 int i;
124 PyObject *dict = PyDict_New();
125 PyObject *v, *k;
126
127 if (dict == NULL((void*)0))
128 return -1;
129
130 for (i = 0; i < count; i++) {
131 v = PyLong_FromLong(i);
132 PyDict_SetItem(dict, v, v);
133 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 133, (PyObject *)(v)); } else _Py_Dealloc((PyObject *)(v));
} while (0)
;
134 }
135
136 while (PyDict_Next(dict, &pos, &k, &v)) {
137 PyObject *o;
138 iterations++;
139
140 i = PyLong_AS_LONG(v)PyLong_AsLong(v) + 1;
141 o = PyLong_FromLong(i);
142 if (o == NULL((void*)0))
143 return -1;
144 if (PyDict_SetItem(dict, k, o) < 0) {
145 Py_DECREF(o)do { if (_Py_RefTotal-- , --((PyObject*)(o))->ob_refcnt !=
0) { if (((PyObject*)o)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 145, (PyObject *)(o)); } else _Py_Dealloc((PyObject *)(o));
} while (0)
;
146 return -1;
147 }
148 Py_DECREF(o)do { if (_Py_RefTotal-- , --((PyObject*)(o))->ob_refcnt !=
0) { if (((PyObject*)o)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 148, (PyObject *)(o)); } else _Py_Dealloc((PyObject *)(o));
} while (0)
;
149 }
150
151 Py_DECREF(dict)do { if (_Py_RefTotal-- , --((PyObject*)(dict))->ob_refcnt
!= 0) { if (((PyObject*)dict)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 151, (PyObject *)(dict)); } else _Py_Dealloc((PyObject *)(dict
)); } while (0)
;
152
153 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 }
161}
162
163static PyObject*
164test_dict_iteration(PyObject* self)
165{
166 int i;
167
168 for (i = 0; i < 200; i++) {
169 if (test_dict_inner(i) < 0) {
170 return NULL((void*)0);
171 }
172 }
173
174 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
175 return Py_None(&_Py_NoneStruct);
176}
177
178
179/* Issue #4701: Check that PyObject_Hash implicitly calls
180 * PyType_Ready if it hasn't already been called
181 */
182static PyTypeObject _HashInheritanceTester_Type = {
183 PyVarObject_HEAD_INIT(NULL, 0){ { 0, 0, 1, ((void*)0) }, 0 },
184 "hashinheritancetester", /* Name of this type */
185 sizeof(PyObject), /* Basic object size */
186 0, /* Item size for varobject */
187 (destructor)PyObject_Del_PyObject_DebugFree, /* 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( 0 | (1L<<18) | 0), /* 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 */
221};
222
223static PyObject*
224test_lazy_hash_inheritance(PyObject* self)
225{
226 PyTypeObject *type;
227 PyObject *obj;
228 Py_hash_t hash;
229
230 type = &_HashInheritanceTester_Type;
231
232 if (type->tp_dict != NULL((void*)0))
233 /* The type has already been initialized. This probably means
234 -R is being used. */
235 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
236
237
238 obj = PyObject_New(PyObject, type)( (PyObject *) _PyObject_New(type) );
239 if (obj == NULL((void*)0)) {
240 PyErr_Clear();
241 PyErr_SetString(
242 TestError,
243 "test_lazy_hash_inheritance: failed to create object");
244 return NULL((void*)0);
245 }
246
247 if (type->tp_dict != NULL((void*)0)) {
248 PyErr_SetString(
249 TestError,
250 "test_lazy_hash_inheritance: type initialised too soon");
251 Py_DECREF(obj)do { if (_Py_RefTotal-- , --((PyObject*)(obj))->ob_refcnt !=
0) { if (((PyObject*)obj)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 251, (PyObject *)(obj)); } else _Py_Dealloc((PyObject *)(obj
)); } while (0)
;
252 return NULL((void*)0);
253 }
254
255 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)do { if (_Py_RefTotal-- , --((PyObject*)(obj))->ob_refcnt !=
0) { if (((PyObject*)obj)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 261, (PyObject *)(obj)); } else _Py_Dealloc((PyObject *)(obj
)); } while (0)
;
262 return NULL((void*)0);
263 }
264
265 if (type->tp_dict == NULL((void*)0)) {
266 PyErr_SetString(
267 TestError,
268 "test_lazy_hash_inheritance: type not initialised by hash()");
269 Py_DECREF(obj)do { if (_Py_RefTotal-- , --((PyObject*)(obj))->ob_refcnt !=
0) { if (((PyObject*)obj)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 269, (PyObject *)(obj)); } else _Py_Dealloc((PyObject *)(obj
)); } while (0)
;
270 return NULL((void*)0);
271 }
272
273 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)do { if (_Py_RefTotal-- , --((PyObject*)(obj))->ob_refcnt !=
0) { if (((PyObject*)obj)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 277, (PyObject *)(obj)); } else _Py_Dealloc((PyObject *)(obj
)); } while (0)
;
278 return NULL((void*)0);
279 }
280
281 Py_DECREF(obj)do { if (_Py_RefTotal-- , --((PyObject*)(obj))->ob_refcnt !=
0) { if (((PyObject*)obj)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 281, (PyObject *)(obj)); } else _Py_Dealloc((PyObject *)(obj
)); } while (0)
;
282
283 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
284}
285
286
287/* 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{
294 PyErr_SetString(
295 TestError,
296 "test_broken_memoryview: expected error in bf_getbuffer");
297 return -1;
298}
299
300static PyBufferProcs memoryviewtester_as_buffer = {
301 (getbufferproc)broken_buffer_getbuffer, /* bf_getbuffer */
302 0, /* bf_releasebuffer */
303};
304
305static PyTypeObject _MemoryViewTester_Type = {
306 PyVarObject_HEAD_INIT(NULL, 0){ { 0, 0, 1, ((void*)0) }, 0 },
307 "memoryviewtester", /* Name of this type */
308 sizeof(PyObject), /* Basic object size */
309 0, /* Item size for varobject */
310 (destructor)PyObject_Del_PyObject_DebugFree, /* 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( 0 | (1L<<18) | 0), /* 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 */
344};
345
346static PyObject*
347test_broken_memoryview(PyObject* self)
348{
349 PyObject *obj = PyObject_New(PyObject, &_MemoryViewTester_Type)( (PyObject *) _PyObject_New(&_MemoryViewTester_Type) );
350 PyObject *res;
351
352 if (obj == NULL((void*)0)) {
353 PyErr_Clear();
354 PyErr_SetString(
355 TestError,
356 "test_broken_memoryview: failed to create object");
357 return NULL((void*)0);
358 }
359
360 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)do { if ((res) == ((void*)0)) ; else do { if (_Py_RefTotal-- ,
--((PyObject*)(res))->ob_refcnt != 0) { if (((PyObject*)res
)->ob_refcnt < 0) _Py_NegativeRefcount("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 365, (PyObject *)(res)); } else _Py_Dealloc((PyObject *)(res
)); } while (0); } while (0)
;
366 Py_DECREF(obj)do { if (_Py_RefTotal-- , --((PyObject*)(obj))->ob_refcnt !=
0) { if (((PyObject*)obj)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 366, (PyObject *)(obj)); } else _Py_Dealloc((PyObject *)(obj
)); } while (0)
;
367 return NULL((void*)0);
368 }
369
370 PyErr_Clear();
371 Py_DECREF(obj)do { if (_Py_RefTotal-- , --((PyObject*)(obj))->ob_refcnt !=
0) { if (((PyObject*)obj)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 371, (PyObject *)(obj)); } else _Py_Dealloc((PyObject *)(obj
)); } while (0)
;
372 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
373}
374
375
376/* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
377 PyLong_{As, From}{Unsigned,}LongLong().
378
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
381 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
382 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)do { if (_Py_RefTotal-- , --((PyObject*)(X))->ob_refcnt !=
0) { if (((PyObject*)X)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 391, (PyObject *)(X)); } else _Py_Dealloc((PyObject *)(X));
} while (0); (X) = ((void*)0)
Py_DECREF(X)do { if (_Py_RefTotal-- , --((PyObject*)(X))->ob_refcnt !=
0) { if (((PyObject*)X)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 391, (PyObject *)(X)); } else _Py_Dealloc((PyObject *)(X));
} while (0)
; (X) = NULL((void*)0)
392
393static PyObject *
394raise_test_long_error(const char* msg)
395{
396 return raiseTestError("test_long_api", msg);
397}
398
399#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
405
406#include "testcapi_long.h"
407
408static PyObject *
409test_long_api(PyObject* self)
410{
411 return TESTNAME(raise_test_long_error);
412}
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
420
421#ifdef HAVE_LONG_LONG1
422
423static PyObject *
424raise_test_longlong_error(const char* msg)
425{
426 return raiseTestError("test_longlong_api", msg);
427}
428
429#define TESTNAME test_longlong_api_inner
430#define TYPENAME PY_LONG_LONGlong 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
435
436#include "testcapi_long.h"
437
438static PyObject *
439test_longlong_api(PyObject* self, PyObject *args)
440{
441 return TESTNAME(raise_test_longlong_error);
442}
443
444#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
450
451/* 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{
459 PyObject *num, *one, *temp;
460 long value;
461 int overflow;
462
463 /* 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((void*)0), 16);
466 if (num == NULL((void*)0))
467 return NULL((void*)0);
468 overflow = 1234;
469 value = PyLong_AsLongAndOverflow(num, &overflow);
470 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 470, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
471 if (value == -1 && PyErr_Occurred())
472 return NULL((void*)0);
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");
479
480 /* Same again, with num = LONG_MAX + 1 */
481 num = PyLong_FromLong(LONG_MAX9223372036854775807L);
482 if (num == NULL((void*)0))
483 return NULL((void*)0);
484 one = PyLong_FromLong(1L);
485 if (one == NULL((void*)0)) {
486 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 486, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
487 return NULL((void*)0);
488 }
489 temp = PyNumber_Add(num, one);
490 Py_DECREF(one)do { if (_Py_RefTotal-- , --((PyObject*)(one))->ob_refcnt !=
0) { if (((PyObject*)one)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 490, (PyObject *)(one)); } else _Py_Dealloc((PyObject *)(one
)); } while (0)
;
491 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 491, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
492 num = temp;
493 if (num == NULL((void*)0))
494 return NULL((void*)0);
495 overflow = 0;
496 value = PyLong_AsLongAndOverflow(num, &overflow);
497 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 497, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
498 if (value == -1 && PyErr_Occurred())
499 return NULL((void*)0);
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");
506
507 /* 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((void*)0), 16);
510 if (num == NULL((void*)0))
511 return NULL((void*)0);
512 overflow = 1234;
513 value = PyLong_AsLongAndOverflow(num, &overflow);
514 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 514, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
515 if (value == -1 && PyErr_Occurred())
516 return NULL((void*)0);
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");
523
524 /* Same again, with num = LONG_MIN - 1 */
525 num = PyLong_FromLong(LONG_MIN(-9223372036854775807L -1L));
526 if (num == NULL((void*)0))
527 return NULL((void*)0);
528 one = PyLong_FromLong(1L);
529 if (one == NULL((void*)0)) {
530 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 530, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
531 return NULL((void*)0);
532 }
533 temp = PyNumber_Subtract(num, one);
534 Py_DECREF(one)do { if (_Py_RefTotal-- , --((PyObject*)(one))->ob_refcnt !=
0) { if (((PyObject*)one)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 534, (PyObject *)(one)); } else _Py_Dealloc((PyObject *)(one
)); } while (0)
;
535 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 535, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
536 num = temp;
537 if (num == NULL((void*)0))
538 return NULL((void*)0);
539 overflow = 0;
540 value = PyLong_AsLongAndOverflow(num, &overflow);
541 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 541, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
542 if (value == -1 && PyErr_Occurred())
543 return NULL((void*)0);
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");
550
551 /* Test that overflow is cleared properly for small values. */
552 num = PyLong_FromString("FF", NULL((void*)0), 16);
553 if (num == NULL((void*)0))
554 return NULL((void*)0);
555 overflow = 1234;
556 value = PyLong_AsLongAndOverflow(num, &overflow);
557 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 557, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
558 if (value == -1 && PyErr_Occurred())
559 return NULL((void*)0);
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");
566
567 num = PyLong_FromString("-FF", NULL((void*)0), 16);
568 if (num == NULL((void*)0))
569 return NULL((void*)0);
570 overflow = 0;
571 value = PyLong_AsLongAndOverflow(num, &overflow);
572 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 572, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
573 if (value == -1 && PyErr_Occurred())
574 return NULL((void*)0);
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");
581
582 num = PyLong_FromLong(LONG_MAX9223372036854775807L);
583 if (num == NULL((void*)0))
584 return NULL((void*)0);
585 overflow = 1234;
586 value = PyLong_AsLongAndOverflow(num, &overflow);
587 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 587, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
588 if (value == -1 && PyErr_Occurred())
589 return NULL((void*)0);
590 if (value != LONG_MAX9223372036854775807L)
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");
596
597 num = PyLong_FromLong(LONG_MIN(-9223372036854775807L -1L));
598 if (num == NULL((void*)0))
599 return NULL((void*)0);
600 overflow = 0;
601 value = PyLong_AsLongAndOverflow(num, &overflow);
602 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 602, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
603 if (value == -1 && PyErr_Occurred())
604 return NULL((void*)0);
605 if (value != LONG_MIN(-9223372036854775807L -1L))
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");
611
612 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
613 return Py_None(&_Py_NoneStruct);
614}
615
616/* 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{
624 PyObject *num, *one, *temp;
625 PY_LONG_LONGlong long value;
626 int overflow;
627
628 /* 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((void*)0), 16);
631 if (num == NULL((void*)0))
632 return NULL((void*)0);
633 overflow = 1234;
634 value = PyLong_AsLongLongAndOverflow(num, &overflow);
635 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 635, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
636 if (value == -1 && PyErr_Occurred())
637 return NULL((void*)0);
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");
644
645 /* Same again, with num = PY_LLONG_MAX + 1 */
646 num = PyLong_FromLongLong(PY_LLONG_MAX9223372036854775807LL);
647 if (num == NULL((void*)0))
648 return NULL((void*)0);
649 one = PyLong_FromLong(1L);
650 if (one == NULL((void*)0)) {
651 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 651, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
652 return NULL((void*)0);
653 }
654 temp = PyNumber_Add(num, one);
655 Py_DECREF(one)do { if (_Py_RefTotal-- , --((PyObject*)(one))->ob_refcnt !=
0) { if (((PyObject*)one)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 655, (PyObject *)(one)); } else _Py_Dealloc((PyObject *)(one
)); } while (0)
;
656 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 656, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
657 num = temp;
658 if (num == NULL((void*)0))
659 return NULL((void*)0);
660 overflow = 0;
661 value = PyLong_AsLongLongAndOverflow(num, &overflow);
662 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 662, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
663 if (value == -1 && PyErr_Occurred())
664 return NULL((void*)0);
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");
671
672 /* 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((void*)0), 16);
675 if (num == NULL((void*)0))
676 return NULL((void*)0);
677 overflow = 1234;
678 value = PyLong_AsLongLongAndOverflow(num, &overflow);
679 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 679, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
680 if (value == -1 && PyErr_Occurred())
681 return NULL((void*)0);
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");
688
689 /* Same again, with num = PY_LLONG_MIN - 1 */
690 num = PyLong_FromLongLong(PY_LLONG_MIN(-9223372036854775807LL -1LL));
691 if (num == NULL((void*)0))
692 return NULL((void*)0);
693 one = PyLong_FromLong(1L);
694 if (one == NULL((void*)0)) {
695 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 695, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
696 return NULL((void*)0);
697 }
698 temp = PyNumber_Subtract(num, one);
699 Py_DECREF(one)do { if (_Py_RefTotal-- , --((PyObject*)(one))->ob_refcnt !=
0) { if (((PyObject*)one)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 699, (PyObject *)(one)); } else _Py_Dealloc((PyObject *)(one
)); } while (0)
;
700 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 700, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
701 num = temp;
702 if (num == NULL((void*)0))
703 return NULL((void*)0);
704 overflow = 0;
705 value = PyLong_AsLongLongAndOverflow(num, &overflow);
706 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 706, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
707 if (value == -1 && PyErr_Occurred())
708 return NULL((void*)0);
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");
715
716 /* Test that overflow is cleared properly for small values. */
717 num = PyLong_FromString("FF", NULL((void*)0), 16);
718 if (num == NULL((void*)0))
719 return NULL((void*)0);
720 overflow = 1234;
721 value = PyLong_AsLongLongAndOverflow(num, &overflow);
722 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 722, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
723 if (value == -1 && PyErr_Occurred())
724 return NULL((void*)0);
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");
731
732 num = PyLong_FromString("-FF", NULL((void*)0), 16);
733 if (num == NULL((void*)0))
734 return NULL((void*)0);
735 overflow = 0;
736 value = PyLong_AsLongLongAndOverflow(num, &overflow);
737 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 737, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
738 if (value == -1 && PyErr_Occurred())
739 return NULL((void*)0);
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");
746
747 num = PyLong_FromLongLong(PY_LLONG_MAX9223372036854775807LL);
748 if (num == NULL((void*)0))
749 return NULL((void*)0);
750 overflow = 1234;
751 value = PyLong_AsLongLongAndOverflow(num, &overflow);
752 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 752, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
753 if (value == -1 && PyErr_Occurred())
754 return NULL((void*)0);
755 if (value != PY_LLONG_MAX9223372036854775807LL)
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");
761
762 num = PyLong_FromLongLong(PY_LLONG_MIN(-9223372036854775807LL -1LL));
763 if (num == NULL((void*)0))
764 return NULL((void*)0);
765 overflow = 0;
766 value = PyLong_AsLongLongAndOverflow(num, &overflow);
767 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 767, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
768 if (value == -1 && PyErr_Occurred())
769 return NULL((void*)0);
770 if (value != PY_LLONG_MIN(-9223372036854775807LL -1LL))
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");
776
777 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
778 return Py_None(&_Py_NoneStruct);
779}
780
781/* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
782 for both long and int arguments. The test may leak a little memory if
783 it fails.
784*/
785static PyObject *
786test_L_code(PyObject *self)
787{
788 PyObject *tuple, *num;
789 PY_LONG_LONGlong long value;
790
791 tuple = PyTuple_New(1);
792 if (tuple == NULL((void*)0))
793 return NULL((void*)0);
794
795 num = PyLong_FromLong(42);
796 if (num == NULL((void*)0))
797 return NULL((void*)0);
798
799 PyTuple_SET_ITEM(tuple, 0, num)(((PyTupleObject *)(tuple))->ob_item[0] = num);
800
801 value = -1;
802 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(tuple, "L:test_L_code", &value) < 0)
803 return NULL((void*)0);
804 if (value != 42)
805 return raiseTestError("test_L_code",
806 "L code returned wrong value for long 42");
807
808 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 808, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
809 num = PyLong_FromLong(42);
810 if (num == NULL((void*)0))
811 return NULL((void*)0);
812
813 PyTuple_SET_ITEM(tuple, 0, num)(((PyTupleObject *)(tuple))->ob_item[0] = num);
814
815 value = -1;
816 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(tuple, "L:test_L_code", &value) < 0)
817 return NULL((void*)0);
818 if (value != 42)
819 return raiseTestError("test_L_code",
820 "L code returned wrong value for int 42");
821
822 Py_DECREF(tuple)do { if (_Py_RefTotal-- , --((PyObject*)(tuple))->ob_refcnt
!= 0) { if (((PyObject*)tuple)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 822, (PyObject *)(tuple)); } else _Py_Dealloc((PyObject *)(
tuple)); } while (0)
;
823 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
824 return Py_None(&_Py_NoneStruct);
825}
826
827#endif /* ifdef HAVE_LONG_LONG */
828
829/* Test tuple argument processing */
830static PyObject *
831getargs_tuple(PyObject *self, PyObject *args)
832{
833 int a, b, c;
834 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i(ii)", &a, &b, &c))
835 return NULL((void*)0);
836 return Py_BuildValue_Py_BuildValue_SizeT("iii", a, b, c);
837}
838
839/* test PyArg_ParseTupleAndKeywords */
840static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
841{
842 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL((void*)0)};
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};
845
846 if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(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((void*)0);
850 return Py_BuildValue_Py_BuildValue_SizeT("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]);
853}
854
855/* Functions to call PyArg_ParseTuple with integer format codes,
856 and return the result.
857*/
858static PyObject *
859getargs_b(PyObject *self, PyObject *args)
860{
861 unsigned char value;
862 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "b", &value))
863 return NULL((void*)0);
864 return PyLong_FromUnsignedLong((unsigned long)value);
865}
866
867static PyObject *
868getargs_B(PyObject *self, PyObject *args)
869{
870 unsigned char value;
871 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "B", &value))
872 return NULL((void*)0);
873 return PyLong_FromUnsignedLong((unsigned long)value);
874}
875
876static PyObject *
877getargs_h(PyObject *self, PyObject *args)
878{
879 short value;
880 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "h", &value))
881 return NULL((void*)0);
882 return PyLong_FromLong((long)value);
883}
884
885static PyObject *
886getargs_H(PyObject *self, PyObject *args)
887{
888 unsigned short value;
889 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "H", &value))
890 return NULL((void*)0);
891 return PyLong_FromUnsignedLong((unsigned long)value);
892}
893
894static PyObject *
895getargs_I(PyObject *self, PyObject *args)
896{
897 unsigned int value;
898 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "I", &value))
899 return NULL((void*)0);
900 return PyLong_FromUnsignedLong((unsigned long)value);
901}
902
903static PyObject *
904getargs_k(PyObject *self, PyObject *args)
905{
906 unsigned long value;
907 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "k", &value))
908 return NULL((void*)0);
909 return PyLong_FromUnsignedLong(value);
910}
911
912static PyObject *
913getargs_i(PyObject *self, PyObject *args)
914{
915 int value;
916 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i", &value))
917 return NULL((void*)0);
918 return PyLong_FromLong((long)value);
919}
920
921static PyObject *
922getargs_l(PyObject *self, PyObject *args)
923{
924 long value;
925 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "l", &value))
926 return NULL((void*)0);
927 return PyLong_FromLong(value);
928}
929
930static PyObject *
931getargs_n(PyObject *self, PyObject *args)
932{
933 Py_ssize_t value;
934 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "n", &value))
935 return NULL((void*)0);
936 return PyLong_FromSsize_t(value);
937}
938
939#ifdef HAVE_LONG_LONG1
940static PyObject *
941getargs_L(PyObject *self, PyObject *args)
942{
943 PY_LONG_LONGlong long value;
944 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "L", &value))
945 return NULL((void*)0);
946 return PyLong_FromLongLong(value);
947}
948
949static PyObject *
950getargs_K(PyObject *self, PyObject *args)
951{
952 unsigned PY_LONG_LONGlong long value;
953 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "K", &value))
954 return NULL((void*)0);
955 return PyLong_FromUnsignedLongLong(value);
956}
957#endif
958
959/* This function not only tests the 'k' getargs code, but also the
960 PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */
961static PyObject *
962test_k_code(PyObject *self)
963{
964 PyObject *tuple, *num;
965 unsigned long value;
966
967 tuple = PyTuple_New(1);
968 if (tuple == NULL((void*)0))
969 return NULL((void*)0);
970
971 /* a number larger than ULONG_MAX even on 64-bit platforms */
972 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL((void*)0), 16);
973 if (num == NULL((void*)0))
974 return NULL((void*)0);
975
976 value = PyLong_AsUnsignedLongMask(num);
977 if (value != ULONG_MAX(9223372036854775807L *2UL +1UL))
978 return raiseTestError("test_k_code",
979 "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
980
981 PyTuple_SET_ITEM(tuple, 0, num)(((PyTupleObject *)(tuple))->ob_item[0] = num);
982
983 value = 0;
984 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(tuple, "k:test_k_code", &value) < 0)
985 return NULL((void*)0);
986 if (value != ULONG_MAX(9223372036854775807L *2UL +1UL))
987 return raiseTestError("test_k_code",
988 "k code returned wrong value for long 0xFFF...FFF");
989
990 Py_DECREF(num)do { if (_Py_RefTotal-- , --((PyObject*)(num))->ob_refcnt !=
0) { if (((PyObject*)num)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 990, (PyObject *)(num)); } else _Py_Dealloc((PyObject *)(num
)); } while (0)
;
991 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL((void*)0), 16);
992 if (num == NULL((void*)0))
993 return NULL((void*)0);
994
995 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");
999
1000 PyTuple_SET_ITEM(tuple, 0, num)(((PyTupleObject *)(tuple))->ob_item[0] = num);
1001
1002 value = 0;
1003 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(tuple, "k:test_k_code", &value) < 0)
1004 return NULL((void*)0);
1005 if (value != (unsigned long)-0x42)
1006 return raiseTestError("test_k_code",
1007 "k code returned wrong value for long -0xFFF..000042");
1008
1009 Py_DECREF(tuple)do { if (_Py_RefTotal-- , --((PyObject*)(tuple))->ob_refcnt
!= 0) { if (((PyObject*)tuple)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1009, (PyObject *)(tuple)); } else _Py_Dealloc((PyObject *)
(tuple)); } while (0)
;
1010 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
1011 return Py_None(&_Py_NoneStruct);
1012}
1013
1014static PyObject *
1015getargs_s(PyObject *self, PyObject *args)
1016{
1017 char *str;
1018 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "s", &str))
1019 return NULL((void*)0);
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_PyArg_ParseTuple_SizeT(args, "s*", &buffer))
1029 return NULL((void*)0);
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_PyArg_ParseTuple_SizeT(args, "s#", &str, &size))
1041 return NULL((void*)0);
1042 return PyBytes_FromStringAndSize(str, size);
1043}
1044
1045static PyObject *
1046getargs_z(PyObject *self, PyObject *args)
1047{
1048 char *str;
1049 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "z", &str))
1050 return NULL((void*)0);
1051 if (str != NULL((void*)0))
1052 return PyBytes_FromString(str);
1053 else
1054 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1055}
1056
1057static PyObject *
1058getargs_z_star(PyObject *self, PyObject *args)
1059{
1060 Py_buffer buffer;
1061 PyObject *bytes;
1062 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "z*", &buffer))
1063 return NULL((void*)0);
1064 if (buffer.buf != NULL((void*)0))
1065 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1066 else {
1067 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
1068 bytes = Py_None(&_Py_NoneStruct);
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_PyArg_ParseTuple_SizeT(args, "z#", &str, &size))
1080 return NULL((void*)0);
1081 if (str != NULL((void*)0))
1082 return PyBytes_FromStringAndSize(str, size);
1083 else
1084 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1085}
1086
1087static PyObject *
1088getargs_y(PyObject *self, PyObject *args)
1089{
1090 char *str;
1091 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "y", &str))
1092 return NULL((void*)0);
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_PyArg_ParseTuple_SizeT(args, "y*", &buffer))
1102 return NULL((void*)0);
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_PyArg_ParseTuple_SizeT(args, "y#", &str, &size))
1114 return NULL((void*)0);
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_PyArg_ParseTuple_SizeT(args, "u", &str))
1124 return NULL((void*)0);
1125 size = Py_UNICODE_strlen(str);
1126 return PyUnicode_FromUnicodePyUnicodeUCS2_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_PyArg_ParseTuple_SizeT(args, "u#", &str, &size))
1135 return NULL((void*)0);
1136 return PyUnicode_FromUnicodePyUnicodeUCS2_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_PyArg_ParseTuple_SizeT(args, "Z", &str))
1145 return NULL((void*)0);
1146 if (str != NULL((void*)0)) {
1147 size = Py_UNICODE_strlen(str);
1148 return PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(str, size);
1149 } else
1150 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
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_PyArg_ParseTuple_SizeT(args, "Z#", &str, &size))
1159 return NULL((void*)0);
1160 if (str != NULL((void*)0))
1161 return PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(str, size);
1162 else
1163 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1164}
1165
1166/* 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((void*)0))
1177 return NULL((void*)0);
1178
1179 obj = PyUnicode_DecodePyUnicodeUCS2_Decode("t\xeate", strlen("t\xeate"),
1180 "latin-1", NULL((void*)0));
1181 if (obj == NULL((void*)0))
1182 return NULL((void*)0);
1183
1184 PyTuple_SET_ITEM(tuple, 0, obj)(((PyTupleObject *)(tuple))->ob_item[0] = obj);
1185
1186 /* These two blocks used to raise a TypeError:
1187 * "argument must be string without null bytes, not str"
1188 */
1189 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(tuple, "s:test_s_code1", &value) < 0)
1190 return NULL((void*)0);
1191
1192 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(tuple, "z:test_s_code2", &value) < 0)
1193 return NULL((void*)0);
1194
1195 Py_DECREF(tuple)do { if (_Py_RefTotal-- , --((PyObject*)(tuple))->ob_refcnt
!= 0) { if (((PyObject*)tuple)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1195, (PyObject *)(tuple)); } else _Py_Dealloc((PyObject *)
(tuple)); } while (0)
;
1196 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1197}
1198
1199static PyObject *
1200test_bug_7414(PyObject *self)
1201{
1202 /* 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((void*)0)};
1206 PyObject *tuple = NULL((void*)0), *dict = NULL((void*)0), *b_str;
1207
1208 tuple = PyTuple_New(0);
1209 if (tuple == NULL((void*)0))
1210 goto failure;
1211 dict = PyDict_New();
1212 if (dict == NULL((void*)0))
1213 goto failure;
1214 b_str = PyUnicode_FromStringPyUnicodeUCS2_FromString("b");
1215 if (b_str == NULL((void*)0))
1216 goto failure;
1217 result = PyDict_SetItemString(dict, "b", b_str);
1218 Py_DECREF(b_str)do { if (_Py_RefTotal-- , --((PyObject*)(b_str))->ob_refcnt
!= 0) { if (((PyObject*)b_str)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1218, (PyObject *)(b_str)); } else _Py_Dealloc((PyObject *)
(b_str)); } while (0)
;
1219 if (result < 0)
1220 goto failure;
1221
1222 result = PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(tuple, dict, "|CC",
1223 kwlist, &a, &b);
1224 if (!result)
1225 goto failure;
1226
1227 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");
1233
1234 Py_DECREF(dict)do { if (_Py_RefTotal-- , --((PyObject*)(dict))->ob_refcnt
!= 0) { if (((PyObject*)dict)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1234, (PyObject *)(dict)); } else _Py_Dealloc((PyObject *)(
dict)); } while (0)
;
1235 Py_DECREF(tuple)do { if (_Py_RefTotal-- , --((PyObject*)(tuple))->ob_refcnt
!= 0) { if (((PyObject*)tuple)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1235, (PyObject *)(tuple)); } else _Py_Dealloc((PyObject *)
(tuple)); } while (0)
;
1236 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1237
1238 failure:
1239 Py_XDECREF(dict)do { if ((dict) == ((void*)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(dict))->ob_refcnt != 0) { if (((PyObject
*)dict)->ob_refcnt < 0) _Py_NegativeRefcount("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1239, (PyObject *)(dict)); } else _Py_Dealloc((PyObject *)(
dict)); } while (0); } while (0)
;
1240 Py_XDECREF(tuple)do { if ((tuple) == ((void*)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(tuple))->ob_refcnt != 0) { if (((PyObject
*)tuple)->ob_refcnt < 0) _Py_NegativeRefcount("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1240, (PyObject *)(tuple)); } else _Py_Dealloc((PyObject *)
(tuple)); } while (0); } while (0)
;
1241 return NULL((void*)0);
1242}
1243
1244
1245static volatile int x;
1246
1247/* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
1248 of an error.
1249*/
1250static PyObject *
1251test_u_code(PyObject *self)
1252{
1253 PyObject *tuple, *obj;
1254 Py_UNICODE *value;
1255 Py_ssize_t len;
1256
1257 /* 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)((25) < 128U ? _Py_ascii_whitespace[(25)] : _PyUnicode_IsWhitespace
(25))
;
1260
1261 tuple = PyTuple_New(1);
1262 if (tuple == NULL((void*)0))
1263 return NULL((void*)0);
1264
1265 obj = PyUnicode_DecodePyUnicodeUCS2_Decode("test", strlen("test"),
1266 "ascii", NULL((void*)0));
1267 if (obj == NULL((void*)0))
1268 return NULL((void*)0);
1269
1270 PyTuple_SET_ITEM(tuple, 0, obj)(((PyTupleObject *)(tuple))->ob_item[0] = obj);
1271
1272 value = 0;
1273 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(tuple, "u:test_u_code", &value) < 0)
1274 return NULL((void*)0);
1275 if (value != PyUnicode_AS_UNICODE(obj)((__builtin_expect(!(((((((PyObject*)(obj))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1275, "PyUnicode_Check(obj)") : (void)0),(((PyUnicodeObject
*)(obj))->str))
)
1276 return raiseTestError("test_u_code",
1277 "u code returned wrong value for u'test'");
1278 value = 0;
1279 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(tuple, "u#:test_u_code", &value, &len) < 0)
1280 return NULL((void*)0);
1281 if (value != PyUnicode_AS_UNICODE(obj)((__builtin_expect(!(((((((PyObject*)(obj))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1281, "PyUnicode_Check(obj)") : (void)0),(((PyUnicodeObject
*)(obj))->str))
||
1282 len != PyUnicode_GET_SIZE(obj)((__builtin_expect(!(((((((PyObject*)(obj))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1282, "PyUnicode_Check(obj)") : (void)0),(((PyUnicodeObject
*)(obj))->length))
)
1283 return raiseTestError("test_u_code",
1284 "u# code returned wrong values for u'test'");
1285
1286 Py_DECREF(tuple)do { if (_Py_RefTotal-- , --((PyObject*)(tuple))->ob_refcnt
!= 0) { if (((PyObject*)tuple)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1286, (PyObject *)(tuple)); } else _Py_Dealloc((PyObject *)
(tuple)); } while (0)
;
1287 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
1288 return Py_None(&_Py_NoneStruct);
1289}
1290
1291/* Test Z and Z# codes for PyArg_ParseTuple */
1292static PyObject *
1293test_Z_code(PyObject *self)
1294{
1295 PyObject *tuple, *obj;
1296 Py_UNICODE *value1, *value2;
1297 Py_ssize_t len1, len2;
1298
1299 tuple = PyTuple_New(2);
1300 if (tuple == NULL((void*)0))
1301 return NULL((void*)0);
1302
1303 obj = PyUnicode_FromStringPyUnicodeUCS2_FromString("test");
1304 PyTuple_SET_ITEM(tuple, 0, obj)(((PyTupleObject *)(tuple))->ob_item[0] = obj);
1305 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
1306 PyTuple_SET_ITEM(tuple, 1, Py_None)(((PyTupleObject *)(tuple))->ob_item[1] = (&_Py_NoneStruct
))
;
1307
1308 /* swap values on purpose */
1309 value1 = NULL((void*)0);
1310 value2 = PyUnicode_AS_UNICODE(obj)((__builtin_expect(!(((((((PyObject*)(obj))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1310, "PyUnicode_Check(obj)") : (void)0),(((PyUnicodeObject
*)(obj))->str))
;
1311
1312 /* Test Z for both values */
1313 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(tuple, "ZZ:test_Z_code", &value1, &value2) < 0)
1314 return NULL((void*)0);
1315 if (value1 != PyUnicode_AS_UNICODE(obj)((__builtin_expect(!(((((((PyObject*)(obj))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1315, "PyUnicode_Check(obj)") : (void)0),(((PyUnicodeObject
*)(obj))->str))
)
1316 return raiseTestError("test_Z_code",
1317 "Z code returned wrong value for 'test'");
1318 if (value2 != NULL((void*)0))
1319 return raiseTestError("test_Z_code",
1320 "Z code returned wrong value for None");
1321
1322 value1 = NULL((void*)0);
1323 value2 = PyUnicode_AS_UNICODE(obj)((__builtin_expect(!(((((((PyObject*)(obj))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1323, "PyUnicode_Check(obj)") : (void)0),(((PyUnicodeObject
*)(obj))->str))
;
1324 len1 = -1;
1325 len2 = -1;
1326
1327 /* Test Z# for both values */
1328 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(tuple, "Z#Z#:test_Z_code", &value1, &len1,
1329 &value2, &len2) < 0)
1330 return NULL((void*)0);
1331 if (value1 != PyUnicode_AS_UNICODE(obj)((__builtin_expect(!(((((((PyObject*)(obj))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1331, "PyUnicode_Check(obj)") : (void)0),(((PyUnicodeObject
*)(obj))->str))
||
1332 len1 != PyUnicode_GET_SIZE(obj)((__builtin_expect(!(((((((PyObject*)(obj))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1332, "PyUnicode_Check(obj)") : (void)0),(((PyUnicodeObject
*)(obj))->length))
)
1333 return raiseTestError("test_Z_code",
1334 "Z# code returned wrong values for 'test'");
1335 if (value2 != NULL((void*)0) ||
1336 len2 != 0)
1337 return raiseTestError("test_Z_code",
1338 "Z# code returned wrong values for None'");
1339
1340 Py_DECREF(tuple)do { if (_Py_RefTotal-- , --((PyObject*)(tuple))->ob_refcnt
!= 0) { if (((PyObject*)tuple)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1340, (PyObject *)(tuple)); } else _Py_Dealloc((PyObject *)
(tuple)); } while (0)
;
1341 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1342}
1343
1344static PyObject *
1345test_widechar(PyObject *self)
1346{
1347#if defined(SIZEOF_WCHAR_T4) && (SIZEOF_WCHAR_T4 == 4)
1348 const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
1349 size_t wtextlen = 1;
1350#else
1351 const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
1352 size_t wtextlen = 2;
1353#endif
1354 PyObject *wide, *utf8;
1355
1356 wide = PyUnicode_FromWideCharPyUnicodeUCS2_FromWideChar(wtext, wtextlen);
1357 if (wide == NULL((void*)0))
1358 return NULL((void*)0);
1359
1360 utf8 = PyUnicode_FromStringPyUnicodeUCS2_FromString("\xf4\x8a\xaf\x8d");
1361 if (utf8 == NULL((void*)0)) {
1362 Py_DECREF(wide)do { if (_Py_RefTotal-- , --((PyObject*)(wide))->ob_refcnt
!= 0) { if (((PyObject*)wide)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1362, (PyObject *)(wide)); } else _Py_Dealloc((PyObject *)(
wide)); } while (0)
;
1363 return NULL((void*)0);
1364 }
1365
1366 if (PyUnicode_GET_SIZE(wide)((__builtin_expect(!(((((((PyObject*)(wide))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1366, "PyUnicode_Check(wide)") : (void)0),(((PyUnicodeObject
*)(wide))->length))
!= PyUnicode_GET_SIZE(utf8)((__builtin_expect(!(((((((PyObject*)(utf8))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1366, "PyUnicode_Check(utf8)") : (void)0),(((PyUnicodeObject
*)(utf8))->length))
) {
1367 Py_DECREF(wide)do { if (_Py_RefTotal-- , --((PyObject*)(wide))->ob_refcnt
!= 0) { if (((PyObject*)wide)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1367, (PyObject *)(wide)); } else _Py_Dealloc((PyObject *)(
wide)); } while (0)
;
1368 Py_DECREF(utf8)do { if (_Py_RefTotal-- , --((PyObject*)(utf8))->ob_refcnt
!= 0) { if (((PyObject*)utf8)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1368, (PyObject *)(utf8)); } else _Py_Dealloc((PyObject *)(
utf8)); } while (0)
;
1369 return raiseTestError("test_widechar",
1370 "wide string and utf8 string "
1371 "have different length");
1372 }
1373 if (PyUnicode_ComparePyUnicodeUCS2_Compare(wide, utf8)) {
1374 Py_DECREF(wide)do { if (_Py_RefTotal-- , --((PyObject*)(wide))->ob_refcnt
!= 0) { if (((PyObject*)wide)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1374, (PyObject *)(wide)); } else _Py_Dealloc((PyObject *)(
wide)); } while (0)
;
1375 Py_DECREF(utf8)do { if (_Py_RefTotal-- , --((PyObject*)(utf8))->ob_refcnt
!= 0) { if (((PyObject*)utf8)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1375, (PyObject *)(utf8)); } else _Py_Dealloc((PyObject *)(
utf8)); } while (0)
;
1376 if (PyErr_Occurred())
1377 return NULL((void*)0);
1378 return raiseTestError("test_widechar",
1379 "wide string and utf8 string "
1380 "are different");
1381 }
1382
1383 Py_DECREF(wide)do { if (_Py_RefTotal-- , --((PyObject*)(wide))->ob_refcnt
!= 0) { if (((PyObject*)wide)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1383, (PyObject *)(wide)); } else _Py_Dealloc((PyObject *)(
wide)); } while (0)
;
1384 Py_DECREF(utf8)do { if (_Py_RefTotal-- , --((PyObject*)(utf8))->ob_refcnt
!= 0) { if (((PyObject*)utf8)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1384, (PyObject *)(utf8)); } else _Py_Dealloc((PyObject *)(
utf8)); } while (0)
;
1385 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1386}
1387
1388static PyObject *
1389unicode_aswidechar(PyObject *self, PyObject *args)
1390{
1391 PyObject *unicode, *result;
1392 Py_ssize_t buflen, size;
1393 wchar_t *buffer;
1394
1395 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "Un", &unicode, &buflen))
1396 return NULL((void*)0);
1397 buffer = PyMem_Malloc(buflen * sizeof(wchar_t));
1398 if (buffer == NULL((void*)0))
1399 return PyErr_NoMemory();
1400
1401 size = PyUnicode_AsWideCharPyUnicodeUCS2_AsWideChar(unicode, buffer, buflen);
1402 if (size == -1) {
1403 PyMem_Free(buffer);
1404 return NULL((void*)0);
1405 }
1406
1407 if (size < buflen)
1408 buflen = size + 1;
1409 else
1410 buflen = size;
1411 result = PyUnicode_FromWideCharPyUnicodeUCS2_FromWideChar(buffer, buflen);
1412 PyMem_Free(buffer);
1413 if (result == NULL((void*)0))
1414 return NULL((void*)0);
1415
1416 return Py_BuildValue_Py_BuildValue_SizeT("(Nn)", result, size);
1417}
1418
1419static PyObject *
1420unicode_aswidecharstring(PyObject *self, PyObject *args)
1421{
1422 PyObject *unicode, *result;
1423 Py_ssize_t size;
1424 wchar_t *buffer;
1425
1426 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "U", &unicode))
1427 return NULL((void*)0);
1428
1429 buffer = PyUnicode_AsWideCharStringPyUnicodeUCS2_AsWideCharString(unicode, &size);
1430 if (buffer == NULL((void*)0))
1431 return NULL((void*)0);
1432
1433 result = PyUnicode_FromWideCharPyUnicodeUCS2_FromWideChar(buffer, size + 1);
1434 PyMem_Free(buffer);
1435 if (result == NULL((void*)0))
1436 return NULL((void*)0);
1437 return Py_BuildValue_Py_BuildValue_SizeT("(Nn)", result, size);
1438}
1439
1440static PyObject *
1441getargs_w_star(PyObject *self, PyObject *args)
1442{
1443 Py_buffer buffer;
1444 PyObject *result;
1445 char *str;
1446
1447 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "w*:getargs_w_star", &buffer))
1448 return NULL((void*)0);
1449
1450 if (2 <= buffer.len) {
1451 str = buffer.buf;
1452 str[0] = '[';
1453 str[buffer.len-1] = ']';
1454 }
1455
1456 result = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1457 PyBuffer_Release(&buffer);
1458 return result;
1459}
1460
1461
1462static PyObject *
1463test_empty_argparse(PyObject *self)
1464{
1465 /* Test that formats can begin with '|'. See issue #4720. */
1466 PyObject *tuple, *dict = NULL((void*)0);
1467 static char *kwlist[] = {NULL((void*)0)};
1468 int result;
1469 tuple = PyTuple_New(0);
1470 if (!tuple)
1471 return NULL((void*)0);
1472 if ((result = PyArg_ParseTuple_PyArg_ParseTuple_SizeT(tuple, "|:test_empty_argparse")) < 0)
1473 goto done;
1474 dict = PyDict_New();
1475 if (!dict)
1476 goto done;
1477 result = PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(tuple, dict, "|:test_empty_argparse", kwlist);
1478 done:
1479 Py_DECREF(tuple)do { if (_Py_RefTotal-- , --((PyObject*)(tuple))->ob_refcnt
!= 0) { if (((PyObject*)tuple)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1479, (PyObject *)(tuple)); } else _Py_Dealloc((PyObject *)
(tuple)); } while (0)
;
1480 Py_XDECREF(dict)do { if ((dict) == ((void*)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(dict))->ob_refcnt != 0) { if (((PyObject
*)dict)->ob_refcnt < 0) _Py_NegativeRefcount("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1480, (PyObject *)(dict)); } else _Py_Dealloc((PyObject *)(
dict)); } while (0); } while (0)
;
1481 if (result < 0)
1482 return NULL((void*)0);
1483 else {
1484 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1485 }
1486}
1487
1488static PyObject *
1489codec_incrementalencoder(PyObject *self, PyObject *args)
1490{
1491 const char *encoding, *errors = NULL((void*)0);
1492 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "s|s:test_incrementalencoder",
1493 &encoding, &errors))
1494 return NULL((void*)0);
1495 return PyCodec_IncrementalEncoder(encoding, errors);
1496}
1497
1498static PyObject *
1499codec_incrementaldecoder(PyObject *self, PyObject *args)
1500{
1501 const char *encoding, *errors = NULL((void*)0);
1502 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "s|s:test_incrementaldecoder",
1503 &encoding, &errors))
1504 return NULL((void*)0);
1505 return PyCodec_IncrementalDecoder(encoding, errors);
1506}
1507
1508
1509/* Simple test of _PyLong_NumBits and _PyLong_Sign. */
1510static PyObject *
1511test_long_numbits(PyObject *self)
1512{
1513 struct triple {
1514 long input;
1515 size_t nbits;
1516 int sign;
1517 } testcases[] = {{0, 0, 0},
1518 {1L, 1, 1},
1519 {-1L, 1, -1},
1520 {2L, 2, 1},
1521 {-2L, 2, -1},
1522 {3L, 2, 1},
1523 {-3L, 2, -1},
1524 {4L, 3, 1},
1525 {-4L, 3, -1},
1526 {0x7fffL, 15, 1}, /* one Python long digit */
1527 {-0x7fffL, 15, -1},
1528 {0xffffL, 16, 1},
1529 {-0xffffL, 16, -1},
1530 {0xfffffffL, 28, 1},
1531 {-0xfffffffL, 28, -1}};
1532 int i;
1533
1534 for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
1535 PyObject *plong = PyLong_FromLong(testcases[i].input);
1536 size_t nbits = _PyLong_NumBits(plong);
1537 int sign = _PyLong_Sign(plong);
1538
1539 Py_DECREF(plong)do { if (_Py_RefTotal-- , --((PyObject*)(plong))->ob_refcnt
!= 0) { if (((PyObject*)plong)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1539, (PyObject *)(plong)); } else _Py_Dealloc((PyObject *)
(plong)); } while (0)
;
1540 if (nbits != testcases[i].nbits)
1541 return raiseTestError("test_long_numbits",
1542 "wrong result for _PyLong_NumBits");
1543 if (sign != testcases[i].sign)
1544 return raiseTestError("test_long_numbits",
1545 "wrong result for _PyLong_Sign");
1546 }
1547 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
1548 return Py_None(&_Py_NoneStruct);
1549}
1550
1551/* Example passing NULLs to PyObject_Str(NULL). */
1552
1553static PyObject *
1554test_null_strings(PyObject *self)
1555{
1556 PyObject *o1 = PyObject_Str(NULL((void*)0)), *o2 = PyObject_Str(NULL((void*)0));
1557 PyObject *tuple = PyTuple_Pack(2, o1, o2);
1558 Py_XDECREF(o1)do { if ((o1) == ((void*)0)) ; else do { if (_Py_RefTotal-- ,
--((PyObject*)(o1))->ob_refcnt != 0) { if (((PyObject*)o1
)->ob_refcnt < 0) _Py_NegativeRefcount("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1558, (PyObject *)(o1)); } else _Py_Dealloc((PyObject *)(o1
)); } while (0); } while (0)
;
1559 Py_XDECREF(o2)do { if ((o2) == ((void*)0)) ; else do { if (_Py_RefTotal-- ,
--((PyObject*)(o2))->ob_refcnt != 0) { if (((PyObject*)o2
)->ob_refcnt < 0) _Py_NegativeRefcount("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1559, (PyObject *)(o2)); } else _Py_Dealloc((PyObject *)(o2
)); } while (0); } while (0)
;
1560 return tuple;
1561}
1562
1563static PyObject *
1564raise_exception(PyObject *self, PyObject *args)
1565{
1566 PyObject *exc;
1567 PyObject *exc_args, *v;
1568 int num_args, i;
1569
1570 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "Oi:raise_exception",
1571 &exc, &num_args))
1572 return NULL((void*)0);
1573
1574 exc_args = PyTuple_New(num_args);
1575 if (exc_args == NULL((void*)0))
1576 return NULL((void*)0);
1577 for (i = 0; i < num_args; ++i) {
1578 v = PyLong_FromLong(i);
1579 if (v == NULL((void*)0)) {
1580 Py_DECREF(exc_args)do { if (_Py_RefTotal-- , --((PyObject*)(exc_args))->ob_refcnt
!= 0) { if (((PyObject*)exc_args)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1580, (PyObject *)(exc_args)); } else _Py_Dealloc((PyObject
*)(exc_args)); } while (0)
;
1581 return NULL((void*)0);
1582 }
1583 PyTuple_SET_ITEM(exc_args, i, v)(((PyTupleObject *)(exc_args))->ob_item[i] = v);
1584 }
1585 PyErr_SetObject(exc, exc_args);
1586 Py_DECREF(exc_args)do { if (_Py_RefTotal-- , --((PyObject*)(exc_args))->ob_refcnt
!= 0) { if (((PyObject*)exc_args)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1586, (PyObject *)(exc_args)); } else _Py_Dealloc((PyObject
*)(exc_args)); } while (0)
;
1587 return NULL((void*)0);
1588}
1589
1590
1591static int test_run_counter = 0;
1592
1593static PyObject *
1594test_datetime_capi(PyObject *self, PyObject *args) {
1595 if (PyDateTimeAPI) {
1596 if (test_run_counter) {
1597 /* Probably regrtest.py -R */
1598 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1599 }
1600 else {
1601 PyErr_SetString(PyExc_AssertionError,
1602 "PyDateTime_CAPI somehow initialized");
1603 return NULL((void*)0);
1604 }
1605 }
1606 test_run_counter++;
1607 PyDateTime_IMPORTPyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import("datetime.datetime_CAPI"
, 0)
;
1608 if (PyDateTimeAPI)
1609 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1610 else
1611 return NULL((void*)0);
1612}
1613
1614
1615#ifdef WITH_THREAD1
1616
1617/* test_thread_state spawns a thread of its own, and that thread releases
1618 * `thread_done` when it's finished. The driver code has to know when the
1619 * thread finishes, because the thread uses a PyObject (the callable) that
1620 * may go away when the driver finishes. The former lack of this explicit
1621 * synchronization caused rare segfaults, so rare that they were seen only
1622 * on a Mac buildbot (although they were possible on any box).
1623 */
1624static PyThread_type_lock thread_done = NULL((void*)0);
1625
1626static int
1627_make_call(void *callable)
1628{
1629 PyObject *rc;
1630 int success;
1631 PyGILState_STATE s = PyGILState_Ensure();
1632 rc = PyObject_CallFunction_PyObject_CallFunction_SizeT((PyObject *)callable, "");
1633 success = (rc != NULL((void*)0));
1634 Py_XDECREF(rc)do { if ((rc) == ((void*)0)) ; else do { if (_Py_RefTotal-- ,
--((PyObject*)(rc))->ob_refcnt != 0) { if (((PyObject*)rc
)->ob_refcnt < 0) _Py_NegativeRefcount("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1634, (PyObject *)(rc)); } else _Py_Dealloc((PyObject *)(rc
)); } while (0); } while (0)
;
1635 PyGILState_Release(s);
1636 return success;
1637}
1638
1639/* Same thing, but releases `thread_done` when it returns. This variant
1640 * should be called only from threads spawned by test_thread_state().
1641 */
1642static void
1643_make_call_from_thread(void *callable)
1644{
1645 _make_call(callable);
1646 PyThread_release_lock(thread_done);
1647}
1648
1649static PyObject *
1650test_thread_state(PyObject *self, PyObject *args)
1651{
1652 PyObject *fn;
1653 int success = 1;
1654
1655 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O:test_thread_state", &fn))
1656 return NULL((void*)0);
1657
1658 if (!PyCallable_Check(fn)) {
1659 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
1660 fn->ob_type->tp_name);
1661 return NULL((void*)0);
1662 }
1663
1664 /* Ensure Python is set up for threading */
1665 PyEval_InitThreads();
1666 thread_done = PyThread_allocate_lock();
1667 if (thread_done == NULL((void*)0))
1668 return PyErr_NoMemory();
1669 PyThread_acquire_lock(thread_done, 1);
1670
1671 /* Start a new thread with our callback. */
1672 PyThread_start_new_thread(_make_call_from_thread, fn);
1673 /* Make the callback with the thread lock held by this thread */
1674 success &= _make_call(fn);
1675 /* Do it all again, but this time with the thread-lock released */
1676 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1677 success &= _make_call(fn);
1678 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
1679 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1680
1681 /* And once more with and without a thread
1682 XXX - should use a lock and work out exactly what we are trying
1683 to test <wink>
1684 */
1685 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1686 PyThread_start_new_thread(_make_call_from_thread, fn);
1687 success &= _make_call(fn);
1688 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
1689 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1690
1691 /* Release lock we acquired above. This is required on HP-UX. */
1692 PyThread_release_lock(thread_done);
1693
1694 PyThread_free_lock(thread_done);
1695 if (!success)
1696 return NULL((void*)0);
1697 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1698}
1699
1700/* test Py_AddPendingCalls using threads */
1701static int _pending_callback(void *arg)
1702{
1703 /* we assume the argument is callable object to which we own a reference */
1704 PyObject *callable = (PyObject *)arg;
1705 PyObject *r = PyObject_CallObject(callable, NULL((void*)0));
1706 Py_DECREF(callable)do { if (_Py_RefTotal-- , --((PyObject*)(callable))->ob_refcnt
!= 0) { if (((PyObject*)callable)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1706, (PyObject *)(callable)); } else _Py_Dealloc((PyObject
*)(callable)); } while (0)
;
1707 Py_XDECREF(r)do { if ((r) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --
((PyObject*)(r))->ob_refcnt != 0) { if (((PyObject*)r)->
ob_refcnt < 0) _Py_NegativeRefcount("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1707, (PyObject *)(r)); } else _Py_Dealloc((PyObject *)(r))
; } while (0); } while (0)
;
1708 return r != NULL((void*)0) ? 0 : -1;
1709}
1710
1711/* The following requests n callbacks to _pending_callback. It can be
1712 * run from any python thread.
1713 */
1714PyObject *pending_threadfunc(PyObject *self, PyObject *arg)
1715{
1716 PyObject *callable;
1717 int r;
1718 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(arg, "O", &callable) == 0)
1719 return NULL((void*)0);
1720
1721 /* create the reference for the callbackwhile we hold the lock */
1722 Py_INCREF(callable)( _Py_RefTotal++ , ((PyObject*)(callable))->ob_refcnt++);
1723
1724 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1725 r = Py_AddPendingCall(&_pending_callback, callable);
1726 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1727
1728 if (r<0) {
1729 Py_DECREF(callable)do { if (_Py_RefTotal-- , --((PyObject*)(callable))->ob_refcnt
!= 0) { if (((PyObject*)callable)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1729, (PyObject *)(callable)); } else _Py_Dealloc((PyObject
*)(callable)); } while (0)
; /* unsuccessful add, destroy the extra reference */
1730 Py_INCREF(Py_False)( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++)
;
1731 return Py_False((PyObject *) &_Py_FalseStruct);
1732 }
1733 Py_INCREF(Py_True)( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++)
;
1734 return Py_True((PyObject *) &_Py_TrueStruct);
1735}
1736#endif
1737
1738/* Some tests of PyUnicode_FromFormat(). This needs more tests. */
1739static PyObject *
1740test_string_from_format(PyObject *self, PyObject *args)
1741{
1742 PyObject *result;
1743 char *msg;
1744 static const Py_UNICODE one[] = {'1', 0};
1745
1746#define CHECK_1_FORMAT(FORMAT, TYPE) \
1747 result = PyUnicode_FromFormatPyUnicodeUCS2_FromFormat(FORMAT, (TYPE)1); \
1748 if (result == NULL((void*)0)) \
1749 return NULL((void*)0); \
1750 if (Py_UNICODE_strcmp(PyUnicode_AS_UNICODE(result)((__builtin_expect(!(((((((PyObject*)(result))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1750, "PyUnicode_Check(result)") : (void)0),(((PyUnicodeObject
*)(result))->str))
, one)) { \
1751 msg = FORMAT " failed at 1"; \
1752 goto Fail; \
1753 } \
1754 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1754, (PyObject *)(result)); } else _Py_Dealloc((PyObject *
)(result)); } while (0)
1755
1756 CHECK_1_FORMAT("%d", int);
1757 CHECK_1_FORMAT("%ld", long);
1758 /* The z width modifier was added in Python 2.5. */
1759 CHECK_1_FORMAT("%zd", Py_ssize_t);
1760
1761 /* The u type code was added in Python 2.5. */
1762 CHECK_1_FORMAT("%u", unsigned int);
1763 CHECK_1_FORMAT("%lu", unsigned long);
1764 CHECK_1_FORMAT("%zu", size_t);
1765
1766 /* "%lld" and "%llu" support added in Python 2.7. */
1767#ifdef HAVE_LONG_LONG1
1768 CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONGlong long);
1769 CHECK_1_FORMAT("%lld", PY_LONG_LONGlong long);
1770#endif
1771
1772 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1773
1774 Fail:
1775 Py_XDECREF(result)do { if ((result) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject
*)result)->ob_refcnt < 0) _Py_NegativeRefcount("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1775, (PyObject *)(result)); } else _Py_Dealloc((PyObject *
)(result)); } while (0); } while (0)
;
1776 return raiseTestError("test_string_from_format", msg);
1777
1778#undef CHECK_1_FORMAT
1779}
1780
1781
1782static PyObject *
1783test_unicode_compare_with_ascii(PyObject *self) {
1784 PyObject *py_s = PyUnicode_FromStringAndSizePyUnicodeUCS2_FromStringAndSize("str\0", 4);
1785 int result;
1786 if (py_s == NULL((void*)0))
1787 return NULL((void*)0);
1788 result = PyUnicode_CompareWithASCIIStringPyUnicodeUCS2_CompareWithASCIIString(py_s, "str");
1789 Py_DECREF(py_s)do { if (_Py_RefTotal-- , --((PyObject*)(py_s))->ob_refcnt
!= 0) { if (((PyObject*)py_s)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1789, (PyObject *)(py_s)); } else _Py_Dealloc((PyObject *)(
py_s)); } while (0)
;
1790 if (!result) {
1791 PyErr_SetString(TestError, "Python string ending in NULL "
1792 "should not compare equal to c string.");
1793 return NULL((void*)0);
1794 }
1795 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1796}
1797
1798/* This is here to provide a docstring for test_descr. */
1799static PyObject *
1800test_with_docstring(PyObject *self)
1801{
1802 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1803}
1804
1805/* Test PyOS_string_to_double. */
1806static PyObject *
1807test_string_to_double(PyObject *self) {
1808 double result;
1809 char *msg;
1810
1811#define CHECK_STRING(STR, expected) \
1812 result = PyOS_string_to_double(STR, NULL((void*)0), NULL((void*)0)); \
1813 if (result == -1.0 && PyErr_Occurred()) \
1814 return NULL((void*)0); \
1815 if (result != expected) { \
1816 msg = "conversion of " STR " to float failed"; \
1817 goto fail; \
1818 }
1819
1820#define CHECK_INVALID(STR) \
1821 result = PyOS_string_to_double(STR, NULL((void*)0), NULL((void*)0)); \
1822 if (result == -1.0 && PyErr_Occurred()) { \
1823 if (PyErr_ExceptionMatches(PyExc_ValueError)) \
1824 PyErr_Clear(); \
1825 else \
1826 return NULL((void*)0); \
1827 } \
1828 else { \
1829 msg = "conversion of " STR " didn't raise ValueError"; \
1830 goto fail; \
1831 }
1832
1833 CHECK_STRING("0.1", 0.1);
1834 CHECK_STRING("1.234", 1.234);
1835 CHECK_STRING("-1.35", -1.35);
1836 CHECK_STRING(".1e01", 1.0);
1837 CHECK_STRING("2.e-2", 0.02);
1838
1839 CHECK_INVALID(" 0.1");
1840 CHECK_INVALID("\t\n-3");
1841 CHECK_INVALID(".123 ");
1842 CHECK_INVALID("3\n");
1843 CHECK_INVALID("123abc");
1844
1845 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1846 fail:
1847 return raiseTestError("test_string_to_double", msg);
1848#undef CHECK_STRING
1849#undef CHECK_INVALID
1850}
1851
1852
1853/* Coverage testing of capsule objects. */
1854
1855static const char *capsule_name = "capsule name";
1856static char *capsule_pointer = "capsule pointer";
1857static char *capsule_context = "capsule context";
1858static const char *capsule_error = NULL((void*)0);
1859static int
1860capsule_destructor_call_count = 0;
1861
1862static void
1863capsule_destructor(PyObject *o) {
1864 capsule_destructor_call_count++;
1865 if (PyCapsule_GetContext(o) != capsule_context) {
1866 capsule_error = "context did not match in destructor!";
1867 } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
1868 capsule_error = "destructor did not match in destructor! (woah!)";
1869 } else if (PyCapsule_GetName(o) != capsule_name) {
1870 capsule_error = "name did not match in destructor!";
1871 } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
1872 capsule_error = "pointer did not match in destructor!";
1873 }
1874}
1875
1876typedef struct {
1877 char *name;
1878 char *module;
1879 char *attribute;
1880} known_capsule;
1881
1882static PyObject *
1883test_capsule(PyObject *self, PyObject *args)
1884{
1885 PyObject *object;
1886 const char *error = NULL((void*)0);
1887 void *pointer;
1888 void *pointer2;
1889 known_capsule known_capsules[] = {
1890 #define KNOWN_CAPSULE(module, name){ module "." name, module, name } { module "." name, module, name }
1891 KNOWN_CAPSULE("_socket", "CAPI"){ "_socket" "." "CAPI", "_socket", "CAPI" },
1892 KNOWN_CAPSULE("_curses", "_C_API"){ "_curses" "." "_C_API", "_curses", "_C_API" },
1893 KNOWN_CAPSULE("datetime", "datetime_CAPI"){ "datetime" "." "datetime_CAPI", "datetime", "datetime_CAPI"
}
,
1894 { NULL((void*)0), NULL((void*)0) },
1895 };
1896 known_capsule *known = &known_capsules[0];
1897
1898#define FAIL(x) { error = (x); goto exit; }
1899
1900#define CHECK_DESTRUCTORif (capsule_error) { FAIL(capsule_error); } else if (!capsule_destructor_call_count
) { FAIL("destructor not called!"); } capsule_destructor_call_count
= 0;
\
1901 if (capsule_error) { \
1902 FAIL(capsule_error); \
1903 } \
1904 else if (!capsule_destructor_call_count) { \
1905 FAIL("destructor not called!"); \
1906 } \
1907 capsule_destructor_call_count = 0; \
1908
1909 object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
1910 PyCapsule_SetContext(object, capsule_context);
1911 capsule_destructor(object);
1912 CHECK_DESTRUCTORif (capsule_error) { FAIL(capsule_error); } else if (!capsule_destructor_call_count
) { FAIL("destructor not called!"); } capsule_destructor_call_count
= 0;
;
1913 Py_DECREF(object)do { if (_Py_RefTotal-- , --((PyObject*)(object))->ob_refcnt
!= 0) { if (((PyObject*)object)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1913, (PyObject *)(object)); } else _Py_Dealloc((PyObject *
)(object)); } while (0)
;
1914 CHECK_DESTRUCTORif (capsule_error) { FAIL(capsule_error); } else if (!capsule_destructor_call_count
) { FAIL("destructor not called!"); } capsule_destructor_call_count
= 0;
;
1915
1916 object = PyCapsule_New(known, "ignored", NULL((void*)0));
1917 PyCapsule_SetPointer(object, capsule_pointer);
1918 PyCapsule_SetName(object, capsule_name);
1919 PyCapsule_SetDestructor(object, capsule_destructor);
1920 PyCapsule_SetContext(object, capsule_context);
1921 capsule_destructor(object);
1922 CHECK_DESTRUCTORif (capsule_error) { FAIL(capsule_error); } else if (!capsule_destructor_call_count
) { FAIL("destructor not called!"); } capsule_destructor_call_count
= 0;
;
1923 /* intentionally access using the wrong name */
1924 pointer2 = PyCapsule_GetPointer(object, "the wrong name");
1925 if (!PyErr_Occurred()) {
1
Taking false branch
1926 FAIL("PyCapsule_GetPointer should have failed but did not!");
1927 }
1928 PyErr_Clear();
1929 if (pointer2) {
2
Taking false branch
1930 if (pointer2 == capsule_pointer) {
1931 FAIL("PyCapsule_GetPointer should not have"
1932 " returned the internal pointer!");
1933 } else {
1934 FAIL("PyCapsule_GetPointer should have "
1935 "returned NULL pointer but did not!");
1936 }
1937 }
1938 PyCapsule_SetDestructor(object, NULL((void*)0));
1939 Py_DECREF(object)do { if (_Py_RefTotal-- , --((PyObject*)(object))->ob_refcnt
!= 0) { if (((PyObject*)object)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1939, (PyObject *)(object)); } else _Py_Dealloc((PyObject *
)(object)); } while (0)
;
1940 if (capsule_destructor_call_count) {
3
Taking false branch
1941 FAIL("destructor called when it should not have been!");
1942 }
1943
1944 for (known = &known_capsules[0]; known->module != NULL((void*)0); known++) {
4
Assigned value is always the same as the existing value
1945 /* yeah, ordinarily I wouldn't do this either,
1946 but it's fine for this test harness.
1947 */
1948 static char buffer[256];
1949#undef FAIL
1950#define FAIL(x) \
1951 { \
1952 sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \__builtin___sprintf_chk (buffer, 0, __builtin_object_size (buffer
, 2 > 1), "%s module: \"%s\" attribute: \"%s\"", x, known->
module, known->attribute)
1953 x, known->module, known->attribute)__builtin___sprintf_chk (buffer, 0, __builtin_object_size (buffer
, 2 > 1), "%s module: \"%s\" attribute: \"%s\"", x, known->
module, known->attribute)
; \
1954 error = buffer; \
1955 goto exit; \
1956 } \
1957
1958 PyObject *module = PyImport_ImportModule(known->module);
1959 if (module) {
1960 pointer = PyCapsule_Import(known->name, 0);
1961 if (!pointer) {
1962 Py_DECREF(module)do { if (_Py_RefTotal-- , --((PyObject*)(module))->ob_refcnt
!= 0) { if (((PyObject*)module)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1962, (PyObject *)(module)); } else _Py_Dealloc((PyObject *
)(module)); } while (0)
;
1963 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
1964 }
1965 object = PyObject_GetAttrString(module, known->attribute);
1966 if (!object) {
1967 Py_DECREF(module)do { if (_Py_RefTotal-- , --((PyObject*)(module))->ob_refcnt
!= 0) { if (((PyObject*)module)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1967, (PyObject *)(module)); } else _Py_Dealloc((PyObject *
)(module)); } while (0)
;
1968 return NULL((void*)0);
1969 }
1970 pointer2 = PyCapsule_GetPointer(object,
1971 "weebles wobble but they don't fall down");
1972 if (!PyErr_Occurred()) {
1973 Py_DECREF(object)do { if (_Py_RefTotal-- , --((PyObject*)(object))->ob_refcnt
!= 0) { if (((PyObject*)object)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1973, (PyObject *)(object)); } else _Py_Dealloc((PyObject *
)(object)); } while (0)
;
1974 Py_DECREF(module)do { if (_Py_RefTotal-- , --((PyObject*)(module))->ob_refcnt
!= 0) { if (((PyObject*)module)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1974, (PyObject *)(module)); } else _Py_Dealloc((PyObject *
)(module)); } while (0)
;
1975 FAIL("PyCapsule_GetPointer should have failed but did not!");
1976 }
1977 PyErr_Clear();
1978 if (pointer2) {
1979 Py_DECREF(module)do { if (_Py_RefTotal-- , --((PyObject*)(module))->ob_refcnt
!= 0) { if (((PyObject*)module)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1979, (PyObject *)(module)); } else _Py_Dealloc((PyObject *
)(module)); } while (0)
;
1980 Py_DECREF(object)do { if (_Py_RefTotal-- , --((PyObject*)(object))->ob_refcnt
!= 0) { if (((PyObject*)object)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1980, (PyObject *)(object)); } else _Py_Dealloc((PyObject *
)(object)); } while (0)
;
1981 if (pointer2 == pointer) {
1982 FAIL("PyCapsule_GetPointer should not have"
1983 " returned its internal pointer!");
1984 } else {
1985 FAIL("PyCapsule_GetPointer should have"
1986 " returned NULL pointer but did not!");
1987 }
1988 }
1989 Py_DECREF(object)do { if (_Py_RefTotal-- , --((PyObject*)(object))->ob_refcnt
!= 0) { if (((PyObject*)object)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1989, (PyObject *)(object)); } else _Py_Dealloc((PyObject *
)(object)); } while (0)
;
1990 Py_DECREF(module)do { if (_Py_RefTotal-- , --((PyObject*)(module))->ob_refcnt
!= 0) { if (((PyObject*)module)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 1990, (PyObject *)(module)); } else _Py_Dealloc((PyObject *
)(module)); } while (0)
;
1991 }
1992 else
1993 PyErr_Clear();
1994 }
1995
1996 exit:
1997 if (error) {
1998 return raiseTestError("test_capsule", error);
1999 }
2000 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2001#undef FAIL
2002}
2003
2004#ifdef HAVE_GETTIMEOFDAY1
2005/* Profiling of integer performance */
2006static void print_delta(int test, struct timeval *s, struct timeval *e)
2007{
2008 e->tv_sec -= s->tv_sec;
2009 e->tv_usec -= s->tv_usec;
2010 if (e->tv_usec < 0) {
2011 e->tv_sec -=1;
2012 e->tv_usec += 1000000;
2013 }
2014 printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec);
2015}
2016
2017static PyObject *
2018profile_int(PyObject *self, PyObject* args)
2019{
2020 int i, k;
2021 struct timeval start, stop;
2022 PyObject *single, **multiple, *op1, *result;
2023
2024 /* Test 1: Allocate and immediately deallocate
2025 many small integers */
2026 gettimeofday(&start, NULL((void*)0));
2027 for(k=0; k < 20000; k++)
2028 for(i=0; i < 1000; i++) {
2029 single = PyLong_FromLong(i);
2030 Py_DECREF(single)do { if (_Py_RefTotal-- , --((PyObject*)(single))->ob_refcnt
!= 0) { if (((PyObject*)single)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2030, (PyObject *)(single)); } else _Py_Dealloc((PyObject *
)(single)); } while (0)
;
2031 }
2032 gettimeofday(&stop, NULL((void*)0));
2033 print_delta(1, &start, &stop);
2034
2035 /* Test 2: Allocate and immediately deallocate
2036 many large integers */
2037 gettimeofday(&start, NULL((void*)0));
2038 for(k=0; k < 20000; k++)
2039 for(i=0; i < 1000; i++) {
2040 single = PyLong_FromLong(i+1000000);
2041 Py_DECREF(single)do { if (_Py_RefTotal-- , --((PyObject*)(single))->ob_refcnt
!= 0) { if (((PyObject*)single)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2041, (PyObject *)(single)); } else _Py_Dealloc((PyObject *
)(single)); } while (0)
;
2042 }
2043 gettimeofday(&stop, NULL((void*)0));
2044 print_delta(2, &start, &stop);
2045
2046 /* Test 3: Allocate a few integers, then release
2047 them all simultaneously. */
2048 multiple = malloc(sizeof(PyObject*) * 1000);
2049 gettimeofday(&start, NULL((void*)0));
2050 for(k=0; k < 20000; k++) {
2051 for(i=0; i < 1000; i++) {
2052 multiple[i] = PyLong_FromLong(i+1000000);
2053 }
2054 for(i=0; i < 1000; i++) {
2055 Py_DECREF(multiple[i])do { if (_Py_RefTotal-- , --((PyObject*)(multiple[i]))->ob_refcnt
!= 0) { if (((PyObject*)multiple[i])->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2055, (PyObject *)(multiple[i])); } else _Py_Dealloc((PyObject
*)(multiple[i])); } while (0)
;
2056 }
2057 }
2058 gettimeofday(&stop, NULL((void*)0));
2059 print_delta(3, &start, &stop);
2060
2061 /* Test 4: Allocate many integers, then release
2062 them all simultaneously. */
2063 multiple = malloc(sizeof(PyObject*) * 1000000);
2064 gettimeofday(&start, NULL((void*)0));
2065 for(k=0; k < 20; k++) {
2066 for(i=0; i < 1000000; i++) {
2067 multiple[i] = PyLong_FromLong(i+1000000);
2068 }
2069 for(i=0; i < 1000000; i++) {
2070 Py_DECREF(multiple[i])do { if (_Py_RefTotal-- , --((PyObject*)(multiple[i]))->ob_refcnt
!= 0) { if (((PyObject*)multiple[i])->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2070, (PyObject *)(multiple[i])); } else _Py_Dealloc((PyObject
*)(multiple[i])); } while (0)
;
2071 }
2072 }
2073 gettimeofday(&stop, NULL((void*)0));
2074 print_delta(4, &start, &stop);
2075
2076 /* Test 5: Allocate many integers < 32000 */
2077 multiple = malloc(sizeof(PyObject*) * 1000000);
2078 gettimeofday(&start, NULL((void*)0));
2079 for(k=0; k < 10; k++) {
2080 for(i=0; i < 1000000; i++) {
2081 multiple[i] = PyLong_FromLong(i+1000);
2082 }
2083 for(i=0; i < 1000000; i++) {
2084 Py_DECREF(multiple[i])do { if (_Py_RefTotal-- , --((PyObject*)(multiple[i]))->ob_refcnt
!= 0) { if (((PyObject*)multiple[i])->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2084, (PyObject *)(multiple[i])); } else _Py_Dealloc((PyObject
*)(multiple[i])); } while (0)
;
2085 }
2086 }
2087 gettimeofday(&stop, NULL((void*)0));
2088 print_delta(5, &start, &stop);
2089
2090 /* Test 6: Perform small int addition */
2091 op1 = PyLong_FromLong(1);
2092 gettimeofday(&start, NULL((void*)0));
2093 for(i=0; i < 10000000; i++) {
2094 result = PyNumber_Add(op1, op1);
2095 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2095, (PyObject *)(result)); } else _Py_Dealloc((PyObject *
)(result)); } while (0)
;
2096 }
2097 gettimeofday(&stop, NULL((void*)0));
2098 Py_DECREF(op1)do { if (_Py_RefTotal-- , --((PyObject*)(op1))->ob_refcnt !=
0) { if (((PyObject*)op1)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2098, (PyObject *)(op1)); } else _Py_Dealloc((PyObject *)(op1
)); } while (0)
;
2099 print_delta(6, &start, &stop);
2100
2101 /* Test 7: Perform medium int addition */
2102 op1 = PyLong_FromLong(1000);
2103 gettimeofday(&start, NULL((void*)0));
2104 for(i=0; i < 10000000; i++) {
2105 result = PyNumber_Add(op1, op1);
2106 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2106, (PyObject *)(result)); } else _Py_Dealloc((PyObject *
)(result)); } while (0)
;
2107 }
2108 gettimeofday(&stop, NULL((void*)0));
2109 Py_DECREF(op1)do { if (_Py_RefTotal-- , --((PyObject*)(op1))->ob_refcnt !=
0) { if (((PyObject*)op1)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2109, (PyObject *)(op1)); } else _Py_Dealloc((PyObject *)(op1
)); } while (0)
;
2110 print_delta(7, &start, &stop);
2111
2112 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
2113 return Py_None(&_Py_NoneStruct);
2114}
2115#endif
2116
2117/* To test the format of tracebacks as printed out. */
2118static PyObject *
2119traceback_print(PyObject *self, PyObject *args)
2120{
2121 PyObject *file;
2122 PyObject *traceback;
2123 int result;
2124
2125 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "OO:traceback_print",
2126 &traceback, &file))
2127 return NULL((void*)0);
2128
2129 result = PyTraceBack_Print(traceback, file);
2130 if (result < 0)
2131 return NULL((void*)0);
2132 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2133}
2134
2135/* To test the format of exceptions as printed out. */
2136static PyObject *
2137exception_print(PyObject *self, PyObject *args)
2138{
2139 PyObject *value;
2140 PyObject *tb;
2141
2142 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O:exception_print",
2143 &value))
2144 return NULL((void*)0);
2145 if (!PyExceptionInstance_Check(value)((((value)->ob_type)->tp_flags & ((1L<<30))) !=
0)
) {
2146 PyErr_Format(PyExc_TypeError, "an exception instance is required");
2147 return NULL((void*)0);
2148 }
2149
2150 tb = PyException_GetTraceback(value);
2151 PyErr_Display((PyObject *) Py_TYPE(value)(((PyObject*)(value))->ob_type), value, tb);
2152 Py_XDECREF(tb)do { if ((tb) == ((void*)0)) ; else do { if (_Py_RefTotal-- ,
--((PyObject*)(tb))->ob_refcnt != 0) { if (((PyObject*)tb
)->ob_refcnt < 0) _Py_NegativeRefcount("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2152, (PyObject *)(tb)); } else _Py_Dealloc((PyObject *)(tb
)); } while (0); } while (0)
;
2153
2154 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2155}
2156
2157
2158
2159
2160/* reliably raise a MemoryError */
2161static PyObject *
2162raise_memoryerror(PyObject *self)
2163{
2164 PyErr_NoMemory();
2165 return NULL((void*)0);
2166}
2167
2168/* Issue 6012 */
2169static PyObject *str1, *str2;
2170static int
2171failing_converter(PyObject *obj, void *arg)
2172{
2173 /* Clone str1, then let the conversion fail. */
2174 assert(str1)(__builtin_expect(!(str1), 0) ? __assert_rtn(__func__, "/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2174, "str1") : (void)0)
;
2175 str2 = str1;
2176 Py_INCREF(str2)( _Py_RefTotal++ , ((PyObject*)(str2))->ob_refcnt++);
2177 return 0;
2178}
2179static PyObject*
2180argparsing(PyObject *o, PyObject *args)
2181{
2182 PyObject *res;
2183 str1 = str2 = NULL((void*)0);
2184 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&O&",
2185 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &str1,
2186 failing_converter, &str2)) {
2187 if (!str2)
2188 /* argument converter not called? */
2189 return NULL((void*)0);
2190 /* Should be 1 */
2191 res = PyLong_FromSsize_t(Py_REFCNT(str2)(((PyObject*)(str2))->ob_refcnt));
2192 Py_DECREF(str2)do { if (_Py_RefTotal-- , --((PyObject*)(str2))->ob_refcnt
!= 0) { if (((PyObject*)str2)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2192, (PyObject *)(str2)); } else _Py_Dealloc((PyObject *)(
str2)); } while (0)
;
2193 PyErr_Clear();
2194 return res;
2195 }
2196 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2197}
2198
2199/* To test that the result of PyCode_NewEmpty has the right members. */
2200static PyObject *
2201code_newempty(PyObject *self, PyObject *args)
2202{
2203 const char *filename;
2204 const char *funcname;
2205 int firstlineno;
2206
2207 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ssi:code_newempty",
2208 &filename, &funcname, &firstlineno))
2209 return NULL((void*)0);
2210
2211 return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno);
2212}
2213
2214/* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException).
2215 Run via Lib/test/test_exceptions.py */
2216static PyObject *
2217make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs)
2218{
2219 const char *name;
2220 const char *doc = NULL((void*)0);
2221 PyObject *base = NULL((void*)0);
2222 PyObject *dict = NULL((void*)0);
2223
2224 static char *kwlist[] = {"name", "doc", "base", "dict", NULL((void*)0)};
2225
2226 if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwargs,
2227 "s|sOO:make_exception_with_doc", kwlist,
2228 &name, &doc, &base, &dict))
2229 return NULL((void*)0);
2230
2231 return PyErr_NewExceptionWithDoc(name, doc, base, dict);
2232}
2233
2234/* Test that the fatal error from not having a current thread doesn't
2235 cause an infinite loop. Run via Lib/test/test_capi.py */
2236static PyObject *
2237crash_no_current_thread(PyObject *self)
2238{
2239 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2240 /* Using PyThreadState_Get() directly allows the test to pass in
2241 !pydebug mode. However, the test only actually tests anything
2242 in pydebug mode, since that's where the infinite loop was in
2243 the first place. */
2244 PyThreadState_Get();
2245 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2246 return NULL((void*)0);
2247}
2248
2249static PyMethodDef TestMethods[] = {
2250 {"raise_exception", raise_exception, METH_VARARGS0x0001},
2251 {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS0x0004},
2252 {"test_config", (PyCFunction)test_config, METH_NOARGS0x0004},
2253 {"test_datetime_capi", test_datetime_capi, METH_NOARGS0x0004},
2254 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS0x0004},
2255 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS0x0004},
2256 {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS0x0004},
2257 {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS0x0004},
2258 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS0x0004},
2259 {"test_long_and_overflow", (PyCFunction)test_long_and_overflow,
2260 METH_NOARGS0x0004},
2261 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS0x0004},
2262 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS0x0004},
2263 {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS0x0004},
2264 {"test_bug_7414", (PyCFunction)test_bug_7414, METH_NOARGS0x0004},
2265 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS0x0004},
2266 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS0x0004},
2267 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS0x0004,
2268 PyDoc_STR("This is a pretty normal docstring.")"This is a pretty normal docstring."},
2269 {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS0x0004},
2270 {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS0x0004},
2271 {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS0x0004},
2272 {"getargs_tuple", getargs_tuple, METH_VARARGS0x0001},
2273 {"getargs_keywords", (PyCFunction)getargs_keywords,
2274 METH_VARARGS0x0001|METH_KEYWORDS0x0002},
2275 {"getargs_b", getargs_b, METH_VARARGS0x0001},
2276 {"getargs_B", getargs_B, METH_VARARGS0x0001},
2277 {"getargs_h", getargs_h, METH_VARARGS0x0001},
2278 {"getargs_H", getargs_H, METH_VARARGS0x0001},
2279 {"getargs_I", getargs_I, METH_VARARGS0x0001},
2280 {"getargs_k", getargs_k, METH_VARARGS0x0001},
2281 {"getargs_i", getargs_i, METH_VARARGS0x0001},
2282 {"getargs_l", getargs_l, METH_VARARGS0x0001},
2283 {"getargs_n", getargs_n, METH_VARARGS0x0001},
2284#ifdef HAVE_LONG_LONG1
2285 {"getargs_L", getargs_L, METH_VARARGS0x0001},
2286 {"getargs_K", getargs_K, METH_VARARGS0x0001},
2287 {"test_longlong_api", test_longlong_api, METH_NOARGS0x0004},
2288 {"test_long_long_and_overflow",
2289 (PyCFunction)test_long_long_and_overflow, METH_NOARGS0x0004},
2290 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS0x0004},
2291#endif
2292 {"getargs_s", getargs_s, METH_VARARGS0x0001},
2293 {"getargs_s_star", getargs_s_star, METH_VARARGS0x0001},
2294 {"getargs_s_hash", getargs_s_hash, METH_VARARGS0x0001},
2295 {"getargs_z", getargs_z, METH_VARARGS0x0001},
2296 {"getargs_z_star", getargs_z_star, METH_VARARGS0x0001},
2297 {"getargs_z_hash", getargs_z_hash, METH_VARARGS0x0001},
2298 {"getargs_y", getargs_y, METH_VARARGS0x0001},
2299 {"getargs_y_star", getargs_y_star, METH_VARARGS0x0001},
2300 {"getargs_y_hash", getargs_y_hash, METH_VARARGS0x0001},
2301 {"getargs_u", getargs_u, METH_VARARGS0x0001},
2302 {"getargs_u_hash", getargs_u_hash, METH_VARARGS0x0001},
2303 {"getargs_Z", getargs_Z, METH_VARARGS0x0001},
2304 {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS0x0001},
2305 {"getargs_w_star", getargs_w_star, METH_VARARGS0x0001},
2306 {"codec_incrementalencoder",
2307 (PyCFunction)codec_incrementalencoder, METH_VARARGS0x0001},
2308 {"codec_incrementaldecoder",
2309 (PyCFunction)codec_incrementaldecoder, METH_VARARGS0x0001},
2310 {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS0x0004},
2311 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS0x0004},
2312 {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS0x0004},
2313 {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS0x0004},
2314 {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS0x0001},
2315 {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS0x0001},
2316#ifdef WITH_THREAD1
2317 {"_test_thread_state", test_thread_state, METH_VARARGS0x0001},
2318 {"_pending_threadfunc", pending_threadfunc, METH_VARARGS0x0001},
2319#endif
2320#ifdef HAVE_GETTIMEOFDAY1
2321 {"profile_int", profile_int, METH_NOARGS0x0004},
2322#endif
2323 {"traceback_print", traceback_print, METH_VARARGS0x0001},
2324 {"exception_print", exception_print, METH_VARARGS0x0001},
2325 {"argparsing", argparsing, METH_VARARGS0x0001},
2326 {"code_newempty", code_newempty, METH_VARARGS0x0001},
2327 {"make_exception_with_doc", (PyCFunction)make_exception_with_doc,
2328 METH_VARARGS0x0001 | METH_KEYWORDS0x0002},
2329 {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS0x0004},
2330 {NULL((void*)0), NULL((void*)0)} /* sentinel */
2331};
2332
2333#define AddSym(d, n, f, v){PyObject *o = f(v); PyDict_SetItemString(d, n, o); do { if (
_Py_RefTotal-- , --((PyObject*)(o))->ob_refcnt != 0) { if (
((PyObject*)o)->ob_refcnt < 0) _Py_NegativeRefcount("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2333, (PyObject *)(o)); } else _Py_Dealloc((PyObject *)(o))
; } while (0);}
{PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o)do { if (_Py_RefTotal-- , --((PyObject*)(o))->ob_refcnt !=
0) { if (((PyObject*)o)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2333, (PyObject *)(o)); } else _Py_Dealloc((PyObject *)(o))
; } while (0)
;}
2334
2335typedef struct {
2336 char bool_member;
2337 char byte_member;
2338 unsigned char ubyte_member;
2339 short short_member;
2340 unsigned short ushort_member;
2341 int int_member;
2342 unsigned int uint_member;
2343 long long_member;
2344 unsigned long ulong_member;
2345 Py_ssize_t pyssizet_member;
2346 float float_member;
2347 double double_member;
2348 char inplace_member[6];
2349#ifdef HAVE_LONG_LONG1
2350 PY_LONG_LONGlong long longlong_member;
2351 unsigned PY_LONG_LONGlong long ulonglong_member;
2352#endif
2353} all_structmembers;
2354
2355typedef struct {
2356 PyObject_HEADPyObject ob_base;
2357 all_structmembers structmembers;
2358} test_structmembers;
2359
2360static struct PyMemberDef test_members[] = {
2361 {"T_BOOL", T_BOOL14, offsetof(test_structmembers, structmembers.bool_member)__builtin_offsetof(test_structmembers, structmembers.bool_member
)
, 0, NULL((void*)0)},
2362 {"T_BYTE", T_BYTE8, offsetof(test_structmembers, structmembers.byte_member)__builtin_offsetof(test_structmembers, structmembers.byte_member
)
, 0, NULL((void*)0)},
2363 {"T_UBYTE", T_UBYTE9, offsetof(test_structmembers, structmembers.ubyte_member)__builtin_offsetof(test_structmembers, structmembers.ubyte_member
)
, 0, NULL((void*)0)},
2364 {"T_SHORT", T_SHORT0, offsetof(test_structmembers, structmembers.short_member)__builtin_offsetof(test_structmembers, structmembers.short_member
)
, 0, NULL((void*)0)},
2365 {"T_USHORT", T_USHORT10, offsetof(test_structmembers, structmembers.ushort_member)__builtin_offsetof(test_structmembers, structmembers.ushort_member
)
, 0, NULL((void*)0)},
2366 {"T_INT", T_INT1, offsetof(test_structmembers, structmembers.int_member)__builtin_offsetof(test_structmembers, structmembers.int_member
)
, 0, NULL((void*)0)},
2367 {"T_UINT", T_UINT11, offsetof(test_structmembers, structmembers.uint_member)__builtin_offsetof(test_structmembers, structmembers.uint_member
)
, 0, NULL((void*)0)},
2368 {"T_LONG", T_LONG2, offsetof(test_structmembers, structmembers.long_member)__builtin_offsetof(test_structmembers, structmembers.long_member
)
, 0, NULL((void*)0)},
2369 {"T_ULONG", T_ULONG12, offsetof(test_structmembers, structmembers.ulong_member)__builtin_offsetof(test_structmembers, structmembers.ulong_member
)
, 0, NULL((void*)0)},
2370 {"T_PYSSIZET", T_PYSSIZET19, offsetof(test_structmembers, structmembers.pyssizet_member)__builtin_offsetof(test_structmembers, structmembers.pyssizet_member
)
, 0, NULL((void*)0)},
2371 {"T_FLOAT", T_FLOAT3, offsetof(test_structmembers, structmembers.float_member)__builtin_offsetof(test_structmembers, structmembers.float_member
)
, 0, NULL((void*)0)},
2372 {"T_DOUBLE", T_DOUBLE4, offsetof(test_structmembers, structmembers.double_member)__builtin_offsetof(test_structmembers, structmembers.double_member
)
, 0, NULL((void*)0)},
2373 {"T_STRING_INPLACE", T_STRING_INPLACE13, offsetof(test_structmembers, structmembers.inplace_member)__builtin_offsetof(test_structmembers, structmembers.inplace_member
)
, 0, NULL((void*)0)},
2374#ifdef HAVE_LONG_LONG1
2375 {"T_LONGLONG", T_LONGLONG17, offsetof(test_structmembers, structmembers.longlong_member)__builtin_offsetof(test_structmembers, structmembers.longlong_member
)
, 0, NULL((void*)0)},
2376 {"T_ULONGLONG", T_ULONGLONG18, offsetof(test_structmembers, structmembers.ulonglong_member)__builtin_offsetof(test_structmembers, structmembers.ulonglong_member
)
, 0, NULL((void*)0)},
2377#endif
2378 {NULL((void*)0)}
2379};
2380
2381
2382static PyObject *
2383test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2384{
2385 static char *keywords[] = {
2386 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
2387 "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET",
2388 "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
2389#ifdef HAVE_LONG_LONG1
2390 "T_LONGLONG", "T_ULONGLONG",
2391#endif
2392 NULL((void*)0)};
2393 static char *fmt = "|bbBhHiIlknfds#"
2394#ifdef HAVE_LONG_LONG1
2395 "LK"
2396#endif
2397 ;
2398 test_structmembers *ob;
2399 const char *s = NULL((void*)0);
2400 Py_ssize_t string_len = 0;
2401 ob = PyObject_New(test_structmembers, type)( (test_structmembers *) _PyObject_New(type) );
2402 if (ob == NULL((void*)0))
2403 return NULL((void*)0);
2404 memset(&ob->structmembers, 0, sizeof(all_structmembers))((__builtin_object_size (&ob->structmembers, 0) != (size_t
) -1) ? __builtin___memset_chk (&ob->structmembers, 0,
sizeof(all_structmembers), __builtin_object_size (&ob->
structmembers, 0)) : __inline_memset_chk (&ob->structmembers
, 0, sizeof(all_structmembers)))
;
2405 if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwargs, fmt, keywords,
2406 &ob->structmembers.bool_member,
2407 &ob->structmembers.byte_member,
2408 &ob->structmembers.ubyte_member,
2409 &ob->structmembers.short_member,
2410 &ob->structmembers.ushort_member,
2411 &ob->structmembers.int_member,
2412 &ob->structmembers.uint_member,
2413 &ob->structmembers.long_member,
2414 &ob->structmembers.ulong_member,
2415 &ob->structmembers.pyssizet_member,
2416 &ob->structmembers.float_member,
2417 &ob->structmembers.double_member,
2418 &s, &string_len
2419#ifdef HAVE_LONG_LONG1
2420 , &ob->structmembers.longlong_member,
2421 &ob->structmembers.ulonglong_member
2422#endif
2423 )) {
2424 Py_DECREF(ob)do { if (_Py_RefTotal-- , --((PyObject*)(ob))->ob_refcnt !=
0) { if (((PyObject*)ob)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2424, (PyObject *)(ob)); } else _Py_Dealloc((PyObject *)(ob
)); } while (0)
;
2425 return NULL((void*)0);
2426 }
2427 if (s != NULL((void*)0)) {
2428 if (string_len > 5) {
2429 Py_DECREF(ob)do { if (_Py_RefTotal-- , --((PyObject*)(ob))->ob_refcnt !=
0) { if (((PyObject*)ob)->ob_refcnt < 0) _Py_NegativeRefcount
("/Users/brett/Dev/python/3.x/py3k/Modules/_testcapimodule.c"
, 2429, (PyObject *)(ob)); } else _Py_Dealloc((PyObject *)(ob
)); } while (0)
;
2430 PyErr_SetString(PyExc_ValueError, "string too long");
2431 return NULL((void*)0);
2432 }
2433 strcpy(ob->structmembers.inplace_member, s)((__builtin_object_size (ob->structmembers.inplace_member,
0) != (size_t) -1) ? __builtin___strcpy_chk (ob->structmembers
.inplace_member, s, __builtin_object_size (ob->structmembers
.inplace_member, 2 > 1)) : __inline_strcpy_chk (ob->structmembers
.inplace_member, s))
;
2434 }
2435 else {
2436 strcpy(ob->structmembers.inplace_member, "")((__builtin_object_size (ob->structmembers.inplace_member,
0) != (size_t) -1) ? __builtin___strcpy_chk (ob->structmembers
.inplace_member, "", __builtin_object_size (ob->structmembers
.inplace_member, 2 > 1)) : __inline_strcpy_chk (ob->structmembers
.inplace_member, ""))
;
2437 }
2438 return (PyObject *)ob;
2439}
2440
2441static void
2442test_structmembers_free(PyObject *ob)
2443{
2444 PyObject_FREE_PyObject_DebugFree(ob);
2445}
2446
2447static PyTypeObject test_structmembersType = {
2448 PyVarObject_HEAD_INIT(NULL, 0){ { 0, 0, 1, ((void*)0) }, 0 },
2449 "test_structmembersType",
2450 sizeof(test_structmembers), /* tp_basicsize */
2451 0, /* tp_itemsize */
2452 test_structmembers_free, /* destructor tp_dealloc */
2453 0, /* tp_print */
2454 0, /* tp_getattr */
2455 0, /* tp_setattr */
2456 0, /* tp_reserved */
2457 0, /* tp_repr */
2458 0, /* tp_as_number */
2459 0, /* tp_as_sequence */
2460 0, /* tp_as_mapping */
2461 0, /* tp_hash */
2462 0, /* tp_call */
2463 0, /* tp_str */
2464 PyObject_GenericGetAttr, /* tp_getattro */
2465 PyObject_GenericSetAttr, /* tp_setattro */
2466 0, /* tp_as_buffer */
2467 0, /* tp_flags */
2468 "Type containing all structmember types",
2469 0, /* traverseproc tp_traverse */
2470 0, /* tp_clear */
2471 0, /* tp_richcompare */
2472 0, /* tp_weaklistoffset */
2473 0, /* tp_iter */
2474 0, /* tp_iternext */
2475 0, /* tp_methods */
2476 test_members, /* tp_members */
2477 0,
2478 0,
2479 0,
2480 0,
2481 0,
2482 0,
2483 0,
2484 0,
2485 test_structmembers_new, /* tp_new */
2486};
2487
2488
2489
2490static struct PyModuleDef _testcapimodule = {
2491 PyModuleDef_HEAD_INIT{ { 0, 0, 1, ((void*)0) }, ((void*)0), 0, ((void*)0), },
2492 "_testcapi",
2493 NULL((void*)0),
2494 -1,
2495 TestMethods,
2496 NULL((void*)0),
2497 NULL((void*)0),
2498 NULL((void*)0),
2499 NULL((void*)0)
2500};
2501
2502PyMODINIT_FUNCPyObject*
2503PyInit__testcapi(void)
2504{
2505 PyObject *m;
2506
2507 m = PyModule_Create(&_testcapimodule)PyModule_Create2TraceRefs(&_testcapimodule, 1013);
2508 if (m == NULL((void*)0))
2509 return NULL((void*)0);
2510
2511 Py_TYPE(&_HashInheritanceTester_Type)(((PyObject*)(&_HashInheritanceTester_Type))->ob_type)=&PyType_Type;
2512 Py_TYPE(&_MemoryViewTester_Type)(((PyObject*)(&_MemoryViewTester_Type))->ob_type)=&PyType_Type;
2513
2514 Py_TYPE(&test_structmembersType)(((PyObject*)(&test_structmembersType))->ob_type)=&PyType_Type;
2515 Py_INCREF(&test_structmembersType)( _Py_RefTotal++ , ((PyObject*)(&test_structmembersType))
->ob_refcnt++)
;
2516 /* don't use a name starting with "test", since we don't want
2517 test_capi to automatically call this */
2518 PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
2519
2520 PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX127));
2521 PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN(-127 -1)));
2522 PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX(127*2 +1)));
2523 PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX32767));
2524 PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN(-32767 -1)));
2525 PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX(32767 *2 +1)));
2526 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX2147483647));
2527 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN(-2147483647 -1)));
2528 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX(2147483647 *2U +1U)));
2529 PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX9223372036854775807L));
2530 PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN(-9223372036854775807L -1L)));
2531 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX(9223372036854775807L *2UL +1UL)));
2532 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX3.40282347e+38F));
2533 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN1.17549435e-38F));
2534 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX1.7976931348623157e+308));
2535 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN2.2250738585072014e-308));
2536 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX9223372036854775807LL));
2537 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN(-9223372036854775807LL -1LL)));
2538 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX(9223372036854775807LL*2ULL +1ULL)));
2539 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1))));
2540 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN(-((Py_ssize_t)(((size_t)-1)>>1))-1)));
2541 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
2542 Py_INCREF(&PyInstanceMethod_Type)( _Py_RefTotal++ , ((PyObject*)(&PyInstanceMethod_Type))->
ob_refcnt++)
;
2543 PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
2544
2545 TestError = PyErr_NewException("_testcapi.error", NULL((void*)0), NULL((void*)0));
2546 Py_INCREF(TestError)( _Py_RefTotal++ , ((PyObject*)(TestError))->ob_refcnt++);
2547 PyModule_AddObject(m, "error", TestError);
2548 return m;
2549}