blob: ae960deba78c97649928f34b07c720b7cc3a0174 [file] [log] [blame]
Tim Peters9ea17ac2001-02-02 05:57:15 +00001/*
2 * C Extension module to test Python interpreter C APIs.
3 *
4 * The 'test_*' functions exported by this module are run as part of the
5 * standard Python regression test, via Lib/test/test_capi.py.
6 */
7
Victor Stinner5c75f372019-04-17 23:02:26 +02008/* The Visual Studio projects builds _testcapi with Py_BUILD_CORE_MODULE
9 define, but we only want to test the public C API, not the internal
10 C API. */
11#undef Py_BUILD_CORE_MODULE
12
Neal Norwitz8866e0a2007-10-27 04:01:17 +000013#define PY_SSIZE_T_CLEAN
14
Tim Peters9ea17ac2001-02-02 05:57:15 +000015#include "Python.h"
Benjamin Peterson91d58bd2009-12-13 21:30:54 +000016#include "datetime.h"
Serhiy Storchakab5181342015-02-06 08:58:56 +020017#include "marshal.h"
Victor Stinnera1c249c2018-11-01 03:15:58 +010018#include "pythread.h"
19#include "structmember.h"
20#include <float.h>
Victor Stinner56e8c292014-07-21 12:30:22 +020021#include <signal.h>
Tim Peters9ea17ac2001-02-02 05:57:15 +000022
Victor Stinner95e9cef2015-03-28 01:26:47 +010023#ifdef MS_WINDOWS
Victor Stinner09e5cf22015-03-30 00:09:18 +020024# include <winsock2.h> /* struct timeval */
Victor Stinner95e9cef2015-03-28 01:26:47 +010025#endif
26
Victor Stinner7b7c6dc2017-08-10 12:37:39 +020027#ifdef HAVE_SYS_WAIT_H
28#include <sys/wait.h> /* For W_STOPCODE */
29#endif
30
Victor Stinner5ed69952018-11-06 15:59:52 +010031#ifdef Py_BUILD_CORE
32# error "_testcapi must test the public Python C API, not CPython internal C API"
33#endif
34
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035static PyObject *TestError; /* set to exception object in init */
Tim Peters9ea17ac2001-02-02 05:57:15 +000036
Tim Peters91621db2001-06-12 20:10:01 +000037/* Raise TestError with test_name + ": " + msg, and return NULL. */
38
39static PyObject *
40raiseTestError(const char* test_name, const char* msg)
41{
Victor Stinner6ced7c42011-03-21 18:15:42 +010042 PyErr_Format(TestError, "%s: %s", test_name, msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 return NULL;
Tim Peters91621db2001-06-12 20:10:01 +000044}
45
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000046/* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
Tim Peters9ea17ac2001-02-02 05:57:15 +000047
48 The ones derived from autoconf on the UNIX-like OSes can be relied
49 upon (in the absence of sloppy cross-compiling), but the Windows
50 platforms have these hardcoded. Better safe than sorry.
51*/
52static PyObject*
Thomas Wouters89f507f2006-12-13 04:49:30 +000053sizeof_error(const char* fatname, const char* typname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 int expected, int got)
Tim Peters9ea17ac2001-02-02 05:57:15 +000055{
Victor Stinner499dfcf2011-03-21 13:26:24 +010056 PyErr_Format(TestError,
57 "%s #define == %d but sizeof(%s) == %d",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 fatname, expected, typname, got);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 return (PyObject*)NULL;
Tim Peters9ea17ac2001-02-02 05:57:15 +000060}
61
62static PyObject*
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +053063test_config(PyObject *self, PyObject *Py_UNUSED(ignored))
Tim Peters9ea17ac2001-02-02 05:57:15 +000064{
Tim Peters9ea17ac2001-02-02 05:57:15 +000065#define CHECK_SIZEOF(FATNAME, TYPE) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 if (FATNAME != sizeof(TYPE)) \
67 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
Tim Peters9ea17ac2001-02-02 05:57:15 +000068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 CHECK_SIZEOF(SIZEOF_SHORT, short);
70 CHECK_SIZEOF(SIZEOF_INT, int);
71 CHECK_SIZEOF(SIZEOF_LONG, long);
72 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
73 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
Benjamin Petersonaf580df2016-09-06 10:46:49 -070074 CHECK_SIZEOF(SIZEOF_LONG_LONG, long long);
Tim Peters9ea17ac2001-02-02 05:57:15 +000075
76#undef CHECK_SIZEOF
77
Serhiy Storchaka228b12e2017-01-23 09:47:21 +020078 Py_RETURN_NONE;
Tim Peters9ea17ac2001-02-02 05:57:15 +000079}
80
Tim Peters5c4d5bf2001-02-12 22:13:26 +000081static PyObject*
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +053082test_sizeof_c_types(PyObject *self, PyObject *Py_UNUSED(ignored))
Victor Stinner01076552013-10-29 19:39:52 +010083{
Ned Deilye37a1942015-03-05 15:47:10 -080084#if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
Serhiy Storchakab48af342015-02-26 15:27:57 +020085#pragma GCC diagnostic push
86#pragma GCC diagnostic ignored "-Wtype-limits"
87#endif
Victor Stinnerf866f972013-10-29 19:59:31 +010088#define CHECK_SIZEOF(TYPE, EXPECTED) \
Victor Stinner01076552013-10-29 19:39:52 +010089 if (EXPECTED != sizeof(TYPE)) { \
90 PyErr_Format(TestError, \
91 "sizeof(%s) = %u instead of %u", \
92 #TYPE, sizeof(TYPE), EXPECTED); \
93 return (PyObject*)NULL; \
94 }
Victor Stinnerf866f972013-10-29 19:59:31 +010095#define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0)
96#define CHECK_SIGNNESS(TYPE, SIGNED) \
97 if (IS_SIGNED(TYPE) != SIGNED) { \
98 PyErr_Format(TestError, \
99 "%s signness is, instead of %i", \
100 #TYPE, IS_SIGNED(TYPE), SIGNED); \
101 return (PyObject*)NULL; \
102 }
Victor Stinner01076552013-10-29 19:39:52 +0100103
104 /* integer types */
Victor Stinnerf866f972013-10-29 19:59:31 +0100105 CHECK_SIZEOF(Py_UCS1, 1);
106 CHECK_SIZEOF(Py_UCS2, 2);
107 CHECK_SIZEOF(Py_UCS4, 4);
108 CHECK_SIGNNESS(Py_UCS1, 0);
109 CHECK_SIGNNESS(Py_UCS2, 0);
110 CHECK_SIGNNESS(Py_UCS4, 0);
Benjamin Peterson9b3d7702016-09-06 13:24:00 -0700111 CHECK_SIZEOF(int32_t, 4);
112 CHECK_SIGNNESS(int32_t, 1);
113 CHECK_SIZEOF(uint32_t, 4);
114 CHECK_SIGNNESS(uint32_t, 0);
115 CHECK_SIZEOF(int64_t, 8);
116 CHECK_SIGNNESS(int64_t, 1);
117 CHECK_SIZEOF(uint64_t, 8);
118 CHECK_SIGNNESS(uint64_t, 0);
Victor Stinner01076552013-10-29 19:39:52 +0100119
120 /* pointer/size types */
Victor Stinnerf866f972013-10-29 19:59:31 +0100121 CHECK_SIZEOF(size_t, sizeof(void *));
122 CHECK_SIGNNESS(size_t, 0);
123 CHECK_SIZEOF(Py_ssize_t, sizeof(void *));
124 CHECK_SIGNNESS(Py_ssize_t, 1);
125
Benjamin Petersonca470632016-09-06 13:47:26 -0700126 CHECK_SIZEOF(uintptr_t, sizeof(void *));
127 CHECK_SIGNNESS(uintptr_t, 0);
128 CHECK_SIZEOF(intptr_t, sizeof(void *));
129 CHECK_SIGNNESS(intptr_t, 1);
Victor Stinner01076552013-10-29 19:39:52 +0100130
Serhiy Storchakad1302c02017-01-23 10:23:58 +0200131 Py_RETURN_NONE;
Victor Stinner01076552013-10-29 19:39:52 +0100132
Victor Stinnerf866f972013-10-29 19:59:31 +0100133#undef IS_SIGNED
134#undef CHECK_SIGNESS
Victor Stinner01076552013-10-29 19:39:52 +0100135#undef CHECK_SIZEOF
Ned Deilye37a1942015-03-05 15:47:10 -0800136#if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
Serhiy Storchakab48af342015-02-26 15:27:57 +0200137#pragma GCC diagnostic pop
138#endif
Victor Stinner01076552013-10-29 19:39:52 +0100139}
140
141
142static PyObject*
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530143test_list_api(PyObject *self, PyObject *Py_UNUSED(ignored))
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 PyObject* list;
146 int i;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 /* SF bug 132008: PyList_Reverse segfaults */
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000149#define NLIST 30
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 list = PyList_New(NLIST);
151 if (list == (PyObject*)NULL)
152 return (PyObject*)NULL;
153 /* list = range(NLIST) */
154 for (i = 0; i < NLIST; ++i) {
155 PyObject* anint = PyLong_FromLong(i);
156 if (anint == (PyObject*)NULL) {
157 Py_DECREF(list);
158 return (PyObject*)NULL;
159 }
160 PyList_SET_ITEM(list, i, anint);
161 }
162 /* list.reverse(), via PyList_Reverse() */
163 i = PyList_Reverse(list); /* should not blow up! */
164 if (i != 0) {
165 Py_DECREF(list);
166 return (PyObject*)NULL;
167 }
168 /* Check that list == range(29, -1, -1) now */
169 for (i = 0; i < NLIST; ++i) {
170 PyObject* anint = PyList_GET_ITEM(list, i);
171 if (PyLong_AS_LONG(anint) != NLIST-1-i) {
172 PyErr_SetString(TestError,
173 "test_list_api: reverse screwed up");
174 Py_DECREF(list);
175 return (PyObject*)NULL;
176 }
177 }
178 Py_DECREF(list);
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000179#undef NLIST
180
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200181 Py_RETURN_NONE;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000182}
183
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000184static int
185test_dict_inner(int count)
186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 Py_ssize_t pos = 0, iterations = 0;
188 int i;
189 PyObject *dict = PyDict_New();
190 PyObject *v, *k;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (dict == NULL)
193 return -1;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 for (i = 0; i < count; i++) {
196 v = PyLong_FromLong(i);
Christian Heimesff369a52013-07-26 14:52:18 +0200197 if (v == NULL) {
Christian Heimesff369a52013-07-26 14:52:18 +0200198 return -1;
199 }
Christian Heimes97cb67b2013-07-20 15:01:26 +0200200 if (PyDict_SetItem(dict, v, v) < 0) {
201 Py_DECREF(v);
202 return -1;
203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 Py_DECREF(v);
205 }
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 while (PyDict_Next(dict, &pos, &k, &v)) {
208 PyObject *o;
209 iterations++;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 i = PyLong_AS_LONG(v) + 1;
212 o = PyLong_FromLong(i);
213 if (o == NULL)
214 return -1;
215 if (PyDict_SetItem(dict, k, o) < 0) {
216 Py_DECREF(o);
217 return -1;
218 }
219 Py_DECREF(o);
220 }
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 Py_DECREF(dict);
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 if (iterations != count) {
225 PyErr_SetString(
226 TestError,
227 "test_dict_iteration: dict iteration went wrong ");
228 return -1;
229 } else {
230 return 0;
231 }
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000232}
233
234static PyObject*
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530235test_dict_iteration(PyObject* self, PyObject *Py_UNUSED(ignored))
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 int i;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 for (i = 0; i < 200; i++) {
240 if (test_dict_inner(i) < 0) {
241 return NULL;
242 }
243 }
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000244
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200245 Py_RETURN_NONE;
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000246}
247
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +0200248static PyObject*
249dict_getitem_knownhash(PyObject *self, PyObject *args)
250{
251 PyObject *mp, *key, *result;
252 Py_ssize_t hash;
253
254 if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash",
255 &mp, &key, &hash)) {
256 return NULL;
257 }
258
259 result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash);
260 if (result == NULL && !PyErr_Occurred()) {
261 _PyErr_SetKeyError(key);
262 return NULL;
263 }
264
265 Py_XINCREF(result);
266 return result;
267}
Tim Peters91621db2001-06-12 20:10:01 +0000268
Victor Stinner3d3f2642016-12-15 17:21:23 +0100269static PyObject*
270dict_hassplittable(PyObject *self, PyObject *arg)
271{
272 if (!PyDict_Check(arg)) {
273 PyErr_Format(PyExc_TypeError,
274 "dict_hassplittable() argument must be dict, not '%s'",
275 arg->ob_type->tp_name);
276 return NULL;
277 }
278
279 return PyBool_FromLong(_PyDict_HasSplitTable((PyDictObject*)arg));
280}
281
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000282/* Issue #4701: Check that PyObject_Hash implicitly calls
283 * PyType_Ready if it hasn't already been called
284 */
285static PyTypeObject _HashInheritanceTester_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 PyVarObject_HEAD_INIT(NULL, 0)
287 "hashinheritancetester", /* Name of this type */
288 sizeof(PyObject), /* Basic object size */
289 0, /* Item size for varobject */
290 (destructor)PyObject_Del, /* tp_dealloc */
291 0, /* tp_print */
292 0, /* tp_getattr */
293 0, /* tp_setattr */
294 0, /* tp_reserved */
295 0, /* tp_repr */
296 0, /* tp_as_number */
297 0, /* tp_as_sequence */
298 0, /* tp_as_mapping */
299 0, /* tp_hash */
300 0, /* tp_call */
301 0, /* tp_str */
302 PyObject_GenericGetAttr, /* tp_getattro */
303 0, /* tp_setattro */
304 0, /* tp_as_buffer */
305 Py_TPFLAGS_DEFAULT, /* tp_flags */
306 0, /* tp_doc */
307 0, /* tp_traverse */
308 0, /* tp_clear */
309 0, /* tp_richcompare */
310 0, /* tp_weaklistoffset */
311 0, /* tp_iter */
312 0, /* tp_iternext */
313 0, /* tp_methods */
314 0, /* tp_members */
315 0, /* tp_getset */
316 0, /* tp_base */
317 0, /* tp_dict */
318 0, /* tp_descr_get */
319 0, /* tp_descr_set */
320 0, /* tp_dictoffset */
321 0, /* tp_init */
322 0, /* tp_alloc */
323 PyType_GenericNew, /* tp_new */
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000324};
325
326static PyObject*
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530327test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 PyTypeObject *type;
330 PyObject *obj;
Antoine Pitrou29aad002010-10-23 19:42:38 +0000331 Py_hash_t hash;
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 type = &_HashInheritanceTester_Type;
Benjamin Petersonf1726372009-05-05 21:11:54 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 if (type->tp_dict != NULL)
336 /* The type has already been initialized. This probably means
337 -R is being used. */
338 Py_RETURN_NONE;
Benjamin Petersonf1726372009-05-05 21:11:54 +0000339
340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 obj = PyObject_New(PyObject, type);
342 if (obj == NULL) {
343 PyErr_Clear();
344 PyErr_SetString(
345 TestError,
346 "test_lazy_hash_inheritance: failed to create object");
347 return NULL;
348 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 if (type->tp_dict != NULL) {
351 PyErr_SetString(
352 TestError,
353 "test_lazy_hash_inheritance: type initialised too soon");
354 Py_DECREF(obj);
355 return NULL;
356 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 hash = PyObject_Hash(obj);
359 if ((hash == -1) && PyErr_Occurred()) {
360 PyErr_Clear();
361 PyErr_SetString(
362 TestError,
363 "test_lazy_hash_inheritance: could not hash object");
364 Py_DECREF(obj);
365 return NULL;
366 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (type->tp_dict == NULL) {
369 PyErr_SetString(
370 TestError,
371 "test_lazy_hash_inheritance: type not initialised by hash()");
372 Py_DECREF(obj);
373 return NULL;
374 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 if (type->tp_hash != PyType_Type.tp_hash) {
377 PyErr_SetString(
378 TestError,
379 "test_lazy_hash_inheritance: unexpected hash function");
380 Py_DECREF(obj);
381 return NULL;
382 }
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 Py_DECREF(obj);
Benjamin Petersonf1726372009-05-05 21:11:54 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 Py_RETURN_NONE;
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000387}
388
389
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700390/* Tests of PyLong_{As, From}{Unsigned,}Long(), and
Tim Petersff70d3c2001-06-14 01:11:03 +0000391 PyLong_{As, From}{Unsigned,}LongLong().
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000392
393 Note that the meat of the test is contained in testcapi_long.h.
394 This is revolting, but delicate code duplication is worse: "almost
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700395 exactly the same" code is needed to test long long, but the ubiquitous
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000396 dependence on type names makes it impossible to use a parameterized
397 function. A giant macro would be even worse than this. A C++ template
398 would be perfect.
399
400 The "report an error" functions are deliberately not part of the #include
401 file: if the test fails, you can set a breakpoint in the appropriate
402 error function directly, and crawl back from there in the debugger.
403*/
404
405#define UNBIND(X) Py_DECREF(X); (X) = NULL
406
407static PyObject *
408raise_test_long_error(const char* msg)
409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 return raiseTestError("test_long_api", msg);
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000411}
412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413#define TESTNAME test_long_api_inner
414#define TYPENAME long
415#define F_S_TO_PY PyLong_FromLong
416#define F_PY_TO_S PyLong_AsLong
417#define F_U_TO_PY PyLong_FromUnsignedLong
418#define F_PY_TO_U PyLong_AsUnsignedLong
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000419
420#include "testcapi_long.h"
421
422static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530423test_long_api(PyObject* self, PyObject *Py_UNUSED(ignored))
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 return TESTNAME(raise_test_long_error);
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000426}
427
428#undef TESTNAME
429#undef TYPENAME
430#undef F_S_TO_PY
431#undef F_PY_TO_S
432#undef F_U_TO_PY
433#undef F_PY_TO_U
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000434
Tim Peters91621db2001-06-12 20:10:01 +0000435static PyObject *
Tim Petersd1a7da62001-06-13 00:35:57 +0000436raise_test_longlong_error(const char* msg)
437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 return raiseTestError("test_longlong_api", msg);
Tim Petersd1a7da62001-06-13 00:35:57 +0000439}
440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441#define TESTNAME test_longlong_api_inner
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700442#define TYPENAME long long
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443#define F_S_TO_PY PyLong_FromLongLong
444#define F_PY_TO_S PyLong_AsLongLong
445#define F_U_TO_PY PyLong_FromUnsignedLongLong
446#define F_PY_TO_U PyLong_AsUnsignedLongLong
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000447
448#include "testcapi_long.h"
Tim Petersd1a7da62001-06-13 00:35:57 +0000449
450static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451test_longlong_api(PyObject* self, PyObject *args)
Tim Peters91621db2001-06-12 20:10:01 +0000452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 return TESTNAME(raise_test_longlong_error);
Tim Peters91621db2001-06-12 20:10:01 +0000454}
455
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000456#undef TESTNAME
457#undef TYPENAME
458#undef F_S_TO_PY
459#undef F_PY_TO_S
460#undef F_U_TO_PY
461#undef F_PY_TO_U
Tim Peterse7c1f9b2001-06-14 00:55:41 +0000462
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000463/* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG
464 is tested by test_long_api_inner. This test will concentrate on proper
465 handling of overflow.
466*/
467
468static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530469test_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 PyObject *num, *one, *temp;
472 long value;
473 int overflow;
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 /* Test that overflow is set properly for a large value. */
476 /* num is a number larger than LONG_MAX even on 64-bit platforms */
477 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
478 if (num == NULL)
479 return NULL;
480 overflow = 1234;
481 value = PyLong_AsLongAndOverflow(num, &overflow);
482 Py_DECREF(num);
483 if (value == -1 && PyErr_Occurred())
484 return NULL;
485 if (value != -1)
486 return raiseTestError("test_long_and_overflow",
487 "return value was not set to -1");
488 if (overflow != 1)
489 return raiseTestError("test_long_and_overflow",
490 "overflow was not set to 1");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 /* Same again, with num = LONG_MAX + 1 */
493 num = PyLong_FromLong(LONG_MAX);
494 if (num == NULL)
495 return NULL;
496 one = PyLong_FromLong(1L);
497 if (one == NULL) {
498 Py_DECREF(num);
499 return NULL;
500 }
501 temp = PyNumber_Add(num, one);
502 Py_DECREF(one);
503 Py_DECREF(num);
504 num = temp;
505 if (num == NULL)
506 return NULL;
507 overflow = 0;
508 value = PyLong_AsLongAndOverflow(num, &overflow);
509 Py_DECREF(num);
510 if (value == -1 && PyErr_Occurred())
511 return NULL;
512 if (value != -1)
513 return raiseTestError("test_long_and_overflow",
514 "return value was not set to -1");
515 if (overflow != 1)
516 return raiseTestError("test_long_and_overflow",
517 "overflow was not set to 1");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 /* Test that overflow is set properly for a large negative value. */
520 /* num is a number smaller than LONG_MIN even on 64-bit platforms */
521 num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
522 if (num == NULL)
523 return NULL;
524 overflow = 1234;
525 value = PyLong_AsLongAndOverflow(num, &overflow);
526 Py_DECREF(num);
527 if (value == -1 && PyErr_Occurred())
528 return NULL;
529 if (value != -1)
530 return raiseTestError("test_long_and_overflow",
531 "return value was not set to -1");
532 if (overflow != -1)
533 return raiseTestError("test_long_and_overflow",
534 "overflow was not set to -1");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 /* Same again, with num = LONG_MIN - 1 */
537 num = PyLong_FromLong(LONG_MIN);
538 if (num == NULL)
539 return NULL;
540 one = PyLong_FromLong(1L);
541 if (one == NULL) {
542 Py_DECREF(num);
543 return NULL;
544 }
545 temp = PyNumber_Subtract(num, one);
546 Py_DECREF(one);
547 Py_DECREF(num);
548 num = temp;
549 if (num == NULL)
550 return NULL;
551 overflow = 0;
552 value = PyLong_AsLongAndOverflow(num, &overflow);
553 Py_DECREF(num);
554 if (value == -1 && PyErr_Occurred())
555 return NULL;
556 if (value != -1)
557 return raiseTestError("test_long_and_overflow",
558 "return value was not set to -1");
559 if (overflow != -1)
560 return raiseTestError("test_long_and_overflow",
561 "overflow was not set to -1");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 /* Test that overflow is cleared properly for small values. */
564 num = PyLong_FromString("FF", NULL, 16);
565 if (num == NULL)
566 return NULL;
567 overflow = 1234;
568 value = PyLong_AsLongAndOverflow(num, &overflow);
569 Py_DECREF(num);
570 if (value == -1 && PyErr_Occurred())
571 return NULL;
572 if (value != 0xFF)
573 return raiseTestError("test_long_and_overflow",
574 "expected return value 0xFF");
575 if (overflow != 0)
576 return raiseTestError("test_long_and_overflow",
577 "overflow was not cleared");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 num = PyLong_FromString("-FF", NULL, 16);
580 if (num == NULL)
581 return NULL;
582 overflow = 0;
583 value = PyLong_AsLongAndOverflow(num, &overflow);
584 Py_DECREF(num);
585 if (value == -1 && PyErr_Occurred())
586 return NULL;
587 if (value != -0xFF)
588 return raiseTestError("test_long_and_overflow",
589 "expected return value 0xFF");
590 if (overflow != 0)
591 return raiseTestError("test_long_and_overflow",
592 "overflow was set incorrectly");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 num = PyLong_FromLong(LONG_MAX);
595 if (num == NULL)
596 return NULL;
597 overflow = 1234;
598 value = PyLong_AsLongAndOverflow(num, &overflow);
599 Py_DECREF(num);
600 if (value == -1 && PyErr_Occurred())
601 return NULL;
602 if (value != LONG_MAX)
603 return raiseTestError("test_long_and_overflow",
604 "expected return value LONG_MAX");
605 if (overflow != 0)
606 return raiseTestError("test_long_and_overflow",
607 "overflow was not cleared");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 num = PyLong_FromLong(LONG_MIN);
610 if (num == NULL)
611 return NULL;
612 overflow = 0;
613 value = PyLong_AsLongAndOverflow(num, &overflow);
614 Py_DECREF(num);
615 if (value == -1 && PyErr_Occurred())
616 return NULL;
617 if (value != LONG_MIN)
618 return raiseTestError("test_long_and_overflow",
619 "expected return value LONG_MIN");
620 if (overflow != 0)
621 return raiseTestError("test_long_and_overflow",
622 "overflow was not cleared");
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000623
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200624 Py_RETURN_NONE;
Mark Dickinson309aa2d2009-12-21 12:37:06 +0000625}
626
Mark Dickinson93f562c2010-01-30 10:30:15 +0000627/* Test the PyLong_AsLongLongAndOverflow API. General conversion to
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700628 long long is tested by test_long_api_inner. This test will
Mark Dickinson93f562c2010-01-30 10:30:15 +0000629 concentrate on proper handling of overflow.
630*/
631
632static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530633test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
Mark Dickinson93f562c2010-01-30 10:30:15 +0000634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 PyObject *num, *one, *temp;
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700636 long long value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 int overflow;
Mark Dickinson93f562c2010-01-30 10:30:15 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 /* Test that overflow is set properly for a large value. */
640 /* num is a number larger than PY_LLONG_MAX on a typical machine. */
641 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
642 if (num == NULL)
643 return NULL;
644 overflow = 1234;
645 value = PyLong_AsLongLongAndOverflow(num, &overflow);
646 Py_DECREF(num);
647 if (value == -1 && PyErr_Occurred())
648 return NULL;
649 if (value != -1)
650 return raiseTestError("test_long_long_and_overflow",
651 "return value was not set to -1");
652 if (overflow != 1)
653 return raiseTestError("test_long_long_and_overflow",
654 "overflow was not set to 1");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 /* Same again, with num = PY_LLONG_MAX + 1 */
657 num = PyLong_FromLongLong(PY_LLONG_MAX);
658 if (num == NULL)
659 return NULL;
660 one = PyLong_FromLong(1L);
661 if (one == NULL) {
662 Py_DECREF(num);
663 return NULL;
664 }
665 temp = PyNumber_Add(num, one);
666 Py_DECREF(one);
667 Py_DECREF(num);
668 num = temp;
669 if (num == NULL)
670 return NULL;
671 overflow = 0;
672 value = PyLong_AsLongLongAndOverflow(num, &overflow);
673 Py_DECREF(num);
674 if (value == -1 && PyErr_Occurred())
675 return NULL;
676 if (value != -1)
677 return raiseTestError("test_long_long_and_overflow",
678 "return value was not set to -1");
679 if (overflow != 1)
680 return raiseTestError("test_long_long_and_overflow",
681 "overflow was not set to 1");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 /* Test that overflow is set properly for a large negative value. */
684 /* num is a number smaller than PY_LLONG_MIN on a typical platform */
685 num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
686 if (num == NULL)
687 return NULL;
688 overflow = 1234;
689 value = PyLong_AsLongLongAndOverflow(num, &overflow);
690 Py_DECREF(num);
691 if (value == -1 && PyErr_Occurred())
692 return NULL;
693 if (value != -1)
694 return raiseTestError("test_long_long_and_overflow",
695 "return value was not set to -1");
696 if (overflow != -1)
697 return raiseTestError("test_long_long_and_overflow",
698 "overflow was not set to -1");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 /* Same again, with num = PY_LLONG_MIN - 1 */
701 num = PyLong_FromLongLong(PY_LLONG_MIN);
702 if (num == NULL)
703 return NULL;
704 one = PyLong_FromLong(1L);
705 if (one == NULL) {
706 Py_DECREF(num);
707 return NULL;
708 }
709 temp = PyNumber_Subtract(num, one);
710 Py_DECREF(one);
711 Py_DECREF(num);
712 num = temp;
713 if (num == NULL)
714 return NULL;
715 overflow = 0;
716 value = PyLong_AsLongLongAndOverflow(num, &overflow);
717 Py_DECREF(num);
718 if (value == -1 && PyErr_Occurred())
719 return NULL;
720 if (value != -1)
721 return raiseTestError("test_long_long_and_overflow",
722 "return value was not set to -1");
723 if (overflow != -1)
724 return raiseTestError("test_long_long_and_overflow",
725 "overflow was not set to -1");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 /* Test that overflow is cleared properly for small values. */
728 num = PyLong_FromString("FF", NULL, 16);
729 if (num == NULL)
730 return NULL;
731 overflow = 1234;
732 value = PyLong_AsLongLongAndOverflow(num, &overflow);
733 Py_DECREF(num);
734 if (value == -1 && PyErr_Occurred())
735 return NULL;
736 if (value != 0xFF)
737 return raiseTestError("test_long_long_and_overflow",
738 "expected return value 0xFF");
739 if (overflow != 0)
740 return raiseTestError("test_long_long_and_overflow",
741 "overflow was not cleared");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 num = PyLong_FromString("-FF", NULL, 16);
744 if (num == NULL)
745 return NULL;
746 overflow = 0;
747 value = PyLong_AsLongLongAndOverflow(num, &overflow);
748 Py_DECREF(num);
749 if (value == -1 && PyErr_Occurred())
750 return NULL;
751 if (value != -0xFF)
752 return raiseTestError("test_long_long_and_overflow",
753 "expected return value 0xFF");
754 if (overflow != 0)
755 return raiseTestError("test_long_long_and_overflow",
756 "overflow was set incorrectly");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 num = PyLong_FromLongLong(PY_LLONG_MAX);
759 if (num == NULL)
760 return NULL;
761 overflow = 1234;
762 value = PyLong_AsLongLongAndOverflow(num, &overflow);
763 Py_DECREF(num);
764 if (value == -1 && PyErr_Occurred())
765 return NULL;
766 if (value != PY_LLONG_MAX)
767 return raiseTestError("test_long_long_and_overflow",
768 "expected return value PY_LLONG_MAX");
769 if (overflow != 0)
770 return raiseTestError("test_long_long_and_overflow",
771 "overflow was not cleared");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 num = PyLong_FromLongLong(PY_LLONG_MIN);
774 if (num == NULL)
775 return NULL;
776 overflow = 0;
777 value = PyLong_AsLongLongAndOverflow(num, &overflow);
778 Py_DECREF(num);
779 if (value == -1 && PyErr_Occurred())
780 return NULL;
781 if (value != PY_LLONG_MIN)
782 return raiseTestError("test_long_long_and_overflow",
783 "expected return value PY_LLONG_MIN");
784 if (overflow != 0)
785 return raiseTestError("test_long_long_and_overflow",
786 "overflow was not cleared");
Mark Dickinson93f562c2010-01-30 10:30:15 +0000787
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200788 Py_RETURN_NONE;
Mark Dickinson93f562c2010-01-30 10:30:15 +0000789}
790
Nadeem Vawda3d5881e2011-09-07 21:40:26 +0200791/* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that
792 non-integer arguments are handled correctly. It should be extended to
793 test overflow handling.
794 */
795
796static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530797test_long_as_size_t(PyObject *self, PyObject *Py_UNUSED(ignored))
Nadeem Vawda3d5881e2011-09-07 21:40:26 +0200798{
799 size_t out_u;
800 Py_ssize_t out_s;
801
802 Py_INCREF(Py_None);
803
804 out_u = PyLong_AsSize_t(Py_None);
805 if (out_u != (size_t)-1 || !PyErr_Occurred())
806 return raiseTestError("test_long_as_size_t",
807 "PyLong_AsSize_t(None) didn't complain");
808 if (!PyErr_ExceptionMatches(PyExc_TypeError))
809 return raiseTestError("test_long_as_size_t",
810 "PyLong_AsSize_t(None) raised "
811 "something other than TypeError");
812 PyErr_Clear();
813
814 out_s = PyLong_AsSsize_t(Py_None);
815 if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred())
816 return raiseTestError("test_long_as_size_t",
817 "PyLong_AsSsize_t(None) didn't complain");
818 if (!PyErr_ExceptionMatches(PyExc_TypeError))
819 return raiseTestError("test_long_as_size_t",
820 "PyLong_AsSsize_t(None) raised "
821 "something other than TypeError");
822 PyErr_Clear();
823
824 /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
825 return Py_None;
826}
827
828/* Test the PyLong_AsDouble API. At present this just tests that
829 non-integer arguments are handled correctly.
830 */
831
832static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530833test_long_as_double(PyObject *self, PyObject *Py_UNUSED(ignored))
Nadeem Vawda3d5881e2011-09-07 21:40:26 +0200834{
835 double out;
836
837 Py_INCREF(Py_None);
838
839 out = PyLong_AsDouble(Py_None);
840 if (out != -1.0 || !PyErr_Occurred())
841 return raiseTestError("test_long_as_double",
842 "PyLong_AsDouble(None) didn't complain");
843 if (!PyErr_ExceptionMatches(PyExc_TypeError))
844 return raiseTestError("test_long_as_double",
845 "PyLong_AsDouble(None) raised "
846 "something other than TypeError");
847 PyErr_Clear();
848
849 /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
850 return Py_None;
851}
852
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700853/* Test the L code for PyArg_ParseTuple. This should deliver a long long
Tim Petersd38b1c72001-09-30 05:09:37 +0000854 for both long and int arguments. The test may leak a little memory if
855 it fails.
856*/
857static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530858test_L_code(PyObject *self, PyObject *Py_UNUSED(ignored))
Tim Petersd38b1c72001-09-30 05:09:37 +0000859{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 PyObject *tuple, *num;
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700861 long long value;
Tim Petersd38b1c72001-09-30 05:09:37 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 tuple = PyTuple_New(1);
864 if (tuple == NULL)
865 return NULL;
Tim Petersd38b1c72001-09-30 05:09:37 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 num = PyLong_FromLong(42);
868 if (num == NULL)
869 return NULL;
Tim Petersd38b1c72001-09-30 05:09:37 +0000870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 PyTuple_SET_ITEM(tuple, 0, num);
Tim Petersd38b1c72001-09-30 05:09:37 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 value = -1;
Oren Milmanba7d7362017-08-29 11:58:27 +0300874 if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +0300876 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 if (value != 42)
878 return raiseTestError("test_L_code",
879 "L code returned wrong value for long 42");
Tim Petersd38b1c72001-09-30 05:09:37 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 Py_DECREF(num);
882 num = PyLong_FromLong(42);
883 if (num == NULL)
884 return NULL;
Tim Petersd38b1c72001-09-30 05:09:37 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 PyTuple_SET_ITEM(tuple, 0, num);
Tim Petersd38b1c72001-09-30 05:09:37 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 value = -1;
Oren Milmanba7d7362017-08-29 11:58:27 +0300889 if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +0300891 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 if (value != 42)
893 return raiseTestError("test_L_code",
894 "L code returned wrong value for int 42");
Tim Petersd38b1c72001-09-30 05:09:37 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 Py_DECREF(tuple);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200897 Py_RETURN_NONE;
Tim Petersd38b1c72001-09-30 05:09:37 +0000898}
899
Serhiy Storchakace412872016-05-08 23:36:44 +0300900static PyObject *
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300901return_none(void *unused)
902{
903 Py_RETURN_NONE;
904}
905
906static PyObject *
907raise_error(void *unused)
908{
909 PyErr_SetNone(PyExc_ValueError);
910 return NULL;
911}
912
913static int
914test_buildvalue_N_error(const char *fmt)
915{
916 PyObject *arg, *res;
917
918 arg = PyList_New(0);
919 if (arg == NULL) {
920 return -1;
921 }
922
923 Py_INCREF(arg);
924 res = Py_BuildValue(fmt, return_none, NULL, arg);
925 if (res == NULL) {
926 return -1;
927 }
928 Py_DECREF(res);
929 if (Py_REFCNT(arg) != 1) {
930 PyErr_Format(TestError, "test_buildvalue_N: "
931 "arg was not decrefed in successful "
932 "Py_BuildValue(\"%s\")", fmt);
933 return -1;
934 }
935
936 Py_INCREF(arg);
937 res = Py_BuildValue(fmt, raise_error, NULL, arg);
938 if (res != NULL || !PyErr_Occurred()) {
939 PyErr_Format(TestError, "test_buildvalue_N: "
940 "Py_BuildValue(\"%s\") didn't complain", fmt);
941 return -1;
942 }
943 PyErr_Clear();
944 if (Py_REFCNT(arg) != 1) {
945 PyErr_Format(TestError, "test_buildvalue_N: "
946 "arg was not decrefed in failed "
947 "Py_BuildValue(\"%s\")", fmt);
948 return -1;
949 }
950 Py_DECREF(arg);
951 return 0;
952}
953
954static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +0200955test_buildvalue_N(PyObject *self, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300956{
957 PyObject *arg, *res;
958
959 arg = PyList_New(0);
960 if (arg == NULL) {
961 return NULL;
962 }
963 Py_INCREF(arg);
964 res = Py_BuildValue("N", arg);
965 if (res == NULL) {
966 return NULL;
967 }
968 if (res != arg) {
969 return raiseTestError("test_buildvalue_N",
970 "Py_BuildValue(\"N\") returned wrong result");
971 }
972 if (Py_REFCNT(arg) != 2) {
973 return raiseTestError("test_buildvalue_N",
974 "arg was not decrefed in Py_BuildValue(\"N\")");
975 }
976 Py_DECREF(res);
977 Py_DECREF(arg);
978
979 if (test_buildvalue_N_error("O&N") < 0)
980 return NULL;
981 if (test_buildvalue_N_error("(O&N)") < 0)
982 return NULL;
983 if (test_buildvalue_N_error("[O&N]") < 0)
984 return NULL;
985 if (test_buildvalue_N_error("{O&N}") < 0)
986 return NULL;
987 if (test_buildvalue_N_error("{()O&(())N}") < 0)
988 return NULL;
989
990 Py_RETURN_NONE;
991}
992
993
994static PyObject *
Serhiy Storchakace412872016-05-08 23:36:44 +0300995get_args(PyObject *self, PyObject *args)
996{
997 if (args == NULL) {
998 args = Py_None;
999 }
1000 Py_INCREF(args);
1001 return args;
1002}
1003
1004static PyObject *
1005get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs)
1006{
1007 if (kwargs == NULL) {
1008 kwargs = Py_None;
1009 }
1010 Py_INCREF(kwargs);
1011 return kwargs;
1012}
1013
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001014/* Test tuple argument processing */
1015static PyObject *
1016getargs_tuple(PyObject *self, PyObject *args)
1017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 int a, b, c;
1019 if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
1020 return NULL;
1021 return Py_BuildValue("iii", a, b, c);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001022}
1023
Christian Heimes380f7f22008-02-28 11:19:05 +00001024/* test PyArg_ParseTupleAndKeywords */
Larry Hastings83a9f482012-03-20 20:06:16 +00001025static PyObject *
1026getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
Christian Heimes380f7f22008-02-28 11:19:05 +00001027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001029 static const char fmt[] = "(ii)i|(i(ii))(iii)i";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
Christian Heimes380f7f22008-02-28 11:19:05 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
1033 &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
1034 &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
1035 return NULL;
1036 return Py_BuildValue("iiiiiiiiii",
1037 int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
1038 int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
Christian Heimes380f7f22008-02-28 11:19:05 +00001039}
1040
Larry Hastings83a9f482012-03-20 20:06:16 +00001041/* test PyArg_ParseTupleAndKeywords keyword-only arguments */
1042static PyObject *
1043getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs)
1044{
1045 static char *keywords[] = {"required", "optional", "keyword_only", NULL};
1046 int required = -1;
1047 int optional = -1;
1048 int keyword_only = -1;
1049
1050 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords,
1051 &required, &optional, &keyword_only))
1052 return NULL;
1053 return Py_BuildValue("iii", required, optional, keyword_only);
1054}
1055
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001056/* test PyArg_ParseTupleAndKeywords positional-only arguments */
1057static PyObject *
1058getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
1059{
1060 static char *keywords[] = {"", "", "keyword", NULL};
1061 int required = -1;
1062 int optional = -1;
1063 int keyword = -1;
1064
1065 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords,
1066 &required, &optional, &keyword))
1067 return NULL;
1068 return Py_BuildValue("iii", required, optional, keyword);
1069}
1070
Thomas Heller3457e4b2003-04-24 16:14:27 +00001071/* Functions to call PyArg_ParseTuple with integer format codes,
1072 and return the result.
1073*/
Thomas Hellera4ea6032003-04-17 18:55:45 +00001074static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +00001075getargs_b(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +00001076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 unsigned char value;
1078 if (!PyArg_ParseTuple(args, "b", &value))
1079 return NULL;
1080 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001081}
1082
Thomas Heller3457e4b2003-04-24 16:14:27 +00001083static PyObject *
1084getargs_B(PyObject *self, PyObject *args)
1085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 unsigned char value;
1087 if (!PyArg_ParseTuple(args, "B", &value))
1088 return NULL;
1089 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +00001090}
1091
1092static PyObject *
Mark Dickinson1554b182009-12-20 16:03:30 +00001093getargs_h(PyObject *self, PyObject *args)
1094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 short value;
1096 if (!PyArg_ParseTuple(args, "h", &value))
1097 return NULL;
1098 return PyLong_FromLong((long)value);
Mark Dickinson1554b182009-12-20 16:03:30 +00001099}
1100
1101static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +00001102getargs_H(PyObject *self, PyObject *args)
1103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 unsigned short value;
1105 if (!PyArg_ParseTuple(args, "H", &value))
1106 return NULL;
1107 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +00001108}
1109
1110static PyObject *
1111getargs_I(PyObject *self, PyObject *args)
1112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 unsigned int value;
1114 if (!PyArg_ParseTuple(args, "I", &value))
1115 return NULL;
1116 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +00001117}
1118
1119static PyObject *
1120getargs_k(PyObject *self, PyObject *args)
1121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 unsigned long value;
1123 if (!PyArg_ParseTuple(args, "k", &value))
1124 return NULL;
1125 return PyLong_FromUnsignedLong(value);
Thomas Heller3457e4b2003-04-24 16:14:27 +00001126}
1127
1128static PyObject *
1129getargs_i(PyObject *self, PyObject *args)
1130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 int value;
1132 if (!PyArg_ParseTuple(args, "i", &value))
1133 return NULL;
1134 return PyLong_FromLong((long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +00001135}
1136
Thomas Hellera4ea6032003-04-17 18:55:45 +00001137static PyObject *
1138getargs_l(PyObject *self, PyObject *args)
1139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 long value;
1141 if (!PyArg_ParseTuple(args, "l", &value))
1142 return NULL;
1143 return PyLong_FromLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001144}
1145
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001146static PyObject *
1147getargs_n(PyObject *self, PyObject *args)
1148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 Py_ssize_t value;
1150 if (!PyArg_ParseTuple(args, "n", &value))
1151 return NULL;
1152 return PyLong_FromSsize_t(value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001153}
1154
Larry Hastingsfaf91e72012-05-05 16:54:29 -07001155static PyObject *
1156getargs_p(PyObject *self, PyObject *args)
1157{
1158 int value;
1159 if (!PyArg_ParseTuple(args, "p", &value))
1160 return NULL;
1161 return PyLong_FromLong(value);
1162}
1163
Thomas Hellera4ea6032003-04-17 18:55:45 +00001164static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +00001165getargs_L(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +00001166{
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001167 long long value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 if (!PyArg_ParseTuple(args, "L", &value))
1169 return NULL;
1170 return PyLong_FromLongLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001171}
1172
Thomas Hellera4ea6032003-04-17 18:55:45 +00001173static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +00001174getargs_K(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +00001175{
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001176 unsigned long long value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 if (!PyArg_ParseTuple(args, "K", &value))
1178 return NULL;
1179 return PyLong_FromUnsignedLongLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001180}
Thomas Hellera4ea6032003-04-17 18:55:45 +00001181
1182/* This function not only tests the 'k' getargs code, but also the
orenmn698845e2017-03-02 13:29:20 +02001183 PyLong_AsUnsignedLongMask() function. */
Thomas Hellera4ea6032003-04-17 18:55:45 +00001184static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301185test_k_code(PyObject *self, PyObject *Py_UNUSED(ignored))
Thomas Hellera4ea6032003-04-17 18:55:45 +00001186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 PyObject *tuple, *num;
1188 unsigned long value;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 tuple = PyTuple_New(1);
1191 if (tuple == NULL)
1192 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 /* a number larger than ULONG_MAX even on 64-bit platforms */
1195 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
1196 if (num == NULL)
1197 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 value = PyLong_AsUnsignedLongMask(num);
1200 if (value != ULONG_MAX)
1201 return raiseTestError("test_k_code",
Georg Brandl4b5b0622016-01-18 08:00:15 +01001202 "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
Thomas Hellera4ea6032003-04-17 18:55:45 +00001203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 PyTuple_SET_ITEM(tuple, 0, num);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 value = 0;
Oren Milmanba7d7362017-08-29 11:58:27 +03001207 if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03001209 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 if (value != ULONG_MAX)
1211 return raiseTestError("test_k_code",
1212 "k code returned wrong value for long 0xFFF...FFF");
Thomas Hellera4ea6032003-04-17 18:55:45 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 Py_DECREF(num);
1215 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
1216 if (num == NULL)
1217 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 value = PyLong_AsUnsignedLongMask(num);
1220 if (value != (unsigned long)-0x42)
1221 return raiseTestError("test_k_code",
orenmn698845e2017-03-02 13:29:20 +02001222 "PyLong_AsUnsignedLongMask() returned wrong "
1223 "value for long -0xFFF..000042");
Thomas Hellera4ea6032003-04-17 18:55:45 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 PyTuple_SET_ITEM(tuple, 0, num);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 value = 0;
Oren Milmanba7d7362017-08-29 11:58:27 +03001228 if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03001230 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 if (value != (unsigned long)-0x42)
1232 return raiseTestError("test_k_code",
1233 "k code returned wrong value for long -0xFFF..000042");
Thomas Hellera4ea6032003-04-17 18:55:45 +00001234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 Py_DECREF(tuple);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001236 Py_RETURN_NONE;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001237}
1238
Victor Stinner06e49dd2010-06-13 18:21:50 +00001239static PyObject *
Serhiy Storchakaf95455d2016-05-16 10:11:47 +03001240getargs_f(PyObject *self, PyObject *args)
1241{
1242 float f;
1243 if (!PyArg_ParseTuple(args, "f", &f))
1244 return NULL;
1245 return PyFloat_FromDouble(f);
1246}
1247
1248static PyObject *
1249getargs_d(PyObject *self, PyObject *args)
1250{
1251 double d;
1252 if (!PyArg_ParseTuple(args, "d", &d))
1253 return NULL;
1254 return PyFloat_FromDouble(d);
1255}
1256
1257static PyObject *
1258getargs_D(PyObject *self, PyObject *args)
1259{
1260 Py_complex cval;
1261 if (!PyArg_ParseTuple(args, "D", &cval))
1262 return NULL;
1263 return PyComplex_FromCComplex(cval);
1264}
1265
1266static PyObject *
1267getargs_S(PyObject *self, PyObject *args)
1268{
1269 PyObject *obj;
1270 if (!PyArg_ParseTuple(args, "S", &obj))
1271 return NULL;
1272 Py_INCREF(obj);
1273 return obj;
1274}
1275
1276static PyObject *
1277getargs_Y(PyObject *self, PyObject *args)
1278{
1279 PyObject *obj;
1280 if (!PyArg_ParseTuple(args, "Y", &obj))
1281 return NULL;
1282 Py_INCREF(obj);
1283 return obj;
1284}
1285
1286static PyObject *
1287getargs_U(PyObject *self, PyObject *args)
1288{
1289 PyObject *obj;
1290 if (!PyArg_ParseTuple(args, "U", &obj))
1291 return NULL;
1292 Py_INCREF(obj);
1293 return obj;
1294}
1295
1296static PyObject *
Eli Bendersky906b88f2011-07-29 07:05:08 +03001297getargs_c(PyObject *self, PyObject *args)
1298{
1299 char c;
1300 if (!PyArg_ParseTuple(args, "c", &c))
1301 return NULL;
Serhiy Storchakac8241fd2016-01-28 19:49:54 +02001302 return PyLong_FromLong((unsigned char)c);
1303}
1304
1305static PyObject *
1306getargs_C(PyObject *self, PyObject *args)
1307{
1308 int c;
1309 if (!PyArg_ParseTuple(args, "C", &c))
1310 return NULL;
1311 return PyLong_FromLong(c);
Eli Bendersky906b88f2011-07-29 07:05:08 +03001312}
1313
1314static PyObject *
Victor Stinner06e49dd2010-06-13 18:21:50 +00001315getargs_s(PyObject *self, PyObject *args)
1316{
1317 char *str;
1318 if (!PyArg_ParseTuple(args, "s", &str))
1319 return NULL;
1320 return PyBytes_FromString(str);
1321}
1322
1323static PyObject *
1324getargs_s_star(PyObject *self, PyObject *args)
1325{
1326 Py_buffer buffer;
1327 PyObject *bytes;
1328 if (!PyArg_ParseTuple(args, "s*", &buffer))
1329 return NULL;
1330 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1331 PyBuffer_Release(&buffer);
1332 return bytes;
1333}
1334
1335static PyObject *
1336getargs_s_hash(PyObject *self, PyObject *args)
1337{
1338 char *str;
1339 Py_ssize_t size;
1340 if (!PyArg_ParseTuple(args, "s#", &str, &size))
1341 return NULL;
1342 return PyBytes_FromStringAndSize(str, size);
1343}
1344
1345static PyObject *
1346getargs_z(PyObject *self, PyObject *args)
1347{
1348 char *str;
1349 if (!PyArg_ParseTuple(args, "z", &str))
1350 return NULL;
1351 if (str != NULL)
1352 return PyBytes_FromString(str);
1353 else
1354 Py_RETURN_NONE;
1355}
1356
1357static PyObject *
1358getargs_z_star(PyObject *self, PyObject *args)
1359{
1360 Py_buffer buffer;
1361 PyObject *bytes;
1362 if (!PyArg_ParseTuple(args, "z*", &buffer))
1363 return NULL;
1364 if (buffer.buf != NULL)
1365 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1366 else {
1367 Py_INCREF(Py_None);
1368 bytes = Py_None;
1369 }
1370 PyBuffer_Release(&buffer);
1371 return bytes;
1372}
1373
1374static PyObject *
1375getargs_z_hash(PyObject *self, PyObject *args)
1376{
1377 char *str;
1378 Py_ssize_t size;
1379 if (!PyArg_ParseTuple(args, "z#", &str, &size))
1380 return NULL;
1381 if (str != NULL)
1382 return PyBytes_FromStringAndSize(str, size);
1383 else
1384 Py_RETURN_NONE;
1385}
1386
1387static PyObject *
1388getargs_y(PyObject *self, PyObject *args)
1389{
1390 char *str;
1391 if (!PyArg_ParseTuple(args, "y", &str))
1392 return NULL;
1393 return PyBytes_FromString(str);
1394}
1395
1396static PyObject *
1397getargs_y_star(PyObject *self, PyObject *args)
1398{
1399 Py_buffer buffer;
1400 PyObject *bytes;
1401 if (!PyArg_ParseTuple(args, "y*", &buffer))
1402 return NULL;
1403 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1404 PyBuffer_Release(&buffer);
1405 return bytes;
1406}
1407
1408static PyObject *
1409getargs_y_hash(PyObject *self, PyObject *args)
1410{
1411 char *str;
1412 Py_ssize_t size;
1413 if (!PyArg_ParseTuple(args, "y#", &str, &size))
1414 return NULL;
1415 return PyBytes_FromStringAndSize(str, size);
1416}
1417
1418static PyObject *
1419getargs_u(PyObject *self, PyObject *args)
1420{
1421 Py_UNICODE *str;
Victor Stinner06e49dd2010-06-13 18:21:50 +00001422 if (!PyArg_ParseTuple(args, "u", &str))
1423 return NULL;
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001424 return PyUnicode_FromWideChar(str, -1);
Victor Stinner06e49dd2010-06-13 18:21:50 +00001425}
1426
1427static PyObject *
1428getargs_u_hash(PyObject *self, PyObject *args)
1429{
1430 Py_UNICODE *str;
1431 Py_ssize_t size;
1432 if (!PyArg_ParseTuple(args, "u#", &str, &size))
1433 return NULL;
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001434 return PyUnicode_FromWideChar(str, size);
Victor Stinner06e49dd2010-06-13 18:21:50 +00001435}
1436
1437static PyObject *
1438getargs_Z(PyObject *self, PyObject *args)
1439{
1440 Py_UNICODE *str;
Victor Stinner06e49dd2010-06-13 18:21:50 +00001441 if (!PyArg_ParseTuple(args, "Z", &str))
1442 return NULL;
1443 if (str != NULL) {
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001444 return PyUnicode_FromWideChar(str, -1);
Victor Stinner06e49dd2010-06-13 18:21:50 +00001445 } else
1446 Py_RETURN_NONE;
1447}
1448
1449static PyObject *
1450getargs_Z_hash(PyObject *self, PyObject *args)
1451{
1452 Py_UNICODE *str;
1453 Py_ssize_t size;
1454 if (!PyArg_ParseTuple(args, "Z#", &str, &size))
1455 return NULL;
1456 if (str != NULL)
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001457 return PyUnicode_FromWideChar(str, size);
Victor Stinner06e49dd2010-06-13 18:21:50 +00001458 else
1459 Py_RETURN_NONE;
1460}
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001461
Serhiy Storchakac8241fd2016-01-28 19:49:54 +02001462static PyObject *
1463getargs_es(PyObject *self, PyObject *args)
1464{
1465 PyObject *arg, *result;
1466 const char *encoding = NULL;
1467 char *str;
1468
1469 if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding))
1470 return NULL;
1471 if (!PyArg_Parse(arg, "es", encoding, &str))
1472 return NULL;
1473 result = PyBytes_FromString(str);
1474 PyMem_Free(str);
1475 return result;
1476}
1477
1478static PyObject *
1479getargs_et(PyObject *self, PyObject *args)
1480{
1481 PyObject *arg, *result;
1482 const char *encoding = NULL;
1483 char *str;
1484
1485 if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding))
1486 return NULL;
1487 if (!PyArg_Parse(arg, "et", encoding, &str))
1488 return NULL;
1489 result = PyBytes_FromString(str);
1490 PyMem_Free(str);
1491 return result;
1492}
1493
1494static PyObject *
1495getargs_es_hash(PyObject *self, PyObject *args)
1496{
1497 PyObject *arg, *result;
1498 const char *encoding = NULL;
1499 PyByteArrayObject *buffer = NULL;
1500 char *str = NULL;
1501 Py_ssize_t size;
1502
1503 if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer))
1504 return NULL;
1505 if (buffer != NULL) {
1506 str = PyByteArray_AS_STRING(buffer);
1507 size = PyByteArray_GET_SIZE(buffer);
1508 }
1509 if (!PyArg_Parse(arg, "es#", encoding, &str, &size))
1510 return NULL;
1511 result = PyBytes_FromStringAndSize(str, size);
1512 if (buffer == NULL)
1513 PyMem_Free(str);
1514 return result;
1515}
1516
1517static PyObject *
1518getargs_et_hash(PyObject *self, PyObject *args)
1519{
1520 PyObject *arg, *result;
1521 const char *encoding = NULL;
1522 PyByteArrayObject *buffer = NULL;
1523 char *str = NULL;
1524 Py_ssize_t size;
1525
1526 if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer))
1527 return NULL;
1528 if (buffer != NULL) {
1529 str = PyByteArray_AS_STRING(buffer);
1530 size = PyByteArray_GET_SIZE(buffer);
1531 }
1532 if (!PyArg_Parse(arg, "et#", encoding, &str, &size))
1533 return NULL;
1534 result = PyBytes_FromStringAndSize(str, size);
1535 if (buffer == NULL)
1536 PyMem_Free(str);
1537 return result;
1538}
1539
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001540/* Test the s and z codes for PyArg_ParseTuple.
1541*/
1542static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301543test_s_code(PyObject *self, PyObject *Py_UNUSED(ignored))
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001544{
1545 /* Unicode strings should be accepted */
1546 PyObject *tuple, *obj;
1547 char *value;
1548
1549 tuple = PyTuple_New(1);
1550 if (tuple == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 return NULL;
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001552
1553 obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 "latin-1", NULL);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001555 if (obj == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 return NULL;
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001557
1558 PyTuple_SET_ITEM(tuple, 0, obj);
1559
1560 /* These two blocks used to raise a TypeError:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 * "argument must be string without null bytes, not str"
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001562 */
Oren Milmanba7d7362017-08-29 11:58:27 +03001563 if (!PyArg_ParseTuple(tuple, "s:test_s_code1", &value)) {
1564 return NULL;
1565 }
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001566
Oren Milmanba7d7362017-08-29 11:58:27 +03001567 if (!PyArg_ParseTuple(tuple, "z:test_s_code2", &value)) {
1568 return NULL;
1569 }
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001570
Alexandre Vassalottib645bc72008-05-15 22:06:59 +00001571 Py_DECREF(tuple);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001572 Py_RETURN_NONE;
1573}
1574
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001575static PyObject *
Larry Hastings8f904da2012-06-22 03:56:29 -07001576parse_tuple_and_keywords(PyObject *self, PyObject *args)
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001577{
Larry Hastings8f904da2012-06-22 03:56:29 -07001578 PyObject *sub_args;
1579 PyObject *sub_kwargs;
Serhiy Storchaka5f161fd2017-05-04 00:03:23 +03001580 const char *sub_format;
Larry Hastings8f904da2012-06-22 03:56:29 -07001581 PyObject *sub_keywords;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001582
Larry Hastings8f904da2012-06-22 03:56:29 -07001583 Py_ssize_t i, size;
1584 char *keywords[8 + 1]; /* space for NULL at end */
1585 PyObject *o;
1586 PyObject *converted[8];
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001587
Larry Hastings8f904da2012-06-22 03:56:29 -07001588 int result;
1589 PyObject *return_value = NULL;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001590
Larry Hastings22701e82012-08-08 14:52:22 -07001591 double buffers[8][4]; /* double ensures alignment where necessary */
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001592
Serhiy Storchaka5f161fd2017-05-04 00:03:23 +03001593 if (!PyArg_ParseTuple(args, "OOsO:parse_tuple_and_keywords",
Larry Hastings8f904da2012-06-22 03:56:29 -07001594 &sub_args, &sub_kwargs,
1595 &sub_format, &sub_keywords))
1596 return NULL;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001597
Larry Hastings8f904da2012-06-22 03:56:29 -07001598 if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) {
1599 PyErr_SetString(PyExc_ValueError,
1600 "parse_tuple_and_keywords: sub_keywords must be either list or tuple");
1601 return NULL;
1602 }
1603
1604 memset(buffers, 0, sizeof(buffers));
1605 memset(converted, 0, sizeof(converted));
1606 memset(keywords, 0, sizeof(keywords));
1607
1608 size = PySequence_Fast_GET_SIZE(sub_keywords);
1609 if (size > 8) {
1610 PyErr_SetString(PyExc_ValueError,
1611 "parse_tuple_and_keywords: too many keywords in sub_keywords");
1612 goto exit;
1613 }
1614
1615 for (i = 0; i < size; i++) {
1616 o = PySequence_Fast_GET_ITEM(sub_keywords, i);
1617 if (!PyUnicode_FSConverter(o, (void *)(converted + i))) {
1618 PyErr_Format(PyExc_ValueError,
Jesus Cea6e1d2b62012-10-04 16:06:30 +02001619 "parse_tuple_and_keywords: could not convert keywords[%zd] to narrow string", i);
Larry Hastings8f904da2012-06-22 03:56:29 -07001620 goto exit;
1621 }
1622 keywords[i] = PyBytes_AS_STRING(converted[i]);
1623 }
1624
1625 result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs,
1626 sub_format, keywords,
1627 buffers + 0, buffers + 1, buffers + 2, buffers + 3,
1628 buffers + 4, buffers + 5, buffers + 6, buffers + 7);
1629
1630 if (result) {
1631 return_value = Py_None;
1632 Py_INCREF(Py_None);
1633 }
1634
1635exit:
1636 size = sizeof(converted) / sizeof(converted[0]);
1637 for (i = 0; i < size; i++) {
1638 Py_XDECREF(converted[i]);
1639 }
1640 return return_value;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001641}
1642
Benjamin Peterson92035012008-12-27 16:00:54 +00001643static volatile int x;
1644
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001645/* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
1646 of an error.
1647*/
1648static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301649test_u_code(PyObject *self, PyObject *Py_UNUSED(ignored))
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 PyObject *tuple, *obj;
1652 Py_UNICODE *value;
1653 Py_ssize_t len;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
1656 /* Just use the macro and check that it compiles */
1657 x = Py_UNICODE_ISSPACE(25);
Benjamin Peterson206e3072008-10-19 14:07:49 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 tuple = PyTuple_New(1);
1660 if (tuple == NULL)
1661 return NULL;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 obj = PyUnicode_Decode("test", strlen("test"),
1664 "ascii", NULL);
1665 if (obj == NULL)
1666 return NULL;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 PyTuple_SET_ITEM(tuple, 0, obj);
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 value = 0;
Oren Milmanba7d7362017-08-29 11:58:27 +03001671 if (!PyArg_ParseTuple(tuple, "u:test_u_code", &value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03001673 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 if (value != PyUnicode_AS_UNICODE(obj))
1675 return raiseTestError("test_u_code",
1676 "u code returned wrong value for u'test'");
1677 value = 0;
Oren Milmanba7d7362017-08-29 11:58:27 +03001678 if (!PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03001680 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 if (value != PyUnicode_AS_UNICODE(obj) ||
1682 len != PyUnicode_GET_SIZE(obj))
1683 return raiseTestError("test_u_code",
1684 "u# code returned wrong values for u'test'");
Tim Petersbaefd9e2003-01-28 20:37:45 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 Py_DECREF(tuple);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001687 Py_RETURN_NONE;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001688}
1689
Guido van Rossumfb67be22007-08-29 18:38:11 +00001690/* Test Z and Z# codes for PyArg_ParseTuple */
1691static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301692test_Z_code(PyObject *self, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb67be22007-08-29 18:38:11 +00001693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 PyObject *tuple, *obj;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001695 const Py_UNICODE *value1, *value2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 Py_ssize_t len1, len2;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 tuple = PyTuple_New(2);
1699 if (tuple == NULL)
1700 return NULL;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 obj = PyUnicode_FromString("test");
1703 PyTuple_SET_ITEM(tuple, 0, obj);
1704 Py_INCREF(Py_None);
1705 PyTuple_SET_ITEM(tuple, 1, Py_None);
Guido van Rossumfb67be22007-08-29 18:38:11 +00001706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 /* swap values on purpose */
1708 value1 = NULL;
1709 value2 = PyUnicode_AS_UNICODE(obj);
Guido van Rossumfb67be22007-08-29 18:38:11 +00001710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 /* Test Z for both values */
Oren Milmanba7d7362017-08-29 11:58:27 +03001712 if (!PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03001714 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 if (value1 != PyUnicode_AS_UNICODE(obj))
1716 return raiseTestError("test_Z_code",
1717 "Z code returned wrong value for 'test'");
1718 if (value2 != NULL)
1719 return raiseTestError("test_Z_code",
1720 "Z code returned wrong value for None");
Guido van Rossumfb67be22007-08-29 18:38:11 +00001721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 value1 = NULL;
1723 value2 = PyUnicode_AS_UNICODE(obj);
1724 len1 = -1;
1725 len2 = -1;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 /* Test Z# for both values */
Oren Milmanba7d7362017-08-29 11:58:27 +03001728 if (!PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1,
1729 &value2, &len2))
1730 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03001732 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 if (value1 != PyUnicode_AS_UNICODE(obj) ||
1734 len1 != PyUnicode_GET_SIZE(obj))
1735 return raiseTestError("test_Z_code",
1736 "Z# code returned wrong values for 'test'");
1737 if (value2 != NULL ||
1738 len2 != 0)
1739 return raiseTestError("test_Z_code",
1740 "Z# code returned wrong values for None'");
Guido van Rossumfb67be22007-08-29 18:38:11 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 Py_DECREF(tuple);
1743 Py_RETURN_NONE;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001744}
1745
Thomas Wouters477c8d52006-05-27 19:21:47 +00001746static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301747test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored))
Mark Dickinson081dfee2009-03-18 14:47:41 +00001748{
1749#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
1751 size_t wtextlen = 1;
Victor Stinnere3b47152011-12-09 20:49:49 +01001752 const wchar_t invalid[1] = {(wchar_t)0x110000u};
Mark Dickinson081dfee2009-03-18 14:47:41 +00001753#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
1755 size_t wtextlen = 2;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001756#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 PyObject *wide, *utf8;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 wide = PyUnicode_FromWideChar(wtext, wtextlen);
1760 if (wide == NULL)
1761 return NULL;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d");
1764 if (utf8 == NULL) {
1765 Py_DECREF(wide);
1766 return NULL;
1767 }
Mark Dickinson081dfee2009-03-18 14:47:41 +00001768
Victor Stinner8ef18872011-11-21 02:06:57 +01001769 if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 Py_DECREF(wide);
1771 Py_DECREF(utf8);
1772 return raiseTestError("test_widechar",
1773 "wide string and utf8 string "
1774 "have different length");
1775 }
1776 if (PyUnicode_Compare(wide, utf8)) {
1777 Py_DECREF(wide);
1778 Py_DECREF(utf8);
1779 if (PyErr_Occurred())
1780 return NULL;
1781 return raiseTestError("test_widechar",
1782 "wide string and utf8 string "
1783 "are different");
1784 }
Mark Dickinson081dfee2009-03-18 14:47:41 +00001785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 Py_DECREF(wide);
1787 Py_DECREF(utf8);
Victor Stinnere3b47152011-12-09 20:49:49 +01001788
1789#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
1790 wide = PyUnicode_FromWideChar(invalid, 1);
1791 if (wide == NULL)
1792 PyErr_Clear();
1793 else
1794 return raiseTestError("test_widechar",
1795 "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail");
1796
1797 wide = PyUnicode_FromUnicode(invalid, 1);
1798 if (wide == NULL)
1799 PyErr_Clear();
1800 else
1801 return raiseTestError("test_widechar",
1802 "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail");
Victor Stinnere5c05332013-03-06 00:39:03 +01001803
1804 wide = PyUnicode_FromUnicode(NULL, 1);
1805 if (wide == NULL)
1806 return NULL;
1807 PyUnicode_AS_UNICODE(wide)[0] = invalid[0];
Ezio Melotti03e667d2013-03-07 21:18:45 +02001808 if (_PyUnicode_Ready(wide) < 0) {
1809 Py_DECREF(wide);
Victor Stinnere5c05332013-03-06 00:39:03 +01001810 PyErr_Clear();
Ezio Melotti03e667d2013-03-07 21:18:45 +02001811 }
1812 else {
1813 Py_DECREF(wide);
Victor Stinnere5c05332013-03-06 00:39:03 +01001814 return raiseTestError("test_widechar",
1815 "PyUnicode_Ready() didn't fail");
Ezio Melotti03e667d2013-03-07 21:18:45 +02001816 }
Victor Stinnere3b47152011-12-09 20:49:49 +01001817#endif
1818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 Py_RETURN_NONE;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001820}
1821
1822static PyObject *
Victor Stinner46c7b3b2010-10-02 11:49:31 +00001823unicode_aswidechar(PyObject *self, PyObject *args)
Victor Stinner1c24bd02010-10-02 11:03:13 +00001824{
1825 PyObject *unicode, *result;
1826 Py_ssize_t buflen, size;
1827 wchar_t *buffer;
1828
1829 if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen))
1830 return NULL;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001831 buffer = PyMem_New(wchar_t, buflen);
Victor Stinner1c24bd02010-10-02 11:03:13 +00001832 if (buffer == NULL)
1833 return PyErr_NoMemory();
1834
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001835 size = PyUnicode_AsWideChar(unicode, buffer, buflen);
Victor Stinner1c24bd02010-10-02 11:03:13 +00001836 if (size == -1) {
1837 PyMem_Free(buffer);
1838 return NULL;
1839 }
1840
1841 if (size < buflen)
1842 buflen = size + 1;
1843 else
1844 buflen = size;
1845 result = PyUnicode_FromWideChar(buffer, buflen);
1846 PyMem_Free(buffer);
1847 if (result == NULL)
1848 return NULL;
1849
1850 return Py_BuildValue("(Nn)", result, size);
1851}
1852
1853static PyObject *
Victor Stinner46c7b3b2010-10-02 11:49:31 +00001854unicode_aswidecharstring(PyObject *self, PyObject *args)
Victor Stinner1c24bd02010-10-02 11:03:13 +00001855{
1856 PyObject *unicode, *result;
1857 Py_ssize_t size;
1858 wchar_t *buffer;
1859
1860 if (!PyArg_ParseTuple(args, "U", &unicode))
1861 return NULL;
1862
Victor Stinnerbeb4135b2010-10-07 01:02:42 +00001863 buffer = PyUnicode_AsWideCharString(unicode, &size);
Victor Stinner1c24bd02010-10-02 11:03:13 +00001864 if (buffer == NULL)
1865 return NULL;
1866
1867 result = PyUnicode_FromWideChar(buffer, size + 1);
1868 PyMem_Free(buffer);
1869 if (result == NULL)
1870 return NULL;
1871 return Py_BuildValue("(Nn)", result, size);
1872}
1873
1874static PyObject *
Serhiy Storchakacc164232016-10-02 21:29:26 +03001875unicode_asucs4(PyObject *self, PyObject *args)
1876{
1877 PyObject *unicode, *result;
1878 Py_UCS4 *buffer;
1879 int copy_null;
1880 Py_ssize_t str_len, buf_len;
1881
1882 if (!PyArg_ParseTuple(args, "Unp:unicode_asucs4", &unicode, &str_len, &copy_null)) {
1883 return NULL;
1884 }
1885
1886 buf_len = str_len + 1;
1887 buffer = PyMem_NEW(Py_UCS4, buf_len);
1888 if (buffer == NULL) {
1889 return PyErr_NoMemory();
1890 }
1891 memset(buffer, 0, sizeof(Py_UCS4)*buf_len);
1892 buffer[str_len] = 0xffffU;
1893
1894 if (!PyUnicode_AsUCS4(unicode, buffer, buf_len, copy_null)) {
1895 PyMem_FREE(buffer);
1896 return NULL;
1897 }
1898
1899 result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, buf_len);
1900 PyMem_FREE(buffer);
1901 return result;
1902}
1903
1904static PyObject *
Xiang Zhangb2110682016-12-20 22:52:33 +08001905unicode_findchar(PyObject *self, PyObject *args)
1906{
1907 PyObject *str;
1908 int direction;
1909 unsigned int ch;
1910 Py_ssize_t result;
1911 Py_ssize_t start, end;
1912
1913 if (!PyArg_ParseTuple(args, "UInni:unicode_findchar", &str, &ch,
1914 &start, &end, &direction)) {
1915 return NULL;
1916 }
1917
1918 result = PyUnicode_FindChar(str, (Py_UCS4)ch, start, end, direction);
1919 if (result == -2)
1920 return NULL;
1921 else
1922 return PyLong_FromSsize_t(result);
1923}
1924
1925static PyObject *
Serhiy Storchaka9c0e1f82016-10-08 22:45:38 +03001926unicode_copycharacters(PyObject *self, PyObject *args)
1927{
1928 PyObject *from, *to, *to_copy;
1929 Py_ssize_t from_start, to_start, how_many, copied;
1930
1931 if (!PyArg_ParseTuple(args, "UnOnn:unicode_copycharacters", &to, &to_start,
1932 &from, &from_start, &how_many)) {
1933 return NULL;
1934 }
1935
Serhiy Storchaka9c0e1f82016-10-08 22:45:38 +03001936 if (!(to_copy = PyUnicode_New(PyUnicode_GET_LENGTH(to),
1937 PyUnicode_MAX_CHAR_VALUE(to)))) {
1938 return NULL;
1939 }
1940 if (PyUnicode_Fill(to_copy, 0, PyUnicode_GET_LENGTH(to_copy), 0U) < 0) {
1941 Py_DECREF(to_copy);
1942 return NULL;
1943 }
1944
1945 if ((copied = PyUnicode_CopyCharacters(to_copy, to_start, from,
1946 from_start, how_many)) < 0) {
1947 Py_DECREF(to_copy);
1948 return NULL;
1949 }
1950
1951 return Py_BuildValue("(Nn)", to_copy, copied);
1952}
1953
1954static PyObject *
Victor Stinner42bf7752011-11-21 22:52:58 +01001955unicode_encodedecimal(PyObject *self, PyObject *args)
1956{
1957 Py_UNICODE *unicode;
1958 Py_ssize_t length;
1959 char *errors = NULL;
1960 PyObject *decimal;
1961 Py_ssize_t decimal_length, new_length;
1962 int res;
1963
1964 if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors))
1965 return NULL;
1966
1967 decimal_length = length * 7; /* len('&#8364;') */
1968 decimal = PyBytes_FromStringAndSize(NULL, decimal_length);
1969 if (decimal == NULL)
1970 return NULL;
1971
1972 res = PyUnicode_EncodeDecimal(unicode, length,
1973 PyBytes_AS_STRING(decimal),
1974 errors);
1975 if (res < 0) {
1976 Py_DECREF(decimal);
1977 return NULL;
1978 }
1979
1980 new_length = strlen(PyBytes_AS_STRING(decimal));
1981 assert(new_length <= decimal_length);
1982 res = _PyBytes_Resize(&decimal, new_length);
1983 if (res < 0)
1984 return NULL;
1985
1986 return decimal;
1987}
1988
1989static PyObject *
1990unicode_transformdecimaltoascii(PyObject *self, PyObject *args)
1991{
1992 Py_UNICODE *unicode;
1993 Py_ssize_t length;
1994 if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length))
1995 return NULL;
1996 return PyUnicode_TransformDecimalToASCII(unicode, length);
1997}
1998
1999static PyObject *
Stefan Krahe6996ed2012-11-02 14:44:20 +01002000unicode_legacy_string(PyObject *self, PyObject *args)
2001{
2002 Py_UNICODE *data;
2003 Py_ssize_t len;
2004 PyObject *u;
2005
2006 if (!PyArg_ParseTuple(args, "u#", &data, &len))
2007 return NULL;
2008
2009 u = PyUnicode_FromUnicode(NULL, len);
2010 if (u == NULL)
2011 return NULL;
2012
2013 memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE));
2014
2015 if (len > 0) { /* The empty string is always ready. */
2016 assert(!PyUnicode_IS_READY(u));
2017 }
2018
2019 return u;
2020}
2021
2022static PyObject *
Victor Stinner25e8ec42010-06-25 00:02:38 +00002023getargs_w_star(PyObject *self, PyObject *args)
2024{
2025 Py_buffer buffer;
2026 PyObject *result;
2027 char *str;
2028
2029 if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer))
2030 return NULL;
2031
2032 if (2 <= buffer.len) {
2033 str = buffer.buf;
2034 str[0] = '[';
2035 str[buffer.len-1] = ']';
2036 }
2037
2038 result = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
2039 PyBuffer_Release(&buffer);
2040 return result;
2041}
2042
2043
2044static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302045test_empty_argparse(PyObject *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson92035012008-12-27 16:00:54 +00002046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 /* Test that formats can begin with '|'. See issue #4720. */
2048 PyObject *tuple, *dict = NULL;
2049 static char *kwlist[] = {NULL};
2050 int result;
2051 tuple = PyTuple_New(0);
2052 if (!tuple)
2053 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03002054 if (!(result = PyArg_ParseTuple(tuple, "|:test_empty_argparse"))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 goto done;
Oren Milmanba7d7362017-08-29 11:58:27 +03002056 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 dict = PyDict_New();
2058 if (!dict)
2059 goto done;
2060 result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
Benjamin Peterson92035012008-12-27 16:00:54 +00002061 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 Py_DECREF(tuple);
2063 Py_XDECREF(dict);
Oren Milmanba7d7362017-08-29 11:58:27 +03002064 if (!result) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03002066 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 else {
2068 Py_RETURN_NONE;
2069 }
Benjamin Peterson92035012008-12-27 16:00:54 +00002070}
2071
2072static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00002073codec_incrementalencoder(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 const char *encoding, *errors = NULL;
2076 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
2077 &encoding, &errors))
2078 return NULL;
2079 return PyCodec_IncrementalEncoder(encoding, errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002080}
2081
Thomas Wouters477c8d52006-05-27 19:21:47 +00002082static PyObject *
2083codec_incrementaldecoder(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 const char *encoding, *errors = NULL;
2086 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
2087 &encoding, &errors))
2088 return NULL;
2089 return PyCodec_IncrementalDecoder(encoding, errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002090}
2091
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00002092
Tim Peters5b8132f2003-01-31 15:52:05 +00002093/* Simple test of _PyLong_NumBits and _PyLong_Sign. */
Tim Petersbaefd9e2003-01-28 20:37:45 +00002094static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302095test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored))
Tim Petersbaefd9e2003-01-28 20:37:45 +00002096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 struct triple {
2098 long input;
2099 size_t nbits;
2100 int sign;
2101 } testcases[] = {{0, 0, 0},
2102 {1L, 1, 1},
2103 {-1L, 1, -1},
2104 {2L, 2, 1},
2105 {-2L, 2, -1},
2106 {3L, 2, 1},
2107 {-3L, 2, -1},
2108 {4L, 3, 1},
2109 {-4L, 3, -1},
Serhiy Storchaka95949422013-08-27 19:40:23 +03002110 {0x7fffL, 15, 1}, /* one Python int digit */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 {-0x7fffL, 15, -1},
2112 {0xffffL, 16, 1},
2113 {-0xffffL, 16, -1},
2114 {0xfffffffL, 28, 1},
2115 {-0xfffffffL, 28, -1}};
Victor Stinner706768c2014-08-16 01:03:39 +02002116 size_t i;
Tim Petersbaefd9e2003-01-28 20:37:45 +00002117
Victor Stinner63941882011-09-29 00:42:28 +02002118 for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) {
Christian Heimes3205e742013-07-26 15:06:48 +02002119 size_t nbits;
2120 int sign;
2121 PyObject *plong;
2122
2123 plong = PyLong_FromLong(testcases[i].input);
Christian Heimesff369a52013-07-26 14:52:18 +02002124 if (plong == NULL)
2125 return NULL;
Christian Heimes3205e742013-07-26 15:06:48 +02002126 nbits = _PyLong_NumBits(plong);
2127 sign = _PyLong_Sign(plong);
Tim Petersbaefd9e2003-01-28 20:37:45 +00002128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 Py_DECREF(plong);
2130 if (nbits != testcases[i].nbits)
2131 return raiseTestError("test_long_numbits",
2132 "wrong result for _PyLong_NumBits");
2133 if (sign != testcases[i].sign)
2134 return raiseTestError("test_long_numbits",
2135 "wrong result for _PyLong_Sign");
2136 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002137 Py_RETURN_NONE;
Tim Petersbaefd9e2003-01-28 20:37:45 +00002138}
2139
Thomas Heller519a0422007-11-15 20:48:54 +00002140/* Example passing NULLs to PyObject_Str(NULL). */
Thomas Woutersa9773292006-04-21 09:43:23 +00002141
2142static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302143test_null_strings(PyObject *self, PyObject *Py_UNUSED(ignored))
Thomas Woutersa9773292006-04-21 09:43:23 +00002144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL);
2146 PyObject *tuple = PyTuple_Pack(2, o1, o2);
2147 Py_XDECREF(o1);
2148 Py_XDECREF(o2);
2149 return tuple;
Thomas Woutersa9773292006-04-21 09:43:23 +00002150}
2151
Jeremy Hyltonede049b2001-09-26 20:01:13 +00002152static PyObject *
2153raise_exception(PyObject *self, PyObject *args)
2154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 PyObject *exc;
2156 PyObject *exc_args, *v;
2157 int num_args, i;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00002158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
2160 &exc, &num_args))
2161 return NULL;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00002162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 exc_args = PyTuple_New(num_args);
2164 if (exc_args == NULL)
2165 return NULL;
2166 for (i = 0; i < num_args; ++i) {
2167 v = PyLong_FromLong(i);
2168 if (v == NULL) {
2169 Py_DECREF(exc_args);
2170 return NULL;
2171 }
2172 PyTuple_SET_ITEM(exc_args, i, v);
2173 }
2174 PyErr_SetObject(exc, exc_args);
2175 Py_DECREF(exc_args);
2176 return NULL;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00002177}
Tim Peters91621db2001-06-12 20:10:01 +00002178
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +02002179static PyObject *
Antoine Pitrou6bc217d2015-06-23 14:31:11 +02002180set_errno(PyObject *self, PyObject *args)
2181{
2182 int new_errno;
2183
2184 if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno))
2185 return NULL;
2186
2187 errno = new_errno;
2188 Py_RETURN_NONE;
2189}
2190
2191static PyObject *
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +02002192test_set_exc_info(PyObject *self, PyObject *args)
2193{
2194 PyObject *orig_exc;
2195 PyObject *new_type, *new_value, *new_tb;
2196 PyObject *type, *value, *tb;
2197 if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info",
2198 &new_type, &new_value, &new_tb))
2199 return NULL;
2200
2201 PyErr_GetExcInfo(&type, &value, &tb);
2202
2203 Py_INCREF(new_type);
2204 Py_INCREF(new_value);
2205 Py_INCREF(new_tb);
2206 PyErr_SetExcInfo(new_type, new_value, new_tb);
2207
2208 orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None);
2209 Py_XDECREF(type);
2210 Py_XDECREF(value);
2211 Py_XDECREF(tb);
2212 return orig_exc;
2213}
Benjamin Peterson16323982010-02-03 01:13:41 +00002214
2215static int test_run_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002216
Benjamin Peterson91d58bd2009-12-13 21:30:54 +00002217static PyObject *
2218test_datetime_capi(PyObject *self, PyObject *args) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 if (PyDateTimeAPI) {
2220 if (test_run_counter) {
2221 /* Probably regrtest.py -R */
2222 Py_RETURN_NONE;
2223 }
2224 else {
2225 PyErr_SetString(PyExc_AssertionError,
2226 "PyDateTime_CAPI somehow initialized");
2227 return NULL;
2228 }
2229 }
2230 test_run_counter++;
2231 PyDateTime_IMPORT;
Paul Ganssle04af5b12018-01-24 17:29:30 -05002232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 if (PyDateTimeAPI)
2234 Py_RETURN_NONE;
2235 else
2236 return NULL;
Benjamin Peterson91d58bd2009-12-13 21:30:54 +00002237}
2238
Paul Ganssle04af5b12018-01-24 17:29:30 -05002239/* Functions exposing the C API type checking for testing */
2240#define MAKE_DATETIME_CHECK_FUNC(check_method, exact_method) \
2241 PyObject *obj; \
2242 int exact = 0; \
2243 if (!PyArg_ParseTuple(args, "O|p", &obj, &exact)) { \
2244 return NULL; \
2245 } \
2246 int rv = exact?exact_method(obj):check_method(obj); \
2247 if (rv) { \
2248 Py_RETURN_TRUE; \
2249 } else { \
2250 Py_RETURN_FALSE; \
2251 }
2252
2253static PyObject *
2254datetime_check_date(PyObject *self, PyObject *args) {
2255 MAKE_DATETIME_CHECK_FUNC(PyDate_Check, PyDate_CheckExact)
2256}
2257
2258static PyObject *
2259datetime_check_time(PyObject *self, PyObject *args) {
2260 MAKE_DATETIME_CHECK_FUNC(PyTime_Check, PyTime_CheckExact)
2261}
2262
2263static PyObject *
2264datetime_check_datetime(PyObject *self, PyObject *args) {
2265 MAKE_DATETIME_CHECK_FUNC(PyDateTime_Check, PyDateTime_CheckExact)
2266}
2267
2268static PyObject *
2269datetime_check_delta(PyObject *self, PyObject *args) {
2270 MAKE_DATETIME_CHECK_FUNC(PyDelta_Check, PyDelta_CheckExact)
2271}
2272
2273static PyObject *
2274datetime_check_tzinfo(PyObject *self, PyObject *args) {
2275 MAKE_DATETIME_CHECK_FUNC(PyTZInfo_Check, PyTZInfo_CheckExact)
2276}
2277
2278
2279/* Makes three variations on timezone representing UTC-5:
2280 1. timezone with offset and name from PyDateTimeAPI
2281 2. timezone with offset and name from PyTimeZone_FromOffsetAndName
2282 3. timezone with offset (no name) from PyTimeZone_FromOffset
2283*/
2284static PyObject *
2285make_timezones_capi(PyObject *self, PyObject *args) {
2286 PyObject *offset = PyDelta_FromDSU(0, -18000, 0);
2287 PyObject *name = PyUnicode_FromString("EST");
2288
2289 PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name);
2290 PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name);
2291 PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset);
2292
2293 Py_DecRef(offset);
2294 Py_DecRef(name);
2295
2296 PyObject *rv = PyTuple_New(3);
2297
2298 PyTuple_SET_ITEM(rv, 0, est_zone_capi);
2299 PyTuple_SET_ITEM(rv, 1, est_zone_macro);
2300 PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname);
2301
2302 return rv;
2303}
2304
2305static PyObject *
Paul Gansslea049f572018-02-22 15:15:32 -05002306get_timezones_offset_zero(PyObject *self, PyObject *args) {
2307 PyObject *offset = PyDelta_FromDSU(0, 0, 0);
2308 PyObject *name = PyUnicode_FromString("");
2309
2310 // These two should return the UTC singleton
2311 PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset);
2312 PyObject *utc_singleton_1 = PyTimeZone_FromOffsetAndName(offset, NULL);
2313
2314 // This one will return +00:00 zone, but not the UTC singleton
2315 PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name);
2316
2317 Py_DecRef(offset);
2318 Py_DecRef(name);
2319
2320 PyObject *rv = PyTuple_New(3);
2321 PyTuple_SET_ITEM(rv, 0, utc_singleton_0);
2322 PyTuple_SET_ITEM(rv, 1, utc_singleton_1);
2323 PyTuple_SET_ITEM(rv, 2, non_utc_zone);
2324
2325 return rv;
2326}
2327
2328static PyObject *
Paul Ganssle04af5b12018-01-24 17:29:30 -05002329get_timezone_utc_capi(PyObject* self, PyObject *args) {
2330 int macro = 0;
2331 if (!PyArg_ParseTuple(args, "|p", &macro)) {
2332 return NULL;
2333 }
2334 if (macro) {
Paul Ganssle58dc03c2018-01-25 08:58:07 -05002335 Py_INCREF(PyDateTime_TimeZone_UTC);
Paul Ganssle04af5b12018-01-24 17:29:30 -05002336 return PyDateTime_TimeZone_UTC;
2337 } else {
Paul Ganssle58dc03c2018-01-25 08:58:07 -05002338 Py_INCREF(PyDateTimeAPI->TimeZone_UTC);
Paul Ganssle04af5b12018-01-24 17:29:30 -05002339 return PyDateTimeAPI->TimeZone_UTC;
2340 }
2341}
2342
Benjamin Peterson16323982010-02-03 01:13:41 +00002343
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002344/* test_thread_state spawns a thread of its own, and that thread releases
2345 * `thread_done` when it's finished. The driver code has to know when the
2346 * thread finishes, because the thread uses a PyObject (the callable) that
2347 * may go away when the driver finishes. The former lack of this explicit
2348 * synchronization caused rare segfaults, so rare that they were seen only
2349 * on a Mac buildbot (although they were possible on any box).
2350 */
2351static PyThread_type_lock thread_done = NULL;
2352
Benjamin Petersona786b022008-08-25 21:05:21 +00002353static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002354_make_call(void *callable)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 PyObject *rc;
2357 int success;
2358 PyGILState_STATE s = PyGILState_Ensure();
Victor Stinner3466bde2016-09-05 18:16:01 -07002359 rc = _PyObject_CallNoArg((PyObject *)callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 success = (rc != NULL);
2361 Py_XDECREF(rc);
2362 PyGILState_Release(s);
2363 return success;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002364}
2365
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002366/* Same thing, but releases `thread_done` when it returns. This variant
2367 * should be called only from threads spawned by test_thread_state().
2368 */
2369static void
2370_make_call_from_thread(void *callable)
2371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 _make_call(callable);
2373 PyThread_release_lock(thread_done);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002374}
2375
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002376static PyObject *
2377test_thread_state(PyObject *self, PyObject *args)
2378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 PyObject *fn;
2380 int success = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
2383 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 if (!PyCallable_Check(fn)) {
2386 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
2387 fn->ob_type->tp_name);
2388 return NULL;
2389 }
Benjamin Petersona786b022008-08-25 21:05:21 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 /* Ensure Python is set up for threading */
2392 PyEval_InitThreads();
2393 thread_done = PyThread_allocate_lock();
2394 if (thread_done == NULL)
2395 return PyErr_NoMemory();
2396 PyThread_acquire_lock(thread_done, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 /* Start a new thread with our callback. */
2399 PyThread_start_new_thread(_make_call_from_thread, fn);
2400 /* Make the callback with the thread lock held by this thread */
2401 success &= _make_call(fn);
2402 /* Do it all again, but this time with the thread-lock released */
2403 Py_BEGIN_ALLOW_THREADS
2404 success &= _make_call(fn);
2405 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
2406 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 /* And once more with and without a thread
2409 XXX - should use a lock and work out exactly what we are trying
2410 to test <wink>
2411 */
2412 Py_BEGIN_ALLOW_THREADS
2413 PyThread_start_new_thread(_make_call_from_thread, fn);
2414 success &= _make_call(fn);
2415 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
2416 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 /* Release lock we acquired above. This is required on HP-UX. */
2419 PyThread_release_lock(thread_done);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 PyThread_free_lock(thread_done);
2422 if (!success)
2423 return NULL;
2424 Py_RETURN_NONE;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002425}
Benjamin Petersona54c9092009-01-13 02:11:23 +00002426
2427/* test Py_AddPendingCalls using threads */
2428static int _pending_callback(void *arg)
2429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 /* we assume the argument is callable object to which we own a reference */
2431 PyObject *callable = (PyObject *)arg;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01002432 PyObject *r = _PyObject_CallNoArg(callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 Py_DECREF(callable);
2434 Py_XDECREF(r);
2435 return r != NULL ? 0 : -1;
Benjamin Petersona54c9092009-01-13 02:11:23 +00002436}
2437
2438/* The following requests n callbacks to _pending_callback. It can be
2439 * run from any python thread.
2440 */
Benjamin Petersonb4588c22018-07-03 22:30:56 -07002441static PyObject *
2442pending_threadfunc(PyObject *self, PyObject *arg)
Benjamin Petersona54c9092009-01-13 02:11:23 +00002443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 PyObject *callable;
2445 int r;
2446 if (PyArg_ParseTuple(arg, "O", &callable) == 0)
2447 return NULL;
Benjamin Petersona54c9092009-01-13 02:11:23 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 /* create the reference for the callbackwhile we hold the lock */
2450 Py_INCREF(callable);
Benjamin Petersona54c9092009-01-13 02:11:23 +00002451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 Py_BEGIN_ALLOW_THREADS
2453 r = Py_AddPendingCall(&_pending_callback, callable);
2454 Py_END_ALLOW_THREADS
Benjamin Petersona54c9092009-01-13 02:11:23 +00002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 if (r<0) {
2457 Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002458 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002460 Py_RETURN_TRUE;
Benjamin Petersona54c9092009-01-13 02:11:23 +00002461}
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002462
Neal Norwitzb0d26332007-08-25 00:49:05 +00002463/* Some tests of PyUnicode_FromFormat(). This needs more tests. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002464static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02002465test_string_from_format(PyObject *self, PyObject *Py_UNUSED(ignored))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 PyObject *result;
2468 char *msg;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002469
Alexander Belopolskye239d232010-12-08 23:31:48 +00002470#define CHECK_1_FORMAT(FORMAT, TYPE) \
2471 result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \
2472 if (result == NULL) \
2473 return NULL; \
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02002474 if (!_PyUnicode_EqualToASCIIString(result, "1")) { \
Alexander Belopolskye239d232010-12-08 23:31:48 +00002475 msg = FORMAT " failed at 1"; \
2476 goto Fail; \
2477 } \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 Py_DECREF(result)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 CHECK_1_FORMAT("%d", int);
2481 CHECK_1_FORMAT("%ld", long);
2482 /* The z width modifier was added in Python 2.5. */
2483 CHECK_1_FORMAT("%zd", Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 /* The u type code was added in Python 2.5. */
2486 CHECK_1_FORMAT("%u", unsigned int);
2487 CHECK_1_FORMAT("%lu", unsigned long);
2488 CHECK_1_FORMAT("%zu", size_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 /* "%lld" and "%llu" support added in Python 2.7. */
Benjamin Petersonaf580df2016-09-06 10:46:49 -07002491 CHECK_1_FORMAT("%llu", unsigned long long);
2492 CHECK_1_FORMAT("%lld", long long);
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002495
2496 Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 Py_XDECREF(result);
2498 return raiseTestError("test_string_from_format", msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002499
2500#undef CHECK_1_FORMAT
2501}
2502
Benjamin Peterson8667a9b2010-01-09 21:45:28 +00002503
2504static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302505test_unicode_compare_with_ascii(PyObject *self, PyObject *Py_UNUSED(ignored)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4);
2507 int result;
2508 if (py_s == NULL)
2509 return NULL;
2510 result = PyUnicode_CompareWithASCIIString(py_s, "str");
2511 Py_DECREF(py_s);
2512 if (!result) {
2513 PyErr_SetString(TestError, "Python string ending in NULL "
2514 "should not compare equal to c string.");
2515 return NULL;
2516 }
2517 Py_RETURN_NONE;
Victor Stinner3e2b7172010-11-09 09:32:19 +00002518}
Benjamin Peterson8667a9b2010-01-09 21:45:28 +00002519
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002520/* This is here to provide a docstring for test_descr. */
2521static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302522test_with_docstring(PyObject *self, PyObject *Py_UNUSED(ignored))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002525}
2526
Mark Dickinson725bfd82009-05-03 20:33:40 +00002527/* Test PyOS_string_to_double. */
2528static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302529test_string_to_double(PyObject *self, PyObject *Py_UNUSED(ignored)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 double result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02002531 const char *msg;
Mark Dickinson725bfd82009-05-03 20:33:40 +00002532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533#define CHECK_STRING(STR, expected) \
2534 result = PyOS_string_to_double(STR, NULL, NULL); \
2535 if (result == -1.0 && PyErr_Occurred()) \
2536 return NULL; \
Benjamin Peterson8f4b2472016-09-07 18:09:22 -07002537 if (result != (double)expected) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 msg = "conversion of " STR " to float failed"; \
2539 goto fail; \
2540 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00002541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542#define CHECK_INVALID(STR) \
2543 result = PyOS_string_to_double(STR, NULL, NULL); \
2544 if (result == -1.0 && PyErr_Occurred()) { \
2545 if (PyErr_ExceptionMatches(PyExc_ValueError)) \
2546 PyErr_Clear(); \
2547 else \
2548 return NULL; \
2549 } \
2550 else { \
2551 msg = "conversion of " STR " didn't raise ValueError"; \
2552 goto fail; \
2553 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00002554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 CHECK_STRING("0.1", 0.1);
2556 CHECK_STRING("1.234", 1.234);
2557 CHECK_STRING("-1.35", -1.35);
2558 CHECK_STRING(".1e01", 1.0);
2559 CHECK_STRING("2.e-2", 0.02);
Mark Dickinson725bfd82009-05-03 20:33:40 +00002560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 CHECK_INVALID(" 0.1");
2562 CHECK_INVALID("\t\n-3");
2563 CHECK_INVALID(".123 ");
2564 CHECK_INVALID("3\n");
2565 CHECK_INVALID("123abc");
Mark Dickinson725bfd82009-05-03 20:33:40 +00002566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 Py_RETURN_NONE;
Mark Dickinson725bfd82009-05-03 20:33:40 +00002568 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 return raiseTestError("test_string_to_double", msg);
Mark Dickinson725bfd82009-05-03 20:33:40 +00002570#undef CHECK_STRING
2571#undef CHECK_INVALID
2572}
2573
2574
Benjamin Petersonb173f782009-05-05 22:31:58 +00002575/* Coverage testing of capsule objects. */
2576
2577static const char *capsule_name = "capsule name";
2578static char *capsule_pointer = "capsule pointer";
2579static char *capsule_context = "capsule context";
2580static const char *capsule_error = NULL;
2581static int
2582capsule_destructor_call_count = 0;
2583
2584static void
2585capsule_destructor(PyObject *o) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 capsule_destructor_call_count++;
2587 if (PyCapsule_GetContext(o) != capsule_context) {
2588 capsule_error = "context did not match in destructor!";
2589 } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
2590 capsule_error = "destructor did not match in destructor! (woah!)";
2591 } else if (PyCapsule_GetName(o) != capsule_name) {
2592 capsule_error = "name did not match in destructor!";
2593 } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
2594 capsule_error = "pointer did not match in destructor!";
2595 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00002596}
2597
2598typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 char *name;
2600 char *module;
2601 char *attribute;
Benjamin Petersonb173f782009-05-05 22:31:58 +00002602} known_capsule;
2603
2604static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02002605test_capsule(PyObject *self, PyObject *Py_UNUSED(ignored))
Benjamin Petersonb173f782009-05-05 22:31:58 +00002606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 PyObject *object;
2608 const char *error = NULL;
2609 void *pointer;
2610 void *pointer2;
2611 known_capsule known_capsules[] = {
2612 #define KNOWN_CAPSULE(module, name) { module "." name, module, name }
2613 KNOWN_CAPSULE("_socket", "CAPI"),
2614 KNOWN_CAPSULE("_curses", "_C_API"),
2615 KNOWN_CAPSULE("datetime", "datetime_CAPI"),
2616 { NULL, NULL },
2617 };
2618 known_capsule *known = &known_capsules[0];
Benjamin Petersonb173f782009-05-05 22:31:58 +00002619
2620#define FAIL(x) { error = (x); goto exit; }
2621
2622#define CHECK_DESTRUCTOR \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 if (capsule_error) { \
2624 FAIL(capsule_error); \
2625 } \
2626 else if (!capsule_destructor_call_count) { \
2627 FAIL("destructor not called!"); \
2628 } \
2629 capsule_destructor_call_count = 0; \
Benjamin Petersonb173f782009-05-05 22:31:58 +00002630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
2632 PyCapsule_SetContext(object, capsule_context);
2633 capsule_destructor(object);
2634 CHECK_DESTRUCTOR;
2635 Py_DECREF(object);
2636 CHECK_DESTRUCTOR;
Benjamin Petersonb173f782009-05-05 22:31:58 +00002637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 object = PyCapsule_New(known, "ignored", NULL);
2639 PyCapsule_SetPointer(object, capsule_pointer);
2640 PyCapsule_SetName(object, capsule_name);
2641 PyCapsule_SetDestructor(object, capsule_destructor);
2642 PyCapsule_SetContext(object, capsule_context);
2643 capsule_destructor(object);
2644 CHECK_DESTRUCTOR;
2645 /* intentionally access using the wrong name */
2646 pointer2 = PyCapsule_GetPointer(object, "the wrong name");
2647 if (!PyErr_Occurred()) {
2648 FAIL("PyCapsule_GetPointer should have failed but did not!");
2649 }
2650 PyErr_Clear();
2651 if (pointer2) {
2652 if (pointer2 == capsule_pointer) {
2653 FAIL("PyCapsule_GetPointer should not have"
2654 " returned the internal pointer!");
2655 } else {
2656 FAIL("PyCapsule_GetPointer should have "
2657 "returned NULL pointer but did not!");
2658 }
2659 }
2660 PyCapsule_SetDestructor(object, NULL);
2661 Py_DECREF(object);
2662 if (capsule_destructor_call_count) {
2663 FAIL("destructor called when it should not have been!");
2664 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00002665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 for (known = &known_capsules[0]; known->module != NULL; known++) {
2667 /* yeah, ordinarily I wouldn't do this either,
2668 but it's fine for this test harness.
2669 */
2670 static char buffer[256];
Benjamin Petersonb173f782009-05-05 22:31:58 +00002671#undef FAIL
2672#define FAIL(x) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 { \
2674 sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
2675 x, known->module, known->attribute); \
2676 error = buffer; \
2677 goto exit; \
2678 } \
Benjamin Petersonb173f782009-05-05 22:31:58 +00002679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 PyObject *module = PyImport_ImportModule(known->module);
2681 if (module) {
2682 pointer = PyCapsule_Import(known->name, 0);
2683 if (!pointer) {
2684 Py_DECREF(module);
2685 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
2686 }
2687 object = PyObject_GetAttrString(module, known->attribute);
2688 if (!object) {
2689 Py_DECREF(module);
2690 return NULL;
2691 }
2692 pointer2 = PyCapsule_GetPointer(object,
2693 "weebles wobble but they don't fall down");
2694 if (!PyErr_Occurred()) {
2695 Py_DECREF(object);
2696 Py_DECREF(module);
2697 FAIL("PyCapsule_GetPointer should have failed but did not!");
2698 }
2699 PyErr_Clear();
2700 if (pointer2) {
2701 Py_DECREF(module);
2702 Py_DECREF(object);
2703 if (pointer2 == pointer) {
2704 FAIL("PyCapsule_GetPointer should not have"
2705 " returned its internal pointer!");
2706 } else {
2707 FAIL("PyCapsule_GetPointer should have"
2708 " returned NULL pointer but did not!");
2709 }
2710 }
2711 Py_DECREF(object);
2712 Py_DECREF(module);
2713 }
2714 else
2715 PyErr_Clear();
2716 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00002717
2718 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 if (error) {
2720 return raiseTestError("test_capsule", error);
2721 }
2722 Py_RETURN_NONE;
Benjamin Petersonb173f782009-05-05 22:31:58 +00002723#undef FAIL
2724}
2725
Guido van Rossumddefaf32007-01-14 03:31:43 +00002726#ifdef HAVE_GETTIMEOFDAY
2727/* Profiling of integer performance */
Martin v. Löwis1c951552008-06-13 07:48:19 +00002728static void print_delta(int test, struct timeval *s, struct timeval *e)
Guido van Rossumddefaf32007-01-14 03:31:43 +00002729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 e->tv_sec -= s->tv_sec;
2731 e->tv_usec -= s->tv_usec;
2732 if (e->tv_usec < 0) {
2733 e->tv_sec -=1;
2734 e->tv_usec += 1000000;
2735 }
2736 printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002737}
2738
2739static PyObject *
2740profile_int(PyObject *self, PyObject* args)
2741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 int i, k;
2743 struct timeval start, stop;
2744 PyObject *single, **multiple, *op1, *result;
Guido van Rossumddefaf32007-01-14 03:31:43 +00002745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 /* Test 1: Allocate and immediately deallocate
2747 many small integers */
2748 gettimeofday(&start, NULL);
2749 for(k=0; k < 20000; k++)
2750 for(i=0; i < 1000; i++) {
2751 single = PyLong_FromLong(i);
2752 Py_DECREF(single);
2753 }
2754 gettimeofday(&stop, NULL);
2755 print_delta(1, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 /* Test 2: Allocate and immediately deallocate
2758 many large integers */
2759 gettimeofday(&start, NULL);
2760 for(k=0; k < 20000; k++)
2761 for(i=0; i < 1000; i++) {
2762 single = PyLong_FromLong(i+1000000);
2763 Py_DECREF(single);
2764 }
2765 gettimeofday(&stop, NULL);
2766 print_delta(2, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 /* Test 3: Allocate a few integers, then release
2769 them all simultaneously. */
2770 multiple = malloc(sizeof(PyObject*) * 1000);
Christian Heimes7e138022013-07-26 15:03:50 +02002771 if (multiple == NULL)
2772 return PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 gettimeofday(&start, NULL);
2774 for(k=0; k < 20000; k++) {
2775 for(i=0; i < 1000; i++) {
2776 multiple[i] = PyLong_FromLong(i+1000000);
2777 }
2778 for(i=0; i < 1000; i++) {
2779 Py_DECREF(multiple[i]);
2780 }
2781 }
2782 gettimeofday(&stop, NULL);
2783 print_delta(3, &start, &stop);
Christian Heimes7e138022013-07-26 15:03:50 +02002784 free(multiple);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 /* Test 4: Allocate many integers, then release
2787 them all simultaneously. */
2788 multiple = malloc(sizeof(PyObject*) * 1000000);
Christian Heimes7e138022013-07-26 15:03:50 +02002789 if (multiple == NULL)
2790 return PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 gettimeofday(&start, NULL);
2792 for(k=0; k < 20; k++) {
2793 for(i=0; i < 1000000; i++) {
2794 multiple[i] = PyLong_FromLong(i+1000000);
2795 }
2796 for(i=0; i < 1000000; i++) {
2797 Py_DECREF(multiple[i]);
2798 }
2799 }
2800 gettimeofday(&stop, NULL);
2801 print_delta(4, &start, &stop);
Christian Heimes7e138022013-07-26 15:03:50 +02002802 free(multiple);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 /* Test 5: Allocate many integers < 32000 */
2805 multiple = malloc(sizeof(PyObject*) * 1000000);
Christian Heimes7e138022013-07-26 15:03:50 +02002806 if (multiple == NULL)
2807 return PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 gettimeofday(&start, NULL);
2809 for(k=0; k < 10; k++) {
2810 for(i=0; i < 1000000; i++) {
2811 multiple[i] = PyLong_FromLong(i+1000);
2812 }
2813 for(i=0; i < 1000000; i++) {
2814 Py_DECREF(multiple[i]);
2815 }
2816 }
2817 gettimeofday(&stop, NULL);
2818 print_delta(5, &start, &stop);
Christian Heimes7e138022013-07-26 15:03:50 +02002819 free(multiple);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 /* Test 6: Perform small int addition */
2822 op1 = PyLong_FromLong(1);
2823 gettimeofday(&start, NULL);
2824 for(i=0; i < 10000000; i++) {
2825 result = PyNumber_Add(op1, op1);
2826 Py_DECREF(result);
2827 }
2828 gettimeofday(&stop, NULL);
2829 Py_DECREF(op1);
2830 print_delta(6, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 /* Test 7: Perform medium int addition */
2833 op1 = PyLong_FromLong(1000);
Christian Heimes66eda262013-07-26 15:54:07 +02002834 if (op1 == NULL)
2835 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 gettimeofday(&start, NULL);
2837 for(i=0; i < 10000000; i++) {
2838 result = PyNumber_Add(op1, op1);
Christian Heimesff369a52013-07-26 14:52:18 +02002839 Py_XDECREF(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 }
2841 gettimeofday(&stop, NULL);
2842 Py_DECREF(op1);
2843 print_delta(7, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002844
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002845 Py_RETURN_NONE;
Guido van Rossumddefaf32007-01-14 03:31:43 +00002846}
2847#endif
2848
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002849/* To test the format of tracebacks as printed out. */
2850static PyObject *
2851traceback_print(PyObject *self, PyObject *args)
2852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 PyObject *file;
2854 PyObject *traceback;
2855 int result;
2856
2857 if (!PyArg_ParseTuple(args, "OO:traceback_print",
2858 &traceback, &file))
2859 return NULL;
2860
2861 result = PyTraceBack_Print(traceback, file);
2862 if (result < 0)
2863 return NULL;
2864 Py_RETURN_NONE;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002865}
2866
Benjamin Petersone6528212008-07-15 15:32:09 +00002867/* To test the format of exceptions as printed out. */
2868static PyObject *
2869exception_print(PyObject *self, PyObject *args)
2870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 PyObject *value;
2872 PyObject *tb;
Benjamin Petersone6528212008-07-15 15:32:09 +00002873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 if (!PyArg_ParseTuple(args, "O:exception_print",
2875 &value))
2876 return NULL;
2877 if (!PyExceptionInstance_Check(value)) {
2878 PyErr_Format(PyExc_TypeError, "an exception instance is required");
2879 return NULL;
2880 }
Benjamin Petersone6528212008-07-15 15:32:09 +00002881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 tb = PyException_GetTraceback(value);
2883 PyErr_Display((PyObject *) Py_TYPE(value), value, tb);
2884 Py_XDECREF(tb);
Benjamin Petersone6528212008-07-15 15:32:09 +00002885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 Py_RETURN_NONE;
Benjamin Petersone6528212008-07-15 15:32:09 +00002887}
2888
2889
2890
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002891
2892/* reliably raise a MemoryError */
2893static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302894raise_memoryerror(PyObject *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 PyErr_NoMemory();
2897 return NULL;
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002898}
2899
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00002900/* Issue 6012 */
2901static PyObject *str1, *str2;
2902static int
2903failing_converter(PyObject *obj, void *arg)
2904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 /* Clone str1, then let the conversion fail. */
2906 assert(str1);
2907 str2 = str1;
2908 Py_INCREF(str2);
2909 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00002910}
2911static PyObject*
2912argparsing(PyObject *o, PyObject *args)
2913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 PyObject *res;
2915 str1 = str2 = NULL;
2916 if (!PyArg_ParseTuple(args, "O&O&",
2917 PyUnicode_FSConverter, &str1,
2918 failing_converter, &str2)) {
2919 if (!str2)
2920 /* argument converter not called? */
2921 return NULL;
2922 /* Should be 1 */
Victor Stinner0fcab4a2011-01-04 12:59:15 +00002923 res = PyLong_FromSsize_t(Py_REFCNT(str2));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 Py_DECREF(str2);
2925 PyErr_Clear();
2926 return res;
2927 }
2928 Py_RETURN_NONE;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00002929}
Benjamin Peterson0067bd62008-08-16 16:11:03 +00002930
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002931/* To test that the result of PyCode_NewEmpty has the right members. */
2932static PyObject *
2933code_newempty(PyObject *self, PyObject *args)
2934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002935 const char *filename;
2936 const char *funcname;
2937 int firstlineno;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 if (!PyArg_ParseTuple(args, "ssi:code_newempty",
2940 &filename, &funcname, &firstlineno))
2941 return NULL;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002944}
2945
Georg Brandl1e28a272009-12-28 08:41:01 +00002946/* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException).
2947 Run via Lib/test/test_exceptions.py */
2948static PyObject *
2949make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs)
2950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 const char *name;
2952 const char *doc = NULL;
2953 PyObject *base = NULL;
2954 PyObject *dict = NULL;
Georg Brandl1e28a272009-12-28 08:41:01 +00002955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 static char *kwlist[] = {"name", "doc", "base", "dict", NULL};
Georg Brandl1e28a272009-12-28 08:41:01 +00002957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
2959 "s|sOO:make_exception_with_doc", kwlist,
2960 &name, &doc, &base, &dict))
2961 return NULL;
Georg Brandl1e28a272009-12-28 08:41:01 +00002962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 return PyErr_NewExceptionWithDoc(name, doc, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +00002964}
2965
Antoine Pitrou5bffa792011-02-24 20:50:49 +00002966static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302967make_memoryview_from_NULL_pointer(PyObject *self, PyObject *Py_UNUSED(ignored))
Antoine Pitrou5bffa792011-02-24 20:50:49 +00002968{
2969 Py_buffer info;
2970 if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0)
2971 return NULL;
2972 return PyMemoryView_FromBuffer(&info);
2973}
Serhiy Storchaka009b8112015-03-18 21:53:15 +02002974
Stefan Krah7213fcc2015-02-01 16:19:23 +01002975static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02002976test_from_contiguous(PyObject* self, PyObject *Py_UNUSED(ignored))
Stefan Krah7213fcc2015-02-01 16:19:23 +01002977{
2978 int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1};
2979 int init[5] = {0, 1, 2, 3, 4};
2980 Py_ssize_t itemsize = sizeof(int);
2981 Py_ssize_t shape = 5;
2982 Py_ssize_t strides = 2 * itemsize;
2983 Py_buffer view = {
2984 data,
2985 NULL,
2986 5 * itemsize,
2987 itemsize,
2988 1,
2989 1,
2990 NULL,
2991 &shape,
2992 &strides,
2993 NULL,
2994 NULL
2995 };
2996 int *ptr;
2997 int i;
2998
2999 PyBuffer_FromContiguous(&view, init, view.len, 'C');
3000 ptr = view.buf;
3001 for (i = 0; i < 5; i++) {
3002 if (ptr[2*i] != i) {
3003 PyErr_SetString(TestError,
3004 "test_from_contiguous: incorrect result");
3005 return NULL;
3006 }
3007 }
3008
3009 view.buf = &data[8];
3010 view.strides[0] = -2 * itemsize;
3011
3012 PyBuffer_FromContiguous(&view, init, view.len, 'C');
3013 ptr = view.buf;
3014 for (i = 0; i < 5; i++) {
3015 if (*(ptr-2*i) != i) {
3016 PyErr_SetString(TestError,
3017 "test_from_contiguous: incorrect result");
3018 return NULL;
3019 }
3020 }
3021
3022 Py_RETURN_NONE;
3023}
Stefan Krah650c1e82015-02-03 21:43:23 +01003024
Stefan Kraha7559c02015-02-03 22:27:21 +01003025#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
Stefan Krah650c1e82015-02-03 21:43:23 +01003026extern PyTypeObject _PyBytesIOBuffer_Type;
3027
Stefan Krah5178d912015-02-03 16:57:21 +01003028static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02003029test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored))
Stefan Krah5178d912015-02-03 16:57:21 +01003030{
Stefan Krah650c1e82015-02-03 21:43:23 +01003031 PyTypeObject *type = &_PyBytesIOBuffer_Type;
Stefan Krah5178d912015-02-03 16:57:21 +01003032 PyObject *b;
3033 char *dummy[1];
3034 int ret, match;
3035
Stefan Krah650c1e82015-02-03 21:43:23 +01003036 /* PyBuffer_FillInfo() */
Stefan Krah5178d912015-02-03 16:57:21 +01003037 ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE);
3038 match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
3039 PyErr_Clear();
3040 if (ret != -1 || match == 0)
3041 goto error;
3042
Stefan Krah650c1e82015-02-03 21:43:23 +01003043 /* bytesiobuf_getbuffer() */
3044 b = type->tp_alloc(type, 0);
Stefan Krah5178d912015-02-03 16:57:21 +01003045 if (b == NULL) {
3046 return NULL;
3047 }
3048
3049 ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE);
3050 Py_DECREF(b);
3051 match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
3052 PyErr_Clear();
3053 if (ret != -1 || match == 0)
3054 goto error;
3055
3056 Py_RETURN_NONE;
3057
3058error:
3059 PyErr_SetString(TestError,
3060 "test_pep3118_obsolete_write_locks: failure");
3061 return NULL;
3062}
Stefan Kraha7559c02015-02-03 22:27:21 +01003063#endif
Antoine Pitrou5bffa792011-02-24 20:50:49 +00003064
Stefan Krah650c1e82015-02-03 21:43:23 +01003065/* This tests functions that historically supported write locks. It is
3066 wrong to call getbuffer() with view==NULL and a compliant getbufferproc
3067 is entitled to segfault in that case. */
3068static PyObject *
3069getbuffer_with_null_view(PyObject* self, PyObject *obj)
3070{
3071 if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0)
3072 return NULL;
3073
3074 Py_RETURN_NONE;
3075}
3076
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00003077/* Test that the fatal error from not having a current thread doesn't
3078 cause an infinite loop. Run via Lib/test/test_capi.py */
3079static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303080crash_no_current_thread(PyObject *self, PyObject *Py_UNUSED(ignored))
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00003081{
3082 Py_BEGIN_ALLOW_THREADS
Jeffrey Yasskinea7b7482010-05-17 16:59:23 +00003083 /* Using PyThreadState_Get() directly allows the test to pass in
3084 !pydebug mode. However, the test only actually tests anything
3085 in pydebug mode, since that's where the infinite loop was in
3086 the first place. */
3087 PyThreadState_Get();
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00003088 Py_END_ALLOW_THREADS
3089 return NULL;
3090}
3091
Antoine Pitrou2f828f22012-01-18 00:21:11 +01003092/* To run some code in a sub-interpreter. */
3093static PyObject *
3094run_in_subinterp(PyObject *self, PyObject *args)
3095{
3096 const char *code;
3097 int r;
3098 PyThreadState *substate, *mainstate;
3099
3100 if (!PyArg_ParseTuple(args, "s:run_in_subinterp",
3101 &code))
3102 return NULL;
3103
3104 mainstate = PyThreadState_Get();
3105
3106 PyThreadState_Swap(NULL);
3107
3108 substate = Py_NewInterpreter();
Brett Cannonb6855682012-02-03 12:08:03 -05003109 if (substate == NULL) {
3110 /* Since no new thread state was created, there is no exception to
3111 propagate; raise a fresh one after swapping in the old thread
3112 state. */
3113 PyThreadState_Swap(mainstate);
3114 PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed");
3115 return NULL;
3116 }
Antoine Pitrou2f828f22012-01-18 00:21:11 +01003117 r = PyRun_SimpleString(code);
3118 Py_EndInterpreter(substate);
3119
3120 PyThreadState_Swap(mainstate);
3121
3122 return PyLong_FromLong(r);
3123}
3124
Victor Stinner3c1b3792014-02-17 00:02:43 +01003125static int
3126check_time_rounding(int round)
3127{
Victor Stinner74474232015-09-02 01:43:56 +02003128 if (round != _PyTime_ROUND_FLOOR
3129 && round != _PyTime_ROUND_CEILING
Pablo Galindo2c15b292017-10-17 15:14:41 +01003130 && round != _PyTime_ROUND_HALF_EVEN
3131 && round != _PyTime_ROUND_UP) {
Victor Stinner3c1b3792014-02-17 00:02:43 +01003132 PyErr_SetString(PyExc_ValueError, "invalid rounding");
3133 return -1;
3134 }
3135 return 0;
3136}
3137
Victor Stinner5d272cc2012-03-13 13:35:55 +01003138static PyObject *
3139test_pytime_object_to_time_t(PyObject *self, PyObject *args)
3140{
3141 PyObject *obj;
3142 time_t sec;
Victor Stinner3c1b3792014-02-17 00:02:43 +01003143 int round;
3144 if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round))
Victor Stinner5d272cc2012-03-13 13:35:55 +01003145 return NULL;
Victor Stinner3c1b3792014-02-17 00:02:43 +01003146 if (check_time_rounding(round) < 0)
3147 return NULL;
3148 if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1)
Victor Stinner5d272cc2012-03-13 13:35:55 +01003149 return NULL;
3150 return _PyLong_FromTime_t(sec);
3151}
3152
3153static PyObject *
3154test_pytime_object_to_timeval(PyObject *self, PyObject *args)
3155{
3156 PyObject *obj;
3157 time_t sec;
3158 long usec;
Victor Stinner3c1b3792014-02-17 00:02:43 +01003159 int round;
3160 if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round))
Victor Stinner5d272cc2012-03-13 13:35:55 +01003161 return NULL;
Victor Stinner3c1b3792014-02-17 00:02:43 +01003162 if (check_time_rounding(round) < 0)
3163 return NULL;
3164 if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1)
Victor Stinner5d272cc2012-03-13 13:35:55 +01003165 return NULL;
3166 return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec);
3167}
3168
Victor Stinner643cd682012-03-02 22:54:03 +01003169static PyObject *
3170test_pytime_object_to_timespec(PyObject *self, PyObject *args)
3171{
3172 PyObject *obj;
3173 time_t sec;
3174 long nsec;
Victor Stinner3c1b3792014-02-17 00:02:43 +01003175 int round;
3176 if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round))
Victor Stinner643cd682012-03-02 22:54:03 +01003177 return NULL;
Victor Stinner3c1b3792014-02-17 00:02:43 +01003178 if (check_time_rounding(round) < 0)
3179 return NULL;
3180 if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1)
Victor Stinner643cd682012-03-02 22:54:03 +01003181 return NULL;
Victor Stinner5d272cc2012-03-13 13:35:55 +01003182 return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec);
Victor Stinner643cd682012-03-02 22:54:03 +01003183}
3184
Antoine Pitrou796564c2013-07-30 19:59:21 +02003185static void
3186slot_tp_del(PyObject *self)
3187{
3188 _Py_IDENTIFIER(__tp_del__);
3189 PyObject *del, *res;
3190 PyObject *error_type, *error_value, *error_traceback;
3191
3192 /* Temporarily resurrect the object. */
3193 assert(self->ob_refcnt == 0);
3194 self->ob_refcnt = 1;
3195
3196 /* Save the current exception, if any. */
3197 PyErr_Fetch(&error_type, &error_value, &error_traceback);
3198
3199 /* Execute __del__ method, if any. */
3200 del = _PyObject_LookupSpecial(self, &PyId___tp_del__);
3201 if (del != NULL) {
INADA Naoki72dccde2017-02-16 09:26:01 +09003202 res = _PyObject_CallNoArg(del);
Antoine Pitrou796564c2013-07-30 19:59:21 +02003203 if (res == NULL)
3204 PyErr_WriteUnraisable(del);
3205 else
3206 Py_DECREF(res);
3207 Py_DECREF(del);
3208 }
3209
3210 /* Restore the saved exception. */
3211 PyErr_Restore(error_type, error_value, error_traceback);
3212
3213 /* Undo the temporary resurrection; can't use DECREF here, it would
3214 * cause a recursive call.
3215 */
3216 assert(self->ob_refcnt > 0);
3217 if (--self->ob_refcnt == 0)
3218 return; /* this is the normal path out */
3219
3220 /* __del__ resurrected it! Make it look like the original Py_DECREF
3221 * never happened.
3222 */
3223 {
3224 Py_ssize_t refcnt = self->ob_refcnt;
3225 _Py_NewReference(self);
3226 self->ob_refcnt = refcnt;
3227 }
INADA Naokid8521422018-05-17 11:07:21 +09003228 assert(!PyType_IS_GC(Py_TYPE(self)) || _PyObject_GC_IS_TRACKED(self));
Antoine Pitrou796564c2013-07-30 19:59:21 +02003229 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
3230 * we need to undo that. */
3231 _Py_DEC_REFTOTAL;
3232 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
3233 * chain, so no more to do there.
3234 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
3235 * _Py_NewReference bumped tp_allocs: both of those need to be
3236 * undone.
3237 */
3238#ifdef COUNT_ALLOCS
3239 --Py_TYPE(self)->tp_frees;
3240 --Py_TYPE(self)->tp_allocs;
3241#endif
3242}
3243
3244static PyObject *
3245with_tp_del(PyObject *self, PyObject *args)
3246{
3247 PyObject *obj;
3248 PyTypeObject *tp;
3249
3250 if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj))
3251 return NULL;
3252 tp = (PyTypeObject *) obj;
3253 if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
3254 PyErr_Format(PyExc_TypeError,
3255 "heap type expected, got %R", obj);
3256 return NULL;
3257 }
3258 tp->tp_del = slot_tp_del;
3259 Py_INCREF(obj);
3260 return obj;
3261}
3262
Antoine Pitroub349e4c2014-08-06 19:31:40 -04003263static PyMethodDef ml;
3264
3265static PyObject *
3266create_cfunction(PyObject *self, PyObject *args)
3267{
3268 return PyCFunction_NewEx(&ml, self, NULL);
3269}
3270
3271static PyMethodDef ml = {
3272 "create_cfunction",
3273 create_cfunction,
3274 METH_NOARGS,
3275 NULL
3276};
3277
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -07003278static PyObject *
3279_test_incref(PyObject *ob)
3280{
3281 Py_INCREF(ob);
3282 return ob;
3283}
3284
3285static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303286test_xincref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -07003287{
3288 PyObject *obj = PyLong_FromLong(0);
3289 Py_XINCREF(_test_incref(obj));
3290 Py_DECREF(obj);
3291 Py_DECREF(obj);
3292 Py_DECREF(obj);
3293 Py_RETURN_NONE;
3294}
3295
3296static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303297test_incref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -07003298{
3299 PyObject *obj = PyLong_FromLong(0);
3300 Py_INCREF(_test_incref(obj));
3301 Py_DECREF(obj);
3302 Py_DECREF(obj);
3303 Py_DECREF(obj);
3304 Py_RETURN_NONE;
3305}
3306
3307static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303308test_xdecref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -07003309{
3310 Py_XDECREF(PyLong_FromLong(0));
3311 Py_RETURN_NONE;
3312}
3313
3314static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303315test_decref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -07003316{
3317 Py_DECREF(PyLong_FromLong(0));
3318 Py_RETURN_NONE;
3319}
Antoine Pitrou2f828f22012-01-18 00:21:11 +01003320
Victor Stinner0507bf52013-07-07 02:05:46 +02003321static PyObject *
Eddie Elizondo474eedf2018-11-13 04:09:31 -08003322test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self),
3323 PyObject *Py_UNUSED(args))
3324{
3325 PyStructSequence_Desc descr;
3326 PyStructSequence_Field descr_fields[3];
3327
3328 descr_fields[0] = (PyStructSequence_Field){"foo", "foo value"};
3329 descr_fields[1] = (PyStructSequence_Field){NULL, "some hidden value"};
3330 descr_fields[2] = (PyStructSequence_Field){0, NULL};
3331
3332 descr.name = "_testcapi.test_descr";
3333 descr.doc = "This is used to test for memory leaks in NewType";
3334 descr.fields = descr_fields;
3335 descr.n_in_sequence = 1;
3336
3337 PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
3338 assert(structseq_type != NULL);
3339 assert(PyType_Check(structseq_type));
3340 assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
3341 Py_DECREF(structseq_type);
3342
3343 Py_RETURN_NONE;
3344}
3345
3346static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303347test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored))
Christian Heimes4efdb412013-07-31 02:36:43 +02003348{
3349 PyObject *obj = PyLong_FromLong(0);
Victor Stinnerfc6a90a2014-10-09 22:15:41 +02003350 Py_IncRef(obj);
Christian Heimes4efdb412013-07-31 02:36:43 +02003351 Py_DecRef(obj);
3352 Py_DecRef(obj);
3353 Py_RETURN_NONE;
3354}
3355
3356static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303357test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored))
Victor Stinner0507bf52013-07-07 02:05:46 +02003358{
3359 void *ptr;
3360
Victor Stinnerdb067af2014-05-02 22:31:14 +02003361 ptr = PyMem_RawMalloc(0);
3362 if (ptr == NULL) {
3363 PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL");
3364 return NULL;
3365 }
3366 PyMem_RawFree(ptr);
3367
3368 ptr = PyMem_RawCalloc(0, 0);
3369 if (ptr == NULL) {
3370 PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL");
3371 return NULL;
3372 }
3373 PyMem_RawFree(ptr);
3374
Victor Stinner0507bf52013-07-07 02:05:46 +02003375 ptr = PyMem_Malloc(0);
3376 if (ptr == NULL) {
3377 PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL");
3378 return NULL;
3379 }
3380 PyMem_Free(ptr);
3381
Victor Stinnerdb067af2014-05-02 22:31:14 +02003382 ptr = PyMem_Calloc(0, 0);
3383 if (ptr == NULL) {
3384 PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL");
3385 return NULL;
3386 }
3387 PyMem_Free(ptr);
3388
Victor Stinner0507bf52013-07-07 02:05:46 +02003389 ptr = PyObject_Malloc(0);
3390 if (ptr == NULL) {
3391 PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL");
3392 return NULL;
3393 }
3394 PyObject_Free(ptr);
3395
Victor Stinnerdb067af2014-05-02 22:31:14 +02003396 ptr = PyObject_Calloc(0, 0);
3397 if (ptr == NULL) {
3398 PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL");
3399 return NULL;
3400 }
3401 PyObject_Free(ptr);
3402
Victor Stinner0507bf52013-07-07 02:05:46 +02003403 Py_RETURN_NONE;
3404}
3405
3406typedef struct {
Victor Stinnerd8f0d922014-06-02 21:57:10 +02003407 PyMemAllocatorEx alloc;
Victor Stinner0507bf52013-07-07 02:05:46 +02003408
3409 size_t malloc_size;
Victor Stinnerdb067af2014-05-02 22:31:14 +02003410 size_t calloc_nelem;
3411 size_t calloc_elsize;
Victor Stinner0507bf52013-07-07 02:05:46 +02003412 void *realloc_ptr;
3413 size_t realloc_new_size;
3414 void *free_ptr;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003415 void *ctx;
Victor Stinner0507bf52013-07-07 02:05:46 +02003416} alloc_hook_t;
3417
Victor Stinner9ed83c42017-10-31 12:18:10 -07003418static void* hook_malloc(void* ctx, size_t size)
Victor Stinner0507bf52013-07-07 02:05:46 +02003419{
3420 alloc_hook_t *hook = (alloc_hook_t *)ctx;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003421 hook->ctx = ctx;
Victor Stinner0507bf52013-07-07 02:05:46 +02003422 hook->malloc_size = size;
3423 return hook->alloc.malloc(hook->alloc.ctx, size);
3424}
3425
Victor Stinner9ed83c42017-10-31 12:18:10 -07003426static void* hook_calloc(void* ctx, size_t nelem, size_t elsize)
Victor Stinnerdb067af2014-05-02 22:31:14 +02003427{
3428 alloc_hook_t *hook = (alloc_hook_t *)ctx;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003429 hook->ctx = ctx;
Victor Stinnerdb067af2014-05-02 22:31:14 +02003430 hook->calloc_nelem = nelem;
3431 hook->calloc_elsize = elsize;
3432 return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize);
3433}
3434
Victor Stinner9ed83c42017-10-31 12:18:10 -07003435static void* hook_realloc(void* ctx, void* ptr, size_t new_size)
Victor Stinner0507bf52013-07-07 02:05:46 +02003436{
3437 alloc_hook_t *hook = (alloc_hook_t *)ctx;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003438 hook->ctx = ctx;
Victor Stinner0507bf52013-07-07 02:05:46 +02003439 hook->realloc_ptr = ptr;
3440 hook->realloc_new_size = new_size;
3441 return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size);
3442}
3443
Victor Stinner9ed83c42017-10-31 12:18:10 -07003444static void hook_free(void *ctx, void *ptr)
Victor Stinner0507bf52013-07-07 02:05:46 +02003445{
3446 alloc_hook_t *hook = (alloc_hook_t *)ctx;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003447 hook->ctx = ctx;
Victor Stinner0507bf52013-07-07 02:05:46 +02003448 hook->free_ptr = ptr;
3449 hook->alloc.free(hook->alloc.ctx, ptr);
3450}
3451
3452static PyObject *
3453test_setallocators(PyMemAllocatorDomain domain)
3454{
3455 PyObject *res = NULL;
3456 const char *error_msg;
3457 alloc_hook_t hook;
Victor Stinnerd8f0d922014-06-02 21:57:10 +02003458 PyMemAllocatorEx alloc;
Victor Stinnerdb067af2014-05-02 22:31:14 +02003459 size_t size, size2, nelem, elsize;
Victor Stinner0507bf52013-07-07 02:05:46 +02003460 void *ptr, *ptr2;
3461
Victor Stinnerdb067af2014-05-02 22:31:14 +02003462 memset(&hook, 0, sizeof(hook));
Victor Stinner0507bf52013-07-07 02:05:46 +02003463
3464 alloc.ctx = &hook;
3465 alloc.malloc = &hook_malloc;
Victor Stinnerdb067af2014-05-02 22:31:14 +02003466 alloc.calloc = &hook_calloc;
Victor Stinner0507bf52013-07-07 02:05:46 +02003467 alloc.realloc = &hook_realloc;
3468 alloc.free = &hook_free;
3469 PyMem_GetAllocator(domain, &hook.alloc);
3470 PyMem_SetAllocator(domain, &alloc);
3471
Victor Stinner9ed83c42017-10-31 12:18:10 -07003472 /* malloc, realloc, free */
Victor Stinner0507bf52013-07-07 02:05:46 +02003473 size = 42;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003474 hook.ctx = NULL;
Victor Stinner0507bf52013-07-07 02:05:46 +02003475 switch(domain)
3476 {
3477 case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break;
3478 case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break;
3479 case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break;
3480 default: ptr = NULL; break;
3481 }
3482
Victor Stinner9ed83c42017-10-31 12:18:10 -07003483#define CHECK_CTX(FUNC) \
3484 if (hook.ctx != &hook) { \
3485 error_msg = FUNC " wrong context"; \
3486 goto fail; \
3487 } \
3488 hook.ctx = NULL; /* reset for next check */
3489
Victor Stinner0507bf52013-07-07 02:05:46 +02003490 if (ptr == NULL) {
3491 error_msg = "malloc failed";
3492 goto fail;
3493 }
Victor Stinner9ed83c42017-10-31 12:18:10 -07003494 CHECK_CTX("malloc");
Victor Stinner0507bf52013-07-07 02:05:46 +02003495 if (hook.malloc_size != size) {
3496 error_msg = "malloc invalid size";
3497 goto fail;
3498 }
3499
3500 size2 = 200;
3501 switch(domain)
3502 {
3503 case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break;
3504 case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break;
3505 case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break;
Christian Heimes865d12a2013-08-02 11:10:51 +02003506 default: ptr2 = NULL; break;
Victor Stinner0507bf52013-07-07 02:05:46 +02003507 }
3508
3509 if (ptr2 == NULL) {
3510 error_msg = "realloc failed";
3511 goto fail;
3512 }
Victor Stinner9ed83c42017-10-31 12:18:10 -07003513 CHECK_CTX("realloc");
Victor Stinner0507bf52013-07-07 02:05:46 +02003514 if (hook.realloc_ptr != ptr
3515 || hook.realloc_new_size != size2) {
3516 error_msg = "realloc invalid parameters";
3517 goto fail;
3518 }
3519
3520 switch(domain)
3521 {
3522 case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break;
3523 case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break;
3524 case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break;
3525 }
3526
Victor Stinner9ed83c42017-10-31 12:18:10 -07003527 CHECK_CTX("free");
Victor Stinner0507bf52013-07-07 02:05:46 +02003528 if (hook.free_ptr != ptr2) {
3529 error_msg = "free invalid pointer";
3530 goto fail;
3531 }
3532
Victor Stinner9ed83c42017-10-31 12:18:10 -07003533 /* calloc, free */
Victor Stinnerdb067af2014-05-02 22:31:14 +02003534 nelem = 2;
3535 elsize = 5;
3536 switch(domain)
3537 {
3538 case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break;
3539 case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break;
3540 case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break;
3541 default: ptr = NULL; break;
3542 }
3543
3544 if (ptr == NULL) {
3545 error_msg = "calloc failed";
3546 goto fail;
3547 }
Victor Stinner9ed83c42017-10-31 12:18:10 -07003548 CHECK_CTX("calloc");
Victor Stinnerdb067af2014-05-02 22:31:14 +02003549 if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) {
3550 error_msg = "calloc invalid nelem or elsize";
3551 goto fail;
3552 }
3553
Victor Stinner9ed83c42017-10-31 12:18:10 -07003554 hook.free_ptr = NULL;
Victor Stinnerdb067af2014-05-02 22:31:14 +02003555 switch(domain)
3556 {
3557 case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break;
3558 case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break;
3559 case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break;
3560 }
3561
Victor Stinner9ed83c42017-10-31 12:18:10 -07003562 CHECK_CTX("calloc free");
3563 if (hook.free_ptr != ptr) {
3564 error_msg = "calloc free invalid pointer";
3565 goto fail;
3566 }
3567
Victor Stinner0507bf52013-07-07 02:05:46 +02003568 Py_INCREF(Py_None);
3569 res = Py_None;
3570 goto finally;
3571
3572fail:
3573 PyErr_SetString(PyExc_RuntimeError, error_msg);
3574
3575finally:
3576 PyMem_SetAllocator(domain, &hook.alloc);
3577 return res;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003578
3579#undef CHECK_CTX
Victor Stinner0507bf52013-07-07 02:05:46 +02003580}
3581
3582static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303583test_pymem_setrawallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
Victor Stinner0507bf52013-07-07 02:05:46 +02003584{
3585 return test_setallocators(PYMEM_DOMAIN_RAW);
3586}
3587
3588static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303589test_pymem_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
Victor Stinner0507bf52013-07-07 02:05:46 +02003590{
3591 return test_setallocators(PYMEM_DOMAIN_MEM);
3592}
3593
3594static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303595test_pyobject_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
Victor Stinner0507bf52013-07-07 02:05:46 +02003596{
3597 return test_setallocators(PYMEM_DOMAIN_OBJ);
3598}
3599
xdegaye85f64302017-07-01 14:14:45 +02003600/* Most part of the following code is inherited from the pyfailmalloc project
3601 * written by Victor Stinner. */
3602static struct {
3603 int installed;
3604 PyMemAllocatorEx raw;
3605 PyMemAllocatorEx mem;
3606 PyMemAllocatorEx obj;
3607} FmHook;
3608
3609static struct {
3610 int start;
3611 int stop;
3612 Py_ssize_t count;
3613} FmData;
3614
3615static int
3616fm_nomemory(void)
3617{
3618 FmData.count++;
3619 if (FmData.count > FmData.start &&
3620 (FmData.stop <= 0 || FmData.count <= FmData.stop)) {
3621 return 1;
3622 }
3623 return 0;
3624}
3625
3626static void *
3627hook_fmalloc(void *ctx, size_t size)
3628{
3629 PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
3630 if (fm_nomemory()) {
3631 return NULL;
3632 }
3633 return alloc->malloc(alloc->ctx, size);
3634}
3635
3636static void *
3637hook_fcalloc(void *ctx, size_t nelem, size_t elsize)
3638{
3639 PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
3640 if (fm_nomemory()) {
3641 return NULL;
3642 }
3643 return alloc->calloc(alloc->ctx, nelem, elsize);
3644}
3645
3646static void *
3647hook_frealloc(void *ctx, void *ptr, size_t new_size)
3648{
3649 PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
3650 if (fm_nomemory()) {
3651 return NULL;
3652 }
3653 return alloc->realloc(alloc->ctx, ptr, new_size);
3654}
3655
3656static void
3657hook_ffree(void *ctx, void *ptr)
3658{
3659 PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
3660 alloc->free(alloc->ctx, ptr);
3661}
3662
3663static void
3664fm_setup_hooks(void)
3665{
3666 PyMemAllocatorEx alloc;
3667
3668 if (FmHook.installed) {
3669 return;
3670 }
3671 FmHook.installed = 1;
3672
3673 alloc.malloc = hook_fmalloc;
3674 alloc.calloc = hook_fcalloc;
3675 alloc.realloc = hook_frealloc;
3676 alloc.free = hook_ffree;
3677 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw);
3678 PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem);
3679 PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj);
3680
3681 alloc.ctx = &FmHook.raw;
3682 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
3683
3684 alloc.ctx = &FmHook.mem;
3685 PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc);
3686
3687 alloc.ctx = &FmHook.obj;
3688 PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc);
3689}
3690
3691static void
3692fm_remove_hooks(void)
3693{
3694 if (FmHook.installed) {
3695 FmHook.installed = 0;
3696 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw);
3697 PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem);
3698 PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj);
3699 }
3700}
3701
3702static PyObject*
3703set_nomemory(PyObject *self, PyObject *args)
3704{
3705 /* Memory allocation fails after 'start' allocation requests, and until
3706 * 'stop' allocation requests except when 'stop' is negative or equal
3707 * to 0 (default) in which case allocation failures never stop. */
3708 FmData.count = 0;
3709 FmData.stop = 0;
3710 if (!PyArg_ParseTuple(args, "i|i", &FmData.start, &FmData.stop)) {
3711 return NULL;
3712 }
3713 fm_setup_hooks();
3714 Py_RETURN_NONE;
3715}
3716
3717static PyObject*
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303718remove_mem_hooks(PyObject *self, PyObject *Py_UNUSED(ignored))
xdegaye85f64302017-07-01 14:14:45 +02003719{
3720 fm_remove_hooks();
3721 Py_RETURN_NONE;
3722}
3723
Larry Hastings44e2eaa2013-11-23 15:37:55 -08003724PyDoc_STRVAR(docstring_empty,
3725""
3726);
3727
3728PyDoc_STRVAR(docstring_no_signature,
3729"This docstring has no signature."
3730);
3731
3732PyDoc_STRVAR(docstring_with_invalid_signature,
Larry Hastings2623c8c2014-02-08 22:15:29 -08003733"docstring_with_invalid_signature($module, /, boo)\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -08003734"\n"
3735"This docstring has an invalid signature."
3736);
3737
Larry Hastings2623c8c2014-02-08 22:15:29 -08003738PyDoc_STRVAR(docstring_with_invalid_signature2,
3739"docstring_with_invalid_signature2($module, /, boo)\n"
3740"\n"
3741"--\n"
3742"\n"
3743"This docstring also has an invalid signature."
3744);
3745
Larry Hastings44e2eaa2013-11-23 15:37:55 -08003746PyDoc_STRVAR(docstring_with_signature,
Larry Hastings2623c8c2014-02-08 22:15:29 -08003747"docstring_with_signature($module, /, sig)\n"
3748"--\n"
3749"\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -08003750"This docstring has a valid signature."
3751);
3752
Zachary Ware8ef887c2015-04-13 18:22:35 -05003753PyDoc_STRVAR(docstring_with_signature_but_no_doc,
3754"docstring_with_signature_but_no_doc($module, /, sig)\n"
3755"--\n"
3756"\n"
3757);
3758
Larry Hastings44e2eaa2013-11-23 15:37:55 -08003759PyDoc_STRVAR(docstring_with_signature_and_extra_newlines,
Larry Hastings2623c8c2014-02-08 22:15:29 -08003760"docstring_with_signature_and_extra_newlines($module, /, parameter)\n"
3761"--\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -08003762"\n"
3763"\n"
3764"This docstring has a valid signature and some extra newlines."
3765);
3766
Larry Hastings16c51912014-01-07 11:53:01 -08003767PyDoc_STRVAR(docstring_with_signature_with_defaults,
Larry Hastings2623c8c2014-02-08 22:15:29 -08003768"docstring_with_signature_with_defaults(module, s='avocado',\n"
3769" b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n"
3770" local=the_number_three, sys=sys.maxsize,\n"
3771" exp=sys.maxsize - 1)\n"
3772"--\n"
Larry Hastings16c51912014-01-07 11:53:01 -08003773"\n"
3774"\n"
3775"\n"
3776"This docstring has a valid signature with parameters,\n"
3777"and the parameters take defaults of varying types."
3778);
3779
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003780typedef struct {
3781 PyThread_type_lock start_event;
3782 PyThread_type_lock exit_event;
3783 PyObject *callback;
3784} test_c_thread_t;
3785
3786static void
3787temporary_c_thread(void *data)
3788{
3789 test_c_thread_t *test_c_thread = data;
3790 PyGILState_STATE state;
3791 PyObject *res;
3792
3793 PyThread_release_lock(test_c_thread->start_event);
3794
3795 /* Allocate a Python thread state for this thread */
3796 state = PyGILState_Ensure();
3797
Victor Stinner3466bde2016-09-05 18:16:01 -07003798 res = _PyObject_CallNoArg(test_c_thread->callback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003799 Py_CLEAR(test_c_thread->callback);
3800
3801 if (res == NULL) {
3802 PyErr_Print();
3803 }
3804 else {
3805 Py_DECREF(res);
3806 }
3807
3808 /* Destroy the Python thread state for this thread */
3809 PyGILState_Release(state);
3810
3811 PyThread_release_lock(test_c_thread->exit_event);
3812
3813 PyThread_exit_thread();
3814}
3815
3816static PyObject *
3817call_in_temporary_c_thread(PyObject *self, PyObject *callback)
3818{
3819 PyObject *res = NULL;
3820 test_c_thread_t test_c_thread;
3821 long thread;
3822
3823 PyEval_InitThreads();
3824
3825 test_c_thread.start_event = PyThread_allocate_lock();
3826 test_c_thread.exit_event = PyThread_allocate_lock();
3827 test_c_thread.callback = NULL;
3828 if (!test_c_thread.start_event || !test_c_thread.exit_event) {
3829 PyErr_SetString(PyExc_RuntimeError, "could not allocate lock");
3830 goto exit;
3831 }
3832
3833 Py_INCREF(callback);
3834 test_c_thread.callback = callback;
3835
3836 PyThread_acquire_lock(test_c_thread.start_event, 1);
3837 PyThread_acquire_lock(test_c_thread.exit_event, 1);
3838
3839 thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread);
3840 if (thread == -1) {
3841 PyErr_SetString(PyExc_RuntimeError, "unable to start the thread");
3842 PyThread_release_lock(test_c_thread.start_event);
3843 PyThread_release_lock(test_c_thread.exit_event);
3844 goto exit;
3845 }
3846
3847 PyThread_acquire_lock(test_c_thread.start_event, 1);
3848 PyThread_release_lock(test_c_thread.start_event);
3849
3850 Py_BEGIN_ALLOW_THREADS
3851 PyThread_acquire_lock(test_c_thread.exit_event, 1);
3852 PyThread_release_lock(test_c_thread.exit_event);
3853 Py_END_ALLOW_THREADS
3854
3855 Py_INCREF(Py_None);
3856 res = Py_None;
3857
3858exit:
3859 Py_CLEAR(test_c_thread.callback);
3860 if (test_c_thread.start_event)
3861 PyThread_free_lock(test_c_thread.start_event);
3862 if (test_c_thread.exit_event)
3863 PyThread_free_lock(test_c_thread.exit_event);
3864 return res;
3865}
Victor Stinner13105102013-12-13 02:17:29 +01003866
Serhiy Storchakab5181342015-02-06 08:58:56 +02003867/* marshal */
3868
3869static PyObject*
3870pymarshal_write_long_to_file(PyObject* self, PyObject *args)
3871{
3872 long value;
3873 char *filename;
3874 int version;
3875 FILE *fp;
3876
3877 if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file",
3878 &value, &filename, &version))
3879 return NULL;
3880
3881 fp = fopen(filename, "wb");
3882 if (fp == NULL) {
3883 PyErr_SetFromErrno(PyExc_OSError);
3884 return NULL;
3885 }
3886
3887 PyMarshal_WriteLongToFile(value, fp, version);
3888
3889 fclose(fp);
3890 if (PyErr_Occurred())
3891 return NULL;
3892 Py_RETURN_NONE;
3893}
3894
3895static PyObject*
3896pymarshal_write_object_to_file(PyObject* self, PyObject *args)
3897{
3898 PyObject *obj;
3899 char *filename;
3900 int version;
3901 FILE *fp;
3902
3903 if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file",
3904 &obj, &filename, &version))
3905 return NULL;
3906
3907 fp = fopen(filename, "wb");
3908 if (fp == NULL) {
3909 PyErr_SetFromErrno(PyExc_OSError);
3910 return NULL;
3911 }
3912
3913 PyMarshal_WriteObjectToFile(obj, fp, version);
3914
3915 fclose(fp);
3916 if (PyErr_Occurred())
3917 return NULL;
3918 Py_RETURN_NONE;
3919}
3920
3921static PyObject*
3922pymarshal_read_short_from_file(PyObject* self, PyObject *args)
3923{
3924 int value;
3925 long pos;
3926 char *filename;
3927 FILE *fp;
3928
3929 if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename))
3930 return NULL;
3931
3932 fp = fopen(filename, "rb");
3933 if (fp == NULL) {
3934 PyErr_SetFromErrno(PyExc_OSError);
3935 return NULL;
3936 }
3937
3938 value = PyMarshal_ReadShortFromFile(fp);
3939 pos = ftell(fp);
3940
3941 fclose(fp);
3942 if (PyErr_Occurred())
3943 return NULL;
3944 return Py_BuildValue("il", value, pos);
3945}
3946
3947static PyObject*
3948pymarshal_read_long_from_file(PyObject* self, PyObject *args)
3949{
3950 long value, pos;
3951 char *filename;
3952 FILE *fp;
3953
3954 if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename))
3955 return NULL;
3956
3957 fp = fopen(filename, "rb");
3958 if (fp == NULL) {
3959 PyErr_SetFromErrno(PyExc_OSError);
3960 return NULL;
3961 }
3962
3963 value = PyMarshal_ReadLongFromFile(fp);
3964 pos = ftell(fp);
3965
3966 fclose(fp);
3967 if (PyErr_Occurred())
3968 return NULL;
3969 return Py_BuildValue("ll", value, pos);
3970}
3971
3972static PyObject*
3973pymarshal_read_last_object_from_file(PyObject* self, PyObject *args)
3974{
3975 PyObject *obj;
3976 long pos;
3977 char *filename;
3978 FILE *fp;
3979
3980 if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename))
3981 return NULL;
3982
3983 fp = fopen(filename, "rb");
3984 if (fp == NULL) {
3985 PyErr_SetFromErrno(PyExc_OSError);
3986 return NULL;
3987 }
3988
3989 obj = PyMarshal_ReadLastObjectFromFile(fp);
3990 pos = ftell(fp);
3991
3992 fclose(fp);
3993 return Py_BuildValue("Nl", obj, pos);
3994}
3995
3996static PyObject*
3997pymarshal_read_object_from_file(PyObject* self, PyObject *args)
3998{
3999 PyObject *obj;
4000 long pos;
4001 char *filename;
4002 FILE *fp;
4003
4004 if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename))
4005 return NULL;
4006
4007 fp = fopen(filename, "rb");
4008 if (fp == NULL) {
4009 PyErr_SetFromErrno(PyExc_OSError);
4010 return NULL;
4011 }
4012
4013 obj = PyMarshal_ReadObjectFromFile(fp);
4014 pos = ftell(fp);
4015
4016 fclose(fp);
4017 return Py_BuildValue("Nl", obj, pos);
4018}
4019
Victor Stinnerefde1462015-03-21 15:04:43 +01004020static PyObject*
4021return_null_without_error(PyObject *self, PyObject *args)
4022{
4023 /* invalid call: return NULL without setting an error,
4024 * _Py_CheckFunctionResult() must detect such bug at runtime. */
4025 PyErr_Clear();
4026 return NULL;
4027}
4028
4029static PyObject*
4030return_result_with_error(PyObject *self, PyObject *args)
4031{
4032 /* invalid call: return a result with an error set,
4033 * _Py_CheckFunctionResult() must detect such bug at runtime. */
4034 PyErr_SetNone(PyExc_ValueError);
4035 Py_RETURN_NONE;
4036}
4037
Victor Stinner992c43f2015-03-27 17:12:45 +01004038static PyObject *
Victor Stinner13019fd2015-04-03 13:10:54 +02004039test_pytime_fromseconds(PyObject *self, PyObject *args)
4040{
4041 int seconds;
4042 _PyTime_t ts;
4043
4044 if (!PyArg_ParseTuple(args, "i", &seconds))
4045 return NULL;
4046 ts = _PyTime_FromSeconds(seconds);
4047 return _PyTime_AsNanosecondsObject(ts);
4048}
4049
4050static PyObject *
Victor Stinner992c43f2015-03-27 17:12:45 +01004051test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
4052{
4053 PyObject *obj;
4054 int round;
4055 _PyTime_t ts;
4056
4057 if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
4058 return NULL;
4059 if (check_time_rounding(round) < 0)
4060 return NULL;
4061 if (_PyTime_FromSecondsObject(&ts, obj, round) == -1)
4062 return NULL;
4063 return _PyTime_AsNanosecondsObject(ts);
4064}
4065
Victor Stinner4bfb4602015-03-27 22:27:24 +01004066static PyObject *
4067test_pytime_assecondsdouble(PyObject *self, PyObject *args)
4068{
Victor Stinnerc29b5852017-11-02 07:28:27 -07004069 PyObject *obj;
Victor Stinner4bfb4602015-03-27 22:27:24 +01004070 _PyTime_t ts;
4071 double d;
4072
Victor Stinnerc29b5852017-11-02 07:28:27 -07004073 if (!PyArg_ParseTuple(args, "O", &obj)) {
Victor Stinner4bfb4602015-03-27 22:27:24 +01004074 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004075 }
4076 if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) {
4077 return NULL;
4078 }
Victor Stinner4bfb4602015-03-27 22:27:24 +01004079 d = _PyTime_AsSecondsDouble(ts);
4080 return PyFloat_FromDouble(d);
4081}
4082
Victor Stinner95e9cef2015-03-28 01:26:47 +01004083static PyObject *
4084test_PyTime_AsTimeval(PyObject *self, PyObject *args)
4085{
Victor Stinnerc29b5852017-11-02 07:28:27 -07004086 PyObject *obj;
Victor Stinner95e9cef2015-03-28 01:26:47 +01004087 int round;
4088 _PyTime_t t;
4089 struct timeval tv;
4090 PyObject *seconds;
4091
Victor Stinnerc29b5852017-11-02 07:28:27 -07004092 if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
Victor Stinner95e9cef2015-03-28 01:26:47 +01004093 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004094 if (check_time_rounding(round) < 0) {
Victor Stinner95e9cef2015-03-28 01:26:47 +01004095 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004096 }
4097 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
Victor Stinner95e9cef2015-03-28 01:26:47 +01004098 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004099 }
4100 if (_PyTime_AsTimeval(t, &tv, round) < 0) {
4101 return NULL;
4102 }
Victor Stinner95e9cef2015-03-28 01:26:47 +01004103
Benjamin Peterson2c134c32017-04-13 01:44:54 -07004104 seconds = PyLong_FromLongLong(tv.tv_sec);
Victor Stinnerc29b5852017-11-02 07:28:27 -07004105 if (seconds == NULL) {
Victor Stinner95e9cef2015-03-28 01:26:47 +01004106 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004107 }
Victor Stinner95e9cef2015-03-28 01:26:47 +01004108 return Py_BuildValue("Nl", seconds, tv.tv_usec);
4109}
4110
Victor Stinner34dc0f42015-03-27 18:19:03 +01004111#ifdef HAVE_CLOCK_GETTIME
4112static PyObject *
4113test_PyTime_AsTimespec(PyObject *self, PyObject *args)
4114{
Victor Stinnerc29b5852017-11-02 07:28:27 -07004115 PyObject *obj;
Victor Stinner34dc0f42015-03-27 18:19:03 +01004116 _PyTime_t t;
4117 struct timespec ts;
4118
Victor Stinnerc29b5852017-11-02 07:28:27 -07004119 if (!PyArg_ParseTuple(args, "O", &obj)) {
Victor Stinner34dc0f42015-03-27 18:19:03 +01004120 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004121 }
4122 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
Victor Stinner34dc0f42015-03-27 18:19:03 +01004123 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004124 }
4125 if (_PyTime_AsTimespec(t, &ts) == -1) {
4126 return NULL;
4127 }
Victor Stinner34dc0f42015-03-27 18:19:03 +01004128 return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec);
4129}
4130#endif
4131
Victor Stinner62d1c702015-04-01 17:47:07 +02004132static PyObject *
4133test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
4134{
Victor Stinnerc29b5852017-11-02 07:28:27 -07004135 PyObject *obj;
Victor Stinner62d1c702015-04-01 17:47:07 +02004136 int round;
4137 _PyTime_t t, ms;
4138
Victor Stinnerc29b5852017-11-02 07:28:27 -07004139 if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
Victor Stinner62d1c702015-04-01 17:47:07 +02004140 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004141 }
4142 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
Victor Stinner62d1c702015-04-01 17:47:07 +02004143 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004144 }
4145 if (check_time_rounding(round) < 0) {
4146 return NULL;
4147 }
Victor Stinner62d1c702015-04-01 17:47:07 +02004148 ms = _PyTime_AsMilliseconds(t, round);
4149 /* This conversion rely on the fact that _PyTime_t is a number of
4150 nanoseconds */
4151 return _PyTime_AsNanosecondsObject(ms);
4152}
4153
4154static PyObject *
4155test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
4156{
Victor Stinnerc29b5852017-11-02 07:28:27 -07004157 PyObject *obj;
Victor Stinner62d1c702015-04-01 17:47:07 +02004158 int round;
4159 _PyTime_t t, ms;
4160
Victor Stinnerc29b5852017-11-02 07:28:27 -07004161 if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
Victor Stinner62d1c702015-04-01 17:47:07 +02004162 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004163 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
Victor Stinner62d1c702015-04-01 17:47:07 +02004164 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004165 }
4166 if (check_time_rounding(round) < 0) {
4167 return NULL;
4168 }
Victor Stinner62d1c702015-04-01 17:47:07 +02004169 ms = _PyTime_AsMicroseconds(t, round);
4170 /* This conversion rely on the fact that _PyTime_t is a number of
4171 nanoseconds */
4172 return _PyTime_AsNanosecondsObject(ms);
4173}
4174
Victor Stinner50856d52015-10-13 00:11:21 +02004175static PyObject*
4176get_recursion_depth(PyObject *self, PyObject *args)
4177{
Victor Stinner50b48572018-11-01 01:51:40 +01004178 PyThreadState *tstate = PyThreadState_Get();
Victor Stinner50856d52015-10-13 00:11:21 +02004179
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07004180 /* subtract one to ignore the frame of the get_recursion_depth() call */
Victor Stinner50856d52015-10-13 00:11:21 +02004181 return PyLong_FromLong(tstate->recursion_depth - 1);
4182}
4183
Victor Stinner34be807c2016-03-14 12:04:26 +01004184static PyObject*
4185pymem_buffer_overflow(PyObject *self, PyObject *args)
4186{
4187 char *buffer;
4188
4189 /* Deliberate buffer overflow to check that PyMem_Free() detects
4190 the overflow when debug hooks are installed. */
4191 buffer = PyMem_Malloc(16);
Victor Stinner414b1cd2019-03-26 14:35:30 +01004192 if (buffer == NULL) {
4193 PyErr_NoMemory();
4194 return NULL;
4195 }
Victor Stinner34be807c2016-03-14 12:04:26 +01004196 buffer[16] = 'x';
4197 PyMem_Free(buffer);
4198
4199 Py_RETURN_NONE;
4200}
4201
4202static PyObject*
4203pymem_api_misuse(PyObject *self, PyObject *args)
4204{
4205 char *buffer;
4206
4207 /* Deliberate misusage of Python allocators:
4208 allococate with PyMem but release with PyMem_Raw. */
4209 buffer = PyMem_Malloc(16);
4210 PyMem_RawFree(buffer);
4211
4212 Py_RETURN_NONE;
4213}
4214
Victor Stinnerc4aec362016-03-14 22:26:53 +01004215static PyObject*
Victor Stinnerad524372016-03-16 12:12:53 +01004216pymem_malloc_without_gil(PyObject *self, PyObject *args)
4217{
4218 char *buffer;
4219
4220 /* Deliberate bug to test debug hooks on Python memory allocators:
4221 call PyMem_Malloc() without holding the GIL */
4222 Py_BEGIN_ALLOW_THREADS
4223 buffer = PyMem_Malloc(10);
4224 Py_END_ALLOW_THREADS
4225
4226 PyMem_Free(buffer);
4227
4228 Py_RETURN_NONE;
4229}
4230
Victor Stinner5d39e042017-11-29 17:20:38 +01004231
4232static PyObject*
4233test_pymem_getallocatorsname(PyObject *self, PyObject *args)
4234{
4235 const char *name = _PyMem_GetAllocatorsName();
4236 if (name == NULL) {
4237 PyErr_SetString(PyExc_RuntimeError, "cannot get allocators name");
4238 return NULL;
4239 }
4240 return PyUnicode_FromString(name);
4241}
4242
4243
Victor Stinnerad524372016-03-16 12:12:53 +01004244static PyObject*
Victor Stinner2b00db62019-04-11 11:33:27 +02004245pyobject_is_freed(PyObject *self, PyObject *op)
4246{
4247 int res = _PyObject_IsFreed(op);
4248 return PyBool_FromLong(res);
4249}
4250
4251
4252static PyObject*
4253pyobject_uninitialized(PyObject *self, PyObject *args)
4254{
4255 PyObject *op = (PyObject *)PyObject_Malloc(sizeof(PyObject));
4256 if (op == NULL) {
4257 return NULL;
4258 }
4259 /* Initialize reference count to avoid early crash in ceval or GC */
4260 Py_REFCNT(op) = 1;
4261 /* object fields like ob_type are uninitialized! */
4262 return op;
4263}
4264
4265
4266static PyObject*
4267pyobject_forbidden_bytes(PyObject *self, PyObject *args)
4268{
4269 /* Allocate an incomplete PyObject structure: truncate 'ob_type' field */
4270 PyObject *op = (PyObject *)PyObject_Malloc(offsetof(PyObject, ob_type));
4271 if (op == NULL) {
4272 return NULL;
4273 }
4274 /* Initialize reference count to avoid early crash in ceval or GC */
4275 Py_REFCNT(op) = 1;
4276 /* ob_type field is after the memory block: part of "forbidden bytes"
4277 when using debug hooks on memory allocatrs! */
4278 return op;
4279}
4280
4281
4282static PyObject*
4283pyobject_freed(PyObject *self, PyObject *args)
4284{
4285 PyObject *op = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type);
4286 if (op == NULL) {
4287 return NULL;
4288 }
4289 Py_TYPE(op)->tp_dealloc(op);
4290 /* Reset reference count to avoid early crash in ceval or GC */
4291 Py_REFCNT(op) = 1;
4292 /* object memory is freed! */
4293 return op;
4294}
4295
4296
4297static PyObject*
Victor Stinnerc4aec362016-03-14 22:26:53 +01004298pyobject_malloc_without_gil(PyObject *self, PyObject *args)
4299{
4300 char *buffer;
4301
Victor Stinnerad524372016-03-16 12:12:53 +01004302 /* Deliberate bug to test debug hooks on Python memory allocators:
4303 call PyObject_Malloc() without holding the GIL */
Victor Stinnerc4aec362016-03-14 22:26:53 +01004304 Py_BEGIN_ALLOW_THREADS
4305 buffer = PyObject_Malloc(10);
4306 Py_END_ALLOW_THREADS
4307
4308 PyObject_Free(buffer);
4309
4310 Py_RETURN_NONE;
4311}
4312
Victor Stinner10b73e12016-03-22 13:39:05 +01004313static PyObject *
4314tracemalloc_track(PyObject *self, PyObject *args)
4315{
4316 unsigned int domain;
4317 PyObject *ptr_obj;
4318 void *ptr;
4319 Py_ssize_t size;
4320 int release_gil = 0;
4321 int res;
4322
4323 if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil))
4324 return NULL;
4325 ptr = PyLong_AsVoidPtr(ptr_obj);
4326 if (PyErr_Occurred())
4327 return NULL;
4328
4329 if (release_gil) {
4330 Py_BEGIN_ALLOW_THREADS
Victor Stinner5ea4c062017-06-20 17:46:36 +02004331 res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
Victor Stinner10b73e12016-03-22 13:39:05 +01004332 Py_END_ALLOW_THREADS
4333 }
4334 else {
Victor Stinner5ea4c062017-06-20 17:46:36 +02004335 res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
Victor Stinner10b73e12016-03-22 13:39:05 +01004336 }
4337
4338 if (res < 0) {
Victor Stinner5ea4c062017-06-20 17:46:36 +02004339 PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Track error");
Victor Stinner10b73e12016-03-22 13:39:05 +01004340 return NULL;
4341 }
4342
4343 Py_RETURN_NONE;
4344}
4345
4346static PyObject *
4347tracemalloc_untrack(PyObject *self, PyObject *args)
4348{
4349 unsigned int domain;
4350 PyObject *ptr_obj;
4351 void *ptr;
4352 int res;
4353
4354 if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
4355 return NULL;
4356 ptr = PyLong_AsVoidPtr(ptr_obj);
4357 if (PyErr_Occurred())
4358 return NULL;
4359
Victor Stinner5ea4c062017-06-20 17:46:36 +02004360 res = PyTraceMalloc_Untrack(domain, (uintptr_t)ptr);
Victor Stinner10b73e12016-03-22 13:39:05 +01004361 if (res < 0) {
Victor Stinner5ea4c062017-06-20 17:46:36 +02004362 PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Untrack error");
Victor Stinner10b73e12016-03-22 13:39:05 +01004363 return NULL;
4364 }
4365
4366 Py_RETURN_NONE;
4367}
4368
4369static PyObject *
4370tracemalloc_get_traceback(PyObject *self, PyObject *args)
4371{
4372 unsigned int domain;
4373 PyObject *ptr_obj;
4374 void *ptr;
4375
4376 if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
4377 return NULL;
4378 ptr = PyLong_AsVoidPtr(ptr_obj);
4379 if (PyErr_Occurred())
4380 return NULL;
4381
Benjamin Petersonca470632016-09-06 13:47:26 -07004382 return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr);
Victor Stinner10b73e12016-03-22 13:39:05 +01004383}
4384
Victor Stinner3b6a6b42016-09-08 12:51:24 -07004385static PyObject *
4386dict_get_version(PyObject *self, PyObject *args)
4387{
4388 PyDictObject *dict;
4389 uint64_t version;
4390
4391 if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict))
4392 return NULL;
4393
4394 version = dict->ma_version_tag;
4395
4396 Py_BUILD_ASSERT(sizeof(unsigned PY_LONG_LONG) >= sizeof(version));
4397 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)version);
4398}
4399
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004400
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07004401static PyObject *
4402raise_SIGINT_then_send_None(PyObject *self, PyObject *args)
4403{
4404 PyGenObject *gen;
4405
4406 if (!PyArg_ParseTuple(args, "O!", &PyGen_Type, &gen))
4407 return NULL;
4408
4409 /* This is used in a test to check what happens if a signal arrives just
4410 as we're in the process of entering a yield from chain (see
4411 bpo-30039).
4412
4413 Needs to be done in C, because:
4414 - we don't have a Python wrapper for raise()
4415 - we need to make sure that the Python-level signal handler doesn't run
4416 *before* we enter the generator frame, which is impossible in Python
4417 because we check for signals before every bytecode operation.
4418 */
4419 raise(SIGINT);
4420 return _PyGen_Send(gen, Py_None);
4421}
4422
4423
Victor Stinner3b5cf852017-06-09 16:48:45 +02004424static int
4425fastcall_args(PyObject *args, PyObject ***stack, Py_ssize_t *nargs)
4426{
4427 if (args == Py_None) {
4428 *stack = NULL;
4429 *nargs = 0;
4430 }
4431 else if (PyTuple_Check(args)) {
Victor Stinnerd17a6932018-11-09 16:56:48 +01004432 *stack = ((PyTupleObject *)args)->ob_item;
Victor Stinner3b5cf852017-06-09 16:48:45 +02004433 *nargs = PyTuple_GET_SIZE(args);
4434 }
4435 else {
4436 PyErr_SetString(PyExc_TypeError, "args must be None or a tuple");
4437 return -1;
4438 }
4439 return 0;
4440}
4441
4442
4443static PyObject *
4444test_pyobject_fastcall(PyObject *self, PyObject *args)
4445{
4446 PyObject *func, *func_args;
4447 PyObject **stack;
4448 Py_ssize_t nargs;
4449
4450 if (!PyArg_ParseTuple(args, "OO", &func, &func_args)) {
4451 return NULL;
4452 }
4453
4454 if (fastcall_args(func_args, &stack, &nargs) < 0) {
4455 return NULL;
4456 }
4457 return _PyObject_FastCall(func, stack, nargs);
4458}
4459
4460
4461static PyObject *
4462test_pyobject_fastcalldict(PyObject *self, PyObject *args)
4463{
4464 PyObject *func, *func_args, *kwargs;
4465 PyObject **stack;
4466 Py_ssize_t nargs;
4467
4468 if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwargs)) {
4469 return NULL;
4470 }
4471
4472 if (fastcall_args(func_args, &stack, &nargs) < 0) {
4473 return NULL;
4474 }
4475
4476 if (kwargs == Py_None) {
4477 kwargs = NULL;
4478 }
4479 else if (!PyDict_Check(kwargs)) {
4480 PyErr_SetString(PyExc_TypeError, "kwnames must be None or a dict");
4481 return NULL;
4482 }
4483
4484 return _PyObject_FastCallDict(func, stack, nargs, kwargs);
4485}
4486
4487
4488static PyObject *
4489test_pyobject_fastcallkeywords(PyObject *self, PyObject *args)
4490{
4491 PyObject *func, *func_args, *kwnames = NULL;
4492 PyObject **stack;
4493 Py_ssize_t nargs, nkw;
4494
4495 if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwnames)) {
4496 return NULL;
4497 }
4498
4499 if (fastcall_args(func_args, &stack, &nargs) < 0) {
4500 return NULL;
4501 }
4502
4503 if (kwnames == Py_None) {
4504 kwnames = NULL;
4505 }
4506 else if (PyTuple_Check(kwnames)) {
4507 nkw = PyTuple_GET_SIZE(kwnames);
4508 if (nargs < nkw) {
4509 PyErr_SetString(PyExc_ValueError, "kwnames longer than args");
4510 return NULL;
4511 }
4512 nargs -= nkw;
4513 }
4514 else {
4515 PyErr_SetString(PyExc_TypeError, "kwnames must be None or a tuple");
4516 return NULL;
4517 }
4518 return _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4519}
4520
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02004521
Victor Stinner64fa4492017-07-10 14:37:49 +02004522static PyObject*
4523stack_pointer(PyObject *self, PyObject *args)
4524{
4525 int v = 5;
4526 return PyLong_FromVoidPtr(&v);
4527}
4528
Victor Stinner3b5cf852017-06-09 16:48:45 +02004529
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02004530#ifdef W_STOPCODE
4531static PyObject*
4532py_w_stopcode(PyObject *self, PyObject *args)
4533{
4534 int sig, status;
4535 if (!PyArg_ParseTuple(args, "i", &sig)) {
4536 return NULL;
4537 }
4538 status = W_STOPCODE(sig);
4539 return PyLong_FromLong(status);
4540}
4541#endif
4542
4543
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09004544static PyObject *
Oren Milman0ccc0f62017-10-08 11:17:46 +03004545get_mapping_keys(PyObject* self, PyObject *obj)
4546{
4547 return PyMapping_Keys(obj);
4548}
4549
4550static PyObject *
4551get_mapping_values(PyObject* self, PyObject *obj)
4552{
4553 return PyMapping_Values(obj);
4554}
4555
4556static PyObject *
4557get_mapping_items(PyObject* self, PyObject *obj)
4558{
4559 return PyMapping_Items(obj);
4560}
4561
4562
4563static PyObject *
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09004564test_pythread_tss_key_state(PyObject *self, PyObject *args)
4565{
4566 Py_tss_t tss_key = Py_tss_NEEDS_INIT;
4567 if (PyThread_tss_is_created(&tss_key)) {
4568 return raiseTestError("test_pythread_tss_key_state",
4569 "TSS key not in an uninitialized state at "
4570 "creation time");
4571 }
4572 if (PyThread_tss_create(&tss_key) != 0) {
4573 PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_create failed");
4574 return NULL;
4575 }
4576 if (!PyThread_tss_is_created(&tss_key)) {
4577 return raiseTestError("test_pythread_tss_key_state",
4578 "PyThread_tss_create succeeded, "
4579 "but with TSS key in an uninitialized state");
4580 }
4581 if (PyThread_tss_create(&tss_key) != 0) {
4582 return raiseTestError("test_pythread_tss_key_state",
4583 "PyThread_tss_create unsuccessful with "
4584 "an already initialized key");
4585 }
4586#define CHECK_TSS_API(expr) \
4587 (void)(expr); \
4588 if (!PyThread_tss_is_created(&tss_key)) { \
4589 return raiseTestError("test_pythread_tss_key_state", \
4590 "TSS key initialization state was not " \
4591 "preserved after calling " #expr); }
4592 CHECK_TSS_API(PyThread_tss_set(&tss_key, NULL));
4593 CHECK_TSS_API(PyThread_tss_get(&tss_key));
4594#undef CHECK_TSS_API
4595 PyThread_tss_delete(&tss_key);
4596 if (PyThread_tss_is_created(&tss_key)) {
4597 return raiseTestError("test_pythread_tss_key_state",
4598 "PyThread_tss_delete called, but did not "
4599 "set the key state to uninitialized");
4600 }
4601
4602 Py_tss_t *ptr_key = PyThread_tss_alloc();
4603 if (ptr_key == NULL) {
4604 PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_alloc failed");
4605 return NULL;
4606 }
4607 if (PyThread_tss_is_created(ptr_key)) {
4608 return raiseTestError("test_pythread_tss_key_state",
4609 "TSS key not in an uninitialized state at "
4610 "allocation time");
4611 }
4612 PyThread_tss_free(ptr_key);
4613 ptr_key = NULL;
4614 Py_RETURN_NONE;
4615}
4616
4617
Yury Selivanovf23746a2018-01-22 19:11:18 -05004618static PyObject*
4619new_hamt(PyObject *self, PyObject *args)
4620{
4621 return _PyContext_NewHamtForTests();
4622}
4623
4624
jdemeyer5a306202018-10-19 23:50:06 +02004625/* def bad_get(self, obj, cls):
4626 cls()
4627 return repr(self)
4628*/
4629static PyObject*
4630bad_get(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4631{
Serhiy Storchakab1dede32018-11-20 20:45:40 +02004632 PyObject *self, *obj, *cls;
4633 if (!_PyArg_UnpackStack(args, nargs, "bad_get", 3, 3, &self, &obj, &cls)) {
jdemeyer5a306202018-10-19 23:50:06 +02004634 return NULL;
4635 }
4636
Serhiy Storchakab1dede32018-11-20 20:45:40 +02004637 PyObject *res = PyObject_CallObject(cls, NULL);
jdemeyer5a306202018-10-19 23:50:06 +02004638 if (res == NULL) {
4639 return NULL;
4640 }
4641 Py_DECREF(res);
4642
Serhiy Storchakab1dede32018-11-20 20:45:40 +02004643 return PyObject_Repr(self);
jdemeyer5a306202018-10-19 23:50:06 +02004644}
4645
4646
Victor Stinner3d4226a2018-08-29 22:21:32 +02004647static PyObject *
4648encode_locale_ex(PyObject *self, PyObject *args)
4649{
4650 PyObject *unicode;
4651 int current_locale = 0;
4652 wchar_t *wstr;
4653 PyObject *res = NULL;
4654 const char *errors = NULL;
4655
4656 if (!PyArg_ParseTuple(args, "U|is", &unicode, &current_locale, &errors)) {
4657 return NULL;
4658 }
4659 wstr = PyUnicode_AsWideCharString(unicode, NULL);
4660 if (wstr == NULL) {
4661 return NULL;
4662 }
4663 _Py_error_handler error_handler = _Py_GetErrorHandler(errors);
4664
4665 char *str = NULL;
4666 size_t error_pos;
4667 const char *reason = NULL;
4668 int ret = _Py_EncodeLocaleEx(wstr,
4669 &str, &error_pos, &reason,
4670 current_locale, error_handler);
4671 PyMem_Free(wstr);
4672
4673 switch(ret) {
4674 case 0:
4675 res = PyBytes_FromString(str);
4676 PyMem_RawFree(str);
4677 break;
4678 case -1:
4679 PyErr_NoMemory();
4680 break;
4681 case -2:
4682 PyErr_Format(PyExc_RuntimeError, "encode error: pos=%zu, reason=%s",
4683 error_pos, reason);
4684 break;
4685 case -3:
4686 PyErr_SetString(PyExc_ValueError, "unsupported error handler");
4687 break;
4688 default:
4689 PyErr_SetString(PyExc_ValueError, "unknow error code");
4690 break;
4691 }
4692 return res;
4693}
4694
4695
4696static PyObject *
4697decode_locale_ex(PyObject *self, PyObject *args)
4698{
4699 char *str;
4700 int current_locale = 0;
4701 PyObject *res = NULL;
4702 const char *errors = NULL;
4703
4704 if (!PyArg_ParseTuple(args, "y|is", &str, &current_locale, &errors)) {
4705 return NULL;
4706 }
4707 _Py_error_handler error_handler = _Py_GetErrorHandler(errors);
4708
4709 wchar_t *wstr = NULL;
4710 size_t wlen = 0;
4711 const char *reason = NULL;
4712 int ret = _Py_DecodeLocaleEx(str,
4713 &wstr, &wlen, &reason,
4714 current_locale, error_handler);
4715
4716 switch(ret) {
4717 case 0:
4718 res = PyUnicode_FromWideChar(wstr, wlen);
4719 PyMem_RawFree(wstr);
4720 break;
4721 case -1:
4722 PyErr_NoMemory();
4723 break;
4724 case -2:
4725 PyErr_Format(PyExc_RuntimeError, "decode error: pos=%zu, reason=%s",
4726 wlen, reason);
4727 break;
4728 case -3:
4729 PyErr_SetString(PyExc_ValueError, "unsupported error handler");
4730 break;
4731 default:
4732 PyErr_SetString(PyExc_ValueError, "unknow error code");
4733 break;
4734 }
4735 return res;
4736}
4737
4738
Victor Stinner2094c2b2018-09-03 17:06:39 +02004739static PyObject *
Victor Stinner1075d162019-03-25 23:19:57 +01004740get_configs(PyObject *self, PyObject *Py_UNUSED(args))
Victor Stinner7ddd56f2018-11-14 00:24:28 +01004741{
Victor Stinner1075d162019-03-25 23:19:57 +01004742 return _Py_GetConfigsAsDict();
Victor Stinner00b137c2018-11-13 19:59:26 +01004743}
4744
4745
Victor Stinner18618e652018-10-25 17:28:11 +02004746#ifdef Py_REF_DEBUG
4747static PyObject *
4748negative_refcount(PyObject *self, PyObject *Py_UNUSED(args))
4749{
4750 PyObject *obj = PyUnicode_FromString("negative_refcount");
4751 if (obj == NULL) {
4752 return NULL;
4753 }
4754 assert(Py_REFCNT(obj) == 1);
4755
4756 Py_REFCNT(obj) = 0;
4757 /* Py_DECREF() must call _Py_NegativeRefcount() and abort Python */
4758 Py_DECREF(obj);
4759
4760 Py_RETURN_NONE;
4761}
4762#endif
4763
4764
Tim Peters9ea17ac2001-02-02 05:57:15 +00004765static PyMethodDef TestMethods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 {"raise_exception", raise_exception, METH_VARARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304767 {"raise_memoryerror", raise_memoryerror, METH_NOARGS},
Antoine Pitrou6bc217d2015-06-23 14:31:11 +02004768 {"set_errno", set_errno, METH_VARARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304769 {"test_config", test_config, METH_NOARGS},
4770 {"test_sizeof_c_types", test_sizeof_c_types, METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771 {"test_datetime_capi", test_datetime_capi, METH_NOARGS},
Paul Ganssle04af5b12018-01-24 17:29:30 -05004772 {"datetime_check_date", datetime_check_date, METH_VARARGS},
4773 {"datetime_check_time", datetime_check_time, METH_VARARGS},
4774 {"datetime_check_datetime", datetime_check_datetime, METH_VARARGS},
4775 {"datetime_check_delta", datetime_check_delta, METH_VARARGS},
4776 {"datetime_check_tzinfo", datetime_check_tzinfo, METH_VARARGS},
4777 {"make_timezones_capi", make_timezones_capi, METH_NOARGS},
Paul Gansslea049f572018-02-22 15:15:32 -05004778 {"get_timezones_offset_zero", get_timezones_offset_zero, METH_NOARGS},
Paul Ganssle04af5b12018-01-24 17:29:30 -05004779 {"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304780 {"test_list_api", test_list_api, METH_NOARGS},
4781 {"test_dict_iteration", test_dict_iteration, METH_NOARGS},
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02004782 {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS},
Victor Stinner3d3f2642016-12-15 17:21:23 +01004783 {"dict_hassplittable", dict_hassplittable, METH_O},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304784 {"test_lazy_hash_inheritance", test_lazy_hash_inheritance,METH_NOARGS},
4785 {"test_long_api", test_long_api, METH_NOARGS},
4786 {"test_xincref_doesnt_leak",test_xincref_doesnt_leak, METH_NOARGS},
4787 {"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS},
4788 {"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak, METH_NOARGS},
4789 {"test_decref_doesnt_leak", test_decref_doesnt_leak, METH_NOARGS},
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004790 {"test_structseq_newtype_doesnt_leak",
4791 test_structseq_newtype_doesnt_leak, METH_NOARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304792 {"test_incref_decref_API", test_incref_decref_API, METH_NOARGS},
4793 {"test_long_and_overflow", test_long_and_overflow, METH_NOARGS},
4794 {"test_long_as_double", test_long_as_double, METH_NOARGS},
4795 {"test_long_as_size_t", test_long_as_size_t, METH_NOARGS},
4796 {"test_long_numbits", test_long_numbits, METH_NOARGS},
4797 {"test_k_code", test_k_code, METH_NOARGS},
4798 {"test_empty_argparse", test_empty_argparse, METH_NOARGS},
Larry Hastings8f904da2012-06-22 03:56:29 -07004799 {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304800 {"test_null_strings", test_null_strings, METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304802 {"test_with_docstring", test_with_docstring, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004803 PyDoc_STR("This is a pretty normal docstring.")},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304804 {"test_string_to_double", test_string_to_double, METH_NOARGS},
4805 {"test_unicode_compare_with_ascii", test_unicode_compare_with_ascii,
4806 METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004807 {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
Stefan Krah7213fcc2015-02-01 16:19:23 +01004808 {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS},
Stefan Kraha7559c02015-02-03 22:27:21 +01004809#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
Stefan Krah5178d912015-02-03 16:57:21 +01004810 {"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS},
Stefan Kraha7559c02015-02-03 22:27:21 +01004811#endif
Stefan Krah650c1e82015-02-03 21:43:23 +01004812 {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
Serhiy Storchaka13e602e2016-05-20 22:31:14 +03004813 {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS},
Serhiy Storchakace412872016-05-08 23:36:44 +03004814 {"get_args", get_args, METH_VARARGS},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004815 {"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs, METH_VARARGS|METH_KEYWORDS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004816 {"getargs_tuple", getargs_tuple, METH_VARARGS},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004817 {"getargs_keywords", (PyCFunction)(void(*)(void))getargs_keywords,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004818 METH_VARARGS|METH_KEYWORDS},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004819 {"getargs_keyword_only", (PyCFunction)(void(*)(void))getargs_keyword_only,
Larry Hastings83a9f482012-03-20 20:06:16 +00004820 METH_VARARGS|METH_KEYWORDS},
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03004821 {"getargs_positional_only_and_keywords",
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004822 (PyCFunction)(void(*)(void))getargs_positional_only_and_keywords,
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03004823 METH_VARARGS|METH_KEYWORDS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824 {"getargs_b", getargs_b, METH_VARARGS},
4825 {"getargs_B", getargs_B, METH_VARARGS},
4826 {"getargs_h", getargs_h, METH_VARARGS},
4827 {"getargs_H", getargs_H, METH_VARARGS},
4828 {"getargs_I", getargs_I, METH_VARARGS},
4829 {"getargs_k", getargs_k, METH_VARARGS},
4830 {"getargs_i", getargs_i, METH_VARARGS},
4831 {"getargs_l", getargs_l, METH_VARARGS},
4832 {"getargs_n", getargs_n, METH_VARARGS},
Larry Hastingsfaf91e72012-05-05 16:54:29 -07004833 {"getargs_p", getargs_p, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004834 {"getargs_L", getargs_L, METH_VARARGS},
4835 {"getargs_K", getargs_K, METH_VARARGS},
4836 {"test_longlong_api", test_longlong_api, METH_NOARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304837 {"test_long_long_and_overflow",test_long_long_and_overflow, METH_NOARGS},
4838 {"test_L_code", test_L_code, METH_NOARGS},
Serhiy Storchakaf95455d2016-05-16 10:11:47 +03004839 {"getargs_f", getargs_f, METH_VARARGS},
4840 {"getargs_d", getargs_d, METH_VARARGS},
4841 {"getargs_D", getargs_D, METH_VARARGS},
4842 {"getargs_S", getargs_S, METH_VARARGS},
4843 {"getargs_Y", getargs_Y, METH_VARARGS},
4844 {"getargs_U", getargs_U, METH_VARARGS},
Eli Bendersky906b88f2011-07-29 07:05:08 +03004845 {"getargs_c", getargs_c, METH_VARARGS},
Serhiy Storchakac8241fd2016-01-28 19:49:54 +02004846 {"getargs_C", getargs_C, METH_VARARGS},
Victor Stinner06e49dd2010-06-13 18:21:50 +00004847 {"getargs_s", getargs_s, METH_VARARGS},
4848 {"getargs_s_star", getargs_s_star, METH_VARARGS},
4849 {"getargs_s_hash", getargs_s_hash, METH_VARARGS},
4850 {"getargs_z", getargs_z, METH_VARARGS},
4851 {"getargs_z_star", getargs_z_star, METH_VARARGS},
4852 {"getargs_z_hash", getargs_z_hash, METH_VARARGS},
4853 {"getargs_y", getargs_y, METH_VARARGS},
4854 {"getargs_y_star", getargs_y_star, METH_VARARGS},
4855 {"getargs_y_hash", getargs_y_hash, METH_VARARGS},
4856 {"getargs_u", getargs_u, METH_VARARGS},
4857 {"getargs_u_hash", getargs_u_hash, METH_VARARGS},
4858 {"getargs_Z", getargs_Z, METH_VARARGS},
4859 {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS},
Victor Stinner25e8ec42010-06-25 00:02:38 +00004860 {"getargs_w_star", getargs_w_star, METH_VARARGS},
Serhiy Storchakac8241fd2016-01-28 19:49:54 +02004861 {"getargs_es", getargs_es, METH_VARARGS},
4862 {"getargs_et", getargs_et, METH_VARARGS},
4863 {"getargs_es_hash", getargs_es_hash, METH_VARARGS},
4864 {"getargs_et_hash", getargs_et_hash, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 {"codec_incrementalencoder",
Victor Stinner1c24bd02010-10-02 11:03:13 +00004866 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 {"codec_incrementaldecoder",
Victor Stinner1c24bd02010-10-02 11:03:13 +00004868 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304869 {"test_s_code", test_s_code, METH_NOARGS},
4870 {"test_u_code", test_u_code, METH_NOARGS},
4871 {"test_Z_code", test_Z_code, METH_NOARGS},
4872 {"test_widechar", test_widechar, METH_NOARGS},
Victor Stinner42bf7752011-11-21 22:52:58 +01004873 {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS},
4874 {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS},
Serhiy Storchakacc164232016-10-02 21:29:26 +03004875 {"unicode_asucs4", unicode_asucs4, METH_VARARGS},
Xiang Zhangb2110682016-12-20 22:52:33 +08004876 {"unicode_findchar", unicode_findchar, METH_VARARGS},
Serhiy Storchaka9c0e1f82016-10-08 22:45:38 +03004877 {"unicode_copycharacters", unicode_copycharacters, METH_VARARGS},
Victor Stinner42bf7752011-11-21 22:52:58 +01004878 {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS},
4879 {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS},
Stefan Krahe6996ed2012-11-02 14:44:20 +01004880 {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS},
Victor Stinner1c24bd02010-10-02 11:03:13 +00004881 {"_test_thread_state", test_thread_state, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 {"_pending_threadfunc", pending_threadfunc, METH_VARARGS},
Guido van Rossumddefaf32007-01-14 03:31:43 +00004883#ifdef HAVE_GETTIMEOFDAY
Victor Stinner1c24bd02010-10-02 11:03:13 +00004884 {"profile_int", profile_int, METH_NOARGS},
Guido van Rossumddefaf32007-01-14 03:31:43 +00004885#endif
Victor Stinner1c24bd02010-10-02 11:03:13 +00004886 {"traceback_print", traceback_print, METH_VARARGS},
4887 {"exception_print", exception_print, METH_VARARGS},
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +02004888 {"set_exc_info", test_set_exc_info, METH_VARARGS},
Victor Stinner1c24bd02010-10-02 11:03:13 +00004889 {"argparsing", argparsing, METH_VARARGS},
4890 {"code_newempty", code_newempty, METH_VARARGS},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004891 {"make_exception_with_doc", (PyCFunction)(void(*)(void))make_exception_with_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004892 METH_VARARGS | METH_KEYWORDS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304893 {"make_memoryview_from_NULL_pointer", make_memoryview_from_NULL_pointer,
Antoine Pitrou5bffa792011-02-24 20:50:49 +00004894 METH_NOARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304895 {"crash_no_current_thread", crash_no_current_thread, METH_NOARGS},
Antoine Pitrou2f828f22012-01-18 00:21:11 +01004896 {"run_in_subinterp", run_in_subinterp, METH_VARARGS},
Victor Stinner5d272cc2012-03-13 13:35:55 +01004897 {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS},
4898 {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS},
Victor Stinner643cd682012-03-02 22:54:03 +01004899 {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS},
Antoine Pitrou796564c2013-07-30 19:59:21 +02004900 {"with_tp_del", with_tp_del, METH_VARARGS},
Antoine Pitroub349e4c2014-08-06 19:31:40 -04004901 {"create_cfunction", create_cfunction, METH_NOARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304902 {"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS},
4903 {"test_pymem_setrawallocators",test_pymem_setrawallocators, METH_NOARGS},
4904 {"test_pymem_setallocators",test_pymem_setallocators, METH_NOARGS},
4905 {"test_pyobject_setallocators",test_pyobject_setallocators, METH_NOARGS},
xdegaye85f64302017-07-01 14:14:45 +02004906 {"set_nomemory", (PyCFunction)set_nomemory, METH_VARARGS,
4907 PyDoc_STR("set_nomemory(start:int, stop:int = 0)")},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304908 {"remove_mem_hooks", remove_mem_hooks, METH_NOARGS,
xdegaye85f64302017-07-01 14:14:45 +02004909 PyDoc_STR("Remove memory hooks.")},
Larry Hastings44e2eaa2013-11-23 15:37:55 -08004910 {"no_docstring",
4911 (PyCFunction)test_with_docstring, METH_NOARGS},
4912 {"docstring_empty",
4913 (PyCFunction)test_with_docstring, METH_NOARGS,
4914 docstring_empty},
4915 {"docstring_no_signature",
4916 (PyCFunction)test_with_docstring, METH_NOARGS,
4917 docstring_no_signature},
4918 {"docstring_with_invalid_signature",
4919 (PyCFunction)test_with_docstring, METH_NOARGS,
4920 docstring_with_invalid_signature},
Larry Hastings2623c8c2014-02-08 22:15:29 -08004921 {"docstring_with_invalid_signature2",
4922 (PyCFunction)test_with_docstring, METH_NOARGS,
4923 docstring_with_invalid_signature2},
Larry Hastings44e2eaa2013-11-23 15:37:55 -08004924 {"docstring_with_signature",
4925 (PyCFunction)test_with_docstring, METH_NOARGS,
4926 docstring_with_signature},
Zachary Ware8ef887c2015-04-13 18:22:35 -05004927 {"docstring_with_signature_but_no_doc",
4928 (PyCFunction)test_with_docstring, METH_NOARGS,
4929 docstring_with_signature_but_no_doc},
Larry Hastings44e2eaa2013-11-23 15:37:55 -08004930 {"docstring_with_signature_and_extra_newlines",
4931 (PyCFunction)test_with_docstring, METH_NOARGS,
4932 docstring_with_signature_and_extra_newlines},
Larry Hastings16c51912014-01-07 11:53:01 -08004933 {"docstring_with_signature_with_defaults",
4934 (PyCFunction)test_with_docstring, METH_NOARGS,
4935 docstring_with_signature_with_defaults},
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004936 {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O,
4937 PyDoc_STR("set_error_class(error_class) -> None")},
Serhiy Storchakab5181342015-02-06 08:58:56 +02004938 {"pymarshal_write_long_to_file",
4939 pymarshal_write_long_to_file, METH_VARARGS},
4940 {"pymarshal_write_object_to_file",
4941 pymarshal_write_object_to_file, METH_VARARGS},
4942 {"pymarshal_read_short_from_file",
4943 pymarshal_read_short_from_file, METH_VARARGS},
4944 {"pymarshal_read_long_from_file",
4945 pymarshal_read_long_from_file, METH_VARARGS},
4946 {"pymarshal_read_last_object_from_file",
4947 pymarshal_read_last_object_from_file, METH_VARARGS},
4948 {"pymarshal_read_object_from_file",
4949 pymarshal_read_object_from_file, METH_VARARGS},
Victor Stinnerefde1462015-03-21 15:04:43 +01004950 {"return_null_without_error",
4951 return_null_without_error, METH_NOARGS},
4952 {"return_result_with_error",
4953 return_result_with_error, METH_NOARGS},
Victor Stinner13019fd2015-04-03 13:10:54 +02004954 {"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS},
Victor Stinner4bfb4602015-03-27 22:27:24 +01004955 {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
4956 {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
Victor Stinner95e9cef2015-03-28 01:26:47 +01004957 {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS},
Victor Stinner34dc0f42015-03-27 18:19:03 +01004958#ifdef HAVE_CLOCK_GETTIME
4959 {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS},
4960#endif
Victor Stinner62d1c702015-04-01 17:47:07 +02004961 {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS},
4962 {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS},
Victor Stinner50856d52015-10-13 00:11:21 +02004963 {"get_recursion_depth", get_recursion_depth, METH_NOARGS},
Victor Stinner34be807c2016-03-14 12:04:26 +01004964 {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS},
4965 {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS},
Victor Stinnerad524372016-03-16 12:12:53 +01004966 {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS},
Victor Stinner5d39e042017-11-29 17:20:38 +01004967 {"pymem_getallocatorsname", test_pymem_getallocatorsname, METH_NOARGS},
Victor Stinner2b00db62019-04-11 11:33:27 +02004968 {"pyobject_is_freed", (PyCFunction)(void(*)(void))pyobject_is_freed, METH_O},
4969 {"pyobject_uninitialized", pyobject_uninitialized, METH_NOARGS},
4970 {"pyobject_forbidden_bytes", pyobject_forbidden_bytes, METH_NOARGS},
4971 {"pyobject_freed", pyobject_freed, METH_NOARGS},
Victor Stinnerc4aec362016-03-14 22:26:53 +01004972 {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS},
Victor Stinner10b73e12016-03-22 13:39:05 +01004973 {"tracemalloc_track", tracemalloc_track, METH_VARARGS},
4974 {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS},
4975 {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS},
Victor Stinner3b6a6b42016-09-08 12:51:24 -07004976 {"dict_get_version", dict_get_version, METH_VARARGS},
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07004977 {"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS},
Victor Stinner3b5cf852017-06-09 16:48:45 +02004978 {"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS},
4979 {"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS},
4980 {"pyobject_fastcallkeywords", test_pyobject_fastcallkeywords, METH_VARARGS},
Victor Stinner64fa4492017-07-10 14:37:49 +02004981 {"stack_pointer", stack_pointer, METH_NOARGS},
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02004982#ifdef W_STOPCODE
4983 {"W_STOPCODE", py_w_stopcode, METH_VARARGS},
4984#endif
Oren Milman0ccc0f62017-10-08 11:17:46 +03004985 {"get_mapping_keys", get_mapping_keys, METH_O},
4986 {"get_mapping_values", get_mapping_values, METH_O},
4987 {"get_mapping_items", get_mapping_items, METH_O},
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09004988 {"test_pythread_tss_key_state", test_pythread_tss_key_state, METH_VARARGS},
Yury Selivanovf23746a2018-01-22 19:11:18 -05004989 {"hamt", new_hamt, METH_NOARGS},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004990 {"bad_get", (PyCFunction)(void(*)(void))bad_get, METH_FASTCALL},
Victor Stinner3d4226a2018-08-29 22:21:32 +02004991 {"EncodeLocaleEx", encode_locale_ex, METH_VARARGS},
4992 {"DecodeLocaleEx", decode_locale_ex, METH_VARARGS},
Victor Stinner1075d162019-03-25 23:19:57 +01004993 {"get_configs", get_configs, METH_NOARGS},
Victor Stinner18618e652018-10-25 17:28:11 +02004994#ifdef Py_REF_DEBUG
4995 {"negative_refcount", negative_refcount, METH_NOARGS},
4996#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004997 {NULL, NULL} /* sentinel */
Tim Peters9ea17ac2001-02-02 05:57:15 +00004998};
4999
Thomas Hellera4ea6032003-04-17 18:55:45 +00005000#define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
5001
Thomas Wouters89f507f2006-12-13 04:49:30 +00005002typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 char bool_member;
5004 char byte_member;
5005 unsigned char ubyte_member;
5006 short short_member;
5007 unsigned short ushort_member;
5008 int int_member;
5009 unsigned int uint_member;
5010 long long_member;
5011 unsigned long ulong_member;
5012 Py_ssize_t pyssizet_member;
5013 float float_member;
5014 double double_member;
5015 char inplace_member[6];
Benjamin Petersonaf580df2016-09-06 10:46:49 -07005016 long long longlong_member;
5017 unsigned long long ulonglong_member;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005018} all_structmembers;
5019
5020typedef struct {
5021 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005022 all_structmembers structmembers;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005023} test_structmembers;
5024
5025static struct PyMemberDef test_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005026 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
5027 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
5028 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
5029 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
5030 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
5031 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
5032 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
5033 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
5034 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
5035 {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL},
5036 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
5037 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
5038 {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005039 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
5040 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005041 {NULL}
Thomas Wouters89f507f2006-12-13 04:49:30 +00005042};
5043
5044
Christian Heimes1af737c2008-01-23 08:24:23 +00005045static PyObject *
5046test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
5047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005048 static char *keywords[] = {
5049 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
5050 "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET",
5051 "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005052 "T_LONGLONG", "T_ULONGLONG",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005053 NULL};
Benjamin Petersoned4aa832016-09-05 17:44:18 -07005054 static const char fmt[] = "|bbBhHiIlknfds#LK";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005055 test_structmembers *ob;
5056 const char *s = NULL;
5057 Py_ssize_t string_len = 0;
5058 ob = PyObject_New(test_structmembers, type);
5059 if (ob == NULL)
5060 return NULL;
5061 memset(&ob->structmembers, 0, sizeof(all_structmembers));
5062 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
5063 &ob->structmembers.bool_member,
5064 &ob->structmembers.byte_member,
5065 &ob->structmembers.ubyte_member,
5066 &ob->structmembers.short_member,
5067 &ob->structmembers.ushort_member,
5068 &ob->structmembers.int_member,
5069 &ob->structmembers.uint_member,
5070 &ob->structmembers.long_member,
5071 &ob->structmembers.ulong_member,
5072 &ob->structmembers.pyssizet_member,
5073 &ob->structmembers.float_member,
5074 &ob->structmembers.double_member,
5075 &s, &string_len
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005076 , &ob->structmembers.longlong_member,
5077 &ob->structmembers.ulonglong_member
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 )) {
5079 Py_DECREF(ob);
5080 return NULL;
5081 }
5082 if (s != NULL) {
5083 if (string_len > 5) {
5084 Py_DECREF(ob);
5085 PyErr_SetString(PyExc_ValueError, "string too long");
5086 return NULL;
5087 }
5088 strcpy(ob->structmembers.inplace_member, s);
5089 }
5090 else {
5091 strcpy(ob->structmembers.inplace_member, "");
5092 }
5093 return (PyObject *)ob;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005094}
5095
Christian Heimes1af737c2008-01-23 08:24:23 +00005096static void
5097test_structmembers_free(PyObject *ob)
5098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005099 PyObject_FREE(ob);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005100}
5101
5102static PyTypeObject test_structmembersType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00005103 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 "test_structmembersType",
5105 sizeof(test_structmembers), /* tp_basicsize */
5106 0, /* tp_itemsize */
5107 test_structmembers_free, /* destructor tp_dealloc */
5108 0, /* tp_print */
5109 0, /* tp_getattr */
5110 0, /* tp_setattr */
5111 0, /* tp_reserved */
5112 0, /* tp_repr */
5113 0, /* tp_as_number */
5114 0, /* tp_as_sequence */
5115 0, /* tp_as_mapping */
5116 0, /* tp_hash */
5117 0, /* tp_call */
5118 0, /* tp_str */
5119 PyObject_GenericGetAttr, /* tp_getattro */
5120 PyObject_GenericSetAttr, /* tp_setattro */
5121 0, /* tp_as_buffer */
5122 0, /* tp_flags */
5123 "Type containing all structmember types",
5124 0, /* traverseproc tp_traverse */
5125 0, /* tp_clear */
5126 0, /* tp_richcompare */
5127 0, /* tp_weaklistoffset */
5128 0, /* tp_iter */
5129 0, /* tp_iternext */
5130 0, /* tp_methods */
5131 test_members, /* tp_members */
5132 0,
5133 0,
5134 0,
5135 0,
5136 0,
5137 0,
5138 0,
5139 0,
5140 test_structmembers_new, /* tp_new */
Thomas Wouters89f507f2006-12-13 04:49:30 +00005141};
5142
5143
Benjamin Petersond51374e2014-04-09 23:55:56 -04005144typedef struct {
5145 PyObject_HEAD
5146} matmulObject;
5147
5148static PyObject *
5149matmulType_matmul(PyObject *self, PyObject *other)
5150{
5151 return Py_BuildValue("(sOO)", "matmul", self, other);
5152}
5153
5154static PyObject *
5155matmulType_imatmul(PyObject *self, PyObject *other)
5156{
5157 return Py_BuildValue("(sOO)", "imatmul", self, other);
5158}
5159
5160static void
5161matmulType_dealloc(PyObject *self)
5162{
Zachary Ware420dc562014-04-23 13:51:27 -05005163 Py_TYPE(self)->tp_free(self);
Benjamin Petersond51374e2014-04-09 23:55:56 -04005164}
5165
5166static PyNumberMethods matmulType_as_number = {
5167 0, /* nb_add */
5168 0, /* nb_subtract */
5169 0, /* nb_multiply */
5170 0, /* nb_remainde r*/
5171 0, /* nb_divmod */
5172 0, /* nb_power */
5173 0, /* nb_negative */
5174 0, /* tp_positive */
5175 0, /* tp_absolute */
5176 0, /* tp_bool */
5177 0, /* nb_invert */
5178 0, /* nb_lshift */
5179 0, /* nb_rshift */
5180 0, /* nb_and */
5181 0, /* nb_xor */
5182 0, /* nb_or */
5183 0, /* nb_int */
5184 0, /* nb_reserved */
5185 0, /* nb_float */
5186 0, /* nb_inplace_add */
5187 0, /* nb_inplace_subtract */
5188 0, /* nb_inplace_multiply */
5189 0, /* nb_inplace_remainder */
5190 0, /* nb_inplace_power */
5191 0, /* nb_inplace_lshift */
5192 0, /* nb_inplace_rshift */
5193 0, /* nb_inplace_and */
5194 0, /* nb_inplace_xor */
5195 0, /* nb_inplace_or */
5196 0, /* nb_floor_divide */
5197 0, /* nb_true_divide */
5198 0, /* nb_inplace_floor_divide */
5199 0, /* nb_inplace_true_divide */
5200 0, /* nb_index */
5201 matmulType_matmul, /* nb_matrix_multiply */
5202 matmulType_imatmul /* nb_matrix_inplace_multiply */
5203};
5204
5205static PyTypeObject matmulType = {
5206 PyVarObject_HEAD_INIT(NULL, 0)
5207 "matmulType",
5208 sizeof(matmulObject), /* tp_basicsize */
5209 0, /* tp_itemsize */
5210 matmulType_dealloc, /* destructor tp_dealloc */
5211 0, /* tp_print */
5212 0, /* tp_getattr */
5213 0, /* tp_setattr */
5214 0, /* tp_reserved */
5215 0, /* tp_repr */
5216 &matmulType_as_number, /* tp_as_number */
5217 0, /* tp_as_sequence */
5218 0, /* tp_as_mapping */
5219 0, /* tp_hash */
5220 0, /* tp_call */
5221 0, /* tp_str */
5222 PyObject_GenericGetAttr, /* tp_getattro */
5223 PyObject_GenericSetAttr, /* tp_setattro */
5224 0, /* tp_as_buffer */
5225 0, /* tp_flags */
5226 "C level type with matrix operations defined",
5227 0, /* traverseproc tp_traverse */
5228 0, /* tp_clear */
5229 0, /* tp_richcompare */
5230 0, /* tp_weaklistoffset */
5231 0, /* tp_iter */
5232 0, /* tp_iternext */
5233 0, /* tp_methods */
5234 0, /* tp_members */
5235 0,
5236 0,
5237 0,
5238 0,
5239 0,
5240 0,
5241 0,
5242 0,
5243 PyType_GenericNew, /* tp_new */
5244 PyObject_Del, /* tp_free */
5245};
5246
Martin v. Löwis1a214512008-06-11 05:26:20 +00005247
Yury Selivanov75445082015-05-11 22:57:16 -04005248typedef struct {
5249 PyObject_HEAD
5250 PyObject *ao_iterator;
5251} awaitObject;
5252
5253
5254static PyObject *
5255awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5256{
5257 PyObject *v;
5258 awaitObject *ao;
5259
5260 if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v))
5261 return NULL;
5262
5263 ao = (awaitObject *)type->tp_alloc(type, 0);
5264 if (ao == NULL) {
5265 return NULL;
5266 }
5267
5268 Py_INCREF(v);
5269 ao->ao_iterator = v;
5270
5271 return (PyObject *)ao;
5272}
5273
5274
5275static void
5276awaitObject_dealloc(awaitObject *ao)
5277{
5278 Py_CLEAR(ao->ao_iterator);
5279 Py_TYPE(ao)->tp_free(ao);
5280}
5281
5282
5283static PyObject *
5284awaitObject_await(awaitObject *ao)
5285{
5286 Py_INCREF(ao->ao_iterator);
5287 return ao->ao_iterator;
5288}
5289
5290static PyAsyncMethods awaitType_as_async = {
Yury Selivanov6ef05902015-05-28 11:21:31 -04005291 (unaryfunc)awaitObject_await, /* am_await */
Yury Selivanov75445082015-05-11 22:57:16 -04005292 0, /* am_aiter */
5293 0 /* am_anext */
5294};
5295
5296
5297static PyTypeObject awaitType = {
5298 PyVarObject_HEAD_INIT(NULL, 0)
5299 "awaitType",
5300 sizeof(awaitObject), /* tp_basicsize */
5301 0, /* tp_itemsize */
5302 (destructor)awaitObject_dealloc, /* destructor tp_dealloc */
5303 0, /* tp_print */
5304 0, /* tp_getattr */
5305 0, /* tp_setattr */
5306 &awaitType_as_async, /* tp_as_async */
5307 0, /* tp_repr */
5308 0, /* tp_as_number */
5309 0, /* tp_as_sequence */
5310 0, /* tp_as_mapping */
5311 0, /* tp_hash */
5312 0, /* tp_call */
5313 0, /* tp_str */
5314 PyObject_GenericGetAttr, /* tp_getattro */
5315 PyObject_GenericSetAttr, /* tp_setattro */
5316 0, /* tp_as_buffer */
5317 0, /* tp_flags */
5318 "C level type with tp_as_async",
5319 0, /* traverseproc tp_traverse */
5320 0, /* tp_clear */
5321 0, /* tp_richcompare */
5322 0, /* tp_weaklistoffset */
5323 0, /* tp_iter */
5324 0, /* tp_iternext */
5325 0, /* tp_methods */
5326 0, /* tp_members */
5327 0,
5328 0,
5329 0,
5330 0,
5331 0,
5332 0,
5333 0,
5334 0,
5335 awaitObject_new, /* tp_new */
5336 PyObject_Del, /* tp_free */
5337};
5338
5339
xdegaye56d1f5c2017-10-26 15:09:06 +02005340static int recurse_infinitely_error_init(PyObject *, PyObject *, PyObject *);
5341
5342static PyTypeObject PyRecursingInfinitelyError_Type = {
5343 PyVarObject_HEAD_INIT(NULL, 0)
5344 "RecursingInfinitelyError", /* tp_name */
5345 sizeof(PyBaseExceptionObject), /* tp_basicsize */
5346 0, /* tp_itemsize */
5347 0, /* tp_dealloc */
5348 0, /* tp_print */
5349 0, /* tp_getattr */
5350 0, /* tp_setattr */
5351 0, /* tp_reserved */
5352 0, /* tp_repr */
5353 0, /* tp_as_number */
5354 0, /* tp_as_sequence */
5355 0, /* tp_as_mapping */
5356 0, /* tp_hash */
5357 0, /* tp_call */
5358 0, /* tp_str */
5359 0, /* tp_getattro */
5360 0, /* tp_setattro */
5361 0, /* tp_as_buffer */
5362 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
5363 "Instantiating this exception starts infinite recursion.", /* tp_doc */
5364 0, /* tp_traverse */
5365 0, /* tp_clear */
5366 0, /* tp_richcompare */
5367 0, /* tp_weaklistoffset */
5368 0, /* tp_iter */
5369 0, /* tp_iternext */
5370 0, /* tp_methods */
5371 0, /* tp_members */
5372 0, /* tp_getset */
5373 0, /* tp_base */
5374 0, /* tp_dict */
5375 0, /* tp_descr_get */
5376 0, /* tp_descr_set */
5377 0, /* tp_dictoffset */
5378 (initproc)recurse_infinitely_error_init, /* tp_init */
5379 0, /* tp_alloc */
5380 0, /* tp_new */
5381};
5382
5383static int
5384recurse_infinitely_error_init(PyObject *self, PyObject *args, PyObject *kwds)
5385{
5386 PyObject *type = (PyObject *)&PyRecursingInfinitelyError_Type;
5387
5388 /* Instantiating this exception starts infinite recursion. */
5389 Py_INCREF(type);
5390 PyErr_SetObject(type, NULL);
5391 return -1;
5392}
5393
5394
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005395/* Test PEP 560 */
5396
5397typedef struct {
5398 PyObject_HEAD
5399 PyObject *item;
5400} PyGenericAliasObject;
5401
5402static void
5403generic_alias_dealloc(PyGenericAliasObject *self)
5404{
5405 Py_CLEAR(self->item);
Victor Stinnere8600892018-01-17 23:08:18 +01005406 Py_TYPE(self)->tp_free((PyObject *)self);
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005407}
5408
5409static PyObject *
5410generic_alias_mro_entries(PyGenericAliasObject *self, PyObject *bases)
5411{
5412 return PyTuple_Pack(1, self->item);
5413}
5414
5415static PyMethodDef generic_alias_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02005416 {"__mro_entries__", (PyCFunction)(void(*)(void))generic_alias_mro_entries, METH_O, NULL},
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005417 {NULL} /* sentinel */
5418};
5419
Benjamin Peterson97ae32c2018-07-03 22:39:09 -07005420static PyTypeObject GenericAlias_Type = {
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005421 PyVarObject_HEAD_INIT(NULL, 0)
5422 "GenericAlias",
5423 sizeof(PyGenericAliasObject),
5424 0,
5425 .tp_dealloc = (destructor)generic_alias_dealloc,
5426 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
5427 .tp_methods = generic_alias_methods,
5428};
5429
5430static PyObject *
5431generic_alias_new(PyObject *item)
5432{
5433 PyGenericAliasObject *o = PyObject_New(PyGenericAliasObject, &GenericAlias_Type);
5434 if (o == NULL) {
5435 return NULL;
5436 }
5437 Py_INCREF(item);
5438 o->item = item;
5439 return (PyObject*) o;
5440}
5441
5442typedef struct {
5443 PyObject_HEAD
5444} PyGenericObject;
5445
5446static PyObject *
Serhiy Storchakace5b0e92018-01-05 00:21:41 +02005447generic_class_getitem(PyObject *type, PyObject *item)
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005448{
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005449 return generic_alias_new(item);
5450}
5451
5452static PyMethodDef generic_methods[] = {
Serhiy Storchakace5b0e92018-01-05 00:21:41 +02005453 {"__class_getitem__", generic_class_getitem, METH_O|METH_CLASS, NULL},
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005454 {NULL} /* sentinel */
5455};
5456
Benjamin Peterson97ae32c2018-07-03 22:39:09 -07005457static PyTypeObject Generic_Type = {
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005458 PyVarObject_HEAD_INIT(NULL, 0)
5459 "Generic",
5460 sizeof(PyGenericObject),
5461 0,
5462 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
5463 .tp_methods = generic_methods,
5464};
5465
5466
Martin v. Löwis1a214512008-06-11 05:26:20 +00005467static struct PyModuleDef _testcapimodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005468 PyModuleDef_HEAD_INIT,
5469 "_testcapi",
5470 NULL,
5471 -1,
5472 TestMethods,
5473 NULL,
5474 NULL,
5475 NULL,
5476 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00005477};
5478
Nick Coghland5cacbb2015-05-23 22:24:10 +10005479/* Per PEP 489, this module will not be converted to multi-phase initialization
5480 */
5481
Mark Hammond62b1ab12002-07-23 06:31:15 +00005482PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00005483PyInit__testcapi(void)
Tim Peters9ea17ac2001-02-02 05:57:15 +00005484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005485 PyObject *m;
Tim Peters9ea17ac2001-02-02 05:57:15 +00005486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005487 m = PyModule_Create(&_testcapimodule);
5488 if (m == NULL)
5489 return NULL;
Tim Peters9ea17ac2001-02-02 05:57:15 +00005490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005491 Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
Hirokazu Yamamoto8ebab5d2008-12-31 06:05:46 +00005492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005493 Py_TYPE(&test_structmembersType)=&PyType_Type;
5494 Py_INCREF(&test_structmembersType);
5495 /* don't use a name starting with "test", since we don't want
5496 test_capi to automatically call this */
5497 PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
Benjamin Petersond51374e2014-04-09 23:55:56 -04005498 if (PyType_Ready(&matmulType) < 0)
5499 return NULL;
5500 Py_INCREF(&matmulType);
5501 PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005502
Yury Selivanov75445082015-05-11 22:57:16 -04005503 if (PyType_Ready(&awaitType) < 0)
5504 return NULL;
5505 Py_INCREF(&awaitType);
5506 PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType);
5507
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005508 if (PyType_Ready(&GenericAlias_Type) < 0)
5509 return NULL;
5510 Py_INCREF(&GenericAlias_Type);
5511 PyModule_AddObject(m, "GenericAlias", (PyObject *)&GenericAlias_Type);
5512
5513 if (PyType_Ready(&Generic_Type) < 0)
5514 return NULL;
5515 Py_INCREF(&Generic_Type);
5516 PyModule_AddObject(m, "Generic", (PyObject *)&Generic_Type);
5517
xdegaye56d1f5c2017-10-26 15:09:06 +02005518 PyRecursingInfinitelyError_Type.tp_base = (PyTypeObject *)PyExc_Exception;
5519 if (PyType_Ready(&PyRecursingInfinitelyError_Type) < 0) {
5520 return NULL;
5521 }
5522 Py_INCREF(&PyRecursingInfinitelyError_Type);
5523 PyModule_AddObject(m, "RecursingInfinitelyError",
5524 (PyObject *)&PyRecursingInfinitelyError_Type);
5525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005526 PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
5527 PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
5528 PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX));
5529 PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX));
5530 PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN));
5531 PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX));
5532 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
5533 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
5534 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
5535 PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX));
5536 PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN));
5537 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
5538 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
5539 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
5540 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
5541 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
5542 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
5543 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
5544 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
5545 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
5546 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
5547 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
Victor Stinner4237d342015-09-10 10:10:39 +02005548 PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005549 Py_INCREF(&PyInstanceMethod_Type);
5550 PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
Thomas Hellera4ea6032003-04-17 18:55:45 +00005551
Larry Hastings2a727912014-01-16 11:32:01 -08005552 PyModule_AddIntConstant(m, "the_number_three", 3);
Victor Stinner5d39e042017-11-29 17:20:38 +01005553#ifdef WITH_PYMALLOC
5554 PyModule_AddObject(m, "WITH_PYMALLOC", Py_True);
5555#else
5556 PyModule_AddObject(m, "WITH_PYMALLOC", Py_False);
5557#endif
Larry Hastings2a727912014-01-16 11:32:01 -08005558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005559 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
5560 Py_INCREF(TestError);
5561 PyModule_AddObject(m, "error", TestError);
5562 return m;
Tim Peters9ea17ac2001-02-02 05:57:15 +00005563}