1 | n/a | /* |
---|
2 | n/a | * C Extension module to test Python interpreter C APIs. |
---|
3 | n/a | * |
---|
4 | n/a | * The 'test_*' functions exported by this module are run as part of the |
---|
5 | n/a | * standard Python regression test, via Lib/test/test_capi.py. |
---|
6 | n/a | */ |
---|
7 | n/a | |
---|
8 | n/a | #define PY_SSIZE_T_CLEAN |
---|
9 | n/a | |
---|
10 | n/a | #include "Python.h" |
---|
11 | n/a | #include <float.h> |
---|
12 | n/a | #include "structmember.h" |
---|
13 | n/a | #include "datetime.h" |
---|
14 | n/a | #include "marshal.h" |
---|
15 | n/a | #include <signal.h> |
---|
16 | n/a | |
---|
17 | n/a | #ifdef MS_WINDOWS |
---|
18 | n/a | # include <winsock2.h> /* struct timeval */ |
---|
19 | n/a | #endif |
---|
20 | n/a | |
---|
21 | n/a | #ifdef WITH_THREAD |
---|
22 | n/a | #include "pythread.h" |
---|
23 | n/a | #endif /* WITH_THREAD */ |
---|
24 | n/a | static PyObject *TestError; /* set to exception object in init */ |
---|
25 | n/a | |
---|
26 | n/a | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
---|
27 | n/a | |
---|
28 | n/a | static PyObject * |
---|
29 | n/a | raiseTestError(const char* test_name, const char* msg) |
---|
30 | n/a | { |
---|
31 | n/a | PyErr_Format(TestError, "%s: %s", test_name, msg); |
---|
32 | n/a | return NULL; |
---|
33 | n/a | } |
---|
34 | n/a | |
---|
35 | n/a | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
---|
36 | n/a | |
---|
37 | n/a | The ones derived from autoconf on the UNIX-like OSes can be relied |
---|
38 | n/a | upon (in the absence of sloppy cross-compiling), but the Windows |
---|
39 | n/a | platforms have these hardcoded. Better safe than sorry. |
---|
40 | n/a | */ |
---|
41 | n/a | static PyObject* |
---|
42 | n/a | sizeof_error(const char* fatname, const char* typname, |
---|
43 | n/a | int expected, int got) |
---|
44 | n/a | { |
---|
45 | n/a | PyErr_Format(TestError, |
---|
46 | n/a | "%s #define == %d but sizeof(%s) == %d", |
---|
47 | n/a | fatname, expected, typname, got); |
---|
48 | n/a | return (PyObject*)NULL; |
---|
49 | n/a | } |
---|
50 | n/a | |
---|
51 | n/a | static PyObject* |
---|
52 | n/a | test_config(PyObject *self) |
---|
53 | n/a | { |
---|
54 | n/a | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
---|
55 | n/a | if (FATNAME != sizeof(TYPE)) \ |
---|
56 | n/a | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
---|
57 | n/a | |
---|
58 | n/a | CHECK_SIZEOF(SIZEOF_SHORT, short); |
---|
59 | n/a | CHECK_SIZEOF(SIZEOF_INT, int); |
---|
60 | n/a | CHECK_SIZEOF(SIZEOF_LONG, long); |
---|
61 | n/a | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
---|
62 | n/a | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
---|
63 | n/a | CHECK_SIZEOF(SIZEOF_LONG_LONG, long long); |
---|
64 | n/a | |
---|
65 | n/a | #undef CHECK_SIZEOF |
---|
66 | n/a | |
---|
67 | n/a | Py_RETURN_NONE; |
---|
68 | n/a | } |
---|
69 | n/a | |
---|
70 | n/a | static PyObject* |
---|
71 | n/a | test_sizeof_c_types(PyObject *self) |
---|
72 | n/a | { |
---|
73 | n/a | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
---|
74 | n/a | #pragma GCC diagnostic push |
---|
75 | n/a | #pragma GCC diagnostic ignored "-Wtype-limits" |
---|
76 | n/a | #endif |
---|
77 | n/a | #define CHECK_SIZEOF(TYPE, EXPECTED) \ |
---|
78 | n/a | if (EXPECTED != sizeof(TYPE)) { \ |
---|
79 | n/a | PyErr_Format(TestError, \ |
---|
80 | n/a | "sizeof(%s) = %u instead of %u", \ |
---|
81 | n/a | #TYPE, sizeof(TYPE), EXPECTED); \ |
---|
82 | n/a | return (PyObject*)NULL; \ |
---|
83 | n/a | } |
---|
84 | n/a | #define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0) |
---|
85 | n/a | #define CHECK_SIGNNESS(TYPE, SIGNED) \ |
---|
86 | n/a | if (IS_SIGNED(TYPE) != SIGNED) { \ |
---|
87 | n/a | PyErr_Format(TestError, \ |
---|
88 | n/a | "%s signness is, instead of %i", \ |
---|
89 | n/a | #TYPE, IS_SIGNED(TYPE), SIGNED); \ |
---|
90 | n/a | return (PyObject*)NULL; \ |
---|
91 | n/a | } |
---|
92 | n/a | |
---|
93 | n/a | /* integer types */ |
---|
94 | n/a | CHECK_SIZEOF(Py_UCS1, 1); |
---|
95 | n/a | CHECK_SIZEOF(Py_UCS2, 2); |
---|
96 | n/a | CHECK_SIZEOF(Py_UCS4, 4); |
---|
97 | n/a | CHECK_SIGNNESS(Py_UCS1, 0); |
---|
98 | n/a | CHECK_SIGNNESS(Py_UCS2, 0); |
---|
99 | n/a | CHECK_SIGNNESS(Py_UCS4, 0); |
---|
100 | n/a | CHECK_SIZEOF(int32_t, 4); |
---|
101 | n/a | CHECK_SIGNNESS(int32_t, 1); |
---|
102 | n/a | CHECK_SIZEOF(uint32_t, 4); |
---|
103 | n/a | CHECK_SIGNNESS(uint32_t, 0); |
---|
104 | n/a | CHECK_SIZEOF(int64_t, 8); |
---|
105 | n/a | CHECK_SIGNNESS(int64_t, 1); |
---|
106 | n/a | CHECK_SIZEOF(uint64_t, 8); |
---|
107 | n/a | CHECK_SIGNNESS(uint64_t, 0); |
---|
108 | n/a | |
---|
109 | n/a | /* pointer/size types */ |
---|
110 | n/a | CHECK_SIZEOF(size_t, sizeof(void *)); |
---|
111 | n/a | CHECK_SIGNNESS(size_t, 0); |
---|
112 | n/a | CHECK_SIZEOF(Py_ssize_t, sizeof(void *)); |
---|
113 | n/a | CHECK_SIGNNESS(Py_ssize_t, 1); |
---|
114 | n/a | |
---|
115 | n/a | CHECK_SIZEOF(uintptr_t, sizeof(void *)); |
---|
116 | n/a | CHECK_SIGNNESS(uintptr_t, 0); |
---|
117 | n/a | CHECK_SIZEOF(intptr_t, sizeof(void *)); |
---|
118 | n/a | CHECK_SIGNNESS(intptr_t, 1); |
---|
119 | n/a | |
---|
120 | n/a | Py_RETURN_NONE; |
---|
121 | n/a | |
---|
122 | n/a | #undef IS_SIGNED |
---|
123 | n/a | #undef CHECK_SIGNESS |
---|
124 | n/a | #undef CHECK_SIZEOF |
---|
125 | n/a | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
---|
126 | n/a | #pragma GCC diagnostic pop |
---|
127 | n/a | #endif |
---|
128 | n/a | } |
---|
129 | n/a | |
---|
130 | n/a | |
---|
131 | n/a | static PyObject* |
---|
132 | n/a | test_list_api(PyObject *self) |
---|
133 | n/a | { |
---|
134 | n/a | PyObject* list; |
---|
135 | n/a | int i; |
---|
136 | n/a | |
---|
137 | n/a | /* SF bug 132008: PyList_Reverse segfaults */ |
---|
138 | n/a | #define NLIST 30 |
---|
139 | n/a | list = PyList_New(NLIST); |
---|
140 | n/a | if (list == (PyObject*)NULL) |
---|
141 | n/a | return (PyObject*)NULL; |
---|
142 | n/a | /* list = range(NLIST) */ |
---|
143 | n/a | for (i = 0; i < NLIST; ++i) { |
---|
144 | n/a | PyObject* anint = PyLong_FromLong(i); |
---|
145 | n/a | if (anint == (PyObject*)NULL) { |
---|
146 | n/a | Py_DECREF(list); |
---|
147 | n/a | return (PyObject*)NULL; |
---|
148 | n/a | } |
---|
149 | n/a | PyList_SET_ITEM(list, i, anint); |
---|
150 | n/a | } |
---|
151 | n/a | /* list.reverse(), via PyList_Reverse() */ |
---|
152 | n/a | i = PyList_Reverse(list); /* should not blow up! */ |
---|
153 | n/a | if (i != 0) { |
---|
154 | n/a | Py_DECREF(list); |
---|
155 | n/a | return (PyObject*)NULL; |
---|
156 | n/a | } |
---|
157 | n/a | /* Check that list == range(29, -1, -1) now */ |
---|
158 | n/a | for (i = 0; i < NLIST; ++i) { |
---|
159 | n/a | PyObject* anint = PyList_GET_ITEM(list, i); |
---|
160 | n/a | if (PyLong_AS_LONG(anint) != NLIST-1-i) { |
---|
161 | n/a | PyErr_SetString(TestError, |
---|
162 | n/a | "test_list_api: reverse screwed up"); |
---|
163 | n/a | Py_DECREF(list); |
---|
164 | n/a | return (PyObject*)NULL; |
---|
165 | n/a | } |
---|
166 | n/a | } |
---|
167 | n/a | Py_DECREF(list); |
---|
168 | n/a | #undef NLIST |
---|
169 | n/a | |
---|
170 | n/a | Py_RETURN_NONE; |
---|
171 | n/a | } |
---|
172 | n/a | |
---|
173 | n/a | static int |
---|
174 | n/a | test_dict_inner(int count) |
---|
175 | n/a | { |
---|
176 | n/a | Py_ssize_t pos = 0, iterations = 0; |
---|
177 | n/a | int i; |
---|
178 | n/a | PyObject *dict = PyDict_New(); |
---|
179 | n/a | PyObject *v, *k; |
---|
180 | n/a | |
---|
181 | n/a | if (dict == NULL) |
---|
182 | n/a | return -1; |
---|
183 | n/a | |
---|
184 | n/a | for (i = 0; i < count; i++) { |
---|
185 | n/a | v = PyLong_FromLong(i); |
---|
186 | n/a | if (v == NULL) { |
---|
187 | n/a | return -1; |
---|
188 | n/a | } |
---|
189 | n/a | if (PyDict_SetItem(dict, v, v) < 0) { |
---|
190 | n/a | Py_DECREF(v); |
---|
191 | n/a | return -1; |
---|
192 | n/a | } |
---|
193 | n/a | Py_DECREF(v); |
---|
194 | n/a | } |
---|
195 | n/a | |
---|
196 | n/a | while (PyDict_Next(dict, &pos, &k, &v)) { |
---|
197 | n/a | PyObject *o; |
---|
198 | n/a | iterations++; |
---|
199 | n/a | |
---|
200 | n/a | i = PyLong_AS_LONG(v) + 1; |
---|
201 | n/a | o = PyLong_FromLong(i); |
---|
202 | n/a | if (o == NULL) |
---|
203 | n/a | return -1; |
---|
204 | n/a | if (PyDict_SetItem(dict, k, o) < 0) { |
---|
205 | n/a | Py_DECREF(o); |
---|
206 | n/a | return -1; |
---|
207 | n/a | } |
---|
208 | n/a | Py_DECREF(o); |
---|
209 | n/a | } |
---|
210 | n/a | |
---|
211 | n/a | Py_DECREF(dict); |
---|
212 | n/a | |
---|
213 | n/a | if (iterations != count) { |
---|
214 | n/a | PyErr_SetString( |
---|
215 | n/a | TestError, |
---|
216 | n/a | "test_dict_iteration: dict iteration went wrong "); |
---|
217 | n/a | return -1; |
---|
218 | n/a | } else { |
---|
219 | n/a | return 0; |
---|
220 | n/a | } |
---|
221 | n/a | } |
---|
222 | n/a | |
---|
223 | n/a | static PyObject* |
---|
224 | n/a | test_dict_iteration(PyObject* self) |
---|
225 | n/a | { |
---|
226 | n/a | int i; |
---|
227 | n/a | |
---|
228 | n/a | for (i = 0; i < 200; i++) { |
---|
229 | n/a | if (test_dict_inner(i) < 0) { |
---|
230 | n/a | return NULL; |
---|
231 | n/a | } |
---|
232 | n/a | } |
---|
233 | n/a | |
---|
234 | n/a | Py_RETURN_NONE; |
---|
235 | n/a | } |
---|
236 | n/a | |
---|
237 | n/a | static PyObject* |
---|
238 | n/a | dict_getitem_knownhash(PyObject *self, PyObject *args) |
---|
239 | n/a | { |
---|
240 | n/a | PyObject *mp, *key, *result; |
---|
241 | n/a | Py_ssize_t hash; |
---|
242 | n/a | |
---|
243 | n/a | if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash", |
---|
244 | n/a | &mp, &key, &hash)) { |
---|
245 | n/a | return NULL; |
---|
246 | n/a | } |
---|
247 | n/a | |
---|
248 | n/a | result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash); |
---|
249 | n/a | if (result == NULL && !PyErr_Occurred()) { |
---|
250 | n/a | _PyErr_SetKeyError(key); |
---|
251 | n/a | return NULL; |
---|
252 | n/a | } |
---|
253 | n/a | |
---|
254 | n/a | Py_XINCREF(result); |
---|
255 | n/a | return result; |
---|
256 | n/a | } |
---|
257 | n/a | |
---|
258 | n/a | static PyObject* |
---|
259 | n/a | dict_hassplittable(PyObject *self, PyObject *arg) |
---|
260 | n/a | { |
---|
261 | n/a | if (!PyDict_Check(arg)) { |
---|
262 | n/a | PyErr_Format(PyExc_TypeError, |
---|
263 | n/a | "dict_hassplittable() argument must be dict, not '%s'", |
---|
264 | n/a | arg->ob_type->tp_name); |
---|
265 | n/a | return NULL; |
---|
266 | n/a | } |
---|
267 | n/a | |
---|
268 | n/a | return PyBool_FromLong(_PyDict_HasSplitTable((PyDictObject*)arg)); |
---|
269 | n/a | } |
---|
270 | n/a | |
---|
271 | n/a | /* Issue #4701: Check that PyObject_Hash implicitly calls |
---|
272 | n/a | * PyType_Ready if it hasn't already been called |
---|
273 | n/a | */ |
---|
274 | n/a | static PyTypeObject _HashInheritanceTester_Type = { |
---|
275 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
276 | n/a | "hashinheritancetester", /* Name of this type */ |
---|
277 | n/a | sizeof(PyObject), /* Basic object size */ |
---|
278 | n/a | 0, /* Item size for varobject */ |
---|
279 | n/a | (destructor)PyObject_Del, /* tp_dealloc */ |
---|
280 | n/a | 0, /* tp_print */ |
---|
281 | n/a | 0, /* tp_getattr */ |
---|
282 | n/a | 0, /* tp_setattr */ |
---|
283 | n/a | 0, /* tp_reserved */ |
---|
284 | n/a | 0, /* tp_repr */ |
---|
285 | n/a | 0, /* tp_as_number */ |
---|
286 | n/a | 0, /* tp_as_sequence */ |
---|
287 | n/a | 0, /* tp_as_mapping */ |
---|
288 | n/a | 0, /* tp_hash */ |
---|
289 | n/a | 0, /* tp_call */ |
---|
290 | n/a | 0, /* tp_str */ |
---|
291 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
292 | n/a | 0, /* tp_setattro */ |
---|
293 | n/a | 0, /* tp_as_buffer */ |
---|
294 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
295 | n/a | 0, /* tp_doc */ |
---|
296 | n/a | 0, /* tp_traverse */ |
---|
297 | n/a | 0, /* tp_clear */ |
---|
298 | n/a | 0, /* tp_richcompare */ |
---|
299 | n/a | 0, /* tp_weaklistoffset */ |
---|
300 | n/a | 0, /* tp_iter */ |
---|
301 | n/a | 0, /* tp_iternext */ |
---|
302 | n/a | 0, /* tp_methods */ |
---|
303 | n/a | 0, /* tp_members */ |
---|
304 | n/a | 0, /* tp_getset */ |
---|
305 | n/a | 0, /* tp_base */ |
---|
306 | n/a | 0, /* tp_dict */ |
---|
307 | n/a | 0, /* tp_descr_get */ |
---|
308 | n/a | 0, /* tp_descr_set */ |
---|
309 | n/a | 0, /* tp_dictoffset */ |
---|
310 | n/a | 0, /* tp_init */ |
---|
311 | n/a | 0, /* tp_alloc */ |
---|
312 | n/a | PyType_GenericNew, /* tp_new */ |
---|
313 | n/a | }; |
---|
314 | n/a | |
---|
315 | n/a | static PyObject* |
---|
316 | n/a | test_lazy_hash_inheritance(PyObject* self) |
---|
317 | n/a | { |
---|
318 | n/a | PyTypeObject *type; |
---|
319 | n/a | PyObject *obj; |
---|
320 | n/a | Py_hash_t hash; |
---|
321 | n/a | |
---|
322 | n/a | type = &_HashInheritanceTester_Type; |
---|
323 | n/a | |
---|
324 | n/a | if (type->tp_dict != NULL) |
---|
325 | n/a | /* The type has already been initialized. This probably means |
---|
326 | n/a | -R is being used. */ |
---|
327 | n/a | Py_RETURN_NONE; |
---|
328 | n/a | |
---|
329 | n/a | |
---|
330 | n/a | obj = PyObject_New(PyObject, type); |
---|
331 | n/a | if (obj == NULL) { |
---|
332 | n/a | PyErr_Clear(); |
---|
333 | n/a | PyErr_SetString( |
---|
334 | n/a | TestError, |
---|
335 | n/a | "test_lazy_hash_inheritance: failed to create object"); |
---|
336 | n/a | return NULL; |
---|
337 | n/a | } |
---|
338 | n/a | |
---|
339 | n/a | if (type->tp_dict != NULL) { |
---|
340 | n/a | PyErr_SetString( |
---|
341 | n/a | TestError, |
---|
342 | n/a | "test_lazy_hash_inheritance: type initialised too soon"); |
---|
343 | n/a | Py_DECREF(obj); |
---|
344 | n/a | return NULL; |
---|
345 | n/a | } |
---|
346 | n/a | |
---|
347 | n/a | hash = PyObject_Hash(obj); |
---|
348 | n/a | if ((hash == -1) && PyErr_Occurred()) { |
---|
349 | n/a | PyErr_Clear(); |
---|
350 | n/a | PyErr_SetString( |
---|
351 | n/a | TestError, |
---|
352 | n/a | "test_lazy_hash_inheritance: could not hash object"); |
---|
353 | n/a | Py_DECREF(obj); |
---|
354 | n/a | return NULL; |
---|
355 | n/a | } |
---|
356 | n/a | |
---|
357 | n/a | if (type->tp_dict == NULL) { |
---|
358 | n/a | PyErr_SetString( |
---|
359 | n/a | TestError, |
---|
360 | n/a | "test_lazy_hash_inheritance: type not initialised by hash()"); |
---|
361 | n/a | Py_DECREF(obj); |
---|
362 | n/a | return NULL; |
---|
363 | n/a | } |
---|
364 | n/a | |
---|
365 | n/a | if (type->tp_hash != PyType_Type.tp_hash) { |
---|
366 | n/a | PyErr_SetString( |
---|
367 | n/a | TestError, |
---|
368 | n/a | "test_lazy_hash_inheritance: unexpected hash function"); |
---|
369 | n/a | Py_DECREF(obj); |
---|
370 | n/a | return NULL; |
---|
371 | n/a | } |
---|
372 | n/a | |
---|
373 | n/a | Py_DECREF(obj); |
---|
374 | n/a | |
---|
375 | n/a | Py_RETURN_NONE; |
---|
376 | n/a | } |
---|
377 | n/a | |
---|
378 | n/a | |
---|
379 | n/a | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and |
---|
380 | n/a | PyLong_{As, From}{Unsigned,}LongLong(). |
---|
381 | n/a | |
---|
382 | n/a | Note that the meat of the test is contained in testcapi_long.h. |
---|
383 | n/a | This is revolting, but delicate code duplication is worse: "almost |
---|
384 | n/a | exactly the same" code is needed to test long long, but the ubiquitous |
---|
385 | n/a | dependence on type names makes it impossible to use a parameterized |
---|
386 | n/a | function. A giant macro would be even worse than this. A C++ template |
---|
387 | n/a | would be perfect. |
---|
388 | n/a | |
---|
389 | n/a | The "report an error" functions are deliberately not part of the #include |
---|
390 | n/a | file: if the test fails, you can set a breakpoint in the appropriate |
---|
391 | n/a | error function directly, and crawl back from there in the debugger. |
---|
392 | n/a | */ |
---|
393 | n/a | |
---|
394 | n/a | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
---|
395 | n/a | |
---|
396 | n/a | static PyObject * |
---|
397 | n/a | raise_test_long_error(const char* msg) |
---|
398 | n/a | { |
---|
399 | n/a | return raiseTestError("test_long_api", msg); |
---|
400 | n/a | } |
---|
401 | n/a | |
---|
402 | n/a | #define TESTNAME test_long_api_inner |
---|
403 | n/a | #define TYPENAME long |
---|
404 | n/a | #define F_S_TO_PY PyLong_FromLong |
---|
405 | n/a | #define F_PY_TO_S PyLong_AsLong |
---|
406 | n/a | #define F_U_TO_PY PyLong_FromUnsignedLong |
---|
407 | n/a | #define F_PY_TO_U PyLong_AsUnsignedLong |
---|
408 | n/a | |
---|
409 | n/a | #include "testcapi_long.h" |
---|
410 | n/a | |
---|
411 | n/a | static PyObject * |
---|
412 | n/a | test_long_api(PyObject* self) |
---|
413 | n/a | { |
---|
414 | n/a | return TESTNAME(raise_test_long_error); |
---|
415 | n/a | } |
---|
416 | n/a | |
---|
417 | n/a | #undef TESTNAME |
---|
418 | n/a | #undef TYPENAME |
---|
419 | n/a | #undef F_S_TO_PY |
---|
420 | n/a | #undef F_PY_TO_S |
---|
421 | n/a | #undef F_U_TO_PY |
---|
422 | n/a | #undef F_PY_TO_U |
---|
423 | n/a | |
---|
424 | n/a | static PyObject * |
---|
425 | n/a | raise_test_longlong_error(const char* msg) |
---|
426 | n/a | { |
---|
427 | n/a | return raiseTestError("test_longlong_api", msg); |
---|
428 | n/a | } |
---|
429 | n/a | |
---|
430 | n/a | #define TESTNAME test_longlong_api_inner |
---|
431 | n/a | #define TYPENAME long long |
---|
432 | n/a | #define F_S_TO_PY PyLong_FromLongLong |
---|
433 | n/a | #define F_PY_TO_S PyLong_AsLongLong |
---|
434 | n/a | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
---|
435 | n/a | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
---|
436 | n/a | |
---|
437 | n/a | #include "testcapi_long.h" |
---|
438 | n/a | |
---|
439 | n/a | static PyObject * |
---|
440 | n/a | test_longlong_api(PyObject* self, PyObject *args) |
---|
441 | n/a | { |
---|
442 | n/a | return TESTNAME(raise_test_longlong_error); |
---|
443 | n/a | } |
---|
444 | n/a | |
---|
445 | n/a | #undef TESTNAME |
---|
446 | n/a | #undef TYPENAME |
---|
447 | n/a | #undef F_S_TO_PY |
---|
448 | n/a | #undef F_PY_TO_S |
---|
449 | n/a | #undef F_U_TO_PY |
---|
450 | n/a | #undef F_PY_TO_U |
---|
451 | n/a | |
---|
452 | n/a | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG |
---|
453 | n/a | is tested by test_long_api_inner. This test will concentrate on proper |
---|
454 | n/a | handling of overflow. |
---|
455 | n/a | */ |
---|
456 | n/a | |
---|
457 | n/a | static PyObject * |
---|
458 | n/a | test_long_and_overflow(PyObject *self) |
---|
459 | n/a | { |
---|
460 | n/a | PyObject *num, *one, *temp; |
---|
461 | n/a | long value; |
---|
462 | n/a | int overflow; |
---|
463 | n/a | |
---|
464 | n/a | /* Test that overflow is set properly for a large value. */ |
---|
465 | n/a | /* num is a number larger than LONG_MAX even on 64-bit platforms */ |
---|
466 | n/a | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
---|
467 | n/a | if (num == NULL) |
---|
468 | n/a | return NULL; |
---|
469 | n/a | overflow = 1234; |
---|
470 | n/a | value = PyLong_AsLongAndOverflow(num, &overflow); |
---|
471 | n/a | Py_DECREF(num); |
---|
472 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
473 | n/a | return NULL; |
---|
474 | n/a | if (value != -1) |
---|
475 | n/a | return raiseTestError("test_long_and_overflow", |
---|
476 | n/a | "return value was not set to -1"); |
---|
477 | n/a | if (overflow != 1) |
---|
478 | n/a | return raiseTestError("test_long_and_overflow", |
---|
479 | n/a | "overflow was not set to 1"); |
---|
480 | n/a | |
---|
481 | n/a | /* Same again, with num = LONG_MAX + 1 */ |
---|
482 | n/a | num = PyLong_FromLong(LONG_MAX); |
---|
483 | n/a | if (num == NULL) |
---|
484 | n/a | return NULL; |
---|
485 | n/a | one = PyLong_FromLong(1L); |
---|
486 | n/a | if (one == NULL) { |
---|
487 | n/a | Py_DECREF(num); |
---|
488 | n/a | return NULL; |
---|
489 | n/a | } |
---|
490 | n/a | temp = PyNumber_Add(num, one); |
---|
491 | n/a | Py_DECREF(one); |
---|
492 | n/a | Py_DECREF(num); |
---|
493 | n/a | num = temp; |
---|
494 | n/a | if (num == NULL) |
---|
495 | n/a | return NULL; |
---|
496 | n/a | overflow = 0; |
---|
497 | n/a | value = PyLong_AsLongAndOverflow(num, &overflow); |
---|
498 | n/a | Py_DECREF(num); |
---|
499 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
500 | n/a | return NULL; |
---|
501 | n/a | if (value != -1) |
---|
502 | n/a | return raiseTestError("test_long_and_overflow", |
---|
503 | n/a | "return value was not set to -1"); |
---|
504 | n/a | if (overflow != 1) |
---|
505 | n/a | return raiseTestError("test_long_and_overflow", |
---|
506 | n/a | "overflow was not set to 1"); |
---|
507 | n/a | |
---|
508 | n/a | /* Test that overflow is set properly for a large negative value. */ |
---|
509 | n/a | /* num is a number smaller than LONG_MIN even on 64-bit platforms */ |
---|
510 | n/a | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
---|
511 | n/a | if (num == NULL) |
---|
512 | n/a | return NULL; |
---|
513 | n/a | overflow = 1234; |
---|
514 | n/a | value = PyLong_AsLongAndOverflow(num, &overflow); |
---|
515 | n/a | Py_DECREF(num); |
---|
516 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
517 | n/a | return NULL; |
---|
518 | n/a | if (value != -1) |
---|
519 | n/a | return raiseTestError("test_long_and_overflow", |
---|
520 | n/a | "return value was not set to -1"); |
---|
521 | n/a | if (overflow != -1) |
---|
522 | n/a | return raiseTestError("test_long_and_overflow", |
---|
523 | n/a | "overflow was not set to -1"); |
---|
524 | n/a | |
---|
525 | n/a | /* Same again, with num = LONG_MIN - 1 */ |
---|
526 | n/a | num = PyLong_FromLong(LONG_MIN); |
---|
527 | n/a | if (num == NULL) |
---|
528 | n/a | return NULL; |
---|
529 | n/a | one = PyLong_FromLong(1L); |
---|
530 | n/a | if (one == NULL) { |
---|
531 | n/a | Py_DECREF(num); |
---|
532 | n/a | return NULL; |
---|
533 | n/a | } |
---|
534 | n/a | temp = PyNumber_Subtract(num, one); |
---|
535 | n/a | Py_DECREF(one); |
---|
536 | n/a | Py_DECREF(num); |
---|
537 | n/a | num = temp; |
---|
538 | n/a | if (num == NULL) |
---|
539 | n/a | return NULL; |
---|
540 | n/a | overflow = 0; |
---|
541 | n/a | value = PyLong_AsLongAndOverflow(num, &overflow); |
---|
542 | n/a | Py_DECREF(num); |
---|
543 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
544 | n/a | return NULL; |
---|
545 | n/a | if (value != -1) |
---|
546 | n/a | return raiseTestError("test_long_and_overflow", |
---|
547 | n/a | "return value was not set to -1"); |
---|
548 | n/a | if (overflow != -1) |
---|
549 | n/a | return raiseTestError("test_long_and_overflow", |
---|
550 | n/a | "overflow was not set to -1"); |
---|
551 | n/a | |
---|
552 | n/a | /* Test that overflow is cleared properly for small values. */ |
---|
553 | n/a | num = PyLong_FromString("FF", NULL, 16); |
---|
554 | n/a | if (num == NULL) |
---|
555 | n/a | return NULL; |
---|
556 | n/a | overflow = 1234; |
---|
557 | n/a | value = PyLong_AsLongAndOverflow(num, &overflow); |
---|
558 | n/a | Py_DECREF(num); |
---|
559 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
560 | n/a | return NULL; |
---|
561 | n/a | if (value != 0xFF) |
---|
562 | n/a | return raiseTestError("test_long_and_overflow", |
---|
563 | n/a | "expected return value 0xFF"); |
---|
564 | n/a | if (overflow != 0) |
---|
565 | n/a | return raiseTestError("test_long_and_overflow", |
---|
566 | n/a | "overflow was not cleared"); |
---|
567 | n/a | |
---|
568 | n/a | num = PyLong_FromString("-FF", NULL, 16); |
---|
569 | n/a | if (num == NULL) |
---|
570 | n/a | return NULL; |
---|
571 | n/a | overflow = 0; |
---|
572 | n/a | value = PyLong_AsLongAndOverflow(num, &overflow); |
---|
573 | n/a | Py_DECREF(num); |
---|
574 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
575 | n/a | return NULL; |
---|
576 | n/a | if (value != -0xFF) |
---|
577 | n/a | return raiseTestError("test_long_and_overflow", |
---|
578 | n/a | "expected return value 0xFF"); |
---|
579 | n/a | if (overflow != 0) |
---|
580 | n/a | return raiseTestError("test_long_and_overflow", |
---|
581 | n/a | "overflow was set incorrectly"); |
---|
582 | n/a | |
---|
583 | n/a | num = PyLong_FromLong(LONG_MAX); |
---|
584 | n/a | if (num == NULL) |
---|
585 | n/a | return NULL; |
---|
586 | n/a | overflow = 1234; |
---|
587 | n/a | value = PyLong_AsLongAndOverflow(num, &overflow); |
---|
588 | n/a | Py_DECREF(num); |
---|
589 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
590 | n/a | return NULL; |
---|
591 | n/a | if (value != LONG_MAX) |
---|
592 | n/a | return raiseTestError("test_long_and_overflow", |
---|
593 | n/a | "expected return value LONG_MAX"); |
---|
594 | n/a | if (overflow != 0) |
---|
595 | n/a | return raiseTestError("test_long_and_overflow", |
---|
596 | n/a | "overflow was not cleared"); |
---|
597 | n/a | |
---|
598 | n/a | num = PyLong_FromLong(LONG_MIN); |
---|
599 | n/a | if (num == NULL) |
---|
600 | n/a | return NULL; |
---|
601 | n/a | overflow = 0; |
---|
602 | n/a | value = PyLong_AsLongAndOverflow(num, &overflow); |
---|
603 | n/a | Py_DECREF(num); |
---|
604 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
605 | n/a | return NULL; |
---|
606 | n/a | if (value != LONG_MIN) |
---|
607 | n/a | return raiseTestError("test_long_and_overflow", |
---|
608 | n/a | "expected return value LONG_MIN"); |
---|
609 | n/a | if (overflow != 0) |
---|
610 | n/a | return raiseTestError("test_long_and_overflow", |
---|
611 | n/a | "overflow was not cleared"); |
---|
612 | n/a | |
---|
613 | n/a | Py_RETURN_NONE; |
---|
614 | n/a | } |
---|
615 | n/a | |
---|
616 | n/a | /* Test the PyLong_AsLongLongAndOverflow API. General conversion to |
---|
617 | n/a | long long is tested by test_long_api_inner. This test will |
---|
618 | n/a | concentrate on proper handling of overflow. |
---|
619 | n/a | */ |
---|
620 | n/a | |
---|
621 | n/a | static PyObject * |
---|
622 | n/a | test_long_long_and_overflow(PyObject *self) |
---|
623 | n/a | { |
---|
624 | n/a | PyObject *num, *one, *temp; |
---|
625 | n/a | long long value; |
---|
626 | n/a | int overflow; |
---|
627 | n/a | |
---|
628 | n/a | /* Test that overflow is set properly for a large value. */ |
---|
629 | n/a | /* num is a number larger than PY_LLONG_MAX on a typical machine. */ |
---|
630 | n/a | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
---|
631 | n/a | if (num == NULL) |
---|
632 | n/a | return NULL; |
---|
633 | n/a | overflow = 1234; |
---|
634 | n/a | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
---|
635 | n/a | Py_DECREF(num); |
---|
636 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
637 | n/a | return NULL; |
---|
638 | n/a | if (value != -1) |
---|
639 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
640 | n/a | "return value was not set to -1"); |
---|
641 | n/a | if (overflow != 1) |
---|
642 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
643 | n/a | "overflow was not set to 1"); |
---|
644 | n/a | |
---|
645 | n/a | /* Same again, with num = PY_LLONG_MAX + 1 */ |
---|
646 | n/a | num = PyLong_FromLongLong(PY_LLONG_MAX); |
---|
647 | n/a | if (num == NULL) |
---|
648 | n/a | return NULL; |
---|
649 | n/a | one = PyLong_FromLong(1L); |
---|
650 | n/a | if (one == NULL) { |
---|
651 | n/a | Py_DECREF(num); |
---|
652 | n/a | return NULL; |
---|
653 | n/a | } |
---|
654 | n/a | temp = PyNumber_Add(num, one); |
---|
655 | n/a | Py_DECREF(one); |
---|
656 | n/a | Py_DECREF(num); |
---|
657 | n/a | num = temp; |
---|
658 | n/a | if (num == NULL) |
---|
659 | n/a | return NULL; |
---|
660 | n/a | overflow = 0; |
---|
661 | n/a | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
---|
662 | n/a | Py_DECREF(num); |
---|
663 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
664 | n/a | return NULL; |
---|
665 | n/a | if (value != -1) |
---|
666 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
667 | n/a | "return value was not set to -1"); |
---|
668 | n/a | if (overflow != 1) |
---|
669 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
670 | n/a | "overflow was not set to 1"); |
---|
671 | n/a | |
---|
672 | n/a | /* Test that overflow is set properly for a large negative value. */ |
---|
673 | n/a | /* num is a number smaller than PY_LLONG_MIN on a typical platform */ |
---|
674 | n/a | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
---|
675 | n/a | if (num == NULL) |
---|
676 | n/a | return NULL; |
---|
677 | n/a | overflow = 1234; |
---|
678 | n/a | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
---|
679 | n/a | Py_DECREF(num); |
---|
680 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
681 | n/a | return NULL; |
---|
682 | n/a | if (value != -1) |
---|
683 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
684 | n/a | "return value was not set to -1"); |
---|
685 | n/a | if (overflow != -1) |
---|
686 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
687 | n/a | "overflow was not set to -1"); |
---|
688 | n/a | |
---|
689 | n/a | /* Same again, with num = PY_LLONG_MIN - 1 */ |
---|
690 | n/a | num = PyLong_FromLongLong(PY_LLONG_MIN); |
---|
691 | n/a | if (num == NULL) |
---|
692 | n/a | return NULL; |
---|
693 | n/a | one = PyLong_FromLong(1L); |
---|
694 | n/a | if (one == NULL) { |
---|
695 | n/a | Py_DECREF(num); |
---|
696 | n/a | return NULL; |
---|
697 | n/a | } |
---|
698 | n/a | temp = PyNumber_Subtract(num, one); |
---|
699 | n/a | Py_DECREF(one); |
---|
700 | n/a | Py_DECREF(num); |
---|
701 | n/a | num = temp; |
---|
702 | n/a | if (num == NULL) |
---|
703 | n/a | return NULL; |
---|
704 | n/a | overflow = 0; |
---|
705 | n/a | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
---|
706 | n/a | Py_DECREF(num); |
---|
707 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
708 | n/a | return NULL; |
---|
709 | n/a | if (value != -1) |
---|
710 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
711 | n/a | "return value was not set to -1"); |
---|
712 | n/a | if (overflow != -1) |
---|
713 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
714 | n/a | "overflow was not set to -1"); |
---|
715 | n/a | |
---|
716 | n/a | /* Test that overflow is cleared properly for small values. */ |
---|
717 | n/a | num = PyLong_FromString("FF", NULL, 16); |
---|
718 | n/a | if (num == NULL) |
---|
719 | n/a | return NULL; |
---|
720 | n/a | overflow = 1234; |
---|
721 | n/a | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
---|
722 | n/a | Py_DECREF(num); |
---|
723 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
724 | n/a | return NULL; |
---|
725 | n/a | if (value != 0xFF) |
---|
726 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
727 | n/a | "expected return value 0xFF"); |
---|
728 | n/a | if (overflow != 0) |
---|
729 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
730 | n/a | "overflow was not cleared"); |
---|
731 | n/a | |
---|
732 | n/a | num = PyLong_FromString("-FF", NULL, 16); |
---|
733 | n/a | if (num == NULL) |
---|
734 | n/a | return NULL; |
---|
735 | n/a | overflow = 0; |
---|
736 | n/a | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
---|
737 | n/a | Py_DECREF(num); |
---|
738 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
739 | n/a | return NULL; |
---|
740 | n/a | if (value != -0xFF) |
---|
741 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
742 | n/a | "expected return value 0xFF"); |
---|
743 | n/a | if (overflow != 0) |
---|
744 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
745 | n/a | "overflow was set incorrectly"); |
---|
746 | n/a | |
---|
747 | n/a | num = PyLong_FromLongLong(PY_LLONG_MAX); |
---|
748 | n/a | if (num == NULL) |
---|
749 | n/a | return NULL; |
---|
750 | n/a | overflow = 1234; |
---|
751 | n/a | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
---|
752 | n/a | Py_DECREF(num); |
---|
753 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
754 | n/a | return NULL; |
---|
755 | n/a | if (value != PY_LLONG_MAX) |
---|
756 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
757 | n/a | "expected return value PY_LLONG_MAX"); |
---|
758 | n/a | if (overflow != 0) |
---|
759 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
760 | n/a | "overflow was not cleared"); |
---|
761 | n/a | |
---|
762 | n/a | num = PyLong_FromLongLong(PY_LLONG_MIN); |
---|
763 | n/a | if (num == NULL) |
---|
764 | n/a | return NULL; |
---|
765 | n/a | overflow = 0; |
---|
766 | n/a | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
---|
767 | n/a | Py_DECREF(num); |
---|
768 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
769 | n/a | return NULL; |
---|
770 | n/a | if (value != PY_LLONG_MIN) |
---|
771 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
772 | n/a | "expected return value PY_LLONG_MIN"); |
---|
773 | n/a | if (overflow != 0) |
---|
774 | n/a | return raiseTestError("test_long_long_and_overflow", |
---|
775 | n/a | "overflow was not cleared"); |
---|
776 | n/a | |
---|
777 | n/a | Py_RETURN_NONE; |
---|
778 | n/a | } |
---|
779 | n/a | |
---|
780 | n/a | /* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that |
---|
781 | n/a | non-integer arguments are handled correctly. It should be extended to |
---|
782 | n/a | test overflow handling. |
---|
783 | n/a | */ |
---|
784 | n/a | |
---|
785 | n/a | static PyObject * |
---|
786 | n/a | test_long_as_size_t(PyObject *self) |
---|
787 | n/a | { |
---|
788 | n/a | size_t out_u; |
---|
789 | n/a | Py_ssize_t out_s; |
---|
790 | n/a | |
---|
791 | n/a | Py_INCREF(Py_None); |
---|
792 | n/a | |
---|
793 | n/a | out_u = PyLong_AsSize_t(Py_None); |
---|
794 | n/a | if (out_u != (size_t)-1 || !PyErr_Occurred()) |
---|
795 | n/a | return raiseTestError("test_long_as_size_t", |
---|
796 | n/a | "PyLong_AsSize_t(None) didn't complain"); |
---|
797 | n/a | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
---|
798 | n/a | return raiseTestError("test_long_as_size_t", |
---|
799 | n/a | "PyLong_AsSize_t(None) raised " |
---|
800 | n/a | "something other than TypeError"); |
---|
801 | n/a | PyErr_Clear(); |
---|
802 | n/a | |
---|
803 | n/a | out_s = PyLong_AsSsize_t(Py_None); |
---|
804 | n/a | if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred()) |
---|
805 | n/a | return raiseTestError("test_long_as_size_t", |
---|
806 | n/a | "PyLong_AsSsize_t(None) didn't complain"); |
---|
807 | n/a | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
---|
808 | n/a | return raiseTestError("test_long_as_size_t", |
---|
809 | n/a | "PyLong_AsSsize_t(None) raised " |
---|
810 | n/a | "something other than TypeError"); |
---|
811 | n/a | PyErr_Clear(); |
---|
812 | n/a | |
---|
813 | n/a | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
---|
814 | n/a | return Py_None; |
---|
815 | n/a | } |
---|
816 | n/a | |
---|
817 | n/a | /* Test the PyLong_AsDouble API. At present this just tests that |
---|
818 | n/a | non-integer arguments are handled correctly. |
---|
819 | n/a | */ |
---|
820 | n/a | |
---|
821 | n/a | static PyObject * |
---|
822 | n/a | test_long_as_double(PyObject *self) |
---|
823 | n/a | { |
---|
824 | n/a | double out; |
---|
825 | n/a | |
---|
826 | n/a | Py_INCREF(Py_None); |
---|
827 | n/a | |
---|
828 | n/a | out = PyLong_AsDouble(Py_None); |
---|
829 | n/a | if (out != -1.0 || !PyErr_Occurred()) |
---|
830 | n/a | return raiseTestError("test_long_as_double", |
---|
831 | n/a | "PyLong_AsDouble(None) didn't complain"); |
---|
832 | n/a | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
---|
833 | n/a | return raiseTestError("test_long_as_double", |
---|
834 | n/a | "PyLong_AsDouble(None) raised " |
---|
835 | n/a | "something other than TypeError"); |
---|
836 | n/a | PyErr_Clear(); |
---|
837 | n/a | |
---|
838 | n/a | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
---|
839 | n/a | return Py_None; |
---|
840 | n/a | } |
---|
841 | n/a | |
---|
842 | n/a | /* Test the L code for PyArg_ParseTuple. This should deliver a long long |
---|
843 | n/a | for both long and int arguments. The test may leak a little memory if |
---|
844 | n/a | it fails. |
---|
845 | n/a | */ |
---|
846 | n/a | static PyObject * |
---|
847 | n/a | test_L_code(PyObject *self) |
---|
848 | n/a | { |
---|
849 | n/a | PyObject *tuple, *num; |
---|
850 | n/a | long long value; |
---|
851 | n/a | |
---|
852 | n/a | tuple = PyTuple_New(1); |
---|
853 | n/a | if (tuple == NULL) |
---|
854 | n/a | return NULL; |
---|
855 | n/a | |
---|
856 | n/a | num = PyLong_FromLong(42); |
---|
857 | n/a | if (num == NULL) |
---|
858 | n/a | return NULL; |
---|
859 | n/a | |
---|
860 | n/a | PyTuple_SET_ITEM(tuple, 0, num); |
---|
861 | n/a | |
---|
862 | n/a | value = -1; |
---|
863 | n/a | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
---|
864 | n/a | return NULL; |
---|
865 | n/a | if (value != 42) |
---|
866 | n/a | return raiseTestError("test_L_code", |
---|
867 | n/a | "L code returned wrong value for long 42"); |
---|
868 | n/a | |
---|
869 | n/a | Py_DECREF(num); |
---|
870 | n/a | num = PyLong_FromLong(42); |
---|
871 | n/a | if (num == NULL) |
---|
872 | n/a | return NULL; |
---|
873 | n/a | |
---|
874 | n/a | PyTuple_SET_ITEM(tuple, 0, num); |
---|
875 | n/a | |
---|
876 | n/a | value = -1; |
---|
877 | n/a | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
---|
878 | n/a | return NULL; |
---|
879 | n/a | if (value != 42) |
---|
880 | n/a | return raiseTestError("test_L_code", |
---|
881 | n/a | "L code returned wrong value for int 42"); |
---|
882 | n/a | |
---|
883 | n/a | Py_DECREF(tuple); |
---|
884 | n/a | Py_RETURN_NONE; |
---|
885 | n/a | } |
---|
886 | n/a | |
---|
887 | n/a | static PyObject * |
---|
888 | n/a | return_none(void *unused) |
---|
889 | n/a | { |
---|
890 | n/a | Py_RETURN_NONE; |
---|
891 | n/a | } |
---|
892 | n/a | |
---|
893 | n/a | static PyObject * |
---|
894 | n/a | raise_error(void *unused) |
---|
895 | n/a | { |
---|
896 | n/a | PyErr_SetNone(PyExc_ValueError); |
---|
897 | n/a | return NULL; |
---|
898 | n/a | } |
---|
899 | n/a | |
---|
900 | n/a | static int |
---|
901 | n/a | test_buildvalue_N_error(const char *fmt) |
---|
902 | n/a | { |
---|
903 | n/a | PyObject *arg, *res; |
---|
904 | n/a | |
---|
905 | n/a | arg = PyList_New(0); |
---|
906 | n/a | if (arg == NULL) { |
---|
907 | n/a | return -1; |
---|
908 | n/a | } |
---|
909 | n/a | |
---|
910 | n/a | Py_INCREF(arg); |
---|
911 | n/a | res = Py_BuildValue(fmt, return_none, NULL, arg); |
---|
912 | n/a | if (res == NULL) { |
---|
913 | n/a | return -1; |
---|
914 | n/a | } |
---|
915 | n/a | Py_DECREF(res); |
---|
916 | n/a | if (Py_REFCNT(arg) != 1) { |
---|
917 | n/a | PyErr_Format(TestError, "test_buildvalue_N: " |
---|
918 | n/a | "arg was not decrefed in successful " |
---|
919 | n/a | "Py_BuildValue(\"%s\")", fmt); |
---|
920 | n/a | return -1; |
---|
921 | n/a | } |
---|
922 | n/a | |
---|
923 | n/a | Py_INCREF(arg); |
---|
924 | n/a | res = Py_BuildValue(fmt, raise_error, NULL, arg); |
---|
925 | n/a | if (res != NULL || !PyErr_Occurred()) { |
---|
926 | n/a | PyErr_Format(TestError, "test_buildvalue_N: " |
---|
927 | n/a | "Py_BuildValue(\"%s\") didn't complain", fmt); |
---|
928 | n/a | return -1; |
---|
929 | n/a | } |
---|
930 | n/a | PyErr_Clear(); |
---|
931 | n/a | if (Py_REFCNT(arg) != 1) { |
---|
932 | n/a | PyErr_Format(TestError, "test_buildvalue_N: " |
---|
933 | n/a | "arg was not decrefed in failed " |
---|
934 | n/a | "Py_BuildValue(\"%s\")", fmt); |
---|
935 | n/a | return -1; |
---|
936 | n/a | } |
---|
937 | n/a | Py_DECREF(arg); |
---|
938 | n/a | return 0; |
---|
939 | n/a | } |
---|
940 | n/a | |
---|
941 | n/a | static PyObject * |
---|
942 | n/a | test_buildvalue_N(PyObject *self, PyObject *noargs) |
---|
943 | n/a | { |
---|
944 | n/a | PyObject *arg, *res; |
---|
945 | n/a | |
---|
946 | n/a | arg = PyList_New(0); |
---|
947 | n/a | if (arg == NULL) { |
---|
948 | n/a | return NULL; |
---|
949 | n/a | } |
---|
950 | n/a | Py_INCREF(arg); |
---|
951 | n/a | res = Py_BuildValue("N", arg); |
---|
952 | n/a | if (res == NULL) { |
---|
953 | n/a | return NULL; |
---|
954 | n/a | } |
---|
955 | n/a | if (res != arg) { |
---|
956 | n/a | return raiseTestError("test_buildvalue_N", |
---|
957 | n/a | "Py_BuildValue(\"N\") returned wrong result"); |
---|
958 | n/a | } |
---|
959 | n/a | if (Py_REFCNT(arg) != 2) { |
---|
960 | n/a | return raiseTestError("test_buildvalue_N", |
---|
961 | n/a | "arg was not decrefed in Py_BuildValue(\"N\")"); |
---|
962 | n/a | } |
---|
963 | n/a | Py_DECREF(res); |
---|
964 | n/a | Py_DECREF(arg); |
---|
965 | n/a | |
---|
966 | n/a | if (test_buildvalue_N_error("O&N") < 0) |
---|
967 | n/a | return NULL; |
---|
968 | n/a | if (test_buildvalue_N_error("(O&N)") < 0) |
---|
969 | n/a | return NULL; |
---|
970 | n/a | if (test_buildvalue_N_error("[O&N]") < 0) |
---|
971 | n/a | return NULL; |
---|
972 | n/a | if (test_buildvalue_N_error("{O&N}") < 0) |
---|
973 | n/a | return NULL; |
---|
974 | n/a | if (test_buildvalue_N_error("{()O&(())N}") < 0) |
---|
975 | n/a | return NULL; |
---|
976 | n/a | |
---|
977 | n/a | Py_RETURN_NONE; |
---|
978 | n/a | } |
---|
979 | n/a | |
---|
980 | n/a | |
---|
981 | n/a | static PyObject * |
---|
982 | n/a | get_args(PyObject *self, PyObject *args) |
---|
983 | n/a | { |
---|
984 | n/a | if (args == NULL) { |
---|
985 | n/a | args = Py_None; |
---|
986 | n/a | } |
---|
987 | n/a | Py_INCREF(args); |
---|
988 | n/a | return args; |
---|
989 | n/a | } |
---|
990 | n/a | |
---|
991 | n/a | static PyObject * |
---|
992 | n/a | get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs) |
---|
993 | n/a | { |
---|
994 | n/a | if (kwargs == NULL) { |
---|
995 | n/a | kwargs = Py_None; |
---|
996 | n/a | } |
---|
997 | n/a | Py_INCREF(kwargs); |
---|
998 | n/a | return kwargs; |
---|
999 | n/a | } |
---|
1000 | n/a | |
---|
1001 | n/a | /* Test tuple argument processing */ |
---|
1002 | n/a | static PyObject * |
---|
1003 | n/a | getargs_tuple(PyObject *self, PyObject *args) |
---|
1004 | n/a | { |
---|
1005 | n/a | int a, b, c; |
---|
1006 | n/a | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
---|
1007 | n/a | return NULL; |
---|
1008 | n/a | return Py_BuildValue("iii", a, b, c); |
---|
1009 | n/a | } |
---|
1010 | n/a | |
---|
1011 | n/a | /* test PyArg_ParseTupleAndKeywords */ |
---|
1012 | n/a | static PyObject * |
---|
1013 | n/a | getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
---|
1014 | n/a | { |
---|
1015 | n/a | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
---|
1016 | n/a | static const char fmt[] = "(ii)i|(i(ii))(iii)i"; |
---|
1017 | n/a | int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
---|
1018 | n/a | |
---|
1019 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
---|
1020 | n/a | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
---|
1021 | n/a | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
---|
1022 | n/a | return NULL; |
---|
1023 | n/a | return Py_BuildValue("iiiiiiiiii", |
---|
1024 | n/a | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
---|
1025 | n/a | int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]); |
---|
1026 | n/a | } |
---|
1027 | n/a | |
---|
1028 | n/a | /* test PyArg_ParseTupleAndKeywords keyword-only arguments */ |
---|
1029 | n/a | static PyObject * |
---|
1030 | n/a | getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs) |
---|
1031 | n/a | { |
---|
1032 | n/a | static char *keywords[] = {"required", "optional", "keyword_only", NULL}; |
---|
1033 | n/a | int required = -1; |
---|
1034 | n/a | int optional = -1; |
---|
1035 | n/a | int keyword_only = -1; |
---|
1036 | n/a | |
---|
1037 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords, |
---|
1038 | n/a | &required, &optional, &keyword_only)) |
---|
1039 | n/a | return NULL; |
---|
1040 | n/a | return Py_BuildValue("iii", required, optional, keyword_only); |
---|
1041 | n/a | } |
---|
1042 | n/a | |
---|
1043 | n/a | /* test PyArg_ParseTupleAndKeywords positional-only arguments */ |
---|
1044 | n/a | static PyObject * |
---|
1045 | n/a | getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
---|
1046 | n/a | { |
---|
1047 | n/a | static char *keywords[] = {"", "", "keyword", NULL}; |
---|
1048 | n/a | int required = -1; |
---|
1049 | n/a | int optional = -1; |
---|
1050 | n/a | int keyword = -1; |
---|
1051 | n/a | |
---|
1052 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords, |
---|
1053 | n/a | &required, &optional, &keyword)) |
---|
1054 | n/a | return NULL; |
---|
1055 | n/a | return Py_BuildValue("iii", required, optional, keyword); |
---|
1056 | n/a | } |
---|
1057 | n/a | |
---|
1058 | n/a | /* Functions to call PyArg_ParseTuple with integer format codes, |
---|
1059 | n/a | and return the result. |
---|
1060 | n/a | */ |
---|
1061 | n/a | static PyObject * |
---|
1062 | n/a | getargs_b(PyObject *self, PyObject *args) |
---|
1063 | n/a | { |
---|
1064 | n/a | unsigned char value; |
---|
1065 | n/a | if (!PyArg_ParseTuple(args, "b", &value)) |
---|
1066 | n/a | return NULL; |
---|
1067 | n/a | return PyLong_FromUnsignedLong((unsigned long)value); |
---|
1068 | n/a | } |
---|
1069 | n/a | |
---|
1070 | n/a | static PyObject * |
---|
1071 | n/a | getargs_B(PyObject *self, PyObject *args) |
---|
1072 | n/a | { |
---|
1073 | n/a | unsigned char value; |
---|
1074 | n/a | if (!PyArg_ParseTuple(args, "B", &value)) |
---|
1075 | n/a | return NULL; |
---|
1076 | n/a | return PyLong_FromUnsignedLong((unsigned long)value); |
---|
1077 | n/a | } |
---|
1078 | n/a | |
---|
1079 | n/a | static PyObject * |
---|
1080 | n/a | getargs_h(PyObject *self, PyObject *args) |
---|
1081 | n/a | { |
---|
1082 | n/a | short value; |
---|
1083 | n/a | if (!PyArg_ParseTuple(args, "h", &value)) |
---|
1084 | n/a | return NULL; |
---|
1085 | n/a | return PyLong_FromLong((long)value); |
---|
1086 | n/a | } |
---|
1087 | n/a | |
---|
1088 | n/a | static PyObject * |
---|
1089 | n/a | getargs_H(PyObject *self, PyObject *args) |
---|
1090 | n/a | { |
---|
1091 | n/a | unsigned short value; |
---|
1092 | n/a | if (!PyArg_ParseTuple(args, "H", &value)) |
---|
1093 | n/a | return NULL; |
---|
1094 | n/a | return PyLong_FromUnsignedLong((unsigned long)value); |
---|
1095 | n/a | } |
---|
1096 | n/a | |
---|
1097 | n/a | static PyObject * |
---|
1098 | n/a | getargs_I(PyObject *self, PyObject *args) |
---|
1099 | n/a | { |
---|
1100 | n/a | unsigned int value; |
---|
1101 | n/a | if (!PyArg_ParseTuple(args, "I", &value)) |
---|
1102 | n/a | return NULL; |
---|
1103 | n/a | return PyLong_FromUnsignedLong((unsigned long)value); |
---|
1104 | n/a | } |
---|
1105 | n/a | |
---|
1106 | n/a | static PyObject * |
---|
1107 | n/a | getargs_k(PyObject *self, PyObject *args) |
---|
1108 | n/a | { |
---|
1109 | n/a | unsigned long value; |
---|
1110 | n/a | if (!PyArg_ParseTuple(args, "k", &value)) |
---|
1111 | n/a | return NULL; |
---|
1112 | n/a | return PyLong_FromUnsignedLong(value); |
---|
1113 | n/a | } |
---|
1114 | n/a | |
---|
1115 | n/a | static PyObject * |
---|
1116 | n/a | getargs_i(PyObject *self, PyObject *args) |
---|
1117 | n/a | { |
---|
1118 | n/a | int value; |
---|
1119 | n/a | if (!PyArg_ParseTuple(args, "i", &value)) |
---|
1120 | n/a | return NULL; |
---|
1121 | n/a | return PyLong_FromLong((long)value); |
---|
1122 | n/a | } |
---|
1123 | n/a | |
---|
1124 | n/a | static PyObject * |
---|
1125 | n/a | getargs_l(PyObject *self, PyObject *args) |
---|
1126 | n/a | { |
---|
1127 | n/a | long value; |
---|
1128 | n/a | if (!PyArg_ParseTuple(args, "l", &value)) |
---|
1129 | n/a | return NULL; |
---|
1130 | n/a | return PyLong_FromLong(value); |
---|
1131 | n/a | } |
---|
1132 | n/a | |
---|
1133 | n/a | static PyObject * |
---|
1134 | n/a | getargs_n(PyObject *self, PyObject *args) |
---|
1135 | n/a | { |
---|
1136 | n/a | Py_ssize_t value; |
---|
1137 | n/a | if (!PyArg_ParseTuple(args, "n", &value)) |
---|
1138 | n/a | return NULL; |
---|
1139 | n/a | return PyLong_FromSsize_t(value); |
---|
1140 | n/a | } |
---|
1141 | n/a | |
---|
1142 | n/a | static PyObject * |
---|
1143 | n/a | getargs_p(PyObject *self, PyObject *args) |
---|
1144 | n/a | { |
---|
1145 | n/a | int value; |
---|
1146 | n/a | if (!PyArg_ParseTuple(args, "p", &value)) |
---|
1147 | n/a | return NULL; |
---|
1148 | n/a | return PyLong_FromLong(value); |
---|
1149 | n/a | } |
---|
1150 | n/a | |
---|
1151 | n/a | static PyObject * |
---|
1152 | n/a | getargs_L(PyObject *self, PyObject *args) |
---|
1153 | n/a | { |
---|
1154 | n/a | long long value; |
---|
1155 | n/a | if (!PyArg_ParseTuple(args, "L", &value)) |
---|
1156 | n/a | return NULL; |
---|
1157 | n/a | return PyLong_FromLongLong(value); |
---|
1158 | n/a | } |
---|
1159 | n/a | |
---|
1160 | n/a | static PyObject * |
---|
1161 | n/a | getargs_K(PyObject *self, PyObject *args) |
---|
1162 | n/a | { |
---|
1163 | n/a | unsigned long long value; |
---|
1164 | n/a | if (!PyArg_ParseTuple(args, "K", &value)) |
---|
1165 | n/a | return NULL; |
---|
1166 | n/a | return PyLong_FromUnsignedLongLong(value); |
---|
1167 | n/a | } |
---|
1168 | n/a | |
---|
1169 | n/a | /* This function not only tests the 'k' getargs code, but also the |
---|
1170 | n/a | PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */ |
---|
1171 | n/a | static PyObject * |
---|
1172 | n/a | test_k_code(PyObject *self) |
---|
1173 | n/a | { |
---|
1174 | n/a | PyObject *tuple, *num; |
---|
1175 | n/a | unsigned long value; |
---|
1176 | n/a | |
---|
1177 | n/a | tuple = PyTuple_New(1); |
---|
1178 | n/a | if (tuple == NULL) |
---|
1179 | n/a | return NULL; |
---|
1180 | n/a | |
---|
1181 | n/a | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
---|
1182 | n/a | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
---|
1183 | n/a | if (num == NULL) |
---|
1184 | n/a | return NULL; |
---|
1185 | n/a | |
---|
1186 | n/a | value = PyLong_AsUnsignedLongMask(num); |
---|
1187 | n/a | if (value != ULONG_MAX) |
---|
1188 | n/a | return raiseTestError("test_k_code", |
---|
1189 | n/a | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
---|
1190 | n/a | |
---|
1191 | n/a | PyTuple_SET_ITEM(tuple, 0, num); |
---|
1192 | n/a | |
---|
1193 | n/a | value = 0; |
---|
1194 | n/a | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
---|
1195 | n/a | return NULL; |
---|
1196 | n/a | if (value != ULONG_MAX) |
---|
1197 | n/a | return raiseTestError("test_k_code", |
---|
1198 | n/a | "k code returned wrong value for long 0xFFF...FFF"); |
---|
1199 | n/a | |
---|
1200 | n/a | Py_DECREF(num); |
---|
1201 | n/a | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
---|
1202 | n/a | if (num == NULL) |
---|
1203 | n/a | return NULL; |
---|
1204 | n/a | |
---|
1205 | n/a | value = PyLong_AsUnsignedLongMask(num); |
---|
1206 | n/a | if (value != (unsigned long)-0x42) |
---|
1207 | n/a | return raiseTestError("test_k_code", |
---|
1208 | n/a | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
---|
1209 | n/a | |
---|
1210 | n/a | PyTuple_SET_ITEM(tuple, 0, num); |
---|
1211 | n/a | |
---|
1212 | n/a | value = 0; |
---|
1213 | n/a | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
---|
1214 | n/a | return NULL; |
---|
1215 | n/a | if (value != (unsigned long)-0x42) |
---|
1216 | n/a | return raiseTestError("test_k_code", |
---|
1217 | n/a | "k code returned wrong value for long -0xFFF..000042"); |
---|
1218 | n/a | |
---|
1219 | n/a | Py_DECREF(tuple); |
---|
1220 | n/a | Py_RETURN_NONE; |
---|
1221 | n/a | } |
---|
1222 | n/a | |
---|
1223 | n/a | static PyObject * |
---|
1224 | n/a | getargs_f(PyObject *self, PyObject *args) |
---|
1225 | n/a | { |
---|
1226 | n/a | float f; |
---|
1227 | n/a | if (!PyArg_ParseTuple(args, "f", &f)) |
---|
1228 | n/a | return NULL; |
---|
1229 | n/a | return PyFloat_FromDouble(f); |
---|
1230 | n/a | } |
---|
1231 | n/a | |
---|
1232 | n/a | static PyObject * |
---|
1233 | n/a | getargs_d(PyObject *self, PyObject *args) |
---|
1234 | n/a | { |
---|
1235 | n/a | double d; |
---|
1236 | n/a | if (!PyArg_ParseTuple(args, "d", &d)) |
---|
1237 | n/a | return NULL; |
---|
1238 | n/a | return PyFloat_FromDouble(d); |
---|
1239 | n/a | } |
---|
1240 | n/a | |
---|
1241 | n/a | static PyObject * |
---|
1242 | n/a | getargs_D(PyObject *self, PyObject *args) |
---|
1243 | n/a | { |
---|
1244 | n/a | Py_complex cval; |
---|
1245 | n/a | if (!PyArg_ParseTuple(args, "D", &cval)) |
---|
1246 | n/a | return NULL; |
---|
1247 | n/a | return PyComplex_FromCComplex(cval); |
---|
1248 | n/a | } |
---|
1249 | n/a | |
---|
1250 | n/a | static PyObject * |
---|
1251 | n/a | getargs_S(PyObject *self, PyObject *args) |
---|
1252 | n/a | { |
---|
1253 | n/a | PyObject *obj; |
---|
1254 | n/a | if (!PyArg_ParseTuple(args, "S", &obj)) |
---|
1255 | n/a | return NULL; |
---|
1256 | n/a | Py_INCREF(obj); |
---|
1257 | n/a | return obj; |
---|
1258 | n/a | } |
---|
1259 | n/a | |
---|
1260 | n/a | static PyObject * |
---|
1261 | n/a | getargs_Y(PyObject *self, PyObject *args) |
---|
1262 | n/a | { |
---|
1263 | n/a | PyObject *obj; |
---|
1264 | n/a | if (!PyArg_ParseTuple(args, "Y", &obj)) |
---|
1265 | n/a | return NULL; |
---|
1266 | n/a | Py_INCREF(obj); |
---|
1267 | n/a | return obj; |
---|
1268 | n/a | } |
---|
1269 | n/a | |
---|
1270 | n/a | static PyObject * |
---|
1271 | n/a | getargs_U(PyObject *self, PyObject *args) |
---|
1272 | n/a | { |
---|
1273 | n/a | PyObject *obj; |
---|
1274 | n/a | if (!PyArg_ParseTuple(args, "U", &obj)) |
---|
1275 | n/a | return NULL; |
---|
1276 | n/a | Py_INCREF(obj); |
---|
1277 | n/a | return obj; |
---|
1278 | n/a | } |
---|
1279 | n/a | |
---|
1280 | n/a | static PyObject * |
---|
1281 | n/a | getargs_c(PyObject *self, PyObject *args) |
---|
1282 | n/a | { |
---|
1283 | n/a | char c; |
---|
1284 | n/a | if (!PyArg_ParseTuple(args, "c", &c)) |
---|
1285 | n/a | return NULL; |
---|
1286 | n/a | return PyLong_FromLong((unsigned char)c); |
---|
1287 | n/a | } |
---|
1288 | n/a | |
---|
1289 | n/a | static PyObject * |
---|
1290 | n/a | getargs_C(PyObject *self, PyObject *args) |
---|
1291 | n/a | { |
---|
1292 | n/a | int c; |
---|
1293 | n/a | if (!PyArg_ParseTuple(args, "C", &c)) |
---|
1294 | n/a | return NULL; |
---|
1295 | n/a | return PyLong_FromLong(c); |
---|
1296 | n/a | } |
---|
1297 | n/a | |
---|
1298 | n/a | static PyObject * |
---|
1299 | n/a | getargs_s(PyObject *self, PyObject *args) |
---|
1300 | n/a | { |
---|
1301 | n/a | char *str; |
---|
1302 | n/a | if (!PyArg_ParseTuple(args, "s", &str)) |
---|
1303 | n/a | return NULL; |
---|
1304 | n/a | return PyBytes_FromString(str); |
---|
1305 | n/a | } |
---|
1306 | n/a | |
---|
1307 | n/a | static PyObject * |
---|
1308 | n/a | getargs_s_star(PyObject *self, PyObject *args) |
---|
1309 | n/a | { |
---|
1310 | n/a | Py_buffer buffer; |
---|
1311 | n/a | PyObject *bytes; |
---|
1312 | n/a | if (!PyArg_ParseTuple(args, "s*", &buffer)) |
---|
1313 | n/a | return NULL; |
---|
1314 | n/a | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
---|
1315 | n/a | PyBuffer_Release(&buffer); |
---|
1316 | n/a | return bytes; |
---|
1317 | n/a | } |
---|
1318 | n/a | |
---|
1319 | n/a | static PyObject * |
---|
1320 | n/a | getargs_s_hash(PyObject *self, PyObject *args) |
---|
1321 | n/a | { |
---|
1322 | n/a | char *str; |
---|
1323 | n/a | Py_ssize_t size; |
---|
1324 | n/a | if (!PyArg_ParseTuple(args, "s#", &str, &size)) |
---|
1325 | n/a | return NULL; |
---|
1326 | n/a | return PyBytes_FromStringAndSize(str, size); |
---|
1327 | n/a | } |
---|
1328 | n/a | |
---|
1329 | n/a | static PyObject * |
---|
1330 | n/a | getargs_z(PyObject *self, PyObject *args) |
---|
1331 | n/a | { |
---|
1332 | n/a | char *str; |
---|
1333 | n/a | if (!PyArg_ParseTuple(args, "z", &str)) |
---|
1334 | n/a | return NULL; |
---|
1335 | n/a | if (str != NULL) |
---|
1336 | n/a | return PyBytes_FromString(str); |
---|
1337 | n/a | else |
---|
1338 | n/a | Py_RETURN_NONE; |
---|
1339 | n/a | } |
---|
1340 | n/a | |
---|
1341 | n/a | static PyObject * |
---|
1342 | n/a | getargs_z_star(PyObject *self, PyObject *args) |
---|
1343 | n/a | { |
---|
1344 | n/a | Py_buffer buffer; |
---|
1345 | n/a | PyObject *bytes; |
---|
1346 | n/a | if (!PyArg_ParseTuple(args, "z*", &buffer)) |
---|
1347 | n/a | return NULL; |
---|
1348 | n/a | if (buffer.buf != NULL) |
---|
1349 | n/a | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
---|
1350 | n/a | else { |
---|
1351 | n/a | Py_INCREF(Py_None); |
---|
1352 | n/a | bytes = Py_None; |
---|
1353 | n/a | } |
---|
1354 | n/a | PyBuffer_Release(&buffer); |
---|
1355 | n/a | return bytes; |
---|
1356 | n/a | } |
---|
1357 | n/a | |
---|
1358 | n/a | static PyObject * |
---|
1359 | n/a | getargs_z_hash(PyObject *self, PyObject *args) |
---|
1360 | n/a | { |
---|
1361 | n/a | char *str; |
---|
1362 | n/a | Py_ssize_t size; |
---|
1363 | n/a | if (!PyArg_ParseTuple(args, "z#", &str, &size)) |
---|
1364 | n/a | return NULL; |
---|
1365 | n/a | if (str != NULL) |
---|
1366 | n/a | return PyBytes_FromStringAndSize(str, size); |
---|
1367 | n/a | else |
---|
1368 | n/a | Py_RETURN_NONE; |
---|
1369 | n/a | } |
---|
1370 | n/a | |
---|
1371 | n/a | static PyObject * |
---|
1372 | n/a | getargs_y(PyObject *self, PyObject *args) |
---|
1373 | n/a | { |
---|
1374 | n/a | char *str; |
---|
1375 | n/a | if (!PyArg_ParseTuple(args, "y", &str)) |
---|
1376 | n/a | return NULL; |
---|
1377 | n/a | return PyBytes_FromString(str); |
---|
1378 | n/a | } |
---|
1379 | n/a | |
---|
1380 | n/a | static PyObject * |
---|
1381 | n/a | getargs_y_star(PyObject *self, PyObject *args) |
---|
1382 | n/a | { |
---|
1383 | n/a | Py_buffer buffer; |
---|
1384 | n/a | PyObject *bytes; |
---|
1385 | n/a | if (!PyArg_ParseTuple(args, "y*", &buffer)) |
---|
1386 | n/a | return NULL; |
---|
1387 | n/a | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
---|
1388 | n/a | PyBuffer_Release(&buffer); |
---|
1389 | n/a | return bytes; |
---|
1390 | n/a | } |
---|
1391 | n/a | |
---|
1392 | n/a | static PyObject * |
---|
1393 | n/a | getargs_y_hash(PyObject *self, PyObject *args) |
---|
1394 | n/a | { |
---|
1395 | n/a | char *str; |
---|
1396 | n/a | Py_ssize_t size; |
---|
1397 | n/a | if (!PyArg_ParseTuple(args, "y#", &str, &size)) |
---|
1398 | n/a | return NULL; |
---|
1399 | n/a | return PyBytes_FromStringAndSize(str, size); |
---|
1400 | n/a | } |
---|
1401 | n/a | |
---|
1402 | n/a | static PyObject * |
---|
1403 | n/a | getargs_u(PyObject *self, PyObject *args) |
---|
1404 | n/a | { |
---|
1405 | n/a | Py_UNICODE *str; |
---|
1406 | n/a | if (!PyArg_ParseTuple(args, "u", &str)) |
---|
1407 | n/a | return NULL; |
---|
1408 | n/a | return PyUnicode_FromWideChar(str, -1); |
---|
1409 | n/a | } |
---|
1410 | n/a | |
---|
1411 | n/a | static PyObject * |
---|
1412 | n/a | getargs_u_hash(PyObject *self, PyObject *args) |
---|
1413 | n/a | { |
---|
1414 | n/a | Py_UNICODE *str; |
---|
1415 | n/a | Py_ssize_t size; |
---|
1416 | n/a | if (!PyArg_ParseTuple(args, "u#", &str, &size)) |
---|
1417 | n/a | return NULL; |
---|
1418 | n/a | return PyUnicode_FromWideChar(str, size); |
---|
1419 | n/a | } |
---|
1420 | n/a | |
---|
1421 | n/a | static PyObject * |
---|
1422 | n/a | getargs_Z(PyObject *self, PyObject *args) |
---|
1423 | n/a | { |
---|
1424 | n/a | Py_UNICODE *str; |
---|
1425 | n/a | if (!PyArg_ParseTuple(args, "Z", &str)) |
---|
1426 | n/a | return NULL; |
---|
1427 | n/a | if (str != NULL) { |
---|
1428 | n/a | return PyUnicode_FromWideChar(str, -1); |
---|
1429 | n/a | } else |
---|
1430 | n/a | Py_RETURN_NONE; |
---|
1431 | n/a | } |
---|
1432 | n/a | |
---|
1433 | n/a | static PyObject * |
---|
1434 | n/a | getargs_Z_hash(PyObject *self, PyObject *args) |
---|
1435 | n/a | { |
---|
1436 | n/a | Py_UNICODE *str; |
---|
1437 | n/a | Py_ssize_t size; |
---|
1438 | n/a | if (!PyArg_ParseTuple(args, "Z#", &str, &size)) |
---|
1439 | n/a | return NULL; |
---|
1440 | n/a | if (str != NULL) |
---|
1441 | n/a | return PyUnicode_FromWideChar(str, size); |
---|
1442 | n/a | else |
---|
1443 | n/a | Py_RETURN_NONE; |
---|
1444 | n/a | } |
---|
1445 | n/a | |
---|
1446 | n/a | static PyObject * |
---|
1447 | n/a | getargs_es(PyObject *self, PyObject *args) |
---|
1448 | n/a | { |
---|
1449 | n/a | PyObject *arg, *result; |
---|
1450 | n/a | const char *encoding = NULL; |
---|
1451 | n/a | char *str; |
---|
1452 | n/a | |
---|
1453 | n/a | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
---|
1454 | n/a | return NULL; |
---|
1455 | n/a | if (!PyArg_Parse(arg, "es", encoding, &str)) |
---|
1456 | n/a | return NULL; |
---|
1457 | n/a | result = PyBytes_FromString(str); |
---|
1458 | n/a | PyMem_Free(str); |
---|
1459 | n/a | return result; |
---|
1460 | n/a | } |
---|
1461 | n/a | |
---|
1462 | n/a | static PyObject * |
---|
1463 | n/a | getargs_et(PyObject *self, PyObject *args) |
---|
1464 | n/a | { |
---|
1465 | n/a | PyObject *arg, *result; |
---|
1466 | n/a | const char *encoding = NULL; |
---|
1467 | n/a | char *str; |
---|
1468 | n/a | |
---|
1469 | n/a | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
---|
1470 | n/a | return NULL; |
---|
1471 | n/a | if (!PyArg_Parse(arg, "et", encoding, &str)) |
---|
1472 | n/a | return NULL; |
---|
1473 | n/a | result = PyBytes_FromString(str); |
---|
1474 | n/a | PyMem_Free(str); |
---|
1475 | n/a | return result; |
---|
1476 | n/a | } |
---|
1477 | n/a | |
---|
1478 | n/a | static PyObject * |
---|
1479 | n/a | getargs_es_hash(PyObject *self, PyObject *args) |
---|
1480 | n/a | { |
---|
1481 | n/a | PyObject *arg, *result; |
---|
1482 | n/a | const char *encoding = NULL; |
---|
1483 | n/a | PyByteArrayObject *buffer = NULL; |
---|
1484 | n/a | char *str = NULL; |
---|
1485 | n/a | Py_ssize_t size; |
---|
1486 | n/a | |
---|
1487 | n/a | if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) |
---|
1488 | n/a | return NULL; |
---|
1489 | n/a | if (buffer != NULL) { |
---|
1490 | n/a | str = PyByteArray_AS_STRING(buffer); |
---|
1491 | n/a | size = PyByteArray_GET_SIZE(buffer); |
---|
1492 | n/a | } |
---|
1493 | n/a | if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) |
---|
1494 | n/a | return NULL; |
---|
1495 | n/a | result = PyBytes_FromStringAndSize(str, size); |
---|
1496 | n/a | if (buffer == NULL) |
---|
1497 | n/a | PyMem_Free(str); |
---|
1498 | n/a | return result; |
---|
1499 | n/a | } |
---|
1500 | n/a | |
---|
1501 | n/a | static PyObject * |
---|
1502 | n/a | getargs_et_hash(PyObject *self, PyObject *args) |
---|
1503 | n/a | { |
---|
1504 | n/a | PyObject *arg, *result; |
---|
1505 | n/a | const char *encoding = NULL; |
---|
1506 | n/a | PyByteArrayObject *buffer = NULL; |
---|
1507 | n/a | char *str = NULL; |
---|
1508 | n/a | Py_ssize_t size; |
---|
1509 | n/a | |
---|
1510 | n/a | if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) |
---|
1511 | n/a | return NULL; |
---|
1512 | n/a | if (buffer != NULL) { |
---|
1513 | n/a | str = PyByteArray_AS_STRING(buffer); |
---|
1514 | n/a | size = PyByteArray_GET_SIZE(buffer); |
---|
1515 | n/a | } |
---|
1516 | n/a | if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) |
---|
1517 | n/a | return NULL; |
---|
1518 | n/a | result = PyBytes_FromStringAndSize(str, size); |
---|
1519 | n/a | if (buffer == NULL) |
---|
1520 | n/a | PyMem_Free(str); |
---|
1521 | n/a | return result; |
---|
1522 | n/a | } |
---|
1523 | n/a | |
---|
1524 | n/a | /* Test the s and z codes for PyArg_ParseTuple. |
---|
1525 | n/a | */ |
---|
1526 | n/a | static PyObject * |
---|
1527 | n/a | test_s_code(PyObject *self) |
---|
1528 | n/a | { |
---|
1529 | n/a | /* Unicode strings should be accepted */ |
---|
1530 | n/a | PyObject *tuple, *obj; |
---|
1531 | n/a | char *value; |
---|
1532 | n/a | |
---|
1533 | n/a | tuple = PyTuple_New(1); |
---|
1534 | n/a | if (tuple == NULL) |
---|
1535 | n/a | return NULL; |
---|
1536 | n/a | |
---|
1537 | n/a | obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), |
---|
1538 | n/a | "latin-1", NULL); |
---|
1539 | n/a | if (obj == NULL) |
---|
1540 | n/a | return NULL; |
---|
1541 | n/a | |
---|
1542 | n/a | PyTuple_SET_ITEM(tuple, 0, obj); |
---|
1543 | n/a | |
---|
1544 | n/a | /* These two blocks used to raise a TypeError: |
---|
1545 | n/a | * "argument must be string without null bytes, not str" |
---|
1546 | n/a | */ |
---|
1547 | n/a | if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0) |
---|
1548 | n/a | return NULL; |
---|
1549 | n/a | |
---|
1550 | n/a | if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0) |
---|
1551 | n/a | return NULL; |
---|
1552 | n/a | |
---|
1553 | n/a | Py_DECREF(tuple); |
---|
1554 | n/a | Py_RETURN_NONE; |
---|
1555 | n/a | } |
---|
1556 | n/a | |
---|
1557 | n/a | static PyObject * |
---|
1558 | n/a | parse_tuple_and_keywords(PyObject *self, PyObject *args) |
---|
1559 | n/a | { |
---|
1560 | n/a | PyObject *sub_args; |
---|
1561 | n/a | PyObject *sub_kwargs; |
---|
1562 | n/a | char *sub_format; |
---|
1563 | n/a | PyObject *sub_keywords; |
---|
1564 | n/a | |
---|
1565 | n/a | Py_ssize_t i, size; |
---|
1566 | n/a | char *keywords[8 + 1]; /* space for NULL at end */ |
---|
1567 | n/a | PyObject *o; |
---|
1568 | n/a | PyObject *converted[8]; |
---|
1569 | n/a | |
---|
1570 | n/a | int result; |
---|
1571 | n/a | PyObject *return_value = NULL; |
---|
1572 | n/a | |
---|
1573 | n/a | double buffers[8][4]; /* double ensures alignment where necessary */ |
---|
1574 | n/a | |
---|
1575 | n/a | if (!PyArg_ParseTuple(args, "OOyO:parse_tuple_and_keywords", |
---|
1576 | n/a | &sub_args, &sub_kwargs, |
---|
1577 | n/a | &sub_format, &sub_keywords)) |
---|
1578 | n/a | return NULL; |
---|
1579 | n/a | |
---|
1580 | n/a | if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) { |
---|
1581 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1582 | n/a | "parse_tuple_and_keywords: sub_keywords must be either list or tuple"); |
---|
1583 | n/a | return NULL; |
---|
1584 | n/a | } |
---|
1585 | n/a | |
---|
1586 | n/a | memset(buffers, 0, sizeof(buffers)); |
---|
1587 | n/a | memset(converted, 0, sizeof(converted)); |
---|
1588 | n/a | memset(keywords, 0, sizeof(keywords)); |
---|
1589 | n/a | |
---|
1590 | n/a | size = PySequence_Fast_GET_SIZE(sub_keywords); |
---|
1591 | n/a | if (size > 8) { |
---|
1592 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1593 | n/a | "parse_tuple_and_keywords: too many keywords in sub_keywords"); |
---|
1594 | n/a | goto exit; |
---|
1595 | n/a | } |
---|
1596 | n/a | |
---|
1597 | n/a | for (i = 0; i < size; i++) { |
---|
1598 | n/a | o = PySequence_Fast_GET_ITEM(sub_keywords, i); |
---|
1599 | n/a | if (!PyUnicode_FSConverter(o, (void *)(converted + i))) { |
---|
1600 | n/a | PyErr_Format(PyExc_ValueError, |
---|
1601 | n/a | "parse_tuple_and_keywords: could not convert keywords[%zd] to narrow string", i); |
---|
1602 | n/a | goto exit; |
---|
1603 | n/a | } |
---|
1604 | n/a | keywords[i] = PyBytes_AS_STRING(converted[i]); |
---|
1605 | n/a | } |
---|
1606 | n/a | |
---|
1607 | n/a | result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs, |
---|
1608 | n/a | sub_format, keywords, |
---|
1609 | n/a | buffers + 0, buffers + 1, buffers + 2, buffers + 3, |
---|
1610 | n/a | buffers + 4, buffers + 5, buffers + 6, buffers + 7); |
---|
1611 | n/a | |
---|
1612 | n/a | if (result) { |
---|
1613 | n/a | return_value = Py_None; |
---|
1614 | n/a | Py_INCREF(Py_None); |
---|
1615 | n/a | } |
---|
1616 | n/a | |
---|
1617 | n/a | exit: |
---|
1618 | n/a | size = sizeof(converted) / sizeof(converted[0]); |
---|
1619 | n/a | for (i = 0; i < size; i++) { |
---|
1620 | n/a | Py_XDECREF(converted[i]); |
---|
1621 | n/a | } |
---|
1622 | n/a | return return_value; |
---|
1623 | n/a | } |
---|
1624 | n/a | |
---|
1625 | n/a | static volatile int x; |
---|
1626 | n/a | |
---|
1627 | n/a | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
---|
1628 | n/a | of an error. |
---|
1629 | n/a | */ |
---|
1630 | n/a | static PyObject * |
---|
1631 | n/a | test_u_code(PyObject *self) |
---|
1632 | n/a | { |
---|
1633 | n/a | PyObject *tuple, *obj; |
---|
1634 | n/a | Py_UNICODE *value; |
---|
1635 | n/a | Py_ssize_t len; |
---|
1636 | n/a | |
---|
1637 | n/a | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
---|
1638 | n/a | /* Just use the macro and check that it compiles */ |
---|
1639 | n/a | x = Py_UNICODE_ISSPACE(25); |
---|
1640 | n/a | |
---|
1641 | n/a | tuple = PyTuple_New(1); |
---|
1642 | n/a | if (tuple == NULL) |
---|
1643 | n/a | return NULL; |
---|
1644 | n/a | |
---|
1645 | n/a | obj = PyUnicode_Decode("test", strlen("test"), |
---|
1646 | n/a | "ascii", NULL); |
---|
1647 | n/a | if (obj == NULL) |
---|
1648 | n/a | return NULL; |
---|
1649 | n/a | |
---|
1650 | n/a | PyTuple_SET_ITEM(tuple, 0, obj); |
---|
1651 | n/a | |
---|
1652 | n/a | value = 0; |
---|
1653 | n/a | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
---|
1654 | n/a | return NULL; |
---|
1655 | n/a | if (value != PyUnicode_AS_UNICODE(obj)) |
---|
1656 | n/a | return raiseTestError("test_u_code", |
---|
1657 | n/a | "u code returned wrong value for u'test'"); |
---|
1658 | n/a | value = 0; |
---|
1659 | n/a | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
---|
1660 | n/a | return NULL; |
---|
1661 | n/a | if (value != PyUnicode_AS_UNICODE(obj) || |
---|
1662 | n/a | len != PyUnicode_GET_SIZE(obj)) |
---|
1663 | n/a | return raiseTestError("test_u_code", |
---|
1664 | n/a | "u# code returned wrong values for u'test'"); |
---|
1665 | n/a | |
---|
1666 | n/a | Py_DECREF(tuple); |
---|
1667 | n/a | Py_RETURN_NONE; |
---|
1668 | n/a | } |
---|
1669 | n/a | |
---|
1670 | n/a | /* Test Z and Z# codes for PyArg_ParseTuple */ |
---|
1671 | n/a | static PyObject * |
---|
1672 | n/a | test_Z_code(PyObject *self) |
---|
1673 | n/a | { |
---|
1674 | n/a | PyObject *tuple, *obj; |
---|
1675 | n/a | const Py_UNICODE *value1, *value2; |
---|
1676 | n/a | Py_ssize_t len1, len2; |
---|
1677 | n/a | |
---|
1678 | n/a | tuple = PyTuple_New(2); |
---|
1679 | n/a | if (tuple == NULL) |
---|
1680 | n/a | return NULL; |
---|
1681 | n/a | |
---|
1682 | n/a | obj = PyUnicode_FromString("test"); |
---|
1683 | n/a | PyTuple_SET_ITEM(tuple, 0, obj); |
---|
1684 | n/a | Py_INCREF(Py_None); |
---|
1685 | n/a | PyTuple_SET_ITEM(tuple, 1, Py_None); |
---|
1686 | n/a | |
---|
1687 | n/a | /* swap values on purpose */ |
---|
1688 | n/a | value1 = NULL; |
---|
1689 | n/a | value2 = PyUnicode_AS_UNICODE(obj); |
---|
1690 | n/a | |
---|
1691 | n/a | /* Test Z for both values */ |
---|
1692 | n/a | if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0) |
---|
1693 | n/a | return NULL; |
---|
1694 | n/a | if (value1 != PyUnicode_AS_UNICODE(obj)) |
---|
1695 | n/a | return raiseTestError("test_Z_code", |
---|
1696 | n/a | "Z code returned wrong value for 'test'"); |
---|
1697 | n/a | if (value2 != NULL) |
---|
1698 | n/a | return raiseTestError("test_Z_code", |
---|
1699 | n/a | "Z code returned wrong value for None"); |
---|
1700 | n/a | |
---|
1701 | n/a | value1 = NULL; |
---|
1702 | n/a | value2 = PyUnicode_AS_UNICODE(obj); |
---|
1703 | n/a | len1 = -1; |
---|
1704 | n/a | len2 = -1; |
---|
1705 | n/a | |
---|
1706 | n/a | /* Test Z# for both values */ |
---|
1707 | n/a | if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
---|
1708 | n/a | &value2, &len2) < 0) |
---|
1709 | n/a | return NULL; |
---|
1710 | n/a | if (value1 != PyUnicode_AS_UNICODE(obj) || |
---|
1711 | n/a | len1 != PyUnicode_GET_SIZE(obj)) |
---|
1712 | n/a | return raiseTestError("test_Z_code", |
---|
1713 | n/a | "Z# code returned wrong values for 'test'"); |
---|
1714 | n/a | if (value2 != NULL || |
---|
1715 | n/a | len2 != 0) |
---|
1716 | n/a | return raiseTestError("test_Z_code", |
---|
1717 | n/a | "Z# code returned wrong values for None'"); |
---|
1718 | n/a | |
---|
1719 | n/a | Py_DECREF(tuple); |
---|
1720 | n/a | Py_RETURN_NONE; |
---|
1721 | n/a | } |
---|
1722 | n/a | |
---|
1723 | n/a | static PyObject * |
---|
1724 | n/a | test_widechar(PyObject *self) |
---|
1725 | n/a | { |
---|
1726 | n/a | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
---|
1727 | n/a | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
---|
1728 | n/a | size_t wtextlen = 1; |
---|
1729 | n/a | const wchar_t invalid[1] = {(wchar_t)0x110000u}; |
---|
1730 | n/a | #else |
---|
1731 | n/a | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
---|
1732 | n/a | size_t wtextlen = 2; |
---|
1733 | n/a | #endif |
---|
1734 | n/a | PyObject *wide, *utf8; |
---|
1735 | n/a | |
---|
1736 | n/a | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
---|
1737 | n/a | if (wide == NULL) |
---|
1738 | n/a | return NULL; |
---|
1739 | n/a | |
---|
1740 | n/a | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
---|
1741 | n/a | if (utf8 == NULL) { |
---|
1742 | n/a | Py_DECREF(wide); |
---|
1743 | n/a | return NULL; |
---|
1744 | n/a | } |
---|
1745 | n/a | |
---|
1746 | n/a | if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) { |
---|
1747 | n/a | Py_DECREF(wide); |
---|
1748 | n/a | Py_DECREF(utf8); |
---|
1749 | n/a | return raiseTestError("test_widechar", |
---|
1750 | n/a | "wide string and utf8 string " |
---|
1751 | n/a | "have different length"); |
---|
1752 | n/a | } |
---|
1753 | n/a | if (PyUnicode_Compare(wide, utf8)) { |
---|
1754 | n/a | Py_DECREF(wide); |
---|
1755 | n/a | Py_DECREF(utf8); |
---|
1756 | n/a | if (PyErr_Occurred()) |
---|
1757 | n/a | return NULL; |
---|
1758 | n/a | return raiseTestError("test_widechar", |
---|
1759 | n/a | "wide string and utf8 string " |
---|
1760 | n/a | "are different"); |
---|
1761 | n/a | } |
---|
1762 | n/a | |
---|
1763 | n/a | Py_DECREF(wide); |
---|
1764 | n/a | Py_DECREF(utf8); |
---|
1765 | n/a | |
---|
1766 | n/a | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
---|
1767 | n/a | wide = PyUnicode_FromWideChar(invalid, 1); |
---|
1768 | n/a | if (wide == NULL) |
---|
1769 | n/a | PyErr_Clear(); |
---|
1770 | n/a | else |
---|
1771 | n/a | return raiseTestError("test_widechar", |
---|
1772 | n/a | "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail"); |
---|
1773 | n/a | |
---|
1774 | n/a | wide = PyUnicode_FromUnicode(invalid, 1); |
---|
1775 | n/a | if (wide == NULL) |
---|
1776 | n/a | PyErr_Clear(); |
---|
1777 | n/a | else |
---|
1778 | n/a | return raiseTestError("test_widechar", |
---|
1779 | n/a | "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail"); |
---|
1780 | n/a | |
---|
1781 | n/a | wide = PyUnicode_FromUnicode(NULL, 1); |
---|
1782 | n/a | if (wide == NULL) |
---|
1783 | n/a | return NULL; |
---|
1784 | n/a | PyUnicode_AS_UNICODE(wide)[0] = invalid[0]; |
---|
1785 | n/a | if (_PyUnicode_Ready(wide) < 0) { |
---|
1786 | n/a | Py_DECREF(wide); |
---|
1787 | n/a | PyErr_Clear(); |
---|
1788 | n/a | } |
---|
1789 | n/a | else { |
---|
1790 | n/a | Py_DECREF(wide); |
---|
1791 | n/a | return raiseTestError("test_widechar", |
---|
1792 | n/a | "PyUnicode_Ready() didn't fail"); |
---|
1793 | n/a | } |
---|
1794 | n/a | #endif |
---|
1795 | n/a | |
---|
1796 | n/a | Py_RETURN_NONE; |
---|
1797 | n/a | } |
---|
1798 | n/a | |
---|
1799 | n/a | static PyObject * |
---|
1800 | n/a | unicode_aswidechar(PyObject *self, PyObject *args) |
---|
1801 | n/a | { |
---|
1802 | n/a | PyObject *unicode, *result; |
---|
1803 | n/a | Py_ssize_t buflen, size; |
---|
1804 | n/a | wchar_t *buffer; |
---|
1805 | n/a | |
---|
1806 | n/a | if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen)) |
---|
1807 | n/a | return NULL; |
---|
1808 | n/a | buffer = PyMem_New(wchar_t, buflen); |
---|
1809 | n/a | if (buffer == NULL) |
---|
1810 | n/a | return PyErr_NoMemory(); |
---|
1811 | n/a | |
---|
1812 | n/a | size = PyUnicode_AsWideChar(unicode, buffer, buflen); |
---|
1813 | n/a | if (size == -1) { |
---|
1814 | n/a | PyMem_Free(buffer); |
---|
1815 | n/a | return NULL; |
---|
1816 | n/a | } |
---|
1817 | n/a | |
---|
1818 | n/a | if (size < buflen) |
---|
1819 | n/a | buflen = size + 1; |
---|
1820 | n/a | else |
---|
1821 | n/a | buflen = size; |
---|
1822 | n/a | result = PyUnicode_FromWideChar(buffer, buflen); |
---|
1823 | n/a | PyMem_Free(buffer); |
---|
1824 | n/a | if (result == NULL) |
---|
1825 | n/a | return NULL; |
---|
1826 | n/a | |
---|
1827 | n/a | return Py_BuildValue("(Nn)", result, size); |
---|
1828 | n/a | } |
---|
1829 | n/a | |
---|
1830 | n/a | static PyObject * |
---|
1831 | n/a | unicode_aswidecharstring(PyObject *self, PyObject *args) |
---|
1832 | n/a | { |
---|
1833 | n/a | PyObject *unicode, *result; |
---|
1834 | n/a | Py_ssize_t size; |
---|
1835 | n/a | wchar_t *buffer; |
---|
1836 | n/a | |
---|
1837 | n/a | if (!PyArg_ParseTuple(args, "U", &unicode)) |
---|
1838 | n/a | return NULL; |
---|
1839 | n/a | |
---|
1840 | n/a | buffer = PyUnicode_AsWideCharString(unicode, &size); |
---|
1841 | n/a | if (buffer == NULL) |
---|
1842 | n/a | return NULL; |
---|
1843 | n/a | |
---|
1844 | n/a | result = PyUnicode_FromWideChar(buffer, size + 1); |
---|
1845 | n/a | PyMem_Free(buffer); |
---|
1846 | n/a | if (result == NULL) |
---|
1847 | n/a | return NULL; |
---|
1848 | n/a | return Py_BuildValue("(Nn)", result, size); |
---|
1849 | n/a | } |
---|
1850 | n/a | |
---|
1851 | n/a | static PyObject * |
---|
1852 | n/a | unicode_asucs4(PyObject *self, PyObject *args) |
---|
1853 | n/a | { |
---|
1854 | n/a | PyObject *unicode, *result; |
---|
1855 | n/a | Py_UCS4 *buffer; |
---|
1856 | n/a | int copy_null; |
---|
1857 | n/a | Py_ssize_t str_len, buf_len; |
---|
1858 | n/a | |
---|
1859 | n/a | if (!PyArg_ParseTuple(args, "Unp:unicode_asucs4", &unicode, &str_len, ©_null)) { |
---|
1860 | n/a | return NULL; |
---|
1861 | n/a | } |
---|
1862 | n/a | |
---|
1863 | n/a | buf_len = str_len + 1; |
---|
1864 | n/a | buffer = PyMem_NEW(Py_UCS4, buf_len); |
---|
1865 | n/a | if (buffer == NULL) { |
---|
1866 | n/a | return PyErr_NoMemory(); |
---|
1867 | n/a | } |
---|
1868 | n/a | memset(buffer, 0, sizeof(Py_UCS4)*buf_len); |
---|
1869 | n/a | buffer[str_len] = 0xffffU; |
---|
1870 | n/a | |
---|
1871 | n/a | if (!PyUnicode_AsUCS4(unicode, buffer, buf_len, copy_null)) { |
---|
1872 | n/a | PyMem_FREE(buffer); |
---|
1873 | n/a | return NULL; |
---|
1874 | n/a | } |
---|
1875 | n/a | |
---|
1876 | n/a | result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, buf_len); |
---|
1877 | n/a | PyMem_FREE(buffer); |
---|
1878 | n/a | return result; |
---|
1879 | n/a | } |
---|
1880 | n/a | |
---|
1881 | n/a | static PyObject * |
---|
1882 | n/a | unicode_findchar(PyObject *self, PyObject *args) |
---|
1883 | n/a | { |
---|
1884 | n/a | PyObject *str; |
---|
1885 | n/a | int direction; |
---|
1886 | n/a | unsigned int ch; |
---|
1887 | n/a | Py_ssize_t result; |
---|
1888 | n/a | Py_ssize_t start, end; |
---|
1889 | n/a | |
---|
1890 | n/a | if (!PyArg_ParseTuple(args, "UInni:unicode_findchar", &str, &ch, |
---|
1891 | n/a | &start, &end, &direction)) { |
---|
1892 | n/a | return NULL; |
---|
1893 | n/a | } |
---|
1894 | n/a | |
---|
1895 | n/a | result = PyUnicode_FindChar(str, (Py_UCS4)ch, start, end, direction); |
---|
1896 | n/a | if (result == -2) |
---|
1897 | n/a | return NULL; |
---|
1898 | n/a | else |
---|
1899 | n/a | return PyLong_FromSsize_t(result); |
---|
1900 | n/a | } |
---|
1901 | n/a | |
---|
1902 | n/a | static PyObject * |
---|
1903 | n/a | unicode_copycharacters(PyObject *self, PyObject *args) |
---|
1904 | n/a | { |
---|
1905 | n/a | PyObject *from, *to, *to_copy; |
---|
1906 | n/a | Py_ssize_t from_start, to_start, how_many, copied; |
---|
1907 | n/a | |
---|
1908 | n/a | if (!PyArg_ParseTuple(args, "UnOnn:unicode_copycharacters", &to, &to_start, |
---|
1909 | n/a | &from, &from_start, &how_many)) { |
---|
1910 | n/a | return NULL; |
---|
1911 | n/a | } |
---|
1912 | n/a | |
---|
1913 | n/a | if (!(to_copy = PyUnicode_New(PyUnicode_GET_LENGTH(to), |
---|
1914 | n/a | PyUnicode_MAX_CHAR_VALUE(to)))) { |
---|
1915 | n/a | return NULL; |
---|
1916 | n/a | } |
---|
1917 | n/a | if (PyUnicode_Fill(to_copy, 0, PyUnicode_GET_LENGTH(to_copy), 0U) < 0) { |
---|
1918 | n/a | Py_DECREF(to_copy); |
---|
1919 | n/a | return NULL; |
---|
1920 | n/a | } |
---|
1921 | n/a | |
---|
1922 | n/a | if ((copied = PyUnicode_CopyCharacters(to_copy, to_start, from, |
---|
1923 | n/a | from_start, how_many)) < 0) { |
---|
1924 | n/a | Py_DECREF(to_copy); |
---|
1925 | n/a | return NULL; |
---|
1926 | n/a | } |
---|
1927 | n/a | |
---|
1928 | n/a | return Py_BuildValue("(Nn)", to_copy, copied); |
---|
1929 | n/a | } |
---|
1930 | n/a | |
---|
1931 | n/a | static PyObject * |
---|
1932 | n/a | unicode_encodedecimal(PyObject *self, PyObject *args) |
---|
1933 | n/a | { |
---|
1934 | n/a | Py_UNICODE *unicode; |
---|
1935 | n/a | Py_ssize_t length; |
---|
1936 | n/a | char *errors = NULL; |
---|
1937 | n/a | PyObject *decimal; |
---|
1938 | n/a | Py_ssize_t decimal_length, new_length; |
---|
1939 | n/a | int res; |
---|
1940 | n/a | |
---|
1941 | n/a | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) |
---|
1942 | n/a | return NULL; |
---|
1943 | n/a | |
---|
1944 | n/a | decimal_length = length * 7; /* len('€') */ |
---|
1945 | n/a | decimal = PyBytes_FromStringAndSize(NULL, decimal_length); |
---|
1946 | n/a | if (decimal == NULL) |
---|
1947 | n/a | return NULL; |
---|
1948 | n/a | |
---|
1949 | n/a | res = PyUnicode_EncodeDecimal(unicode, length, |
---|
1950 | n/a | PyBytes_AS_STRING(decimal), |
---|
1951 | n/a | errors); |
---|
1952 | n/a | if (res < 0) { |
---|
1953 | n/a | Py_DECREF(decimal); |
---|
1954 | n/a | return NULL; |
---|
1955 | n/a | } |
---|
1956 | n/a | |
---|
1957 | n/a | new_length = strlen(PyBytes_AS_STRING(decimal)); |
---|
1958 | n/a | assert(new_length <= decimal_length); |
---|
1959 | n/a | res = _PyBytes_Resize(&decimal, new_length); |
---|
1960 | n/a | if (res < 0) |
---|
1961 | n/a | return NULL; |
---|
1962 | n/a | |
---|
1963 | n/a | return decimal; |
---|
1964 | n/a | } |
---|
1965 | n/a | |
---|
1966 | n/a | static PyObject * |
---|
1967 | n/a | unicode_transformdecimaltoascii(PyObject *self, PyObject *args) |
---|
1968 | n/a | { |
---|
1969 | n/a | Py_UNICODE *unicode; |
---|
1970 | n/a | Py_ssize_t length; |
---|
1971 | n/a | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length)) |
---|
1972 | n/a | return NULL; |
---|
1973 | n/a | return PyUnicode_TransformDecimalToASCII(unicode, length); |
---|
1974 | n/a | } |
---|
1975 | n/a | |
---|
1976 | n/a | static PyObject * |
---|
1977 | n/a | unicode_legacy_string(PyObject *self, PyObject *args) |
---|
1978 | n/a | { |
---|
1979 | n/a | Py_UNICODE *data; |
---|
1980 | n/a | Py_ssize_t len; |
---|
1981 | n/a | PyObject *u; |
---|
1982 | n/a | |
---|
1983 | n/a | if (!PyArg_ParseTuple(args, "u#", &data, &len)) |
---|
1984 | n/a | return NULL; |
---|
1985 | n/a | |
---|
1986 | n/a | u = PyUnicode_FromUnicode(NULL, len); |
---|
1987 | n/a | if (u == NULL) |
---|
1988 | n/a | return NULL; |
---|
1989 | n/a | |
---|
1990 | n/a | memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE)); |
---|
1991 | n/a | |
---|
1992 | n/a | if (len > 0) { /* The empty string is always ready. */ |
---|
1993 | n/a | assert(!PyUnicode_IS_READY(u)); |
---|
1994 | n/a | } |
---|
1995 | n/a | |
---|
1996 | n/a | return u; |
---|
1997 | n/a | } |
---|
1998 | n/a | |
---|
1999 | n/a | static PyObject * |
---|
2000 | n/a | getargs_w_star(PyObject *self, PyObject *args) |
---|
2001 | n/a | { |
---|
2002 | n/a | Py_buffer buffer; |
---|
2003 | n/a | PyObject *result; |
---|
2004 | n/a | char *str; |
---|
2005 | n/a | |
---|
2006 | n/a | if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) |
---|
2007 | n/a | return NULL; |
---|
2008 | n/a | |
---|
2009 | n/a | if (2 <= buffer.len) { |
---|
2010 | n/a | str = buffer.buf; |
---|
2011 | n/a | str[0] = '['; |
---|
2012 | n/a | str[buffer.len-1] = ']'; |
---|
2013 | n/a | } |
---|
2014 | n/a | |
---|
2015 | n/a | result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
---|
2016 | n/a | PyBuffer_Release(&buffer); |
---|
2017 | n/a | return result; |
---|
2018 | n/a | } |
---|
2019 | n/a | |
---|
2020 | n/a | |
---|
2021 | n/a | static PyObject * |
---|
2022 | n/a | test_empty_argparse(PyObject *self) |
---|
2023 | n/a | { |
---|
2024 | n/a | /* Test that formats can begin with '|'. See issue #4720. */ |
---|
2025 | n/a | PyObject *tuple, *dict = NULL; |
---|
2026 | n/a | static char *kwlist[] = {NULL}; |
---|
2027 | n/a | int result; |
---|
2028 | n/a | tuple = PyTuple_New(0); |
---|
2029 | n/a | if (!tuple) |
---|
2030 | n/a | return NULL; |
---|
2031 | n/a | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
---|
2032 | n/a | goto done; |
---|
2033 | n/a | dict = PyDict_New(); |
---|
2034 | n/a | if (!dict) |
---|
2035 | n/a | goto done; |
---|
2036 | n/a | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
---|
2037 | n/a | done: |
---|
2038 | n/a | Py_DECREF(tuple); |
---|
2039 | n/a | Py_XDECREF(dict); |
---|
2040 | n/a | if (result < 0) |
---|
2041 | n/a | return NULL; |
---|
2042 | n/a | else { |
---|
2043 | n/a | Py_RETURN_NONE; |
---|
2044 | n/a | } |
---|
2045 | n/a | } |
---|
2046 | n/a | |
---|
2047 | n/a | static PyObject * |
---|
2048 | n/a | codec_incrementalencoder(PyObject *self, PyObject *args) |
---|
2049 | n/a | { |
---|
2050 | n/a | const char *encoding, *errors = NULL; |
---|
2051 | n/a | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
---|
2052 | n/a | &encoding, &errors)) |
---|
2053 | n/a | return NULL; |
---|
2054 | n/a | return PyCodec_IncrementalEncoder(encoding, errors); |
---|
2055 | n/a | } |
---|
2056 | n/a | |
---|
2057 | n/a | static PyObject * |
---|
2058 | n/a | codec_incrementaldecoder(PyObject *self, PyObject *args) |
---|
2059 | n/a | { |
---|
2060 | n/a | const char *encoding, *errors = NULL; |
---|
2061 | n/a | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
---|
2062 | n/a | &encoding, &errors)) |
---|
2063 | n/a | return NULL; |
---|
2064 | n/a | return PyCodec_IncrementalDecoder(encoding, errors); |
---|
2065 | n/a | } |
---|
2066 | n/a | |
---|
2067 | n/a | |
---|
2068 | n/a | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
---|
2069 | n/a | static PyObject * |
---|
2070 | n/a | test_long_numbits(PyObject *self) |
---|
2071 | n/a | { |
---|
2072 | n/a | struct triple { |
---|
2073 | n/a | long input; |
---|
2074 | n/a | size_t nbits; |
---|
2075 | n/a | int sign; |
---|
2076 | n/a | } testcases[] = {{0, 0, 0}, |
---|
2077 | n/a | {1L, 1, 1}, |
---|
2078 | n/a | {-1L, 1, -1}, |
---|
2079 | n/a | {2L, 2, 1}, |
---|
2080 | n/a | {-2L, 2, -1}, |
---|
2081 | n/a | {3L, 2, 1}, |
---|
2082 | n/a | {-3L, 2, -1}, |
---|
2083 | n/a | {4L, 3, 1}, |
---|
2084 | n/a | {-4L, 3, -1}, |
---|
2085 | n/a | {0x7fffL, 15, 1}, /* one Python int digit */ |
---|
2086 | n/a | {-0x7fffL, 15, -1}, |
---|
2087 | n/a | {0xffffL, 16, 1}, |
---|
2088 | n/a | {-0xffffL, 16, -1}, |
---|
2089 | n/a | {0xfffffffL, 28, 1}, |
---|
2090 | n/a | {-0xfffffffL, 28, -1}}; |
---|
2091 | n/a | size_t i; |
---|
2092 | n/a | |
---|
2093 | n/a | for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) { |
---|
2094 | n/a | size_t nbits; |
---|
2095 | n/a | int sign; |
---|
2096 | n/a | PyObject *plong; |
---|
2097 | n/a | |
---|
2098 | n/a | plong = PyLong_FromLong(testcases[i].input); |
---|
2099 | n/a | if (plong == NULL) |
---|
2100 | n/a | return NULL; |
---|
2101 | n/a | nbits = _PyLong_NumBits(plong); |
---|
2102 | n/a | sign = _PyLong_Sign(plong); |
---|
2103 | n/a | |
---|
2104 | n/a | Py_DECREF(plong); |
---|
2105 | n/a | if (nbits != testcases[i].nbits) |
---|
2106 | n/a | return raiseTestError("test_long_numbits", |
---|
2107 | n/a | "wrong result for _PyLong_NumBits"); |
---|
2108 | n/a | if (sign != testcases[i].sign) |
---|
2109 | n/a | return raiseTestError("test_long_numbits", |
---|
2110 | n/a | "wrong result for _PyLong_Sign"); |
---|
2111 | n/a | } |
---|
2112 | n/a | Py_RETURN_NONE; |
---|
2113 | n/a | } |
---|
2114 | n/a | |
---|
2115 | n/a | /* Example passing NULLs to PyObject_Str(NULL). */ |
---|
2116 | n/a | |
---|
2117 | n/a | static PyObject * |
---|
2118 | n/a | test_null_strings(PyObject *self) |
---|
2119 | n/a | { |
---|
2120 | n/a | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
---|
2121 | n/a | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
---|
2122 | n/a | Py_XDECREF(o1); |
---|
2123 | n/a | Py_XDECREF(o2); |
---|
2124 | n/a | return tuple; |
---|
2125 | n/a | } |
---|
2126 | n/a | |
---|
2127 | n/a | static PyObject * |
---|
2128 | n/a | raise_exception(PyObject *self, PyObject *args) |
---|
2129 | n/a | { |
---|
2130 | n/a | PyObject *exc; |
---|
2131 | n/a | PyObject *exc_args, *v; |
---|
2132 | n/a | int num_args, i; |
---|
2133 | n/a | |
---|
2134 | n/a | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
---|
2135 | n/a | &exc, &num_args)) |
---|
2136 | n/a | return NULL; |
---|
2137 | n/a | |
---|
2138 | n/a | exc_args = PyTuple_New(num_args); |
---|
2139 | n/a | if (exc_args == NULL) |
---|
2140 | n/a | return NULL; |
---|
2141 | n/a | for (i = 0; i < num_args; ++i) { |
---|
2142 | n/a | v = PyLong_FromLong(i); |
---|
2143 | n/a | if (v == NULL) { |
---|
2144 | n/a | Py_DECREF(exc_args); |
---|
2145 | n/a | return NULL; |
---|
2146 | n/a | } |
---|
2147 | n/a | PyTuple_SET_ITEM(exc_args, i, v); |
---|
2148 | n/a | } |
---|
2149 | n/a | PyErr_SetObject(exc, exc_args); |
---|
2150 | n/a | Py_DECREF(exc_args); |
---|
2151 | n/a | return NULL; |
---|
2152 | n/a | } |
---|
2153 | n/a | |
---|
2154 | n/a | static PyObject * |
---|
2155 | n/a | set_errno(PyObject *self, PyObject *args) |
---|
2156 | n/a | { |
---|
2157 | n/a | int new_errno; |
---|
2158 | n/a | |
---|
2159 | n/a | if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno)) |
---|
2160 | n/a | return NULL; |
---|
2161 | n/a | |
---|
2162 | n/a | errno = new_errno; |
---|
2163 | n/a | Py_RETURN_NONE; |
---|
2164 | n/a | } |
---|
2165 | n/a | |
---|
2166 | n/a | static PyObject * |
---|
2167 | n/a | test_set_exc_info(PyObject *self, PyObject *args) |
---|
2168 | n/a | { |
---|
2169 | n/a | PyObject *orig_exc; |
---|
2170 | n/a | PyObject *new_type, *new_value, *new_tb; |
---|
2171 | n/a | PyObject *type, *value, *tb; |
---|
2172 | n/a | if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info", |
---|
2173 | n/a | &new_type, &new_value, &new_tb)) |
---|
2174 | n/a | return NULL; |
---|
2175 | n/a | |
---|
2176 | n/a | PyErr_GetExcInfo(&type, &value, &tb); |
---|
2177 | n/a | |
---|
2178 | n/a | Py_INCREF(new_type); |
---|
2179 | n/a | Py_INCREF(new_value); |
---|
2180 | n/a | Py_INCREF(new_tb); |
---|
2181 | n/a | PyErr_SetExcInfo(new_type, new_value, new_tb); |
---|
2182 | n/a | |
---|
2183 | n/a | orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None); |
---|
2184 | n/a | Py_XDECREF(type); |
---|
2185 | n/a | Py_XDECREF(value); |
---|
2186 | n/a | Py_XDECREF(tb); |
---|
2187 | n/a | return orig_exc; |
---|
2188 | n/a | } |
---|
2189 | n/a | |
---|
2190 | n/a | static int test_run_counter = 0; |
---|
2191 | n/a | |
---|
2192 | n/a | static PyObject * |
---|
2193 | n/a | test_datetime_capi(PyObject *self, PyObject *args) { |
---|
2194 | n/a | if (PyDateTimeAPI) { |
---|
2195 | n/a | if (test_run_counter) { |
---|
2196 | n/a | /* Probably regrtest.py -R */ |
---|
2197 | n/a | Py_RETURN_NONE; |
---|
2198 | n/a | } |
---|
2199 | n/a | else { |
---|
2200 | n/a | PyErr_SetString(PyExc_AssertionError, |
---|
2201 | n/a | "PyDateTime_CAPI somehow initialized"); |
---|
2202 | n/a | return NULL; |
---|
2203 | n/a | } |
---|
2204 | n/a | } |
---|
2205 | n/a | test_run_counter++; |
---|
2206 | n/a | PyDateTime_IMPORT; |
---|
2207 | n/a | if (PyDateTimeAPI) |
---|
2208 | n/a | Py_RETURN_NONE; |
---|
2209 | n/a | else |
---|
2210 | n/a | return NULL; |
---|
2211 | n/a | } |
---|
2212 | n/a | |
---|
2213 | n/a | |
---|
2214 | n/a | #ifdef WITH_THREAD |
---|
2215 | n/a | |
---|
2216 | n/a | /* test_thread_state spawns a thread of its own, and that thread releases |
---|
2217 | n/a | * `thread_done` when it's finished. The driver code has to know when the |
---|
2218 | n/a | * thread finishes, because the thread uses a PyObject (the callable) that |
---|
2219 | n/a | * may go away when the driver finishes. The former lack of this explicit |
---|
2220 | n/a | * synchronization caused rare segfaults, so rare that they were seen only |
---|
2221 | n/a | * on a Mac buildbot (although they were possible on any box). |
---|
2222 | n/a | */ |
---|
2223 | n/a | static PyThread_type_lock thread_done = NULL; |
---|
2224 | n/a | |
---|
2225 | n/a | static int |
---|
2226 | n/a | _make_call(void *callable) |
---|
2227 | n/a | { |
---|
2228 | n/a | PyObject *rc; |
---|
2229 | n/a | int success; |
---|
2230 | n/a | PyGILState_STATE s = PyGILState_Ensure(); |
---|
2231 | n/a | rc = _PyObject_CallNoArg((PyObject *)callable); |
---|
2232 | n/a | success = (rc != NULL); |
---|
2233 | n/a | Py_XDECREF(rc); |
---|
2234 | n/a | PyGILState_Release(s); |
---|
2235 | n/a | return success; |
---|
2236 | n/a | } |
---|
2237 | n/a | |
---|
2238 | n/a | /* Same thing, but releases `thread_done` when it returns. This variant |
---|
2239 | n/a | * should be called only from threads spawned by test_thread_state(). |
---|
2240 | n/a | */ |
---|
2241 | n/a | static void |
---|
2242 | n/a | _make_call_from_thread(void *callable) |
---|
2243 | n/a | { |
---|
2244 | n/a | _make_call(callable); |
---|
2245 | n/a | PyThread_release_lock(thread_done); |
---|
2246 | n/a | } |
---|
2247 | n/a | |
---|
2248 | n/a | static PyObject * |
---|
2249 | n/a | test_thread_state(PyObject *self, PyObject *args) |
---|
2250 | n/a | { |
---|
2251 | n/a | PyObject *fn; |
---|
2252 | n/a | int success = 1; |
---|
2253 | n/a | |
---|
2254 | n/a | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
---|
2255 | n/a | return NULL; |
---|
2256 | n/a | |
---|
2257 | n/a | if (!PyCallable_Check(fn)) { |
---|
2258 | n/a | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
---|
2259 | n/a | fn->ob_type->tp_name); |
---|
2260 | n/a | return NULL; |
---|
2261 | n/a | } |
---|
2262 | n/a | |
---|
2263 | n/a | /* Ensure Python is set up for threading */ |
---|
2264 | n/a | PyEval_InitThreads(); |
---|
2265 | n/a | thread_done = PyThread_allocate_lock(); |
---|
2266 | n/a | if (thread_done == NULL) |
---|
2267 | n/a | return PyErr_NoMemory(); |
---|
2268 | n/a | PyThread_acquire_lock(thread_done, 1); |
---|
2269 | n/a | |
---|
2270 | n/a | /* Start a new thread with our callback. */ |
---|
2271 | n/a | PyThread_start_new_thread(_make_call_from_thread, fn); |
---|
2272 | n/a | /* Make the callback with the thread lock held by this thread */ |
---|
2273 | n/a | success &= _make_call(fn); |
---|
2274 | n/a | /* Do it all again, but this time with the thread-lock released */ |
---|
2275 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
2276 | n/a | success &= _make_call(fn); |
---|
2277 | n/a | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
---|
2278 | n/a | Py_END_ALLOW_THREADS |
---|
2279 | n/a | |
---|
2280 | n/a | /* And once more with and without a thread |
---|
2281 | n/a | XXX - should use a lock and work out exactly what we are trying |
---|
2282 | n/a | to test <wink> |
---|
2283 | n/a | */ |
---|
2284 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
2285 | n/a | PyThread_start_new_thread(_make_call_from_thread, fn); |
---|
2286 | n/a | success &= _make_call(fn); |
---|
2287 | n/a | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
---|
2288 | n/a | Py_END_ALLOW_THREADS |
---|
2289 | n/a | |
---|
2290 | n/a | /* Release lock we acquired above. This is required on HP-UX. */ |
---|
2291 | n/a | PyThread_release_lock(thread_done); |
---|
2292 | n/a | |
---|
2293 | n/a | PyThread_free_lock(thread_done); |
---|
2294 | n/a | if (!success) |
---|
2295 | n/a | return NULL; |
---|
2296 | n/a | Py_RETURN_NONE; |
---|
2297 | n/a | } |
---|
2298 | n/a | |
---|
2299 | n/a | /* test Py_AddPendingCalls using threads */ |
---|
2300 | n/a | static int _pending_callback(void *arg) |
---|
2301 | n/a | { |
---|
2302 | n/a | /* we assume the argument is callable object to which we own a reference */ |
---|
2303 | n/a | PyObject *callable = (PyObject *)arg; |
---|
2304 | n/a | PyObject *r = _PyObject_CallNoArg(callable); |
---|
2305 | n/a | Py_DECREF(callable); |
---|
2306 | n/a | Py_XDECREF(r); |
---|
2307 | n/a | return r != NULL ? 0 : -1; |
---|
2308 | n/a | } |
---|
2309 | n/a | |
---|
2310 | n/a | /* The following requests n callbacks to _pending_callback. It can be |
---|
2311 | n/a | * run from any python thread. |
---|
2312 | n/a | */ |
---|
2313 | n/a | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
---|
2314 | n/a | { |
---|
2315 | n/a | PyObject *callable; |
---|
2316 | n/a | int r; |
---|
2317 | n/a | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
---|
2318 | n/a | return NULL; |
---|
2319 | n/a | |
---|
2320 | n/a | /* create the reference for the callbackwhile we hold the lock */ |
---|
2321 | n/a | Py_INCREF(callable); |
---|
2322 | n/a | |
---|
2323 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
2324 | n/a | r = Py_AddPendingCall(&_pending_callback, callable); |
---|
2325 | n/a | Py_END_ALLOW_THREADS |
---|
2326 | n/a | |
---|
2327 | n/a | if (r<0) { |
---|
2328 | n/a | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
---|
2329 | n/a | Py_RETURN_FALSE; |
---|
2330 | n/a | } |
---|
2331 | n/a | Py_RETURN_TRUE; |
---|
2332 | n/a | } |
---|
2333 | n/a | #endif |
---|
2334 | n/a | |
---|
2335 | n/a | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
---|
2336 | n/a | static PyObject * |
---|
2337 | n/a | test_string_from_format(PyObject *self, PyObject *args) |
---|
2338 | n/a | { |
---|
2339 | n/a | PyObject *result; |
---|
2340 | n/a | char *msg; |
---|
2341 | n/a | |
---|
2342 | n/a | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
---|
2343 | n/a | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
---|
2344 | n/a | if (result == NULL) \ |
---|
2345 | n/a | return NULL; \ |
---|
2346 | n/a | if (!_PyUnicode_EqualToASCIIString(result, "1")) { \ |
---|
2347 | n/a | msg = FORMAT " failed at 1"; \ |
---|
2348 | n/a | goto Fail; \ |
---|
2349 | n/a | } \ |
---|
2350 | n/a | Py_DECREF(result) |
---|
2351 | n/a | |
---|
2352 | n/a | CHECK_1_FORMAT("%d", int); |
---|
2353 | n/a | CHECK_1_FORMAT("%ld", long); |
---|
2354 | n/a | /* The z width modifier was added in Python 2.5. */ |
---|
2355 | n/a | CHECK_1_FORMAT("%zd", Py_ssize_t); |
---|
2356 | n/a | |
---|
2357 | n/a | /* The u type code was added in Python 2.5. */ |
---|
2358 | n/a | CHECK_1_FORMAT("%u", unsigned int); |
---|
2359 | n/a | CHECK_1_FORMAT("%lu", unsigned long); |
---|
2360 | n/a | CHECK_1_FORMAT("%zu", size_t); |
---|
2361 | n/a | |
---|
2362 | n/a | /* "%lld" and "%llu" support added in Python 2.7. */ |
---|
2363 | n/a | CHECK_1_FORMAT("%llu", unsigned long long); |
---|
2364 | n/a | CHECK_1_FORMAT("%lld", long long); |
---|
2365 | n/a | |
---|
2366 | n/a | Py_RETURN_NONE; |
---|
2367 | n/a | |
---|
2368 | n/a | Fail: |
---|
2369 | n/a | Py_XDECREF(result); |
---|
2370 | n/a | return raiseTestError("test_string_from_format", msg); |
---|
2371 | n/a | |
---|
2372 | n/a | #undef CHECK_1_FORMAT |
---|
2373 | n/a | } |
---|
2374 | n/a | |
---|
2375 | n/a | |
---|
2376 | n/a | static PyObject * |
---|
2377 | n/a | test_unicode_compare_with_ascii(PyObject *self) { |
---|
2378 | n/a | PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); |
---|
2379 | n/a | int result; |
---|
2380 | n/a | if (py_s == NULL) |
---|
2381 | n/a | return NULL; |
---|
2382 | n/a | result = PyUnicode_CompareWithASCIIString(py_s, "str"); |
---|
2383 | n/a | Py_DECREF(py_s); |
---|
2384 | n/a | if (!result) { |
---|
2385 | n/a | PyErr_SetString(TestError, "Python string ending in NULL " |
---|
2386 | n/a | "should not compare equal to c string."); |
---|
2387 | n/a | return NULL; |
---|
2388 | n/a | } |
---|
2389 | n/a | Py_RETURN_NONE; |
---|
2390 | n/a | } |
---|
2391 | n/a | |
---|
2392 | n/a | /* This is here to provide a docstring for test_descr. */ |
---|
2393 | n/a | static PyObject * |
---|
2394 | n/a | test_with_docstring(PyObject *self) |
---|
2395 | n/a | { |
---|
2396 | n/a | Py_RETURN_NONE; |
---|
2397 | n/a | } |
---|
2398 | n/a | |
---|
2399 | n/a | /* Test PyOS_string_to_double. */ |
---|
2400 | n/a | static PyObject * |
---|
2401 | n/a | test_string_to_double(PyObject *self) { |
---|
2402 | n/a | double result; |
---|
2403 | n/a | char *msg; |
---|
2404 | n/a | |
---|
2405 | n/a | #define CHECK_STRING(STR, expected) \ |
---|
2406 | n/a | result = PyOS_string_to_double(STR, NULL, NULL); \ |
---|
2407 | n/a | if (result == -1.0 && PyErr_Occurred()) \ |
---|
2408 | n/a | return NULL; \ |
---|
2409 | n/a | if (result != (double)expected) { \ |
---|
2410 | n/a | msg = "conversion of " STR " to float failed"; \ |
---|
2411 | n/a | goto fail; \ |
---|
2412 | n/a | } |
---|
2413 | n/a | |
---|
2414 | n/a | #define CHECK_INVALID(STR) \ |
---|
2415 | n/a | result = PyOS_string_to_double(STR, NULL, NULL); \ |
---|
2416 | n/a | if (result == -1.0 && PyErr_Occurred()) { \ |
---|
2417 | n/a | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
---|
2418 | n/a | PyErr_Clear(); \ |
---|
2419 | n/a | else \ |
---|
2420 | n/a | return NULL; \ |
---|
2421 | n/a | } \ |
---|
2422 | n/a | else { \ |
---|
2423 | n/a | msg = "conversion of " STR " didn't raise ValueError"; \ |
---|
2424 | n/a | goto fail; \ |
---|
2425 | n/a | } |
---|
2426 | n/a | |
---|
2427 | n/a | CHECK_STRING("0.1", 0.1); |
---|
2428 | n/a | CHECK_STRING("1.234", 1.234); |
---|
2429 | n/a | CHECK_STRING("-1.35", -1.35); |
---|
2430 | n/a | CHECK_STRING(".1e01", 1.0); |
---|
2431 | n/a | CHECK_STRING("2.e-2", 0.02); |
---|
2432 | n/a | |
---|
2433 | n/a | CHECK_INVALID(" 0.1"); |
---|
2434 | n/a | CHECK_INVALID("\t\n-3"); |
---|
2435 | n/a | CHECK_INVALID(".123 "); |
---|
2436 | n/a | CHECK_INVALID("3\n"); |
---|
2437 | n/a | CHECK_INVALID("123abc"); |
---|
2438 | n/a | |
---|
2439 | n/a | Py_RETURN_NONE; |
---|
2440 | n/a | fail: |
---|
2441 | n/a | return raiseTestError("test_string_to_double", msg); |
---|
2442 | n/a | #undef CHECK_STRING |
---|
2443 | n/a | #undef CHECK_INVALID |
---|
2444 | n/a | } |
---|
2445 | n/a | |
---|
2446 | n/a | |
---|
2447 | n/a | /* Coverage testing of capsule objects. */ |
---|
2448 | n/a | |
---|
2449 | n/a | static const char *capsule_name = "capsule name"; |
---|
2450 | n/a | static char *capsule_pointer = "capsule pointer"; |
---|
2451 | n/a | static char *capsule_context = "capsule context"; |
---|
2452 | n/a | static const char *capsule_error = NULL; |
---|
2453 | n/a | static int |
---|
2454 | n/a | capsule_destructor_call_count = 0; |
---|
2455 | n/a | |
---|
2456 | n/a | static void |
---|
2457 | n/a | capsule_destructor(PyObject *o) { |
---|
2458 | n/a | capsule_destructor_call_count++; |
---|
2459 | n/a | if (PyCapsule_GetContext(o) != capsule_context) { |
---|
2460 | n/a | capsule_error = "context did not match in destructor!"; |
---|
2461 | n/a | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
---|
2462 | n/a | capsule_error = "destructor did not match in destructor! (woah!)"; |
---|
2463 | n/a | } else if (PyCapsule_GetName(o) != capsule_name) { |
---|
2464 | n/a | capsule_error = "name did not match in destructor!"; |
---|
2465 | n/a | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
---|
2466 | n/a | capsule_error = "pointer did not match in destructor!"; |
---|
2467 | n/a | } |
---|
2468 | n/a | } |
---|
2469 | n/a | |
---|
2470 | n/a | typedef struct { |
---|
2471 | n/a | char *name; |
---|
2472 | n/a | char *module; |
---|
2473 | n/a | char *attribute; |
---|
2474 | n/a | } known_capsule; |
---|
2475 | n/a | |
---|
2476 | n/a | static PyObject * |
---|
2477 | n/a | test_capsule(PyObject *self, PyObject *args) |
---|
2478 | n/a | { |
---|
2479 | n/a | PyObject *object; |
---|
2480 | n/a | const char *error = NULL; |
---|
2481 | n/a | void *pointer; |
---|
2482 | n/a | void *pointer2; |
---|
2483 | n/a | known_capsule known_capsules[] = { |
---|
2484 | n/a | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
---|
2485 | n/a | KNOWN_CAPSULE("_socket", "CAPI"), |
---|
2486 | n/a | KNOWN_CAPSULE("_curses", "_C_API"), |
---|
2487 | n/a | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
---|
2488 | n/a | { NULL, NULL }, |
---|
2489 | n/a | }; |
---|
2490 | n/a | known_capsule *known = &known_capsules[0]; |
---|
2491 | n/a | |
---|
2492 | n/a | #define FAIL(x) { error = (x); goto exit; } |
---|
2493 | n/a | |
---|
2494 | n/a | #define CHECK_DESTRUCTOR \ |
---|
2495 | n/a | if (capsule_error) { \ |
---|
2496 | n/a | FAIL(capsule_error); \ |
---|
2497 | n/a | } \ |
---|
2498 | n/a | else if (!capsule_destructor_call_count) { \ |
---|
2499 | n/a | FAIL("destructor not called!"); \ |
---|
2500 | n/a | } \ |
---|
2501 | n/a | capsule_destructor_call_count = 0; \ |
---|
2502 | n/a | |
---|
2503 | n/a | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
---|
2504 | n/a | PyCapsule_SetContext(object, capsule_context); |
---|
2505 | n/a | capsule_destructor(object); |
---|
2506 | n/a | CHECK_DESTRUCTOR; |
---|
2507 | n/a | Py_DECREF(object); |
---|
2508 | n/a | CHECK_DESTRUCTOR; |
---|
2509 | n/a | |
---|
2510 | n/a | object = PyCapsule_New(known, "ignored", NULL); |
---|
2511 | n/a | PyCapsule_SetPointer(object, capsule_pointer); |
---|
2512 | n/a | PyCapsule_SetName(object, capsule_name); |
---|
2513 | n/a | PyCapsule_SetDestructor(object, capsule_destructor); |
---|
2514 | n/a | PyCapsule_SetContext(object, capsule_context); |
---|
2515 | n/a | capsule_destructor(object); |
---|
2516 | n/a | CHECK_DESTRUCTOR; |
---|
2517 | n/a | /* intentionally access using the wrong name */ |
---|
2518 | n/a | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
---|
2519 | n/a | if (!PyErr_Occurred()) { |
---|
2520 | n/a | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
---|
2521 | n/a | } |
---|
2522 | n/a | PyErr_Clear(); |
---|
2523 | n/a | if (pointer2) { |
---|
2524 | n/a | if (pointer2 == capsule_pointer) { |
---|
2525 | n/a | FAIL("PyCapsule_GetPointer should not have" |
---|
2526 | n/a | " returned the internal pointer!"); |
---|
2527 | n/a | } else { |
---|
2528 | n/a | FAIL("PyCapsule_GetPointer should have " |
---|
2529 | n/a | "returned NULL pointer but did not!"); |
---|
2530 | n/a | } |
---|
2531 | n/a | } |
---|
2532 | n/a | PyCapsule_SetDestructor(object, NULL); |
---|
2533 | n/a | Py_DECREF(object); |
---|
2534 | n/a | if (capsule_destructor_call_count) { |
---|
2535 | n/a | FAIL("destructor called when it should not have been!"); |
---|
2536 | n/a | } |
---|
2537 | n/a | |
---|
2538 | n/a | for (known = &known_capsules[0]; known->module != NULL; known++) { |
---|
2539 | n/a | /* yeah, ordinarily I wouldn't do this either, |
---|
2540 | n/a | but it's fine for this test harness. |
---|
2541 | n/a | */ |
---|
2542 | n/a | static char buffer[256]; |
---|
2543 | n/a | #undef FAIL |
---|
2544 | n/a | #define FAIL(x) \ |
---|
2545 | n/a | { \ |
---|
2546 | n/a | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
---|
2547 | n/a | x, known->module, known->attribute); \ |
---|
2548 | n/a | error = buffer; \ |
---|
2549 | n/a | goto exit; \ |
---|
2550 | n/a | } \ |
---|
2551 | n/a | |
---|
2552 | n/a | PyObject *module = PyImport_ImportModule(known->module); |
---|
2553 | n/a | if (module) { |
---|
2554 | n/a | pointer = PyCapsule_Import(known->name, 0); |
---|
2555 | n/a | if (!pointer) { |
---|
2556 | n/a | Py_DECREF(module); |
---|
2557 | n/a | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
---|
2558 | n/a | } |
---|
2559 | n/a | object = PyObject_GetAttrString(module, known->attribute); |
---|
2560 | n/a | if (!object) { |
---|
2561 | n/a | Py_DECREF(module); |
---|
2562 | n/a | return NULL; |
---|
2563 | n/a | } |
---|
2564 | n/a | pointer2 = PyCapsule_GetPointer(object, |
---|
2565 | n/a | "weebles wobble but they don't fall down"); |
---|
2566 | n/a | if (!PyErr_Occurred()) { |
---|
2567 | n/a | Py_DECREF(object); |
---|
2568 | n/a | Py_DECREF(module); |
---|
2569 | n/a | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
---|
2570 | n/a | } |
---|
2571 | n/a | PyErr_Clear(); |
---|
2572 | n/a | if (pointer2) { |
---|
2573 | n/a | Py_DECREF(module); |
---|
2574 | n/a | Py_DECREF(object); |
---|
2575 | n/a | if (pointer2 == pointer) { |
---|
2576 | n/a | FAIL("PyCapsule_GetPointer should not have" |
---|
2577 | n/a | " returned its internal pointer!"); |
---|
2578 | n/a | } else { |
---|
2579 | n/a | FAIL("PyCapsule_GetPointer should have" |
---|
2580 | n/a | " returned NULL pointer but did not!"); |
---|
2581 | n/a | } |
---|
2582 | n/a | } |
---|
2583 | n/a | Py_DECREF(object); |
---|
2584 | n/a | Py_DECREF(module); |
---|
2585 | n/a | } |
---|
2586 | n/a | else |
---|
2587 | n/a | PyErr_Clear(); |
---|
2588 | n/a | } |
---|
2589 | n/a | |
---|
2590 | n/a | exit: |
---|
2591 | n/a | if (error) { |
---|
2592 | n/a | return raiseTestError("test_capsule", error); |
---|
2593 | n/a | } |
---|
2594 | n/a | Py_RETURN_NONE; |
---|
2595 | n/a | #undef FAIL |
---|
2596 | n/a | } |
---|
2597 | n/a | |
---|
2598 | n/a | #ifdef HAVE_GETTIMEOFDAY |
---|
2599 | n/a | /* Profiling of integer performance */ |
---|
2600 | n/a | static void print_delta(int test, struct timeval *s, struct timeval *e) |
---|
2601 | n/a | { |
---|
2602 | n/a | e->tv_sec -= s->tv_sec; |
---|
2603 | n/a | e->tv_usec -= s->tv_usec; |
---|
2604 | n/a | if (e->tv_usec < 0) { |
---|
2605 | n/a | e->tv_sec -=1; |
---|
2606 | n/a | e->tv_usec += 1000000; |
---|
2607 | n/a | } |
---|
2608 | n/a | printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec); |
---|
2609 | n/a | } |
---|
2610 | n/a | |
---|
2611 | n/a | static PyObject * |
---|
2612 | n/a | profile_int(PyObject *self, PyObject* args) |
---|
2613 | n/a | { |
---|
2614 | n/a | int i, k; |
---|
2615 | n/a | struct timeval start, stop; |
---|
2616 | n/a | PyObject *single, **multiple, *op1, *result; |
---|
2617 | n/a | |
---|
2618 | n/a | /* Test 1: Allocate and immediately deallocate |
---|
2619 | n/a | many small integers */ |
---|
2620 | n/a | gettimeofday(&start, NULL); |
---|
2621 | n/a | for(k=0; k < 20000; k++) |
---|
2622 | n/a | for(i=0; i < 1000; i++) { |
---|
2623 | n/a | single = PyLong_FromLong(i); |
---|
2624 | n/a | Py_DECREF(single); |
---|
2625 | n/a | } |
---|
2626 | n/a | gettimeofday(&stop, NULL); |
---|
2627 | n/a | print_delta(1, &start, &stop); |
---|
2628 | n/a | |
---|
2629 | n/a | /* Test 2: Allocate and immediately deallocate |
---|
2630 | n/a | many large integers */ |
---|
2631 | n/a | gettimeofday(&start, NULL); |
---|
2632 | n/a | for(k=0; k < 20000; k++) |
---|
2633 | n/a | for(i=0; i < 1000; i++) { |
---|
2634 | n/a | single = PyLong_FromLong(i+1000000); |
---|
2635 | n/a | Py_DECREF(single); |
---|
2636 | n/a | } |
---|
2637 | n/a | gettimeofday(&stop, NULL); |
---|
2638 | n/a | print_delta(2, &start, &stop); |
---|
2639 | n/a | |
---|
2640 | n/a | /* Test 3: Allocate a few integers, then release |
---|
2641 | n/a | them all simultaneously. */ |
---|
2642 | n/a | multiple = malloc(sizeof(PyObject*) * 1000); |
---|
2643 | n/a | if (multiple == NULL) |
---|
2644 | n/a | return PyErr_NoMemory(); |
---|
2645 | n/a | gettimeofday(&start, NULL); |
---|
2646 | n/a | for(k=0; k < 20000; k++) { |
---|
2647 | n/a | for(i=0; i < 1000; i++) { |
---|
2648 | n/a | multiple[i] = PyLong_FromLong(i+1000000); |
---|
2649 | n/a | } |
---|
2650 | n/a | for(i=0; i < 1000; i++) { |
---|
2651 | n/a | Py_DECREF(multiple[i]); |
---|
2652 | n/a | } |
---|
2653 | n/a | } |
---|
2654 | n/a | gettimeofday(&stop, NULL); |
---|
2655 | n/a | print_delta(3, &start, &stop); |
---|
2656 | n/a | free(multiple); |
---|
2657 | n/a | |
---|
2658 | n/a | /* Test 4: Allocate many integers, then release |
---|
2659 | n/a | them all simultaneously. */ |
---|
2660 | n/a | multiple = malloc(sizeof(PyObject*) * 1000000); |
---|
2661 | n/a | if (multiple == NULL) |
---|
2662 | n/a | return PyErr_NoMemory(); |
---|
2663 | n/a | gettimeofday(&start, NULL); |
---|
2664 | n/a | for(k=0; k < 20; k++) { |
---|
2665 | n/a | for(i=0; i < 1000000; i++) { |
---|
2666 | n/a | multiple[i] = PyLong_FromLong(i+1000000); |
---|
2667 | n/a | } |
---|
2668 | n/a | for(i=0; i < 1000000; i++) { |
---|
2669 | n/a | Py_DECREF(multiple[i]); |
---|
2670 | n/a | } |
---|
2671 | n/a | } |
---|
2672 | n/a | gettimeofday(&stop, NULL); |
---|
2673 | n/a | print_delta(4, &start, &stop); |
---|
2674 | n/a | free(multiple); |
---|
2675 | n/a | |
---|
2676 | n/a | /* Test 5: Allocate many integers < 32000 */ |
---|
2677 | n/a | multiple = malloc(sizeof(PyObject*) * 1000000); |
---|
2678 | n/a | if (multiple == NULL) |
---|
2679 | n/a | return PyErr_NoMemory(); |
---|
2680 | n/a | gettimeofday(&start, NULL); |
---|
2681 | n/a | for(k=0; k < 10; k++) { |
---|
2682 | n/a | for(i=0; i < 1000000; i++) { |
---|
2683 | n/a | multiple[i] = PyLong_FromLong(i+1000); |
---|
2684 | n/a | } |
---|
2685 | n/a | for(i=0; i < 1000000; i++) { |
---|
2686 | n/a | Py_DECREF(multiple[i]); |
---|
2687 | n/a | } |
---|
2688 | n/a | } |
---|
2689 | n/a | gettimeofday(&stop, NULL); |
---|
2690 | n/a | print_delta(5, &start, &stop); |
---|
2691 | n/a | free(multiple); |
---|
2692 | n/a | |
---|
2693 | n/a | /* Test 6: Perform small int addition */ |
---|
2694 | n/a | op1 = PyLong_FromLong(1); |
---|
2695 | n/a | gettimeofday(&start, NULL); |
---|
2696 | n/a | for(i=0; i < 10000000; i++) { |
---|
2697 | n/a | result = PyNumber_Add(op1, op1); |
---|
2698 | n/a | Py_DECREF(result); |
---|
2699 | n/a | } |
---|
2700 | n/a | gettimeofday(&stop, NULL); |
---|
2701 | n/a | Py_DECREF(op1); |
---|
2702 | n/a | print_delta(6, &start, &stop); |
---|
2703 | n/a | |
---|
2704 | n/a | /* Test 7: Perform medium int addition */ |
---|
2705 | n/a | op1 = PyLong_FromLong(1000); |
---|
2706 | n/a | if (op1 == NULL) |
---|
2707 | n/a | return NULL; |
---|
2708 | n/a | gettimeofday(&start, NULL); |
---|
2709 | n/a | for(i=0; i < 10000000; i++) { |
---|
2710 | n/a | result = PyNumber_Add(op1, op1); |
---|
2711 | n/a | Py_XDECREF(result); |
---|
2712 | n/a | } |
---|
2713 | n/a | gettimeofday(&stop, NULL); |
---|
2714 | n/a | Py_DECREF(op1); |
---|
2715 | n/a | print_delta(7, &start, &stop); |
---|
2716 | n/a | |
---|
2717 | n/a | Py_RETURN_NONE; |
---|
2718 | n/a | } |
---|
2719 | n/a | #endif |
---|
2720 | n/a | |
---|
2721 | n/a | /* To test the format of tracebacks as printed out. */ |
---|
2722 | n/a | static PyObject * |
---|
2723 | n/a | traceback_print(PyObject *self, PyObject *args) |
---|
2724 | n/a | { |
---|
2725 | n/a | PyObject *file; |
---|
2726 | n/a | PyObject *traceback; |
---|
2727 | n/a | int result; |
---|
2728 | n/a | |
---|
2729 | n/a | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
---|
2730 | n/a | &traceback, &file)) |
---|
2731 | n/a | return NULL; |
---|
2732 | n/a | |
---|
2733 | n/a | result = PyTraceBack_Print(traceback, file); |
---|
2734 | n/a | if (result < 0) |
---|
2735 | n/a | return NULL; |
---|
2736 | n/a | Py_RETURN_NONE; |
---|
2737 | n/a | } |
---|
2738 | n/a | |
---|
2739 | n/a | /* To test the format of exceptions as printed out. */ |
---|
2740 | n/a | static PyObject * |
---|
2741 | n/a | exception_print(PyObject *self, PyObject *args) |
---|
2742 | n/a | { |
---|
2743 | n/a | PyObject *value; |
---|
2744 | n/a | PyObject *tb; |
---|
2745 | n/a | |
---|
2746 | n/a | if (!PyArg_ParseTuple(args, "O:exception_print", |
---|
2747 | n/a | &value)) |
---|
2748 | n/a | return NULL; |
---|
2749 | n/a | if (!PyExceptionInstance_Check(value)) { |
---|
2750 | n/a | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
---|
2751 | n/a | return NULL; |
---|
2752 | n/a | } |
---|
2753 | n/a | |
---|
2754 | n/a | tb = PyException_GetTraceback(value); |
---|
2755 | n/a | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
---|
2756 | n/a | Py_XDECREF(tb); |
---|
2757 | n/a | |
---|
2758 | n/a | Py_RETURN_NONE; |
---|
2759 | n/a | } |
---|
2760 | n/a | |
---|
2761 | n/a | |
---|
2762 | n/a | |
---|
2763 | n/a | |
---|
2764 | n/a | /* reliably raise a MemoryError */ |
---|
2765 | n/a | static PyObject * |
---|
2766 | n/a | raise_memoryerror(PyObject *self) |
---|
2767 | n/a | { |
---|
2768 | n/a | PyErr_NoMemory(); |
---|
2769 | n/a | return NULL; |
---|
2770 | n/a | } |
---|
2771 | n/a | |
---|
2772 | n/a | /* Issue 6012 */ |
---|
2773 | n/a | static PyObject *str1, *str2; |
---|
2774 | n/a | static int |
---|
2775 | n/a | failing_converter(PyObject *obj, void *arg) |
---|
2776 | n/a | { |
---|
2777 | n/a | /* Clone str1, then let the conversion fail. */ |
---|
2778 | n/a | assert(str1); |
---|
2779 | n/a | str2 = str1; |
---|
2780 | n/a | Py_INCREF(str2); |
---|
2781 | n/a | return 0; |
---|
2782 | n/a | } |
---|
2783 | n/a | static PyObject* |
---|
2784 | n/a | argparsing(PyObject *o, PyObject *args) |
---|
2785 | n/a | { |
---|
2786 | n/a | PyObject *res; |
---|
2787 | n/a | str1 = str2 = NULL; |
---|
2788 | n/a | if (!PyArg_ParseTuple(args, "O&O&", |
---|
2789 | n/a | PyUnicode_FSConverter, &str1, |
---|
2790 | n/a | failing_converter, &str2)) { |
---|
2791 | n/a | if (!str2) |
---|
2792 | n/a | /* argument converter not called? */ |
---|
2793 | n/a | return NULL; |
---|
2794 | n/a | /* Should be 1 */ |
---|
2795 | n/a | res = PyLong_FromSsize_t(Py_REFCNT(str2)); |
---|
2796 | n/a | Py_DECREF(str2); |
---|
2797 | n/a | PyErr_Clear(); |
---|
2798 | n/a | return res; |
---|
2799 | n/a | } |
---|
2800 | n/a | Py_RETURN_NONE; |
---|
2801 | n/a | } |
---|
2802 | n/a | |
---|
2803 | n/a | /* To test that the result of PyCode_NewEmpty has the right members. */ |
---|
2804 | n/a | static PyObject * |
---|
2805 | n/a | code_newempty(PyObject *self, PyObject *args) |
---|
2806 | n/a | { |
---|
2807 | n/a | const char *filename; |
---|
2808 | n/a | const char *funcname; |
---|
2809 | n/a | int firstlineno; |
---|
2810 | n/a | |
---|
2811 | n/a | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
---|
2812 | n/a | &filename, &funcname, &firstlineno)) |
---|
2813 | n/a | return NULL; |
---|
2814 | n/a | |
---|
2815 | n/a | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
---|
2816 | n/a | } |
---|
2817 | n/a | |
---|
2818 | n/a | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
---|
2819 | n/a | Run via Lib/test/test_exceptions.py */ |
---|
2820 | n/a | static PyObject * |
---|
2821 | n/a | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
---|
2822 | n/a | { |
---|
2823 | n/a | const char *name; |
---|
2824 | n/a | const char *doc = NULL; |
---|
2825 | n/a | PyObject *base = NULL; |
---|
2826 | n/a | PyObject *dict = NULL; |
---|
2827 | n/a | |
---|
2828 | n/a | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
---|
2829 | n/a | |
---|
2830 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
---|
2831 | n/a | "s|sOO:make_exception_with_doc", kwlist, |
---|
2832 | n/a | &name, &doc, &base, &dict)) |
---|
2833 | n/a | return NULL; |
---|
2834 | n/a | |
---|
2835 | n/a | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
---|
2836 | n/a | } |
---|
2837 | n/a | |
---|
2838 | n/a | static PyObject * |
---|
2839 | n/a | make_memoryview_from_NULL_pointer(PyObject *self) |
---|
2840 | n/a | { |
---|
2841 | n/a | Py_buffer info; |
---|
2842 | n/a | if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0) |
---|
2843 | n/a | return NULL; |
---|
2844 | n/a | return PyMemoryView_FromBuffer(&info); |
---|
2845 | n/a | } |
---|
2846 | n/a | |
---|
2847 | n/a | static PyObject * |
---|
2848 | n/a | test_from_contiguous(PyObject* self, PyObject *noargs) |
---|
2849 | n/a | { |
---|
2850 | n/a | int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; |
---|
2851 | n/a | int init[5] = {0, 1, 2, 3, 4}; |
---|
2852 | n/a | Py_ssize_t itemsize = sizeof(int); |
---|
2853 | n/a | Py_ssize_t shape = 5; |
---|
2854 | n/a | Py_ssize_t strides = 2 * itemsize; |
---|
2855 | n/a | Py_buffer view = { |
---|
2856 | n/a | data, |
---|
2857 | n/a | NULL, |
---|
2858 | n/a | 5 * itemsize, |
---|
2859 | n/a | itemsize, |
---|
2860 | n/a | 1, |
---|
2861 | n/a | 1, |
---|
2862 | n/a | NULL, |
---|
2863 | n/a | &shape, |
---|
2864 | n/a | &strides, |
---|
2865 | n/a | NULL, |
---|
2866 | n/a | NULL |
---|
2867 | n/a | }; |
---|
2868 | n/a | int *ptr; |
---|
2869 | n/a | int i; |
---|
2870 | n/a | |
---|
2871 | n/a | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
---|
2872 | n/a | ptr = view.buf; |
---|
2873 | n/a | for (i = 0; i < 5; i++) { |
---|
2874 | n/a | if (ptr[2*i] != i) { |
---|
2875 | n/a | PyErr_SetString(TestError, |
---|
2876 | n/a | "test_from_contiguous: incorrect result"); |
---|
2877 | n/a | return NULL; |
---|
2878 | n/a | } |
---|
2879 | n/a | } |
---|
2880 | n/a | |
---|
2881 | n/a | view.buf = &data[8]; |
---|
2882 | n/a | view.strides[0] = -2 * itemsize; |
---|
2883 | n/a | |
---|
2884 | n/a | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
---|
2885 | n/a | ptr = view.buf; |
---|
2886 | n/a | for (i = 0; i < 5; i++) { |
---|
2887 | n/a | if (*(ptr-2*i) != i) { |
---|
2888 | n/a | PyErr_SetString(TestError, |
---|
2889 | n/a | "test_from_contiguous: incorrect result"); |
---|
2890 | n/a | return NULL; |
---|
2891 | n/a | } |
---|
2892 | n/a | } |
---|
2893 | n/a | |
---|
2894 | n/a | Py_RETURN_NONE; |
---|
2895 | n/a | } |
---|
2896 | n/a | |
---|
2897 | n/a | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
---|
2898 | n/a | extern PyTypeObject _PyBytesIOBuffer_Type; |
---|
2899 | n/a | |
---|
2900 | n/a | static PyObject * |
---|
2901 | n/a | test_pep3118_obsolete_write_locks(PyObject* self, PyObject *noargs) |
---|
2902 | n/a | { |
---|
2903 | n/a | PyTypeObject *type = &_PyBytesIOBuffer_Type; |
---|
2904 | n/a | PyObject *b; |
---|
2905 | n/a | char *dummy[1]; |
---|
2906 | n/a | int ret, match; |
---|
2907 | n/a | |
---|
2908 | n/a | /* PyBuffer_FillInfo() */ |
---|
2909 | n/a | ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE); |
---|
2910 | n/a | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
---|
2911 | n/a | PyErr_Clear(); |
---|
2912 | n/a | if (ret != -1 || match == 0) |
---|
2913 | n/a | goto error; |
---|
2914 | n/a | |
---|
2915 | n/a | /* bytesiobuf_getbuffer() */ |
---|
2916 | n/a | b = type->tp_alloc(type, 0); |
---|
2917 | n/a | if (b == NULL) { |
---|
2918 | n/a | return NULL; |
---|
2919 | n/a | } |
---|
2920 | n/a | |
---|
2921 | n/a | ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE); |
---|
2922 | n/a | Py_DECREF(b); |
---|
2923 | n/a | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
---|
2924 | n/a | PyErr_Clear(); |
---|
2925 | n/a | if (ret != -1 || match == 0) |
---|
2926 | n/a | goto error; |
---|
2927 | n/a | |
---|
2928 | n/a | Py_RETURN_NONE; |
---|
2929 | n/a | |
---|
2930 | n/a | error: |
---|
2931 | n/a | PyErr_SetString(TestError, |
---|
2932 | n/a | "test_pep3118_obsolete_write_locks: failure"); |
---|
2933 | n/a | return NULL; |
---|
2934 | n/a | } |
---|
2935 | n/a | #endif |
---|
2936 | n/a | |
---|
2937 | n/a | /* This tests functions that historically supported write locks. It is |
---|
2938 | n/a | wrong to call getbuffer() with view==NULL and a compliant getbufferproc |
---|
2939 | n/a | is entitled to segfault in that case. */ |
---|
2940 | n/a | static PyObject * |
---|
2941 | n/a | getbuffer_with_null_view(PyObject* self, PyObject *obj) |
---|
2942 | n/a | { |
---|
2943 | n/a | if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0) |
---|
2944 | n/a | return NULL; |
---|
2945 | n/a | |
---|
2946 | n/a | Py_RETURN_NONE; |
---|
2947 | n/a | } |
---|
2948 | n/a | |
---|
2949 | n/a | /* Test that the fatal error from not having a current thread doesn't |
---|
2950 | n/a | cause an infinite loop. Run via Lib/test/test_capi.py */ |
---|
2951 | n/a | static PyObject * |
---|
2952 | n/a | crash_no_current_thread(PyObject *self) |
---|
2953 | n/a | { |
---|
2954 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
2955 | n/a | /* Using PyThreadState_Get() directly allows the test to pass in |
---|
2956 | n/a | !pydebug mode. However, the test only actually tests anything |
---|
2957 | n/a | in pydebug mode, since that's where the infinite loop was in |
---|
2958 | n/a | the first place. */ |
---|
2959 | n/a | PyThreadState_Get(); |
---|
2960 | n/a | Py_END_ALLOW_THREADS |
---|
2961 | n/a | return NULL; |
---|
2962 | n/a | } |
---|
2963 | n/a | |
---|
2964 | n/a | /* To run some code in a sub-interpreter. */ |
---|
2965 | n/a | static PyObject * |
---|
2966 | n/a | run_in_subinterp(PyObject *self, PyObject *args) |
---|
2967 | n/a | { |
---|
2968 | n/a | const char *code; |
---|
2969 | n/a | int r; |
---|
2970 | n/a | PyThreadState *substate, *mainstate; |
---|
2971 | n/a | |
---|
2972 | n/a | if (!PyArg_ParseTuple(args, "s:run_in_subinterp", |
---|
2973 | n/a | &code)) |
---|
2974 | n/a | return NULL; |
---|
2975 | n/a | |
---|
2976 | n/a | mainstate = PyThreadState_Get(); |
---|
2977 | n/a | |
---|
2978 | n/a | PyThreadState_Swap(NULL); |
---|
2979 | n/a | |
---|
2980 | n/a | substate = Py_NewInterpreter(); |
---|
2981 | n/a | if (substate == NULL) { |
---|
2982 | n/a | /* Since no new thread state was created, there is no exception to |
---|
2983 | n/a | propagate; raise a fresh one after swapping in the old thread |
---|
2984 | n/a | state. */ |
---|
2985 | n/a | PyThreadState_Swap(mainstate); |
---|
2986 | n/a | PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed"); |
---|
2987 | n/a | return NULL; |
---|
2988 | n/a | } |
---|
2989 | n/a | r = PyRun_SimpleString(code); |
---|
2990 | n/a | Py_EndInterpreter(substate); |
---|
2991 | n/a | |
---|
2992 | n/a | PyThreadState_Swap(mainstate); |
---|
2993 | n/a | |
---|
2994 | n/a | return PyLong_FromLong(r); |
---|
2995 | n/a | } |
---|
2996 | n/a | |
---|
2997 | n/a | static int |
---|
2998 | n/a | check_time_rounding(int round) |
---|
2999 | n/a | { |
---|
3000 | n/a | if (round != _PyTime_ROUND_FLOOR |
---|
3001 | n/a | && round != _PyTime_ROUND_CEILING |
---|
3002 | n/a | && round != _PyTime_ROUND_HALF_EVEN) { |
---|
3003 | n/a | PyErr_SetString(PyExc_ValueError, "invalid rounding"); |
---|
3004 | n/a | return -1; |
---|
3005 | n/a | } |
---|
3006 | n/a | return 0; |
---|
3007 | n/a | } |
---|
3008 | n/a | |
---|
3009 | n/a | static PyObject * |
---|
3010 | n/a | test_pytime_object_to_time_t(PyObject *self, PyObject *args) |
---|
3011 | n/a | { |
---|
3012 | n/a | PyObject *obj; |
---|
3013 | n/a | time_t sec; |
---|
3014 | n/a | int round; |
---|
3015 | n/a | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round)) |
---|
3016 | n/a | return NULL; |
---|
3017 | n/a | if (check_time_rounding(round) < 0) |
---|
3018 | n/a | return NULL; |
---|
3019 | n/a | if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) |
---|
3020 | n/a | return NULL; |
---|
3021 | n/a | return _PyLong_FromTime_t(sec); |
---|
3022 | n/a | } |
---|
3023 | n/a | |
---|
3024 | n/a | static PyObject * |
---|
3025 | n/a | test_pytime_object_to_timeval(PyObject *self, PyObject *args) |
---|
3026 | n/a | { |
---|
3027 | n/a | PyObject *obj; |
---|
3028 | n/a | time_t sec; |
---|
3029 | n/a | long usec; |
---|
3030 | n/a | int round; |
---|
3031 | n/a | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round)) |
---|
3032 | n/a | return NULL; |
---|
3033 | n/a | if (check_time_rounding(round) < 0) |
---|
3034 | n/a | return NULL; |
---|
3035 | n/a | if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) |
---|
3036 | n/a | return NULL; |
---|
3037 | n/a | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec); |
---|
3038 | n/a | } |
---|
3039 | n/a | |
---|
3040 | n/a | static PyObject * |
---|
3041 | n/a | test_pytime_object_to_timespec(PyObject *self, PyObject *args) |
---|
3042 | n/a | { |
---|
3043 | n/a | PyObject *obj; |
---|
3044 | n/a | time_t sec; |
---|
3045 | n/a | long nsec; |
---|
3046 | n/a | int round; |
---|
3047 | n/a | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round)) |
---|
3048 | n/a | return NULL; |
---|
3049 | n/a | if (check_time_rounding(round) < 0) |
---|
3050 | n/a | return NULL; |
---|
3051 | n/a | if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) |
---|
3052 | n/a | return NULL; |
---|
3053 | n/a | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec); |
---|
3054 | n/a | } |
---|
3055 | n/a | |
---|
3056 | n/a | static void |
---|
3057 | n/a | slot_tp_del(PyObject *self) |
---|
3058 | n/a | { |
---|
3059 | n/a | _Py_IDENTIFIER(__tp_del__); |
---|
3060 | n/a | PyObject *del, *res; |
---|
3061 | n/a | PyObject *error_type, *error_value, *error_traceback; |
---|
3062 | n/a | |
---|
3063 | n/a | /* Temporarily resurrect the object. */ |
---|
3064 | n/a | assert(self->ob_refcnt == 0); |
---|
3065 | n/a | self->ob_refcnt = 1; |
---|
3066 | n/a | |
---|
3067 | n/a | /* Save the current exception, if any. */ |
---|
3068 | n/a | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
---|
3069 | n/a | |
---|
3070 | n/a | /* Execute __del__ method, if any. */ |
---|
3071 | n/a | del = _PyObject_LookupSpecial(self, &PyId___tp_del__); |
---|
3072 | n/a | if (del != NULL) { |
---|
3073 | n/a | res = PyEval_CallObject(del, NULL); |
---|
3074 | n/a | if (res == NULL) |
---|
3075 | n/a | PyErr_WriteUnraisable(del); |
---|
3076 | n/a | else |
---|
3077 | n/a | Py_DECREF(res); |
---|
3078 | n/a | Py_DECREF(del); |
---|
3079 | n/a | } |
---|
3080 | n/a | |
---|
3081 | n/a | /* Restore the saved exception. */ |
---|
3082 | n/a | PyErr_Restore(error_type, error_value, error_traceback); |
---|
3083 | n/a | |
---|
3084 | n/a | /* Undo the temporary resurrection; can't use DECREF here, it would |
---|
3085 | n/a | * cause a recursive call. |
---|
3086 | n/a | */ |
---|
3087 | n/a | assert(self->ob_refcnt > 0); |
---|
3088 | n/a | if (--self->ob_refcnt == 0) |
---|
3089 | n/a | return; /* this is the normal path out */ |
---|
3090 | n/a | |
---|
3091 | n/a | /* __del__ resurrected it! Make it look like the original Py_DECREF |
---|
3092 | n/a | * never happened. |
---|
3093 | n/a | */ |
---|
3094 | n/a | { |
---|
3095 | n/a | Py_ssize_t refcnt = self->ob_refcnt; |
---|
3096 | n/a | _Py_NewReference(self); |
---|
3097 | n/a | self->ob_refcnt = refcnt; |
---|
3098 | n/a | } |
---|
3099 | n/a | assert(!PyType_IS_GC(Py_TYPE(self)) || |
---|
3100 | n/a | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
---|
3101 | n/a | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
---|
3102 | n/a | * we need to undo that. */ |
---|
3103 | n/a | _Py_DEC_REFTOTAL; |
---|
3104 | n/a | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
---|
3105 | n/a | * chain, so no more to do there. |
---|
3106 | n/a | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
---|
3107 | n/a | * _Py_NewReference bumped tp_allocs: both of those need to be |
---|
3108 | n/a | * undone. |
---|
3109 | n/a | */ |
---|
3110 | n/a | #ifdef COUNT_ALLOCS |
---|
3111 | n/a | --Py_TYPE(self)->tp_frees; |
---|
3112 | n/a | --Py_TYPE(self)->tp_allocs; |
---|
3113 | n/a | #endif |
---|
3114 | n/a | } |
---|
3115 | n/a | |
---|
3116 | n/a | static PyObject * |
---|
3117 | n/a | with_tp_del(PyObject *self, PyObject *args) |
---|
3118 | n/a | { |
---|
3119 | n/a | PyObject *obj; |
---|
3120 | n/a | PyTypeObject *tp; |
---|
3121 | n/a | |
---|
3122 | n/a | if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj)) |
---|
3123 | n/a | return NULL; |
---|
3124 | n/a | tp = (PyTypeObject *) obj; |
---|
3125 | n/a | if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) { |
---|
3126 | n/a | PyErr_Format(PyExc_TypeError, |
---|
3127 | n/a | "heap type expected, got %R", obj); |
---|
3128 | n/a | return NULL; |
---|
3129 | n/a | } |
---|
3130 | n/a | tp->tp_del = slot_tp_del; |
---|
3131 | n/a | Py_INCREF(obj); |
---|
3132 | n/a | return obj; |
---|
3133 | n/a | } |
---|
3134 | n/a | |
---|
3135 | n/a | static PyMethodDef ml; |
---|
3136 | n/a | |
---|
3137 | n/a | static PyObject * |
---|
3138 | n/a | create_cfunction(PyObject *self, PyObject *args) |
---|
3139 | n/a | { |
---|
3140 | n/a | return PyCFunction_NewEx(&ml, self, NULL); |
---|
3141 | n/a | } |
---|
3142 | n/a | |
---|
3143 | n/a | static PyMethodDef ml = { |
---|
3144 | n/a | "create_cfunction", |
---|
3145 | n/a | create_cfunction, |
---|
3146 | n/a | METH_NOARGS, |
---|
3147 | n/a | NULL |
---|
3148 | n/a | }; |
---|
3149 | n/a | |
---|
3150 | n/a | static PyObject * |
---|
3151 | n/a | _test_incref(PyObject *ob) |
---|
3152 | n/a | { |
---|
3153 | n/a | Py_INCREF(ob); |
---|
3154 | n/a | return ob; |
---|
3155 | n/a | } |
---|
3156 | n/a | |
---|
3157 | n/a | static PyObject * |
---|
3158 | n/a | test_xincref_doesnt_leak(PyObject *ob) |
---|
3159 | n/a | { |
---|
3160 | n/a | PyObject *obj = PyLong_FromLong(0); |
---|
3161 | n/a | Py_XINCREF(_test_incref(obj)); |
---|
3162 | n/a | Py_DECREF(obj); |
---|
3163 | n/a | Py_DECREF(obj); |
---|
3164 | n/a | Py_DECREF(obj); |
---|
3165 | n/a | Py_RETURN_NONE; |
---|
3166 | n/a | } |
---|
3167 | n/a | |
---|
3168 | n/a | static PyObject * |
---|
3169 | n/a | test_incref_doesnt_leak(PyObject *ob) |
---|
3170 | n/a | { |
---|
3171 | n/a | PyObject *obj = PyLong_FromLong(0); |
---|
3172 | n/a | Py_INCREF(_test_incref(obj)); |
---|
3173 | n/a | Py_DECREF(obj); |
---|
3174 | n/a | Py_DECREF(obj); |
---|
3175 | n/a | Py_DECREF(obj); |
---|
3176 | n/a | Py_RETURN_NONE; |
---|
3177 | n/a | } |
---|
3178 | n/a | |
---|
3179 | n/a | static PyObject * |
---|
3180 | n/a | test_xdecref_doesnt_leak(PyObject *ob) |
---|
3181 | n/a | { |
---|
3182 | n/a | Py_XDECREF(PyLong_FromLong(0)); |
---|
3183 | n/a | Py_RETURN_NONE; |
---|
3184 | n/a | } |
---|
3185 | n/a | |
---|
3186 | n/a | static PyObject * |
---|
3187 | n/a | test_decref_doesnt_leak(PyObject *ob) |
---|
3188 | n/a | { |
---|
3189 | n/a | Py_DECREF(PyLong_FromLong(0)); |
---|
3190 | n/a | Py_RETURN_NONE; |
---|
3191 | n/a | } |
---|
3192 | n/a | |
---|
3193 | n/a | static PyObject * |
---|
3194 | n/a | test_incref_decref_API(PyObject *ob) |
---|
3195 | n/a | { |
---|
3196 | n/a | PyObject *obj = PyLong_FromLong(0); |
---|
3197 | n/a | Py_IncRef(obj); |
---|
3198 | n/a | Py_DecRef(obj); |
---|
3199 | n/a | Py_DecRef(obj); |
---|
3200 | n/a | Py_RETURN_NONE; |
---|
3201 | n/a | } |
---|
3202 | n/a | |
---|
3203 | n/a | static PyObject * |
---|
3204 | n/a | test_pymem_alloc0(PyObject *self) |
---|
3205 | n/a | { |
---|
3206 | n/a | void *ptr; |
---|
3207 | n/a | |
---|
3208 | n/a | ptr = PyMem_RawMalloc(0); |
---|
3209 | n/a | if (ptr == NULL) { |
---|
3210 | n/a | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL"); |
---|
3211 | n/a | return NULL; |
---|
3212 | n/a | } |
---|
3213 | n/a | PyMem_RawFree(ptr); |
---|
3214 | n/a | |
---|
3215 | n/a | ptr = PyMem_RawCalloc(0, 0); |
---|
3216 | n/a | if (ptr == NULL) { |
---|
3217 | n/a | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL"); |
---|
3218 | n/a | return NULL; |
---|
3219 | n/a | } |
---|
3220 | n/a | PyMem_RawFree(ptr); |
---|
3221 | n/a | |
---|
3222 | n/a | ptr = PyMem_Malloc(0); |
---|
3223 | n/a | if (ptr == NULL) { |
---|
3224 | n/a | PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL"); |
---|
3225 | n/a | return NULL; |
---|
3226 | n/a | } |
---|
3227 | n/a | PyMem_Free(ptr); |
---|
3228 | n/a | |
---|
3229 | n/a | ptr = PyMem_Calloc(0, 0); |
---|
3230 | n/a | if (ptr == NULL) { |
---|
3231 | n/a | PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL"); |
---|
3232 | n/a | return NULL; |
---|
3233 | n/a | } |
---|
3234 | n/a | PyMem_Free(ptr); |
---|
3235 | n/a | |
---|
3236 | n/a | ptr = PyObject_Malloc(0); |
---|
3237 | n/a | if (ptr == NULL) { |
---|
3238 | n/a | PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL"); |
---|
3239 | n/a | return NULL; |
---|
3240 | n/a | } |
---|
3241 | n/a | PyObject_Free(ptr); |
---|
3242 | n/a | |
---|
3243 | n/a | ptr = PyObject_Calloc(0, 0); |
---|
3244 | n/a | if (ptr == NULL) { |
---|
3245 | n/a | PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL"); |
---|
3246 | n/a | return NULL; |
---|
3247 | n/a | } |
---|
3248 | n/a | PyObject_Free(ptr); |
---|
3249 | n/a | |
---|
3250 | n/a | Py_RETURN_NONE; |
---|
3251 | n/a | } |
---|
3252 | n/a | |
---|
3253 | n/a | typedef struct { |
---|
3254 | n/a | PyMemAllocatorEx alloc; |
---|
3255 | n/a | |
---|
3256 | n/a | size_t malloc_size; |
---|
3257 | n/a | size_t calloc_nelem; |
---|
3258 | n/a | size_t calloc_elsize; |
---|
3259 | n/a | void *realloc_ptr; |
---|
3260 | n/a | size_t realloc_new_size; |
---|
3261 | n/a | void *free_ptr; |
---|
3262 | n/a | } alloc_hook_t; |
---|
3263 | n/a | |
---|
3264 | n/a | static void* hook_malloc (void* ctx, size_t size) |
---|
3265 | n/a | { |
---|
3266 | n/a | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
---|
3267 | n/a | hook->malloc_size = size; |
---|
3268 | n/a | return hook->alloc.malloc(hook->alloc.ctx, size); |
---|
3269 | n/a | } |
---|
3270 | n/a | |
---|
3271 | n/a | static void* hook_calloc (void* ctx, size_t nelem, size_t elsize) |
---|
3272 | n/a | { |
---|
3273 | n/a | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
---|
3274 | n/a | hook->calloc_nelem = nelem; |
---|
3275 | n/a | hook->calloc_elsize = elsize; |
---|
3276 | n/a | return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize); |
---|
3277 | n/a | } |
---|
3278 | n/a | |
---|
3279 | n/a | static void* hook_realloc (void* ctx, void* ptr, size_t new_size) |
---|
3280 | n/a | { |
---|
3281 | n/a | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
---|
3282 | n/a | hook->realloc_ptr = ptr; |
---|
3283 | n/a | hook->realloc_new_size = new_size; |
---|
3284 | n/a | return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size); |
---|
3285 | n/a | } |
---|
3286 | n/a | |
---|
3287 | n/a | static void hook_free (void *ctx, void *ptr) |
---|
3288 | n/a | { |
---|
3289 | n/a | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
---|
3290 | n/a | hook->free_ptr = ptr; |
---|
3291 | n/a | hook->alloc.free(hook->alloc.ctx, ptr); |
---|
3292 | n/a | } |
---|
3293 | n/a | |
---|
3294 | n/a | static PyObject * |
---|
3295 | n/a | test_setallocators(PyMemAllocatorDomain domain) |
---|
3296 | n/a | { |
---|
3297 | n/a | PyObject *res = NULL; |
---|
3298 | n/a | const char *error_msg; |
---|
3299 | n/a | alloc_hook_t hook; |
---|
3300 | n/a | PyMemAllocatorEx alloc; |
---|
3301 | n/a | size_t size, size2, nelem, elsize; |
---|
3302 | n/a | void *ptr, *ptr2; |
---|
3303 | n/a | |
---|
3304 | n/a | memset(&hook, 0, sizeof(hook)); |
---|
3305 | n/a | |
---|
3306 | n/a | alloc.ctx = &hook; |
---|
3307 | n/a | alloc.malloc = &hook_malloc; |
---|
3308 | n/a | alloc.calloc = &hook_calloc; |
---|
3309 | n/a | alloc.realloc = &hook_realloc; |
---|
3310 | n/a | alloc.free = &hook_free; |
---|
3311 | n/a | PyMem_GetAllocator(domain, &hook.alloc); |
---|
3312 | n/a | PyMem_SetAllocator(domain, &alloc); |
---|
3313 | n/a | |
---|
3314 | n/a | size = 42; |
---|
3315 | n/a | switch(domain) |
---|
3316 | n/a | { |
---|
3317 | n/a | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break; |
---|
3318 | n/a | case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break; |
---|
3319 | n/a | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break; |
---|
3320 | n/a | default: ptr = NULL; break; |
---|
3321 | n/a | } |
---|
3322 | n/a | |
---|
3323 | n/a | if (ptr == NULL) { |
---|
3324 | n/a | error_msg = "malloc failed"; |
---|
3325 | n/a | goto fail; |
---|
3326 | n/a | } |
---|
3327 | n/a | |
---|
3328 | n/a | if (hook.malloc_size != size) { |
---|
3329 | n/a | error_msg = "malloc invalid size"; |
---|
3330 | n/a | goto fail; |
---|
3331 | n/a | } |
---|
3332 | n/a | |
---|
3333 | n/a | size2 = 200; |
---|
3334 | n/a | switch(domain) |
---|
3335 | n/a | { |
---|
3336 | n/a | case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break; |
---|
3337 | n/a | case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break; |
---|
3338 | n/a | case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break; |
---|
3339 | n/a | default: ptr2 = NULL; break; |
---|
3340 | n/a | } |
---|
3341 | n/a | |
---|
3342 | n/a | if (ptr2 == NULL) { |
---|
3343 | n/a | error_msg = "realloc failed"; |
---|
3344 | n/a | goto fail; |
---|
3345 | n/a | } |
---|
3346 | n/a | |
---|
3347 | n/a | if (hook.realloc_ptr != ptr |
---|
3348 | n/a | || hook.realloc_new_size != size2) { |
---|
3349 | n/a | error_msg = "realloc invalid parameters"; |
---|
3350 | n/a | goto fail; |
---|
3351 | n/a | } |
---|
3352 | n/a | |
---|
3353 | n/a | switch(domain) |
---|
3354 | n/a | { |
---|
3355 | n/a | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break; |
---|
3356 | n/a | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break; |
---|
3357 | n/a | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break; |
---|
3358 | n/a | } |
---|
3359 | n/a | |
---|
3360 | n/a | if (hook.free_ptr != ptr2) { |
---|
3361 | n/a | error_msg = "free invalid pointer"; |
---|
3362 | n/a | goto fail; |
---|
3363 | n/a | } |
---|
3364 | n/a | |
---|
3365 | n/a | nelem = 2; |
---|
3366 | n/a | elsize = 5; |
---|
3367 | n/a | switch(domain) |
---|
3368 | n/a | { |
---|
3369 | n/a | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break; |
---|
3370 | n/a | case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break; |
---|
3371 | n/a | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break; |
---|
3372 | n/a | default: ptr = NULL; break; |
---|
3373 | n/a | } |
---|
3374 | n/a | |
---|
3375 | n/a | if (ptr == NULL) { |
---|
3376 | n/a | error_msg = "calloc failed"; |
---|
3377 | n/a | goto fail; |
---|
3378 | n/a | } |
---|
3379 | n/a | |
---|
3380 | n/a | if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) { |
---|
3381 | n/a | error_msg = "calloc invalid nelem or elsize"; |
---|
3382 | n/a | goto fail; |
---|
3383 | n/a | } |
---|
3384 | n/a | |
---|
3385 | n/a | switch(domain) |
---|
3386 | n/a | { |
---|
3387 | n/a | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break; |
---|
3388 | n/a | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break; |
---|
3389 | n/a | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break; |
---|
3390 | n/a | } |
---|
3391 | n/a | |
---|
3392 | n/a | Py_INCREF(Py_None); |
---|
3393 | n/a | res = Py_None; |
---|
3394 | n/a | goto finally; |
---|
3395 | n/a | |
---|
3396 | n/a | fail: |
---|
3397 | n/a | PyErr_SetString(PyExc_RuntimeError, error_msg); |
---|
3398 | n/a | |
---|
3399 | n/a | finally: |
---|
3400 | n/a | PyMem_SetAllocator(domain, &hook.alloc); |
---|
3401 | n/a | return res; |
---|
3402 | n/a | } |
---|
3403 | n/a | |
---|
3404 | n/a | static PyObject * |
---|
3405 | n/a | test_pymem_setrawallocators(PyObject *self) |
---|
3406 | n/a | { |
---|
3407 | n/a | return test_setallocators(PYMEM_DOMAIN_RAW); |
---|
3408 | n/a | } |
---|
3409 | n/a | |
---|
3410 | n/a | static PyObject * |
---|
3411 | n/a | test_pymem_setallocators(PyObject *self) |
---|
3412 | n/a | { |
---|
3413 | n/a | return test_setallocators(PYMEM_DOMAIN_MEM); |
---|
3414 | n/a | } |
---|
3415 | n/a | |
---|
3416 | n/a | static PyObject * |
---|
3417 | n/a | test_pyobject_setallocators(PyObject *self) |
---|
3418 | n/a | { |
---|
3419 | n/a | return test_setallocators(PYMEM_DOMAIN_OBJ); |
---|
3420 | n/a | } |
---|
3421 | n/a | |
---|
3422 | n/a | PyDoc_STRVAR(docstring_empty, |
---|
3423 | n/a | "" |
---|
3424 | n/a | ); |
---|
3425 | n/a | |
---|
3426 | n/a | PyDoc_STRVAR(docstring_no_signature, |
---|
3427 | n/a | "This docstring has no signature." |
---|
3428 | n/a | ); |
---|
3429 | n/a | |
---|
3430 | n/a | PyDoc_STRVAR(docstring_with_invalid_signature, |
---|
3431 | n/a | "docstring_with_invalid_signature($module, /, boo)\n" |
---|
3432 | n/a | "\n" |
---|
3433 | n/a | "This docstring has an invalid signature." |
---|
3434 | n/a | ); |
---|
3435 | n/a | |
---|
3436 | n/a | PyDoc_STRVAR(docstring_with_invalid_signature2, |
---|
3437 | n/a | "docstring_with_invalid_signature2($module, /, boo)\n" |
---|
3438 | n/a | "\n" |
---|
3439 | n/a | "--\n" |
---|
3440 | n/a | "\n" |
---|
3441 | n/a | "This docstring also has an invalid signature." |
---|
3442 | n/a | ); |
---|
3443 | n/a | |
---|
3444 | n/a | PyDoc_STRVAR(docstring_with_signature, |
---|
3445 | n/a | "docstring_with_signature($module, /, sig)\n" |
---|
3446 | n/a | "--\n" |
---|
3447 | n/a | "\n" |
---|
3448 | n/a | "This docstring has a valid signature." |
---|
3449 | n/a | ); |
---|
3450 | n/a | |
---|
3451 | n/a | PyDoc_STRVAR(docstring_with_signature_but_no_doc, |
---|
3452 | n/a | "docstring_with_signature_but_no_doc($module, /, sig)\n" |
---|
3453 | n/a | "--\n" |
---|
3454 | n/a | "\n" |
---|
3455 | n/a | ); |
---|
3456 | n/a | |
---|
3457 | n/a | PyDoc_STRVAR(docstring_with_signature_and_extra_newlines, |
---|
3458 | n/a | "docstring_with_signature_and_extra_newlines($module, /, parameter)\n" |
---|
3459 | n/a | "--\n" |
---|
3460 | n/a | "\n" |
---|
3461 | n/a | "\n" |
---|
3462 | n/a | "This docstring has a valid signature and some extra newlines." |
---|
3463 | n/a | ); |
---|
3464 | n/a | |
---|
3465 | n/a | PyDoc_STRVAR(docstring_with_signature_with_defaults, |
---|
3466 | n/a | "docstring_with_signature_with_defaults(module, s='avocado',\n" |
---|
3467 | n/a | " b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n" |
---|
3468 | n/a | " local=the_number_three, sys=sys.maxsize,\n" |
---|
3469 | n/a | " exp=sys.maxsize - 1)\n" |
---|
3470 | n/a | "--\n" |
---|
3471 | n/a | "\n" |
---|
3472 | n/a | "\n" |
---|
3473 | n/a | "\n" |
---|
3474 | n/a | "This docstring has a valid signature with parameters,\n" |
---|
3475 | n/a | "and the parameters take defaults of varying types." |
---|
3476 | n/a | ); |
---|
3477 | n/a | |
---|
3478 | n/a | #ifdef WITH_THREAD |
---|
3479 | n/a | typedef struct { |
---|
3480 | n/a | PyThread_type_lock start_event; |
---|
3481 | n/a | PyThread_type_lock exit_event; |
---|
3482 | n/a | PyObject *callback; |
---|
3483 | n/a | } test_c_thread_t; |
---|
3484 | n/a | |
---|
3485 | n/a | static void |
---|
3486 | n/a | temporary_c_thread(void *data) |
---|
3487 | n/a | { |
---|
3488 | n/a | test_c_thread_t *test_c_thread = data; |
---|
3489 | n/a | PyGILState_STATE state; |
---|
3490 | n/a | PyObject *res; |
---|
3491 | n/a | |
---|
3492 | n/a | PyThread_release_lock(test_c_thread->start_event); |
---|
3493 | n/a | |
---|
3494 | n/a | /* Allocate a Python thread state for this thread */ |
---|
3495 | n/a | state = PyGILState_Ensure(); |
---|
3496 | n/a | |
---|
3497 | n/a | res = _PyObject_CallNoArg(test_c_thread->callback); |
---|
3498 | n/a | Py_CLEAR(test_c_thread->callback); |
---|
3499 | n/a | |
---|
3500 | n/a | if (res == NULL) { |
---|
3501 | n/a | PyErr_Print(); |
---|
3502 | n/a | } |
---|
3503 | n/a | else { |
---|
3504 | n/a | Py_DECREF(res); |
---|
3505 | n/a | } |
---|
3506 | n/a | |
---|
3507 | n/a | /* Destroy the Python thread state for this thread */ |
---|
3508 | n/a | PyGILState_Release(state); |
---|
3509 | n/a | |
---|
3510 | n/a | PyThread_release_lock(test_c_thread->exit_event); |
---|
3511 | n/a | |
---|
3512 | n/a | PyThread_exit_thread(); |
---|
3513 | n/a | } |
---|
3514 | n/a | |
---|
3515 | n/a | static PyObject * |
---|
3516 | n/a | call_in_temporary_c_thread(PyObject *self, PyObject *callback) |
---|
3517 | n/a | { |
---|
3518 | n/a | PyObject *res = NULL; |
---|
3519 | n/a | test_c_thread_t test_c_thread; |
---|
3520 | n/a | long thread; |
---|
3521 | n/a | |
---|
3522 | n/a | PyEval_InitThreads(); |
---|
3523 | n/a | |
---|
3524 | n/a | test_c_thread.start_event = PyThread_allocate_lock(); |
---|
3525 | n/a | test_c_thread.exit_event = PyThread_allocate_lock(); |
---|
3526 | n/a | test_c_thread.callback = NULL; |
---|
3527 | n/a | if (!test_c_thread.start_event || !test_c_thread.exit_event) { |
---|
3528 | n/a | PyErr_SetString(PyExc_RuntimeError, "could not allocate lock"); |
---|
3529 | n/a | goto exit; |
---|
3530 | n/a | } |
---|
3531 | n/a | |
---|
3532 | n/a | Py_INCREF(callback); |
---|
3533 | n/a | test_c_thread.callback = callback; |
---|
3534 | n/a | |
---|
3535 | n/a | PyThread_acquire_lock(test_c_thread.start_event, 1); |
---|
3536 | n/a | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
---|
3537 | n/a | |
---|
3538 | n/a | thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread); |
---|
3539 | n/a | if (thread == -1) { |
---|
3540 | n/a | PyErr_SetString(PyExc_RuntimeError, "unable to start the thread"); |
---|
3541 | n/a | PyThread_release_lock(test_c_thread.start_event); |
---|
3542 | n/a | PyThread_release_lock(test_c_thread.exit_event); |
---|
3543 | n/a | goto exit; |
---|
3544 | n/a | } |
---|
3545 | n/a | |
---|
3546 | n/a | PyThread_acquire_lock(test_c_thread.start_event, 1); |
---|
3547 | n/a | PyThread_release_lock(test_c_thread.start_event); |
---|
3548 | n/a | |
---|
3549 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
3550 | n/a | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
---|
3551 | n/a | PyThread_release_lock(test_c_thread.exit_event); |
---|
3552 | n/a | Py_END_ALLOW_THREADS |
---|
3553 | n/a | |
---|
3554 | n/a | Py_INCREF(Py_None); |
---|
3555 | n/a | res = Py_None; |
---|
3556 | n/a | |
---|
3557 | n/a | exit: |
---|
3558 | n/a | Py_CLEAR(test_c_thread.callback); |
---|
3559 | n/a | if (test_c_thread.start_event) |
---|
3560 | n/a | PyThread_free_lock(test_c_thread.start_event); |
---|
3561 | n/a | if (test_c_thread.exit_event) |
---|
3562 | n/a | PyThread_free_lock(test_c_thread.exit_event); |
---|
3563 | n/a | return res; |
---|
3564 | n/a | } |
---|
3565 | n/a | #endif /* WITH_THREAD */ |
---|
3566 | n/a | |
---|
3567 | n/a | static PyObject* |
---|
3568 | n/a | test_raise_signal(PyObject* self, PyObject *args) |
---|
3569 | n/a | { |
---|
3570 | n/a | int signum, err; |
---|
3571 | n/a | |
---|
3572 | n/a | if (PyArg_ParseTuple(args, "i:raise_signal", &signum) < 0) |
---|
3573 | n/a | return NULL; |
---|
3574 | n/a | |
---|
3575 | n/a | err = raise(signum); |
---|
3576 | n/a | if (err) |
---|
3577 | n/a | return PyErr_SetFromErrno(PyExc_OSError); |
---|
3578 | n/a | |
---|
3579 | n/a | if (PyErr_CheckSignals() < 0) |
---|
3580 | n/a | return NULL; |
---|
3581 | n/a | |
---|
3582 | n/a | Py_RETURN_NONE; |
---|
3583 | n/a | } |
---|
3584 | n/a | |
---|
3585 | n/a | /* marshal */ |
---|
3586 | n/a | |
---|
3587 | n/a | static PyObject* |
---|
3588 | n/a | pymarshal_write_long_to_file(PyObject* self, PyObject *args) |
---|
3589 | n/a | { |
---|
3590 | n/a | long value; |
---|
3591 | n/a | char *filename; |
---|
3592 | n/a | int version; |
---|
3593 | n/a | FILE *fp; |
---|
3594 | n/a | |
---|
3595 | n/a | if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file", |
---|
3596 | n/a | &value, &filename, &version)) |
---|
3597 | n/a | return NULL; |
---|
3598 | n/a | |
---|
3599 | n/a | fp = fopen(filename, "wb"); |
---|
3600 | n/a | if (fp == NULL) { |
---|
3601 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
3602 | n/a | return NULL; |
---|
3603 | n/a | } |
---|
3604 | n/a | |
---|
3605 | n/a | PyMarshal_WriteLongToFile(value, fp, version); |
---|
3606 | n/a | |
---|
3607 | n/a | fclose(fp); |
---|
3608 | n/a | if (PyErr_Occurred()) |
---|
3609 | n/a | return NULL; |
---|
3610 | n/a | Py_RETURN_NONE; |
---|
3611 | n/a | } |
---|
3612 | n/a | |
---|
3613 | n/a | static PyObject* |
---|
3614 | n/a | pymarshal_write_object_to_file(PyObject* self, PyObject *args) |
---|
3615 | n/a | { |
---|
3616 | n/a | PyObject *obj; |
---|
3617 | n/a | char *filename; |
---|
3618 | n/a | int version; |
---|
3619 | n/a | FILE *fp; |
---|
3620 | n/a | |
---|
3621 | n/a | if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file", |
---|
3622 | n/a | &obj, &filename, &version)) |
---|
3623 | n/a | return NULL; |
---|
3624 | n/a | |
---|
3625 | n/a | fp = fopen(filename, "wb"); |
---|
3626 | n/a | if (fp == NULL) { |
---|
3627 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
3628 | n/a | return NULL; |
---|
3629 | n/a | } |
---|
3630 | n/a | |
---|
3631 | n/a | PyMarshal_WriteObjectToFile(obj, fp, version); |
---|
3632 | n/a | |
---|
3633 | n/a | fclose(fp); |
---|
3634 | n/a | if (PyErr_Occurred()) |
---|
3635 | n/a | return NULL; |
---|
3636 | n/a | Py_RETURN_NONE; |
---|
3637 | n/a | } |
---|
3638 | n/a | |
---|
3639 | n/a | static PyObject* |
---|
3640 | n/a | pymarshal_read_short_from_file(PyObject* self, PyObject *args) |
---|
3641 | n/a | { |
---|
3642 | n/a | int value; |
---|
3643 | n/a | long pos; |
---|
3644 | n/a | char *filename; |
---|
3645 | n/a | FILE *fp; |
---|
3646 | n/a | |
---|
3647 | n/a | if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename)) |
---|
3648 | n/a | return NULL; |
---|
3649 | n/a | |
---|
3650 | n/a | fp = fopen(filename, "rb"); |
---|
3651 | n/a | if (fp == NULL) { |
---|
3652 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
3653 | n/a | return NULL; |
---|
3654 | n/a | } |
---|
3655 | n/a | |
---|
3656 | n/a | value = PyMarshal_ReadShortFromFile(fp); |
---|
3657 | n/a | pos = ftell(fp); |
---|
3658 | n/a | |
---|
3659 | n/a | fclose(fp); |
---|
3660 | n/a | if (PyErr_Occurred()) |
---|
3661 | n/a | return NULL; |
---|
3662 | n/a | return Py_BuildValue("il", value, pos); |
---|
3663 | n/a | } |
---|
3664 | n/a | |
---|
3665 | n/a | static PyObject* |
---|
3666 | n/a | pymarshal_read_long_from_file(PyObject* self, PyObject *args) |
---|
3667 | n/a | { |
---|
3668 | n/a | long value, pos; |
---|
3669 | n/a | char *filename; |
---|
3670 | n/a | FILE *fp; |
---|
3671 | n/a | |
---|
3672 | n/a | if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename)) |
---|
3673 | n/a | return NULL; |
---|
3674 | n/a | |
---|
3675 | n/a | fp = fopen(filename, "rb"); |
---|
3676 | n/a | if (fp == NULL) { |
---|
3677 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
3678 | n/a | return NULL; |
---|
3679 | n/a | } |
---|
3680 | n/a | |
---|
3681 | n/a | value = PyMarshal_ReadLongFromFile(fp); |
---|
3682 | n/a | pos = ftell(fp); |
---|
3683 | n/a | |
---|
3684 | n/a | fclose(fp); |
---|
3685 | n/a | if (PyErr_Occurred()) |
---|
3686 | n/a | return NULL; |
---|
3687 | n/a | return Py_BuildValue("ll", value, pos); |
---|
3688 | n/a | } |
---|
3689 | n/a | |
---|
3690 | n/a | static PyObject* |
---|
3691 | n/a | pymarshal_read_last_object_from_file(PyObject* self, PyObject *args) |
---|
3692 | n/a | { |
---|
3693 | n/a | PyObject *obj; |
---|
3694 | n/a | long pos; |
---|
3695 | n/a | char *filename; |
---|
3696 | n/a | FILE *fp; |
---|
3697 | n/a | |
---|
3698 | n/a | if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename)) |
---|
3699 | n/a | return NULL; |
---|
3700 | n/a | |
---|
3701 | n/a | fp = fopen(filename, "rb"); |
---|
3702 | n/a | if (fp == NULL) { |
---|
3703 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
3704 | n/a | return NULL; |
---|
3705 | n/a | } |
---|
3706 | n/a | |
---|
3707 | n/a | obj = PyMarshal_ReadLastObjectFromFile(fp); |
---|
3708 | n/a | pos = ftell(fp); |
---|
3709 | n/a | |
---|
3710 | n/a | fclose(fp); |
---|
3711 | n/a | return Py_BuildValue("Nl", obj, pos); |
---|
3712 | n/a | } |
---|
3713 | n/a | |
---|
3714 | n/a | static PyObject* |
---|
3715 | n/a | pymarshal_read_object_from_file(PyObject* self, PyObject *args) |
---|
3716 | n/a | { |
---|
3717 | n/a | PyObject *obj; |
---|
3718 | n/a | long pos; |
---|
3719 | n/a | char *filename; |
---|
3720 | n/a | FILE *fp; |
---|
3721 | n/a | |
---|
3722 | n/a | if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename)) |
---|
3723 | n/a | return NULL; |
---|
3724 | n/a | |
---|
3725 | n/a | fp = fopen(filename, "rb"); |
---|
3726 | n/a | if (fp == NULL) { |
---|
3727 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
3728 | n/a | return NULL; |
---|
3729 | n/a | } |
---|
3730 | n/a | |
---|
3731 | n/a | obj = PyMarshal_ReadObjectFromFile(fp); |
---|
3732 | n/a | pos = ftell(fp); |
---|
3733 | n/a | |
---|
3734 | n/a | fclose(fp); |
---|
3735 | n/a | return Py_BuildValue("Nl", obj, pos); |
---|
3736 | n/a | } |
---|
3737 | n/a | |
---|
3738 | n/a | static PyObject* |
---|
3739 | n/a | return_null_without_error(PyObject *self, PyObject *args) |
---|
3740 | n/a | { |
---|
3741 | n/a | /* invalid call: return NULL without setting an error, |
---|
3742 | n/a | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
---|
3743 | n/a | PyErr_Clear(); |
---|
3744 | n/a | return NULL; |
---|
3745 | n/a | } |
---|
3746 | n/a | |
---|
3747 | n/a | static PyObject* |
---|
3748 | n/a | return_result_with_error(PyObject *self, PyObject *args) |
---|
3749 | n/a | { |
---|
3750 | n/a | /* invalid call: return a result with an error set, |
---|
3751 | n/a | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
---|
3752 | n/a | PyErr_SetNone(PyExc_ValueError); |
---|
3753 | n/a | Py_RETURN_NONE; |
---|
3754 | n/a | } |
---|
3755 | n/a | |
---|
3756 | n/a | static PyObject * |
---|
3757 | n/a | test_pytime_fromseconds(PyObject *self, PyObject *args) |
---|
3758 | n/a | { |
---|
3759 | n/a | int seconds; |
---|
3760 | n/a | _PyTime_t ts; |
---|
3761 | n/a | |
---|
3762 | n/a | if (!PyArg_ParseTuple(args, "i", &seconds)) |
---|
3763 | n/a | return NULL; |
---|
3764 | n/a | ts = _PyTime_FromSeconds(seconds); |
---|
3765 | n/a | return _PyTime_AsNanosecondsObject(ts); |
---|
3766 | n/a | } |
---|
3767 | n/a | |
---|
3768 | n/a | static PyObject * |
---|
3769 | n/a | test_pytime_fromsecondsobject(PyObject *self, PyObject *args) |
---|
3770 | n/a | { |
---|
3771 | n/a | PyObject *obj; |
---|
3772 | n/a | int round; |
---|
3773 | n/a | _PyTime_t ts; |
---|
3774 | n/a | |
---|
3775 | n/a | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
---|
3776 | n/a | return NULL; |
---|
3777 | n/a | if (check_time_rounding(round) < 0) |
---|
3778 | n/a | return NULL; |
---|
3779 | n/a | if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) |
---|
3780 | n/a | return NULL; |
---|
3781 | n/a | return _PyTime_AsNanosecondsObject(ts); |
---|
3782 | n/a | } |
---|
3783 | n/a | |
---|
3784 | n/a | static PyObject * |
---|
3785 | n/a | test_pytime_assecondsdouble(PyObject *self, PyObject *args) |
---|
3786 | n/a | { |
---|
3787 | n/a | long long ns; |
---|
3788 | n/a | _PyTime_t ts; |
---|
3789 | n/a | double d; |
---|
3790 | n/a | |
---|
3791 | n/a | if (!PyArg_ParseTuple(args, "L", &ns)) |
---|
3792 | n/a | return NULL; |
---|
3793 | n/a | ts = _PyTime_FromNanoseconds(ns); |
---|
3794 | n/a | d = _PyTime_AsSecondsDouble(ts); |
---|
3795 | n/a | return PyFloat_FromDouble(d); |
---|
3796 | n/a | } |
---|
3797 | n/a | |
---|
3798 | n/a | static PyObject * |
---|
3799 | n/a | test_PyTime_AsTimeval(PyObject *self, PyObject *args) |
---|
3800 | n/a | { |
---|
3801 | n/a | long long ns; |
---|
3802 | n/a | int round; |
---|
3803 | n/a | _PyTime_t t; |
---|
3804 | n/a | struct timeval tv; |
---|
3805 | n/a | PyObject *seconds; |
---|
3806 | n/a | |
---|
3807 | n/a | if (!PyArg_ParseTuple(args, "Li", &ns, &round)) |
---|
3808 | n/a | return NULL; |
---|
3809 | n/a | if (check_time_rounding(round) < 0) |
---|
3810 | n/a | return NULL; |
---|
3811 | n/a | t = _PyTime_FromNanoseconds(ns); |
---|
3812 | n/a | if (_PyTime_AsTimeval(t, &tv, round) < 0) |
---|
3813 | n/a | return NULL; |
---|
3814 | n/a | |
---|
3815 | n/a | seconds = PyLong_FromLong((long long)tv.tv_sec); |
---|
3816 | n/a | if (seconds == NULL) |
---|
3817 | n/a | return NULL; |
---|
3818 | n/a | return Py_BuildValue("Nl", seconds, tv.tv_usec); |
---|
3819 | n/a | } |
---|
3820 | n/a | |
---|
3821 | n/a | #ifdef HAVE_CLOCK_GETTIME |
---|
3822 | n/a | static PyObject * |
---|
3823 | n/a | test_PyTime_AsTimespec(PyObject *self, PyObject *args) |
---|
3824 | n/a | { |
---|
3825 | n/a | long long ns; |
---|
3826 | n/a | _PyTime_t t; |
---|
3827 | n/a | struct timespec ts; |
---|
3828 | n/a | |
---|
3829 | n/a | if (!PyArg_ParseTuple(args, "L", &ns)) |
---|
3830 | n/a | return NULL; |
---|
3831 | n/a | t = _PyTime_FromNanoseconds(ns); |
---|
3832 | n/a | if (_PyTime_AsTimespec(t, &ts) == -1) |
---|
3833 | n/a | return NULL; |
---|
3834 | n/a | return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); |
---|
3835 | n/a | } |
---|
3836 | n/a | #endif |
---|
3837 | n/a | |
---|
3838 | n/a | static PyObject * |
---|
3839 | n/a | test_PyTime_AsMilliseconds(PyObject *self, PyObject *args) |
---|
3840 | n/a | { |
---|
3841 | n/a | long long ns; |
---|
3842 | n/a | int round; |
---|
3843 | n/a | _PyTime_t t, ms; |
---|
3844 | n/a | |
---|
3845 | n/a | if (!PyArg_ParseTuple(args, "Li", &ns, &round)) |
---|
3846 | n/a | return NULL; |
---|
3847 | n/a | if (check_time_rounding(round) < 0) |
---|
3848 | n/a | return NULL; |
---|
3849 | n/a | t = _PyTime_FromNanoseconds(ns); |
---|
3850 | n/a | ms = _PyTime_AsMilliseconds(t, round); |
---|
3851 | n/a | /* This conversion rely on the fact that _PyTime_t is a number of |
---|
3852 | n/a | nanoseconds */ |
---|
3853 | n/a | return _PyTime_AsNanosecondsObject(ms); |
---|
3854 | n/a | } |
---|
3855 | n/a | |
---|
3856 | n/a | static PyObject * |
---|
3857 | n/a | test_PyTime_AsMicroseconds(PyObject *self, PyObject *args) |
---|
3858 | n/a | { |
---|
3859 | n/a | long long ns; |
---|
3860 | n/a | int round; |
---|
3861 | n/a | _PyTime_t t, ms; |
---|
3862 | n/a | |
---|
3863 | n/a | if (!PyArg_ParseTuple(args, "Li", &ns, &round)) |
---|
3864 | n/a | return NULL; |
---|
3865 | n/a | if (check_time_rounding(round) < 0) |
---|
3866 | n/a | return NULL; |
---|
3867 | n/a | t = _PyTime_FromNanoseconds(ns); |
---|
3868 | n/a | ms = _PyTime_AsMicroseconds(t, round); |
---|
3869 | n/a | /* This conversion rely on the fact that _PyTime_t is a number of |
---|
3870 | n/a | nanoseconds */ |
---|
3871 | n/a | return _PyTime_AsNanosecondsObject(ms); |
---|
3872 | n/a | } |
---|
3873 | n/a | |
---|
3874 | n/a | static PyObject* |
---|
3875 | n/a | get_recursion_depth(PyObject *self, PyObject *args) |
---|
3876 | n/a | { |
---|
3877 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
3878 | n/a | |
---|
3879 | n/a | /* subtract one to ignore the frame of the get_recursion_depth() call */ |
---|
3880 | n/a | return PyLong_FromLong(tstate->recursion_depth - 1); |
---|
3881 | n/a | } |
---|
3882 | n/a | |
---|
3883 | n/a | static PyObject* |
---|
3884 | n/a | pymem_buffer_overflow(PyObject *self, PyObject *args) |
---|
3885 | n/a | { |
---|
3886 | n/a | char *buffer; |
---|
3887 | n/a | |
---|
3888 | n/a | /* Deliberate buffer overflow to check that PyMem_Free() detects |
---|
3889 | n/a | the overflow when debug hooks are installed. */ |
---|
3890 | n/a | buffer = PyMem_Malloc(16); |
---|
3891 | n/a | buffer[16] = 'x'; |
---|
3892 | n/a | PyMem_Free(buffer); |
---|
3893 | n/a | |
---|
3894 | n/a | Py_RETURN_NONE; |
---|
3895 | n/a | } |
---|
3896 | n/a | |
---|
3897 | n/a | static PyObject* |
---|
3898 | n/a | pymem_api_misuse(PyObject *self, PyObject *args) |
---|
3899 | n/a | { |
---|
3900 | n/a | char *buffer; |
---|
3901 | n/a | |
---|
3902 | n/a | /* Deliberate misusage of Python allocators: |
---|
3903 | n/a | allococate with PyMem but release with PyMem_Raw. */ |
---|
3904 | n/a | buffer = PyMem_Malloc(16); |
---|
3905 | n/a | PyMem_RawFree(buffer); |
---|
3906 | n/a | |
---|
3907 | n/a | Py_RETURN_NONE; |
---|
3908 | n/a | } |
---|
3909 | n/a | |
---|
3910 | n/a | static PyObject* |
---|
3911 | n/a | pymem_malloc_without_gil(PyObject *self, PyObject *args) |
---|
3912 | n/a | { |
---|
3913 | n/a | char *buffer; |
---|
3914 | n/a | |
---|
3915 | n/a | /* Deliberate bug to test debug hooks on Python memory allocators: |
---|
3916 | n/a | call PyMem_Malloc() without holding the GIL */ |
---|
3917 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
3918 | n/a | buffer = PyMem_Malloc(10); |
---|
3919 | n/a | Py_END_ALLOW_THREADS |
---|
3920 | n/a | |
---|
3921 | n/a | PyMem_Free(buffer); |
---|
3922 | n/a | |
---|
3923 | n/a | Py_RETURN_NONE; |
---|
3924 | n/a | } |
---|
3925 | n/a | |
---|
3926 | n/a | static PyObject* |
---|
3927 | n/a | pyobject_malloc_without_gil(PyObject *self, PyObject *args) |
---|
3928 | n/a | { |
---|
3929 | n/a | char *buffer; |
---|
3930 | n/a | |
---|
3931 | n/a | /* Deliberate bug to test debug hooks on Python memory allocators: |
---|
3932 | n/a | call PyObject_Malloc() without holding the GIL */ |
---|
3933 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
3934 | n/a | buffer = PyObject_Malloc(10); |
---|
3935 | n/a | Py_END_ALLOW_THREADS |
---|
3936 | n/a | |
---|
3937 | n/a | PyObject_Free(buffer); |
---|
3938 | n/a | |
---|
3939 | n/a | Py_RETURN_NONE; |
---|
3940 | n/a | } |
---|
3941 | n/a | |
---|
3942 | n/a | static PyObject * |
---|
3943 | n/a | tracemalloc_track(PyObject *self, PyObject *args) |
---|
3944 | n/a | { |
---|
3945 | n/a | unsigned int domain; |
---|
3946 | n/a | PyObject *ptr_obj; |
---|
3947 | n/a | void *ptr; |
---|
3948 | n/a | Py_ssize_t size; |
---|
3949 | n/a | int release_gil = 0; |
---|
3950 | n/a | int res; |
---|
3951 | n/a | |
---|
3952 | n/a | if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil)) |
---|
3953 | n/a | return NULL; |
---|
3954 | n/a | ptr = PyLong_AsVoidPtr(ptr_obj); |
---|
3955 | n/a | if (PyErr_Occurred()) |
---|
3956 | n/a | return NULL; |
---|
3957 | n/a | |
---|
3958 | n/a | if (release_gil) { |
---|
3959 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
3960 | n/a | res = _PyTraceMalloc_Track(domain, (uintptr_t)ptr, size); |
---|
3961 | n/a | Py_END_ALLOW_THREADS |
---|
3962 | n/a | } |
---|
3963 | n/a | else { |
---|
3964 | n/a | res = _PyTraceMalloc_Track(domain, (uintptr_t)ptr, size); |
---|
3965 | n/a | } |
---|
3966 | n/a | |
---|
3967 | n/a | if (res < 0) { |
---|
3968 | n/a | PyErr_SetString(PyExc_RuntimeError, "_PyTraceMalloc_Track error"); |
---|
3969 | n/a | return NULL; |
---|
3970 | n/a | } |
---|
3971 | n/a | |
---|
3972 | n/a | Py_RETURN_NONE; |
---|
3973 | n/a | } |
---|
3974 | n/a | |
---|
3975 | n/a | static PyObject * |
---|
3976 | n/a | tracemalloc_untrack(PyObject *self, PyObject *args) |
---|
3977 | n/a | { |
---|
3978 | n/a | unsigned int domain; |
---|
3979 | n/a | PyObject *ptr_obj; |
---|
3980 | n/a | void *ptr; |
---|
3981 | n/a | int res; |
---|
3982 | n/a | |
---|
3983 | n/a | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
---|
3984 | n/a | return NULL; |
---|
3985 | n/a | ptr = PyLong_AsVoidPtr(ptr_obj); |
---|
3986 | n/a | if (PyErr_Occurred()) |
---|
3987 | n/a | return NULL; |
---|
3988 | n/a | |
---|
3989 | n/a | res = _PyTraceMalloc_Untrack(domain, (uintptr_t)ptr); |
---|
3990 | n/a | if (res < 0) { |
---|
3991 | n/a | PyErr_SetString(PyExc_RuntimeError, "_PyTraceMalloc_Track error"); |
---|
3992 | n/a | return NULL; |
---|
3993 | n/a | } |
---|
3994 | n/a | |
---|
3995 | n/a | Py_RETURN_NONE; |
---|
3996 | n/a | } |
---|
3997 | n/a | |
---|
3998 | n/a | static PyObject * |
---|
3999 | n/a | tracemalloc_get_traceback(PyObject *self, PyObject *args) |
---|
4000 | n/a | { |
---|
4001 | n/a | unsigned int domain; |
---|
4002 | n/a | PyObject *ptr_obj; |
---|
4003 | n/a | void *ptr; |
---|
4004 | n/a | |
---|
4005 | n/a | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
---|
4006 | n/a | return NULL; |
---|
4007 | n/a | ptr = PyLong_AsVoidPtr(ptr_obj); |
---|
4008 | n/a | if (PyErr_Occurred()) |
---|
4009 | n/a | return NULL; |
---|
4010 | n/a | |
---|
4011 | n/a | return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr); |
---|
4012 | n/a | } |
---|
4013 | n/a | |
---|
4014 | n/a | static PyObject * |
---|
4015 | n/a | dict_get_version(PyObject *self, PyObject *args) |
---|
4016 | n/a | { |
---|
4017 | n/a | PyDictObject *dict; |
---|
4018 | n/a | uint64_t version; |
---|
4019 | n/a | |
---|
4020 | n/a | if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict)) |
---|
4021 | n/a | return NULL; |
---|
4022 | n/a | |
---|
4023 | n/a | version = dict->ma_version_tag; |
---|
4024 | n/a | |
---|
4025 | n/a | Py_BUILD_ASSERT(sizeof(unsigned PY_LONG_LONG) >= sizeof(version)); |
---|
4026 | n/a | return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)version); |
---|
4027 | n/a | } |
---|
4028 | n/a | |
---|
4029 | n/a | |
---|
4030 | n/a | static PyMethodDef TestMethods[] = { |
---|
4031 | n/a | {"raise_exception", raise_exception, METH_VARARGS}, |
---|
4032 | n/a | {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, |
---|
4033 | n/a | {"set_errno", set_errno, METH_VARARGS}, |
---|
4034 | n/a | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
---|
4035 | n/a | {"test_sizeof_c_types", (PyCFunction)test_sizeof_c_types, METH_NOARGS}, |
---|
4036 | n/a | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
---|
4037 | n/a | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
---|
4038 | n/a | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
---|
4039 | n/a | {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS}, |
---|
4040 | n/a | {"dict_hassplittable", dict_hassplittable, METH_O}, |
---|
4041 | n/a | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
---|
4042 | n/a | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
---|
4043 | n/a | {"test_xincref_doesnt_leak",(PyCFunction)test_xincref_doesnt_leak, METH_NOARGS}, |
---|
4044 | n/a | {"test_incref_doesnt_leak", (PyCFunction)test_incref_doesnt_leak, METH_NOARGS}, |
---|
4045 | n/a | {"test_xdecref_doesnt_leak",(PyCFunction)test_xdecref_doesnt_leak, METH_NOARGS}, |
---|
4046 | n/a | {"test_decref_doesnt_leak", (PyCFunction)test_decref_doesnt_leak, METH_NOARGS}, |
---|
4047 | n/a | {"test_incref_decref_API", (PyCFunction)test_incref_decref_API, METH_NOARGS}, |
---|
4048 | n/a | {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, |
---|
4049 | n/a | METH_NOARGS}, |
---|
4050 | n/a | {"test_long_as_double", (PyCFunction)test_long_as_double,METH_NOARGS}, |
---|
4051 | n/a | {"test_long_as_size_t", (PyCFunction)test_long_as_size_t,METH_NOARGS}, |
---|
4052 | n/a | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
---|
4053 | n/a | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
---|
4054 | n/a | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
---|
4055 | n/a | {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, |
---|
4056 | n/a | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
---|
4057 | n/a | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
---|
4058 | n/a | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
---|
4059 | n/a | PyDoc_STR("This is a pretty normal docstring.")}, |
---|
4060 | n/a | {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS}, |
---|
4061 | n/a | {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS}, |
---|
4062 | n/a | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
---|
4063 | n/a | {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS}, |
---|
4064 | n/a | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
---|
4065 | n/a | {"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS}, |
---|
4066 | n/a | #endif |
---|
4067 | n/a | {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O}, |
---|
4068 | n/a | {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS}, |
---|
4069 | n/a | {"get_args", get_args, METH_VARARGS}, |
---|
4070 | n/a | {"get_kwargs", (PyCFunction)get_kwargs, METH_VARARGS|METH_KEYWORDS}, |
---|
4071 | n/a | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
---|
4072 | n/a | {"getargs_keywords", (PyCFunction)getargs_keywords, |
---|
4073 | n/a | METH_VARARGS|METH_KEYWORDS}, |
---|
4074 | n/a | {"getargs_keyword_only", (PyCFunction)getargs_keyword_only, |
---|
4075 | n/a | METH_VARARGS|METH_KEYWORDS}, |
---|
4076 | n/a | {"getargs_positional_only_and_keywords", |
---|
4077 | n/a | (PyCFunction)getargs_positional_only_and_keywords, |
---|
4078 | n/a | METH_VARARGS|METH_KEYWORDS}, |
---|
4079 | n/a | {"getargs_b", getargs_b, METH_VARARGS}, |
---|
4080 | n/a | {"getargs_B", getargs_B, METH_VARARGS}, |
---|
4081 | n/a | {"getargs_h", getargs_h, METH_VARARGS}, |
---|
4082 | n/a | {"getargs_H", getargs_H, METH_VARARGS}, |
---|
4083 | n/a | {"getargs_I", getargs_I, METH_VARARGS}, |
---|
4084 | n/a | {"getargs_k", getargs_k, METH_VARARGS}, |
---|
4085 | n/a | {"getargs_i", getargs_i, METH_VARARGS}, |
---|
4086 | n/a | {"getargs_l", getargs_l, METH_VARARGS}, |
---|
4087 | n/a | {"getargs_n", getargs_n, METH_VARARGS}, |
---|
4088 | n/a | {"getargs_p", getargs_p, METH_VARARGS}, |
---|
4089 | n/a | {"getargs_L", getargs_L, METH_VARARGS}, |
---|
4090 | n/a | {"getargs_K", getargs_K, METH_VARARGS}, |
---|
4091 | n/a | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
---|
4092 | n/a | {"test_long_long_and_overflow", |
---|
4093 | n/a | (PyCFunction)test_long_long_and_overflow, METH_NOARGS}, |
---|
4094 | n/a | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
---|
4095 | n/a | {"getargs_f", getargs_f, METH_VARARGS}, |
---|
4096 | n/a | {"getargs_d", getargs_d, METH_VARARGS}, |
---|
4097 | n/a | {"getargs_D", getargs_D, METH_VARARGS}, |
---|
4098 | n/a | {"getargs_S", getargs_S, METH_VARARGS}, |
---|
4099 | n/a | {"getargs_Y", getargs_Y, METH_VARARGS}, |
---|
4100 | n/a | {"getargs_U", getargs_U, METH_VARARGS}, |
---|
4101 | n/a | {"getargs_c", getargs_c, METH_VARARGS}, |
---|
4102 | n/a | {"getargs_C", getargs_C, METH_VARARGS}, |
---|
4103 | n/a | {"getargs_s", getargs_s, METH_VARARGS}, |
---|
4104 | n/a | {"getargs_s_star", getargs_s_star, METH_VARARGS}, |
---|
4105 | n/a | {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
---|
4106 | n/a | {"getargs_z", getargs_z, METH_VARARGS}, |
---|
4107 | n/a | {"getargs_z_star", getargs_z_star, METH_VARARGS}, |
---|
4108 | n/a | {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
---|
4109 | n/a | {"getargs_y", getargs_y, METH_VARARGS}, |
---|
4110 | n/a | {"getargs_y_star", getargs_y_star, METH_VARARGS}, |
---|
4111 | n/a | {"getargs_y_hash", getargs_y_hash, METH_VARARGS}, |
---|
4112 | n/a | {"getargs_u", getargs_u, METH_VARARGS}, |
---|
4113 | n/a | {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
---|
4114 | n/a | {"getargs_Z", getargs_Z, METH_VARARGS}, |
---|
4115 | n/a | {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS}, |
---|
4116 | n/a | {"getargs_w_star", getargs_w_star, METH_VARARGS}, |
---|
4117 | n/a | {"getargs_es", getargs_es, METH_VARARGS}, |
---|
4118 | n/a | {"getargs_et", getargs_et, METH_VARARGS}, |
---|
4119 | n/a | {"getargs_es_hash", getargs_es_hash, METH_VARARGS}, |
---|
4120 | n/a | {"getargs_et_hash", getargs_et_hash, METH_VARARGS}, |
---|
4121 | n/a | {"codec_incrementalencoder", |
---|
4122 | n/a | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
---|
4123 | n/a | {"codec_incrementaldecoder", |
---|
4124 | n/a | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
---|
4125 | n/a | {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS}, |
---|
4126 | n/a | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
---|
4127 | n/a | {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS}, |
---|
4128 | n/a | {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
---|
4129 | n/a | {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS}, |
---|
4130 | n/a | {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS}, |
---|
4131 | n/a | {"unicode_asucs4", unicode_asucs4, METH_VARARGS}, |
---|
4132 | n/a | {"unicode_findchar", unicode_findchar, METH_VARARGS}, |
---|
4133 | n/a | {"unicode_copycharacters", unicode_copycharacters, METH_VARARGS}, |
---|
4134 | n/a | {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, |
---|
4135 | n/a | {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS}, |
---|
4136 | n/a | {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS}, |
---|
4137 | n/a | #ifdef WITH_THREAD |
---|
4138 | n/a | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
---|
4139 | n/a | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
---|
4140 | n/a | #endif |
---|
4141 | n/a | #ifdef HAVE_GETTIMEOFDAY |
---|
4142 | n/a | {"profile_int", profile_int, METH_NOARGS}, |
---|
4143 | n/a | #endif |
---|
4144 | n/a | {"traceback_print", traceback_print, METH_VARARGS}, |
---|
4145 | n/a | {"exception_print", exception_print, METH_VARARGS}, |
---|
4146 | n/a | {"set_exc_info", test_set_exc_info, METH_VARARGS}, |
---|
4147 | n/a | {"argparsing", argparsing, METH_VARARGS}, |
---|
4148 | n/a | {"code_newempty", code_newempty, METH_VARARGS}, |
---|
4149 | n/a | {"make_exception_with_doc", (PyCFunction)make_exception_with_doc, |
---|
4150 | n/a | METH_VARARGS | METH_KEYWORDS}, |
---|
4151 | n/a | {"make_memoryview_from_NULL_pointer", (PyCFunction)make_memoryview_from_NULL_pointer, |
---|
4152 | n/a | METH_NOARGS}, |
---|
4153 | n/a | {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS}, |
---|
4154 | n/a | {"run_in_subinterp", run_in_subinterp, METH_VARARGS}, |
---|
4155 | n/a | {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS}, |
---|
4156 | n/a | {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS}, |
---|
4157 | n/a | {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS}, |
---|
4158 | n/a | {"with_tp_del", with_tp_del, METH_VARARGS}, |
---|
4159 | n/a | {"create_cfunction", create_cfunction, METH_NOARGS}, |
---|
4160 | n/a | {"test_pymem_alloc0", |
---|
4161 | n/a | (PyCFunction)test_pymem_alloc0, METH_NOARGS}, |
---|
4162 | n/a | {"test_pymem_setrawallocators", |
---|
4163 | n/a | (PyCFunction)test_pymem_setrawallocators, METH_NOARGS}, |
---|
4164 | n/a | {"test_pymem_setallocators", |
---|
4165 | n/a | (PyCFunction)test_pymem_setallocators, METH_NOARGS}, |
---|
4166 | n/a | {"test_pyobject_setallocators", |
---|
4167 | n/a | (PyCFunction)test_pyobject_setallocators, METH_NOARGS}, |
---|
4168 | n/a | {"no_docstring", |
---|
4169 | n/a | (PyCFunction)test_with_docstring, METH_NOARGS}, |
---|
4170 | n/a | {"docstring_empty", |
---|
4171 | n/a | (PyCFunction)test_with_docstring, METH_NOARGS, |
---|
4172 | n/a | docstring_empty}, |
---|
4173 | n/a | {"docstring_no_signature", |
---|
4174 | n/a | (PyCFunction)test_with_docstring, METH_NOARGS, |
---|
4175 | n/a | docstring_no_signature}, |
---|
4176 | n/a | {"docstring_with_invalid_signature", |
---|
4177 | n/a | (PyCFunction)test_with_docstring, METH_NOARGS, |
---|
4178 | n/a | docstring_with_invalid_signature}, |
---|
4179 | n/a | {"docstring_with_invalid_signature2", |
---|
4180 | n/a | (PyCFunction)test_with_docstring, METH_NOARGS, |
---|
4181 | n/a | docstring_with_invalid_signature2}, |
---|
4182 | n/a | {"docstring_with_signature", |
---|
4183 | n/a | (PyCFunction)test_with_docstring, METH_NOARGS, |
---|
4184 | n/a | docstring_with_signature}, |
---|
4185 | n/a | {"docstring_with_signature_but_no_doc", |
---|
4186 | n/a | (PyCFunction)test_with_docstring, METH_NOARGS, |
---|
4187 | n/a | docstring_with_signature_but_no_doc}, |
---|
4188 | n/a | {"docstring_with_signature_and_extra_newlines", |
---|
4189 | n/a | (PyCFunction)test_with_docstring, METH_NOARGS, |
---|
4190 | n/a | docstring_with_signature_and_extra_newlines}, |
---|
4191 | n/a | {"docstring_with_signature_with_defaults", |
---|
4192 | n/a | (PyCFunction)test_with_docstring, METH_NOARGS, |
---|
4193 | n/a | docstring_with_signature_with_defaults}, |
---|
4194 | n/a | {"raise_signal", |
---|
4195 | n/a | (PyCFunction)test_raise_signal, METH_VARARGS}, |
---|
4196 | n/a | #ifdef WITH_THREAD |
---|
4197 | n/a | {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, |
---|
4198 | n/a | PyDoc_STR("set_error_class(error_class) -> None")}, |
---|
4199 | n/a | #endif |
---|
4200 | n/a | {"pymarshal_write_long_to_file", |
---|
4201 | n/a | pymarshal_write_long_to_file, METH_VARARGS}, |
---|
4202 | n/a | {"pymarshal_write_object_to_file", |
---|
4203 | n/a | pymarshal_write_object_to_file, METH_VARARGS}, |
---|
4204 | n/a | {"pymarshal_read_short_from_file", |
---|
4205 | n/a | pymarshal_read_short_from_file, METH_VARARGS}, |
---|
4206 | n/a | {"pymarshal_read_long_from_file", |
---|
4207 | n/a | pymarshal_read_long_from_file, METH_VARARGS}, |
---|
4208 | n/a | {"pymarshal_read_last_object_from_file", |
---|
4209 | n/a | pymarshal_read_last_object_from_file, METH_VARARGS}, |
---|
4210 | n/a | {"pymarshal_read_object_from_file", |
---|
4211 | n/a | pymarshal_read_object_from_file, METH_VARARGS}, |
---|
4212 | n/a | {"return_null_without_error", |
---|
4213 | n/a | return_null_without_error, METH_NOARGS}, |
---|
4214 | n/a | {"return_result_with_error", |
---|
4215 | n/a | return_result_with_error, METH_NOARGS}, |
---|
4216 | n/a | {"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS}, |
---|
4217 | n/a | {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS}, |
---|
4218 | n/a | {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS}, |
---|
4219 | n/a | {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS}, |
---|
4220 | n/a | #ifdef HAVE_CLOCK_GETTIME |
---|
4221 | n/a | {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS}, |
---|
4222 | n/a | #endif |
---|
4223 | n/a | {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS}, |
---|
4224 | n/a | {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS}, |
---|
4225 | n/a | {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, |
---|
4226 | n/a | {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS}, |
---|
4227 | n/a | {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS}, |
---|
4228 | n/a | {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS}, |
---|
4229 | n/a | {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS}, |
---|
4230 | n/a | {"tracemalloc_track", tracemalloc_track, METH_VARARGS}, |
---|
4231 | n/a | {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS}, |
---|
4232 | n/a | {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS}, |
---|
4233 | n/a | {"dict_get_version", dict_get_version, METH_VARARGS}, |
---|
4234 | n/a | {NULL, NULL} /* sentinel */ |
---|
4235 | n/a | }; |
---|
4236 | n/a | |
---|
4237 | n/a | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
---|
4238 | n/a | |
---|
4239 | n/a | typedef struct { |
---|
4240 | n/a | char bool_member; |
---|
4241 | n/a | char byte_member; |
---|
4242 | n/a | unsigned char ubyte_member; |
---|
4243 | n/a | short short_member; |
---|
4244 | n/a | unsigned short ushort_member; |
---|
4245 | n/a | int int_member; |
---|
4246 | n/a | unsigned int uint_member; |
---|
4247 | n/a | long long_member; |
---|
4248 | n/a | unsigned long ulong_member; |
---|
4249 | n/a | Py_ssize_t pyssizet_member; |
---|
4250 | n/a | float float_member; |
---|
4251 | n/a | double double_member; |
---|
4252 | n/a | char inplace_member[6]; |
---|
4253 | n/a | long long longlong_member; |
---|
4254 | n/a | unsigned long long ulonglong_member; |
---|
4255 | n/a | } all_structmembers; |
---|
4256 | n/a | |
---|
4257 | n/a | typedef struct { |
---|
4258 | n/a | PyObject_HEAD |
---|
4259 | n/a | all_structmembers structmembers; |
---|
4260 | n/a | } test_structmembers; |
---|
4261 | n/a | |
---|
4262 | n/a | static struct PyMemberDef test_members[] = { |
---|
4263 | n/a | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
---|
4264 | n/a | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
---|
4265 | n/a | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
---|
4266 | n/a | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
---|
4267 | n/a | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
---|
4268 | n/a | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
---|
4269 | n/a | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
---|
4270 | n/a | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
---|
4271 | n/a | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
---|
4272 | n/a | {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, |
---|
4273 | n/a | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
---|
4274 | n/a | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
---|
4275 | n/a | {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL}, |
---|
4276 | n/a | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
---|
4277 | n/a | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
---|
4278 | n/a | {NULL} |
---|
4279 | n/a | }; |
---|
4280 | n/a | |
---|
4281 | n/a | |
---|
4282 | n/a | static PyObject * |
---|
4283 | n/a | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
---|
4284 | n/a | { |
---|
4285 | n/a | static char *keywords[] = { |
---|
4286 | n/a | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
---|
4287 | n/a | "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", |
---|
4288 | n/a | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
---|
4289 | n/a | "T_LONGLONG", "T_ULONGLONG", |
---|
4290 | n/a | NULL}; |
---|
4291 | n/a | static const char fmt[] = "|bbBhHiIlknfds#LK"; |
---|
4292 | n/a | test_structmembers *ob; |
---|
4293 | n/a | const char *s = NULL; |
---|
4294 | n/a | Py_ssize_t string_len = 0; |
---|
4295 | n/a | ob = PyObject_New(test_structmembers, type); |
---|
4296 | n/a | if (ob == NULL) |
---|
4297 | n/a | return NULL; |
---|
4298 | n/a | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
---|
4299 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
---|
4300 | n/a | &ob->structmembers.bool_member, |
---|
4301 | n/a | &ob->structmembers.byte_member, |
---|
4302 | n/a | &ob->structmembers.ubyte_member, |
---|
4303 | n/a | &ob->structmembers.short_member, |
---|
4304 | n/a | &ob->structmembers.ushort_member, |
---|
4305 | n/a | &ob->structmembers.int_member, |
---|
4306 | n/a | &ob->structmembers.uint_member, |
---|
4307 | n/a | &ob->structmembers.long_member, |
---|
4308 | n/a | &ob->structmembers.ulong_member, |
---|
4309 | n/a | &ob->structmembers.pyssizet_member, |
---|
4310 | n/a | &ob->structmembers.float_member, |
---|
4311 | n/a | &ob->structmembers.double_member, |
---|
4312 | n/a | &s, &string_len |
---|
4313 | n/a | , &ob->structmembers.longlong_member, |
---|
4314 | n/a | &ob->structmembers.ulonglong_member |
---|
4315 | n/a | )) { |
---|
4316 | n/a | Py_DECREF(ob); |
---|
4317 | n/a | return NULL; |
---|
4318 | n/a | } |
---|
4319 | n/a | if (s != NULL) { |
---|
4320 | n/a | if (string_len > 5) { |
---|
4321 | n/a | Py_DECREF(ob); |
---|
4322 | n/a | PyErr_SetString(PyExc_ValueError, "string too long"); |
---|
4323 | n/a | return NULL; |
---|
4324 | n/a | } |
---|
4325 | n/a | strcpy(ob->structmembers.inplace_member, s); |
---|
4326 | n/a | } |
---|
4327 | n/a | else { |
---|
4328 | n/a | strcpy(ob->structmembers.inplace_member, ""); |
---|
4329 | n/a | } |
---|
4330 | n/a | return (PyObject *)ob; |
---|
4331 | n/a | } |
---|
4332 | n/a | |
---|
4333 | n/a | static void |
---|
4334 | n/a | test_structmembers_free(PyObject *ob) |
---|
4335 | n/a | { |
---|
4336 | n/a | PyObject_FREE(ob); |
---|
4337 | n/a | } |
---|
4338 | n/a | |
---|
4339 | n/a | static PyTypeObject test_structmembersType = { |
---|
4340 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
4341 | n/a | "test_structmembersType", |
---|
4342 | n/a | sizeof(test_structmembers), /* tp_basicsize */ |
---|
4343 | n/a | 0, /* tp_itemsize */ |
---|
4344 | n/a | test_structmembers_free, /* destructor tp_dealloc */ |
---|
4345 | n/a | 0, /* tp_print */ |
---|
4346 | n/a | 0, /* tp_getattr */ |
---|
4347 | n/a | 0, /* tp_setattr */ |
---|
4348 | n/a | 0, /* tp_reserved */ |
---|
4349 | n/a | 0, /* tp_repr */ |
---|
4350 | n/a | 0, /* tp_as_number */ |
---|
4351 | n/a | 0, /* tp_as_sequence */ |
---|
4352 | n/a | 0, /* tp_as_mapping */ |
---|
4353 | n/a | 0, /* tp_hash */ |
---|
4354 | n/a | 0, /* tp_call */ |
---|
4355 | n/a | 0, /* tp_str */ |
---|
4356 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
4357 | n/a | PyObject_GenericSetAttr, /* tp_setattro */ |
---|
4358 | n/a | 0, /* tp_as_buffer */ |
---|
4359 | n/a | 0, /* tp_flags */ |
---|
4360 | n/a | "Type containing all structmember types", |
---|
4361 | n/a | 0, /* traverseproc tp_traverse */ |
---|
4362 | n/a | 0, /* tp_clear */ |
---|
4363 | n/a | 0, /* tp_richcompare */ |
---|
4364 | n/a | 0, /* tp_weaklistoffset */ |
---|
4365 | n/a | 0, /* tp_iter */ |
---|
4366 | n/a | 0, /* tp_iternext */ |
---|
4367 | n/a | 0, /* tp_methods */ |
---|
4368 | n/a | test_members, /* tp_members */ |
---|
4369 | n/a | 0, |
---|
4370 | n/a | 0, |
---|
4371 | n/a | 0, |
---|
4372 | n/a | 0, |
---|
4373 | n/a | 0, |
---|
4374 | n/a | 0, |
---|
4375 | n/a | 0, |
---|
4376 | n/a | 0, |
---|
4377 | n/a | test_structmembers_new, /* tp_new */ |
---|
4378 | n/a | }; |
---|
4379 | n/a | |
---|
4380 | n/a | |
---|
4381 | n/a | typedef struct { |
---|
4382 | n/a | PyObject_HEAD |
---|
4383 | n/a | } matmulObject; |
---|
4384 | n/a | |
---|
4385 | n/a | static PyObject * |
---|
4386 | n/a | matmulType_matmul(PyObject *self, PyObject *other) |
---|
4387 | n/a | { |
---|
4388 | n/a | return Py_BuildValue("(sOO)", "matmul", self, other); |
---|
4389 | n/a | } |
---|
4390 | n/a | |
---|
4391 | n/a | static PyObject * |
---|
4392 | n/a | matmulType_imatmul(PyObject *self, PyObject *other) |
---|
4393 | n/a | { |
---|
4394 | n/a | return Py_BuildValue("(sOO)", "imatmul", self, other); |
---|
4395 | n/a | } |
---|
4396 | n/a | |
---|
4397 | n/a | static void |
---|
4398 | n/a | matmulType_dealloc(PyObject *self) |
---|
4399 | n/a | { |
---|
4400 | n/a | Py_TYPE(self)->tp_free(self); |
---|
4401 | n/a | } |
---|
4402 | n/a | |
---|
4403 | n/a | static PyNumberMethods matmulType_as_number = { |
---|
4404 | n/a | 0, /* nb_add */ |
---|
4405 | n/a | 0, /* nb_subtract */ |
---|
4406 | n/a | 0, /* nb_multiply */ |
---|
4407 | n/a | 0, /* nb_remainde r*/ |
---|
4408 | n/a | 0, /* nb_divmod */ |
---|
4409 | n/a | 0, /* nb_power */ |
---|
4410 | n/a | 0, /* nb_negative */ |
---|
4411 | n/a | 0, /* tp_positive */ |
---|
4412 | n/a | 0, /* tp_absolute */ |
---|
4413 | n/a | 0, /* tp_bool */ |
---|
4414 | n/a | 0, /* nb_invert */ |
---|
4415 | n/a | 0, /* nb_lshift */ |
---|
4416 | n/a | 0, /* nb_rshift */ |
---|
4417 | n/a | 0, /* nb_and */ |
---|
4418 | n/a | 0, /* nb_xor */ |
---|
4419 | n/a | 0, /* nb_or */ |
---|
4420 | n/a | 0, /* nb_int */ |
---|
4421 | n/a | 0, /* nb_reserved */ |
---|
4422 | n/a | 0, /* nb_float */ |
---|
4423 | n/a | 0, /* nb_inplace_add */ |
---|
4424 | n/a | 0, /* nb_inplace_subtract */ |
---|
4425 | n/a | 0, /* nb_inplace_multiply */ |
---|
4426 | n/a | 0, /* nb_inplace_remainder */ |
---|
4427 | n/a | 0, /* nb_inplace_power */ |
---|
4428 | n/a | 0, /* nb_inplace_lshift */ |
---|
4429 | n/a | 0, /* nb_inplace_rshift */ |
---|
4430 | n/a | 0, /* nb_inplace_and */ |
---|
4431 | n/a | 0, /* nb_inplace_xor */ |
---|
4432 | n/a | 0, /* nb_inplace_or */ |
---|
4433 | n/a | 0, /* nb_floor_divide */ |
---|
4434 | n/a | 0, /* nb_true_divide */ |
---|
4435 | n/a | 0, /* nb_inplace_floor_divide */ |
---|
4436 | n/a | 0, /* nb_inplace_true_divide */ |
---|
4437 | n/a | 0, /* nb_index */ |
---|
4438 | n/a | matmulType_matmul, /* nb_matrix_multiply */ |
---|
4439 | n/a | matmulType_imatmul /* nb_matrix_inplace_multiply */ |
---|
4440 | n/a | }; |
---|
4441 | n/a | |
---|
4442 | n/a | static PyTypeObject matmulType = { |
---|
4443 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
4444 | n/a | "matmulType", |
---|
4445 | n/a | sizeof(matmulObject), /* tp_basicsize */ |
---|
4446 | n/a | 0, /* tp_itemsize */ |
---|
4447 | n/a | matmulType_dealloc, /* destructor tp_dealloc */ |
---|
4448 | n/a | 0, /* tp_print */ |
---|
4449 | n/a | 0, /* tp_getattr */ |
---|
4450 | n/a | 0, /* tp_setattr */ |
---|
4451 | n/a | 0, /* tp_reserved */ |
---|
4452 | n/a | 0, /* tp_repr */ |
---|
4453 | n/a | &matmulType_as_number, /* tp_as_number */ |
---|
4454 | n/a | 0, /* tp_as_sequence */ |
---|
4455 | n/a | 0, /* tp_as_mapping */ |
---|
4456 | n/a | 0, /* tp_hash */ |
---|
4457 | n/a | 0, /* tp_call */ |
---|
4458 | n/a | 0, /* tp_str */ |
---|
4459 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
4460 | n/a | PyObject_GenericSetAttr, /* tp_setattro */ |
---|
4461 | n/a | 0, /* tp_as_buffer */ |
---|
4462 | n/a | 0, /* tp_flags */ |
---|
4463 | n/a | "C level type with matrix operations defined", |
---|
4464 | n/a | 0, /* traverseproc tp_traverse */ |
---|
4465 | n/a | 0, /* tp_clear */ |
---|
4466 | n/a | 0, /* tp_richcompare */ |
---|
4467 | n/a | 0, /* tp_weaklistoffset */ |
---|
4468 | n/a | 0, /* tp_iter */ |
---|
4469 | n/a | 0, /* tp_iternext */ |
---|
4470 | n/a | 0, /* tp_methods */ |
---|
4471 | n/a | 0, /* tp_members */ |
---|
4472 | n/a | 0, |
---|
4473 | n/a | 0, |
---|
4474 | n/a | 0, |
---|
4475 | n/a | 0, |
---|
4476 | n/a | 0, |
---|
4477 | n/a | 0, |
---|
4478 | n/a | 0, |
---|
4479 | n/a | 0, |
---|
4480 | n/a | PyType_GenericNew, /* tp_new */ |
---|
4481 | n/a | PyObject_Del, /* tp_free */ |
---|
4482 | n/a | }; |
---|
4483 | n/a | |
---|
4484 | n/a | |
---|
4485 | n/a | typedef struct { |
---|
4486 | n/a | PyObject_HEAD |
---|
4487 | n/a | PyObject *ao_iterator; |
---|
4488 | n/a | } awaitObject; |
---|
4489 | n/a | |
---|
4490 | n/a | |
---|
4491 | n/a | static PyObject * |
---|
4492 | n/a | awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
4493 | n/a | { |
---|
4494 | n/a | PyObject *v; |
---|
4495 | n/a | awaitObject *ao; |
---|
4496 | n/a | |
---|
4497 | n/a | if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v)) |
---|
4498 | n/a | return NULL; |
---|
4499 | n/a | |
---|
4500 | n/a | ao = (awaitObject *)type->tp_alloc(type, 0); |
---|
4501 | n/a | if (ao == NULL) { |
---|
4502 | n/a | return NULL; |
---|
4503 | n/a | } |
---|
4504 | n/a | |
---|
4505 | n/a | Py_INCREF(v); |
---|
4506 | n/a | ao->ao_iterator = v; |
---|
4507 | n/a | |
---|
4508 | n/a | return (PyObject *)ao; |
---|
4509 | n/a | } |
---|
4510 | n/a | |
---|
4511 | n/a | |
---|
4512 | n/a | static void |
---|
4513 | n/a | awaitObject_dealloc(awaitObject *ao) |
---|
4514 | n/a | { |
---|
4515 | n/a | Py_CLEAR(ao->ao_iterator); |
---|
4516 | n/a | Py_TYPE(ao)->tp_free(ao); |
---|
4517 | n/a | } |
---|
4518 | n/a | |
---|
4519 | n/a | |
---|
4520 | n/a | static PyObject * |
---|
4521 | n/a | awaitObject_await(awaitObject *ao) |
---|
4522 | n/a | { |
---|
4523 | n/a | Py_INCREF(ao->ao_iterator); |
---|
4524 | n/a | return ao->ao_iterator; |
---|
4525 | n/a | } |
---|
4526 | n/a | |
---|
4527 | n/a | static PyAsyncMethods awaitType_as_async = { |
---|
4528 | n/a | (unaryfunc)awaitObject_await, /* am_await */ |
---|
4529 | n/a | 0, /* am_aiter */ |
---|
4530 | n/a | 0 /* am_anext */ |
---|
4531 | n/a | }; |
---|
4532 | n/a | |
---|
4533 | n/a | |
---|
4534 | n/a | static PyTypeObject awaitType = { |
---|
4535 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
4536 | n/a | "awaitType", |
---|
4537 | n/a | sizeof(awaitObject), /* tp_basicsize */ |
---|
4538 | n/a | 0, /* tp_itemsize */ |
---|
4539 | n/a | (destructor)awaitObject_dealloc, /* destructor tp_dealloc */ |
---|
4540 | n/a | 0, /* tp_print */ |
---|
4541 | n/a | 0, /* tp_getattr */ |
---|
4542 | n/a | 0, /* tp_setattr */ |
---|
4543 | n/a | &awaitType_as_async, /* tp_as_async */ |
---|
4544 | n/a | 0, /* tp_repr */ |
---|
4545 | n/a | 0, /* tp_as_number */ |
---|
4546 | n/a | 0, /* tp_as_sequence */ |
---|
4547 | n/a | 0, /* tp_as_mapping */ |
---|
4548 | n/a | 0, /* tp_hash */ |
---|
4549 | n/a | 0, /* tp_call */ |
---|
4550 | n/a | 0, /* tp_str */ |
---|
4551 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
4552 | n/a | PyObject_GenericSetAttr, /* tp_setattro */ |
---|
4553 | n/a | 0, /* tp_as_buffer */ |
---|
4554 | n/a | 0, /* tp_flags */ |
---|
4555 | n/a | "C level type with tp_as_async", |
---|
4556 | n/a | 0, /* traverseproc tp_traverse */ |
---|
4557 | n/a | 0, /* tp_clear */ |
---|
4558 | n/a | 0, /* tp_richcompare */ |
---|
4559 | n/a | 0, /* tp_weaklistoffset */ |
---|
4560 | n/a | 0, /* tp_iter */ |
---|
4561 | n/a | 0, /* tp_iternext */ |
---|
4562 | n/a | 0, /* tp_methods */ |
---|
4563 | n/a | 0, /* tp_members */ |
---|
4564 | n/a | 0, |
---|
4565 | n/a | 0, |
---|
4566 | n/a | 0, |
---|
4567 | n/a | 0, |
---|
4568 | n/a | 0, |
---|
4569 | n/a | 0, |
---|
4570 | n/a | 0, |
---|
4571 | n/a | 0, |
---|
4572 | n/a | awaitObject_new, /* tp_new */ |
---|
4573 | n/a | PyObject_Del, /* tp_free */ |
---|
4574 | n/a | }; |
---|
4575 | n/a | |
---|
4576 | n/a | |
---|
4577 | n/a | static struct PyModuleDef _testcapimodule = { |
---|
4578 | n/a | PyModuleDef_HEAD_INIT, |
---|
4579 | n/a | "_testcapi", |
---|
4580 | n/a | NULL, |
---|
4581 | n/a | -1, |
---|
4582 | n/a | TestMethods, |
---|
4583 | n/a | NULL, |
---|
4584 | n/a | NULL, |
---|
4585 | n/a | NULL, |
---|
4586 | n/a | NULL |
---|
4587 | n/a | }; |
---|
4588 | n/a | |
---|
4589 | n/a | /* Per PEP 489, this module will not be converted to multi-phase initialization |
---|
4590 | n/a | */ |
---|
4591 | n/a | |
---|
4592 | n/a | PyMODINIT_FUNC |
---|
4593 | n/a | PyInit__testcapi(void) |
---|
4594 | n/a | { |
---|
4595 | n/a | PyObject *m; |
---|
4596 | n/a | |
---|
4597 | n/a | m = PyModule_Create(&_testcapimodule); |
---|
4598 | n/a | if (m == NULL) |
---|
4599 | n/a | return NULL; |
---|
4600 | n/a | |
---|
4601 | n/a | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
---|
4602 | n/a | |
---|
4603 | n/a | Py_TYPE(&test_structmembersType)=&PyType_Type; |
---|
4604 | n/a | Py_INCREF(&test_structmembersType); |
---|
4605 | n/a | /* don't use a name starting with "test", since we don't want |
---|
4606 | n/a | test_capi to automatically call this */ |
---|
4607 | n/a | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
---|
4608 | n/a | if (PyType_Ready(&matmulType) < 0) |
---|
4609 | n/a | return NULL; |
---|
4610 | n/a | Py_INCREF(&matmulType); |
---|
4611 | n/a | PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType); |
---|
4612 | n/a | |
---|
4613 | n/a | if (PyType_Ready(&awaitType) < 0) |
---|
4614 | n/a | return NULL; |
---|
4615 | n/a | Py_INCREF(&awaitType); |
---|
4616 | n/a | PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType); |
---|
4617 | n/a | |
---|
4618 | n/a | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
---|
4619 | n/a | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
---|
4620 | n/a | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
---|
4621 | n/a | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
---|
4622 | n/a | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
---|
4623 | n/a | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
---|
4624 | n/a | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
---|
4625 | n/a | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
---|
4626 | n/a | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
---|
4627 | n/a | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
---|
4628 | n/a | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
---|
4629 | n/a | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
---|
4630 | n/a | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
---|
4631 | n/a | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
---|
4632 | n/a | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
---|
4633 | n/a | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
---|
4634 | n/a | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
---|
4635 | n/a | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
---|
4636 | n/a | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
---|
4637 | n/a | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
---|
4638 | n/a | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); |
---|
4639 | n/a | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); |
---|
4640 | n/a | PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t))); |
---|
4641 | n/a | Py_INCREF(&PyInstanceMethod_Type); |
---|
4642 | n/a | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
---|
4643 | n/a | |
---|
4644 | n/a | PyModule_AddIntConstant(m, "the_number_three", 3); |
---|
4645 | n/a | |
---|
4646 | n/a | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
---|
4647 | n/a | Py_INCREF(TestError); |
---|
4648 | n/a | PyModule_AddObject(m, "error", TestError); |
---|
4649 | n/a | return m; |
---|
4650 | n/a | } |
---|