blob: 8f34e935353ba1f0c99ccbe865224c212ab11e50 [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 */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200291 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 0, /* tp_getattr */
293 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200294 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 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
Zackery Spytzdc247652019-06-06 14:39:23 -0600828static PyObject *
829test_long_as_unsigned_long_long_mask(PyObject *self,
830 PyObject *Py_UNUSED(ignored))
831{
832 unsigned long long res = PyLong_AsUnsignedLongLongMask(NULL);
833
834 if (res != (unsigned long long)-1 || !PyErr_Occurred()) {
835 return raiseTestError("test_long_as_unsigned_long_long_mask",
836 "PyLong_AsUnsignedLongLongMask(NULL) didn't "
837 "complain");
838 }
839 if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
840 return raiseTestError("test_long_as_unsigned_long_long_mask",
841 "PyLong_AsUnsignedLongLongMask(NULL) raised "
842 "something other than SystemError");
843 }
844 PyErr_Clear();
845 Py_RETURN_NONE;
846}
847
Nadeem Vawda3d5881e2011-09-07 21:40:26 +0200848/* Test the PyLong_AsDouble API. At present this just tests that
849 non-integer arguments are handled correctly.
850 */
851
852static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530853test_long_as_double(PyObject *self, PyObject *Py_UNUSED(ignored))
Nadeem Vawda3d5881e2011-09-07 21:40:26 +0200854{
855 double out;
856
857 Py_INCREF(Py_None);
858
859 out = PyLong_AsDouble(Py_None);
860 if (out != -1.0 || !PyErr_Occurred())
861 return raiseTestError("test_long_as_double",
862 "PyLong_AsDouble(None) didn't complain");
863 if (!PyErr_ExceptionMatches(PyExc_TypeError))
864 return raiseTestError("test_long_as_double",
865 "PyLong_AsDouble(None) raised "
866 "something other than TypeError");
867 PyErr_Clear();
868
869 /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
870 return Py_None;
871}
872
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700873/* Test the L code for PyArg_ParseTuple. This should deliver a long long
Tim Petersd38b1c72001-09-30 05:09:37 +0000874 for both long and int arguments. The test may leak a little memory if
875 it fails.
876*/
877static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530878test_L_code(PyObject *self, PyObject *Py_UNUSED(ignored))
Tim Petersd38b1c72001-09-30 05:09:37 +0000879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 PyObject *tuple, *num;
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700881 long long value;
Tim Petersd38b1c72001-09-30 05:09:37 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 tuple = PyTuple_New(1);
884 if (tuple == NULL)
885 return NULL;
Tim Petersd38b1c72001-09-30 05:09:37 +0000886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 num = PyLong_FromLong(42);
888 if (num == NULL)
889 return NULL;
Tim Petersd38b1c72001-09-30 05:09:37 +0000890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 PyTuple_SET_ITEM(tuple, 0, num);
Tim Petersd38b1c72001-09-30 05:09:37 +0000892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 value = -1;
Oren Milmanba7d7362017-08-29 11:58:27 +0300894 if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +0300896 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 if (value != 42)
898 return raiseTestError("test_L_code",
899 "L code returned wrong value for long 42");
Tim Petersd38b1c72001-09-30 05:09:37 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 Py_DECREF(num);
902 num = PyLong_FromLong(42);
903 if (num == NULL)
904 return NULL;
Tim Petersd38b1c72001-09-30 05:09:37 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 PyTuple_SET_ITEM(tuple, 0, num);
Tim Petersd38b1c72001-09-30 05:09:37 +0000907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 value = -1;
Oren Milmanba7d7362017-08-29 11:58:27 +0300909 if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +0300911 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 if (value != 42)
913 return raiseTestError("test_L_code",
914 "L code returned wrong value for int 42");
Tim Petersd38b1c72001-09-30 05:09:37 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 Py_DECREF(tuple);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200917 Py_RETURN_NONE;
Tim Petersd38b1c72001-09-30 05:09:37 +0000918}
919
Serhiy Storchakace412872016-05-08 23:36:44 +0300920static PyObject *
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300921return_none(void *unused)
922{
923 Py_RETURN_NONE;
924}
925
926static PyObject *
927raise_error(void *unused)
928{
929 PyErr_SetNone(PyExc_ValueError);
930 return NULL;
931}
932
933static int
934test_buildvalue_N_error(const char *fmt)
935{
936 PyObject *arg, *res;
937
938 arg = PyList_New(0);
939 if (arg == NULL) {
940 return -1;
941 }
942
943 Py_INCREF(arg);
944 res = Py_BuildValue(fmt, return_none, NULL, arg);
945 if (res == NULL) {
946 return -1;
947 }
948 Py_DECREF(res);
949 if (Py_REFCNT(arg) != 1) {
950 PyErr_Format(TestError, "test_buildvalue_N: "
951 "arg was not decrefed in successful "
952 "Py_BuildValue(\"%s\")", fmt);
953 return -1;
954 }
955
956 Py_INCREF(arg);
957 res = Py_BuildValue(fmt, raise_error, NULL, arg);
958 if (res != NULL || !PyErr_Occurred()) {
959 PyErr_Format(TestError, "test_buildvalue_N: "
960 "Py_BuildValue(\"%s\") didn't complain", fmt);
961 return -1;
962 }
963 PyErr_Clear();
964 if (Py_REFCNT(arg) != 1) {
965 PyErr_Format(TestError, "test_buildvalue_N: "
966 "arg was not decrefed in failed "
967 "Py_BuildValue(\"%s\")", fmt);
968 return -1;
969 }
970 Py_DECREF(arg);
971 return 0;
972}
973
974static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +0200975test_buildvalue_N(PyObject *self, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300976{
977 PyObject *arg, *res;
978
979 arg = PyList_New(0);
980 if (arg == NULL) {
981 return NULL;
982 }
983 Py_INCREF(arg);
984 res = Py_BuildValue("N", arg);
985 if (res == NULL) {
986 return NULL;
987 }
988 if (res != arg) {
989 return raiseTestError("test_buildvalue_N",
990 "Py_BuildValue(\"N\") returned wrong result");
991 }
992 if (Py_REFCNT(arg) != 2) {
993 return raiseTestError("test_buildvalue_N",
994 "arg was not decrefed in Py_BuildValue(\"N\")");
995 }
996 Py_DECREF(res);
997 Py_DECREF(arg);
998
999 if (test_buildvalue_N_error("O&N") < 0)
1000 return NULL;
1001 if (test_buildvalue_N_error("(O&N)") < 0)
1002 return NULL;
1003 if (test_buildvalue_N_error("[O&N]") < 0)
1004 return NULL;
1005 if (test_buildvalue_N_error("{O&N}") < 0)
1006 return NULL;
1007 if (test_buildvalue_N_error("{()O&(())N}") < 0)
1008 return NULL;
1009
1010 Py_RETURN_NONE;
1011}
1012
1013
1014static PyObject *
Serhiy Storchakace412872016-05-08 23:36:44 +03001015get_args(PyObject *self, PyObject *args)
1016{
1017 if (args == NULL) {
1018 args = Py_None;
1019 }
1020 Py_INCREF(args);
1021 return args;
1022}
1023
1024static PyObject *
1025get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs)
1026{
1027 if (kwargs == NULL) {
1028 kwargs = Py_None;
1029 }
1030 Py_INCREF(kwargs);
1031 return kwargs;
1032}
1033
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001034/* Test tuple argument processing */
1035static PyObject *
1036getargs_tuple(PyObject *self, PyObject *args)
1037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 int a, b, c;
1039 if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
1040 return NULL;
1041 return Py_BuildValue("iii", a, b, c);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001042}
1043
Christian Heimes380f7f22008-02-28 11:19:05 +00001044/* test PyArg_ParseTupleAndKeywords */
Larry Hastings83a9f482012-03-20 20:06:16 +00001045static PyObject *
1046getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
Christian Heimes380f7f22008-02-28 11:19:05 +00001047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001049 static const char fmt[] = "(ii)i|(i(ii))(iii)i";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
Christian Heimes380f7f22008-02-28 11:19:05 +00001051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
1053 &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
1054 &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
1055 return NULL;
1056 return Py_BuildValue("iiiiiiiiii",
1057 int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
1058 int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
Christian Heimes380f7f22008-02-28 11:19:05 +00001059}
1060
Larry Hastings83a9f482012-03-20 20:06:16 +00001061/* test PyArg_ParseTupleAndKeywords keyword-only arguments */
1062static PyObject *
1063getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs)
1064{
1065 static char *keywords[] = {"required", "optional", "keyword_only", NULL};
1066 int required = -1;
1067 int optional = -1;
1068 int keyword_only = -1;
1069
1070 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords,
1071 &required, &optional, &keyword_only))
1072 return NULL;
1073 return Py_BuildValue("iii", required, optional, keyword_only);
1074}
1075
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001076/* test PyArg_ParseTupleAndKeywords positional-only arguments */
1077static PyObject *
1078getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
1079{
1080 static char *keywords[] = {"", "", "keyword", NULL};
1081 int required = -1;
1082 int optional = -1;
1083 int keyword = -1;
1084
1085 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords,
1086 &required, &optional, &keyword))
1087 return NULL;
1088 return Py_BuildValue("iii", required, optional, keyword);
1089}
1090
Thomas Heller3457e4b2003-04-24 16:14:27 +00001091/* Functions to call PyArg_ParseTuple with integer format codes,
1092 and return the result.
1093*/
Thomas Hellera4ea6032003-04-17 18:55:45 +00001094static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +00001095getargs_b(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +00001096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 unsigned char value;
1098 if (!PyArg_ParseTuple(args, "b", &value))
1099 return NULL;
1100 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001101}
1102
Thomas Heller3457e4b2003-04-24 16:14:27 +00001103static PyObject *
1104getargs_B(PyObject *self, PyObject *args)
1105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 unsigned char value;
1107 if (!PyArg_ParseTuple(args, "B", &value))
1108 return NULL;
1109 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +00001110}
1111
1112static PyObject *
Mark Dickinson1554b182009-12-20 16:03:30 +00001113getargs_h(PyObject *self, PyObject *args)
1114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 short value;
1116 if (!PyArg_ParseTuple(args, "h", &value))
1117 return NULL;
1118 return PyLong_FromLong((long)value);
Mark Dickinson1554b182009-12-20 16:03:30 +00001119}
1120
1121static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +00001122getargs_H(PyObject *self, PyObject *args)
1123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 unsigned short value;
1125 if (!PyArg_ParseTuple(args, "H", &value))
1126 return NULL;
1127 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +00001128}
1129
1130static PyObject *
1131getargs_I(PyObject *self, PyObject *args)
1132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 unsigned int value;
1134 if (!PyArg_ParseTuple(args, "I", &value))
1135 return NULL;
1136 return PyLong_FromUnsignedLong((unsigned long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +00001137}
1138
1139static PyObject *
1140getargs_k(PyObject *self, PyObject *args)
1141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 unsigned long value;
1143 if (!PyArg_ParseTuple(args, "k", &value))
1144 return NULL;
1145 return PyLong_FromUnsignedLong(value);
Thomas Heller3457e4b2003-04-24 16:14:27 +00001146}
1147
1148static PyObject *
1149getargs_i(PyObject *self, PyObject *args)
1150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 int value;
1152 if (!PyArg_ParseTuple(args, "i", &value))
1153 return NULL;
1154 return PyLong_FromLong((long)value);
Thomas Heller3457e4b2003-04-24 16:14:27 +00001155}
1156
Thomas Hellera4ea6032003-04-17 18:55:45 +00001157static PyObject *
1158getargs_l(PyObject *self, PyObject *args)
1159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 long value;
1161 if (!PyArg_ParseTuple(args, "l", &value))
1162 return NULL;
1163 return PyLong_FromLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001164}
1165
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001166static PyObject *
1167getargs_n(PyObject *self, PyObject *args)
1168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 Py_ssize_t value;
1170 if (!PyArg_ParseTuple(args, "n", &value))
1171 return NULL;
1172 return PyLong_FromSsize_t(value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001173}
1174
Larry Hastingsfaf91e72012-05-05 16:54:29 -07001175static PyObject *
1176getargs_p(PyObject *self, PyObject *args)
1177{
1178 int value;
1179 if (!PyArg_ParseTuple(args, "p", &value))
1180 return NULL;
1181 return PyLong_FromLong(value);
1182}
1183
Thomas Hellera4ea6032003-04-17 18:55:45 +00001184static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +00001185getargs_L(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +00001186{
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001187 long long value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 if (!PyArg_ParseTuple(args, "L", &value))
1189 return NULL;
1190 return PyLong_FromLongLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001191}
1192
Thomas Hellera4ea6032003-04-17 18:55:45 +00001193static PyObject *
Thomas Heller3457e4b2003-04-24 16:14:27 +00001194getargs_K(PyObject *self, PyObject *args)
Thomas Hellera4ea6032003-04-17 18:55:45 +00001195{
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001196 unsigned long long value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 if (!PyArg_ParseTuple(args, "K", &value))
1198 return NULL;
1199 return PyLong_FromUnsignedLongLong(value);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001200}
Thomas Hellera4ea6032003-04-17 18:55:45 +00001201
1202/* This function not only tests the 'k' getargs code, but also the
orenmn698845e2017-03-02 13:29:20 +02001203 PyLong_AsUnsignedLongMask() function. */
Thomas Hellera4ea6032003-04-17 18:55:45 +00001204static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301205test_k_code(PyObject *self, PyObject *Py_UNUSED(ignored))
Thomas Hellera4ea6032003-04-17 18:55:45 +00001206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 PyObject *tuple, *num;
1208 unsigned long value;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 tuple = PyTuple_New(1);
1211 if (tuple == NULL)
1212 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 /* a number larger than ULONG_MAX even on 64-bit platforms */
1215 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", 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 != ULONG_MAX)
1221 return raiseTestError("test_k_code",
Georg Brandl4b5b0622016-01-18 08:00:15 +01001222 "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
Thomas Hellera4ea6032003-04-17 18:55:45 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 PyTuple_SET_ITEM(tuple, 0, num);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 value = 0;
Oren Milmanba7d7362017-08-29 11:58:27 +03001227 if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03001229 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 if (value != ULONG_MAX)
1231 return raiseTestError("test_k_code",
1232 "k code returned wrong value for long 0xFFF...FFF");
Thomas Hellera4ea6032003-04-17 18:55:45 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 Py_DECREF(num);
1235 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
1236 if (num == NULL)
1237 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 value = PyLong_AsUnsignedLongMask(num);
1240 if (value != (unsigned long)-0x42)
1241 return raiseTestError("test_k_code",
orenmn698845e2017-03-02 13:29:20 +02001242 "PyLong_AsUnsignedLongMask() returned wrong "
1243 "value for long -0xFFF..000042");
Thomas Hellera4ea6032003-04-17 18:55:45 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 PyTuple_SET_ITEM(tuple, 0, num);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 value = 0;
Oren Milmanba7d7362017-08-29 11:58:27 +03001248 if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03001250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (value != (unsigned long)-0x42)
1252 return raiseTestError("test_k_code",
1253 "k code returned wrong value for long -0xFFF..000042");
Thomas Hellera4ea6032003-04-17 18:55:45 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 Py_DECREF(tuple);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001256 Py_RETURN_NONE;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001257}
1258
Victor Stinner06e49dd2010-06-13 18:21:50 +00001259static PyObject *
Serhiy Storchakaf95455d2016-05-16 10:11:47 +03001260getargs_f(PyObject *self, PyObject *args)
1261{
1262 float f;
1263 if (!PyArg_ParseTuple(args, "f", &f))
1264 return NULL;
1265 return PyFloat_FromDouble(f);
1266}
1267
1268static PyObject *
1269getargs_d(PyObject *self, PyObject *args)
1270{
1271 double d;
1272 if (!PyArg_ParseTuple(args, "d", &d))
1273 return NULL;
1274 return PyFloat_FromDouble(d);
1275}
1276
1277static PyObject *
1278getargs_D(PyObject *self, PyObject *args)
1279{
1280 Py_complex cval;
1281 if (!PyArg_ParseTuple(args, "D", &cval))
1282 return NULL;
1283 return PyComplex_FromCComplex(cval);
1284}
1285
1286static PyObject *
1287getargs_S(PyObject *self, PyObject *args)
1288{
1289 PyObject *obj;
1290 if (!PyArg_ParseTuple(args, "S", &obj))
1291 return NULL;
1292 Py_INCREF(obj);
1293 return obj;
1294}
1295
1296static PyObject *
1297getargs_Y(PyObject *self, PyObject *args)
1298{
1299 PyObject *obj;
1300 if (!PyArg_ParseTuple(args, "Y", &obj))
1301 return NULL;
1302 Py_INCREF(obj);
1303 return obj;
1304}
1305
1306static PyObject *
1307getargs_U(PyObject *self, PyObject *args)
1308{
1309 PyObject *obj;
1310 if (!PyArg_ParseTuple(args, "U", &obj))
1311 return NULL;
1312 Py_INCREF(obj);
1313 return obj;
1314}
1315
1316static PyObject *
Eli Bendersky906b88f2011-07-29 07:05:08 +03001317getargs_c(PyObject *self, PyObject *args)
1318{
1319 char c;
1320 if (!PyArg_ParseTuple(args, "c", &c))
1321 return NULL;
Serhiy Storchakac8241fd2016-01-28 19:49:54 +02001322 return PyLong_FromLong((unsigned char)c);
1323}
1324
1325static PyObject *
1326getargs_C(PyObject *self, PyObject *args)
1327{
1328 int c;
1329 if (!PyArg_ParseTuple(args, "C", &c))
1330 return NULL;
1331 return PyLong_FromLong(c);
Eli Bendersky906b88f2011-07-29 07:05:08 +03001332}
1333
1334static PyObject *
Victor Stinner06e49dd2010-06-13 18:21:50 +00001335getargs_s(PyObject *self, PyObject *args)
1336{
1337 char *str;
1338 if (!PyArg_ParseTuple(args, "s", &str))
1339 return NULL;
1340 return PyBytes_FromString(str);
1341}
1342
1343static PyObject *
1344getargs_s_star(PyObject *self, PyObject *args)
1345{
1346 Py_buffer buffer;
1347 PyObject *bytes;
1348 if (!PyArg_ParseTuple(args, "s*", &buffer))
1349 return NULL;
1350 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1351 PyBuffer_Release(&buffer);
1352 return bytes;
1353}
1354
1355static PyObject *
1356getargs_s_hash(PyObject *self, PyObject *args)
1357{
1358 char *str;
1359 Py_ssize_t size;
1360 if (!PyArg_ParseTuple(args, "s#", &str, &size))
1361 return NULL;
1362 return PyBytes_FromStringAndSize(str, size);
1363}
1364
1365static PyObject *
1366getargs_z(PyObject *self, PyObject *args)
1367{
1368 char *str;
1369 if (!PyArg_ParseTuple(args, "z", &str))
1370 return NULL;
1371 if (str != NULL)
1372 return PyBytes_FromString(str);
1373 else
1374 Py_RETURN_NONE;
1375}
1376
1377static PyObject *
1378getargs_z_star(PyObject *self, PyObject *args)
1379{
1380 Py_buffer buffer;
1381 PyObject *bytes;
1382 if (!PyArg_ParseTuple(args, "z*", &buffer))
1383 return NULL;
1384 if (buffer.buf != NULL)
1385 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1386 else {
1387 Py_INCREF(Py_None);
1388 bytes = Py_None;
1389 }
1390 PyBuffer_Release(&buffer);
1391 return bytes;
1392}
1393
1394static PyObject *
1395getargs_z_hash(PyObject *self, PyObject *args)
1396{
1397 char *str;
1398 Py_ssize_t size;
1399 if (!PyArg_ParseTuple(args, "z#", &str, &size))
1400 return NULL;
1401 if (str != NULL)
1402 return PyBytes_FromStringAndSize(str, size);
1403 else
1404 Py_RETURN_NONE;
1405}
1406
1407static PyObject *
1408getargs_y(PyObject *self, PyObject *args)
1409{
1410 char *str;
1411 if (!PyArg_ParseTuple(args, "y", &str))
1412 return NULL;
1413 return PyBytes_FromString(str);
1414}
1415
1416static PyObject *
1417getargs_y_star(PyObject *self, PyObject *args)
1418{
1419 Py_buffer buffer;
1420 PyObject *bytes;
1421 if (!PyArg_ParseTuple(args, "y*", &buffer))
1422 return NULL;
1423 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1424 PyBuffer_Release(&buffer);
1425 return bytes;
1426}
1427
1428static PyObject *
1429getargs_y_hash(PyObject *self, PyObject *args)
1430{
1431 char *str;
1432 Py_ssize_t size;
1433 if (!PyArg_ParseTuple(args, "y#", &str, &size))
1434 return NULL;
1435 return PyBytes_FromStringAndSize(str, size);
1436}
1437
1438static PyObject *
1439getargs_u(PyObject *self, PyObject *args)
1440{
1441 Py_UNICODE *str;
Victor Stinner06e49dd2010-06-13 18:21:50 +00001442 if (!PyArg_ParseTuple(args, "u", &str))
1443 return NULL;
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001444 return PyUnicode_FromWideChar(str, -1);
Victor Stinner06e49dd2010-06-13 18:21:50 +00001445}
1446
1447static PyObject *
1448getargs_u_hash(PyObject *self, PyObject *args)
1449{
1450 Py_UNICODE *str;
1451 Py_ssize_t size;
1452 if (!PyArg_ParseTuple(args, "u#", &str, &size))
1453 return NULL;
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001454 return PyUnicode_FromWideChar(str, size);
Victor Stinner06e49dd2010-06-13 18:21:50 +00001455}
1456
1457static PyObject *
1458getargs_Z(PyObject *self, PyObject *args)
1459{
1460 Py_UNICODE *str;
Victor Stinner06e49dd2010-06-13 18:21:50 +00001461 if (!PyArg_ParseTuple(args, "Z", &str))
1462 return NULL;
1463 if (str != NULL) {
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001464 return PyUnicode_FromWideChar(str, -1);
Victor Stinner06e49dd2010-06-13 18:21:50 +00001465 } else
1466 Py_RETURN_NONE;
1467}
1468
1469static PyObject *
1470getargs_Z_hash(PyObject *self, PyObject *args)
1471{
1472 Py_UNICODE *str;
1473 Py_ssize_t size;
1474 if (!PyArg_ParseTuple(args, "Z#", &str, &size))
1475 return NULL;
1476 if (str != NULL)
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001477 return PyUnicode_FromWideChar(str, size);
Victor Stinner06e49dd2010-06-13 18:21:50 +00001478 else
1479 Py_RETURN_NONE;
1480}
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001481
Serhiy Storchakac8241fd2016-01-28 19:49:54 +02001482static PyObject *
1483getargs_es(PyObject *self, PyObject *args)
1484{
1485 PyObject *arg, *result;
1486 const char *encoding = NULL;
1487 char *str;
1488
1489 if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding))
1490 return NULL;
1491 if (!PyArg_Parse(arg, "es", encoding, &str))
1492 return NULL;
1493 result = PyBytes_FromString(str);
1494 PyMem_Free(str);
1495 return result;
1496}
1497
1498static PyObject *
1499getargs_et(PyObject *self, PyObject *args)
1500{
1501 PyObject *arg, *result;
1502 const char *encoding = NULL;
1503 char *str;
1504
1505 if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding))
1506 return NULL;
1507 if (!PyArg_Parse(arg, "et", encoding, &str))
1508 return NULL;
1509 result = PyBytes_FromString(str);
1510 PyMem_Free(str);
1511 return result;
1512}
1513
1514static PyObject *
1515getargs_es_hash(PyObject *self, PyObject *args)
1516{
1517 PyObject *arg, *result;
1518 const char *encoding = NULL;
1519 PyByteArrayObject *buffer = NULL;
1520 char *str = NULL;
1521 Py_ssize_t size;
1522
1523 if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer))
1524 return NULL;
1525 if (buffer != NULL) {
1526 str = PyByteArray_AS_STRING(buffer);
1527 size = PyByteArray_GET_SIZE(buffer);
1528 }
1529 if (!PyArg_Parse(arg, "es#", encoding, &str, &size))
1530 return NULL;
1531 result = PyBytes_FromStringAndSize(str, size);
1532 if (buffer == NULL)
1533 PyMem_Free(str);
1534 return result;
1535}
1536
1537static PyObject *
1538getargs_et_hash(PyObject *self, PyObject *args)
1539{
1540 PyObject *arg, *result;
1541 const char *encoding = NULL;
1542 PyByteArrayObject *buffer = NULL;
1543 char *str = NULL;
1544 Py_ssize_t size;
1545
1546 if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer))
1547 return NULL;
1548 if (buffer != NULL) {
1549 str = PyByteArray_AS_STRING(buffer);
1550 size = PyByteArray_GET_SIZE(buffer);
1551 }
1552 if (!PyArg_Parse(arg, "et#", encoding, &str, &size))
1553 return NULL;
1554 result = PyBytes_FromStringAndSize(str, size);
1555 if (buffer == NULL)
1556 PyMem_Free(str);
1557 return result;
1558}
1559
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001560/* Test the s and z codes for PyArg_ParseTuple.
1561*/
1562static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301563test_s_code(PyObject *self, PyObject *Py_UNUSED(ignored))
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001564{
1565 /* Unicode strings should be accepted */
1566 PyObject *tuple, *obj;
1567 char *value;
1568
1569 tuple = PyTuple_New(1);
1570 if (tuple == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 return NULL;
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001572
1573 obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 "latin-1", NULL);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001575 if (obj == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 return NULL;
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001577
1578 PyTuple_SET_ITEM(tuple, 0, obj);
1579
1580 /* These two blocks used to raise a TypeError:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 * "argument must be string without null bytes, not str"
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001582 */
Oren Milmanba7d7362017-08-29 11:58:27 +03001583 if (!PyArg_ParseTuple(tuple, "s:test_s_code1", &value)) {
1584 return NULL;
1585 }
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001586
Oren Milmanba7d7362017-08-29 11:58:27 +03001587 if (!PyArg_ParseTuple(tuple, "z:test_s_code2", &value)) {
1588 return NULL;
1589 }
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001590
Alexandre Vassalottib645bc72008-05-15 22:06:59 +00001591 Py_DECREF(tuple);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +00001592 Py_RETURN_NONE;
1593}
1594
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001595static PyObject *
Larry Hastings8f904da2012-06-22 03:56:29 -07001596parse_tuple_and_keywords(PyObject *self, PyObject *args)
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001597{
Larry Hastings8f904da2012-06-22 03:56:29 -07001598 PyObject *sub_args;
1599 PyObject *sub_kwargs;
Serhiy Storchaka5f161fd2017-05-04 00:03:23 +03001600 const char *sub_format;
Larry Hastings8f904da2012-06-22 03:56:29 -07001601 PyObject *sub_keywords;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001602
Larry Hastings8f904da2012-06-22 03:56:29 -07001603 Py_ssize_t i, size;
1604 char *keywords[8 + 1]; /* space for NULL at end */
1605 PyObject *o;
1606 PyObject *converted[8];
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001607
Larry Hastings8f904da2012-06-22 03:56:29 -07001608 int result;
1609 PyObject *return_value = NULL;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001610
Larry Hastings22701e82012-08-08 14:52:22 -07001611 double buffers[8][4]; /* double ensures alignment where necessary */
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001612
Serhiy Storchaka5f161fd2017-05-04 00:03:23 +03001613 if (!PyArg_ParseTuple(args, "OOsO:parse_tuple_and_keywords",
Larry Hastings8f904da2012-06-22 03:56:29 -07001614 &sub_args, &sub_kwargs,
1615 &sub_format, &sub_keywords))
1616 return NULL;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001617
Larry Hastings8f904da2012-06-22 03:56:29 -07001618 if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) {
1619 PyErr_SetString(PyExc_ValueError,
1620 "parse_tuple_and_keywords: sub_keywords must be either list or tuple");
1621 return NULL;
1622 }
1623
1624 memset(buffers, 0, sizeof(buffers));
1625 memset(converted, 0, sizeof(converted));
1626 memset(keywords, 0, sizeof(keywords));
1627
1628 size = PySequence_Fast_GET_SIZE(sub_keywords);
1629 if (size > 8) {
1630 PyErr_SetString(PyExc_ValueError,
1631 "parse_tuple_and_keywords: too many keywords in sub_keywords");
1632 goto exit;
1633 }
1634
1635 for (i = 0; i < size; i++) {
1636 o = PySequence_Fast_GET_ITEM(sub_keywords, i);
1637 if (!PyUnicode_FSConverter(o, (void *)(converted + i))) {
1638 PyErr_Format(PyExc_ValueError,
Jesus Cea6e1d2b62012-10-04 16:06:30 +02001639 "parse_tuple_and_keywords: could not convert keywords[%zd] to narrow string", i);
Larry Hastings8f904da2012-06-22 03:56:29 -07001640 goto exit;
1641 }
1642 keywords[i] = PyBytes_AS_STRING(converted[i]);
1643 }
1644
1645 result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs,
1646 sub_format, keywords,
1647 buffers + 0, buffers + 1, buffers + 2, buffers + 3,
1648 buffers + 4, buffers + 5, buffers + 6, buffers + 7);
1649
1650 if (result) {
1651 return_value = Py_None;
1652 Py_INCREF(Py_None);
1653 }
1654
1655exit:
1656 size = sizeof(converted) / sizeof(converted[0]);
1657 for (i = 0; i < size; i++) {
1658 Py_XDECREF(converted[i]);
1659 }
1660 return return_value;
Mark Dickinsonf08173b2009-12-03 10:59:46 +00001661}
1662
Benjamin Peterson92035012008-12-27 16:00:54 +00001663static volatile int x;
1664
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001665/* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
1666 of an error.
1667*/
1668static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301669test_u_code(PyObject *self, PyObject *Py_UNUSED(ignored))
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 PyObject *tuple, *obj;
1672 Py_UNICODE *value;
1673 Py_ssize_t len;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
1676 /* Just use the macro and check that it compiles */
1677 x = Py_UNICODE_ISSPACE(25);
Benjamin Peterson206e3072008-10-19 14:07:49 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 tuple = PyTuple_New(1);
1680 if (tuple == NULL)
1681 return NULL;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 obj = PyUnicode_Decode("test", strlen("test"),
1684 "ascii", NULL);
1685 if (obj == NULL)
1686 return NULL;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 PyTuple_SET_ITEM(tuple, 0, obj);
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 value = 0;
Oren Milmanba7d7362017-08-29 11:58:27 +03001691 if (!PyArg_ParseTuple(tuple, "u:test_u_code", &value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03001693 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 if (value != PyUnicode_AS_UNICODE(obj))
1695 return raiseTestError("test_u_code",
1696 "u code returned wrong value for u'test'");
1697 value = 0;
Oren Milmanba7d7362017-08-29 11:58:27 +03001698 if (!PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03001700 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 if (value != PyUnicode_AS_UNICODE(obj) ||
1702 len != PyUnicode_GET_SIZE(obj))
1703 return raiseTestError("test_u_code",
1704 "u# code returned wrong values for u'test'");
Tim Petersbaefd9e2003-01-28 20:37:45 +00001705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 Py_DECREF(tuple);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001707 Py_RETURN_NONE;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001708}
1709
Guido van Rossumfb67be22007-08-29 18:38:11 +00001710/* Test Z and Z# codes for PyArg_ParseTuple */
1711static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301712test_Z_code(PyObject *self, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb67be22007-08-29 18:38:11 +00001713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 PyObject *tuple, *obj;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001715 const Py_UNICODE *value1, *value2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 Py_ssize_t len1, len2;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 tuple = PyTuple_New(2);
1719 if (tuple == NULL)
1720 return NULL;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 obj = PyUnicode_FromString("test");
1723 PyTuple_SET_ITEM(tuple, 0, obj);
1724 Py_INCREF(Py_None);
1725 PyTuple_SET_ITEM(tuple, 1, Py_None);
Guido van Rossumfb67be22007-08-29 18:38:11 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 /* swap values on purpose */
1728 value1 = NULL;
1729 value2 = PyUnicode_AS_UNICODE(obj);
Guido van Rossumfb67be22007-08-29 18:38:11 +00001730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 /* Test Z for both values */
Oren Milmanba7d7362017-08-29 11:58:27 +03001732 if (!PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03001734 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 if (value1 != PyUnicode_AS_UNICODE(obj))
1736 return raiseTestError("test_Z_code",
1737 "Z code returned wrong value for 'test'");
1738 if (value2 != NULL)
1739 return raiseTestError("test_Z_code",
1740 "Z code returned wrong value for None");
Guido van Rossumfb67be22007-08-29 18:38:11 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 value1 = NULL;
1743 value2 = PyUnicode_AS_UNICODE(obj);
1744 len1 = -1;
1745 len2 = -1;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 /* Test Z# for both values */
Oren Milmanba7d7362017-08-29 11:58:27 +03001748 if (!PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1,
1749 &value2, &len2))
1750 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03001752 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (value1 != PyUnicode_AS_UNICODE(obj) ||
1754 len1 != PyUnicode_GET_SIZE(obj))
1755 return raiseTestError("test_Z_code",
1756 "Z# code returned wrong values for 'test'");
1757 if (value2 != NULL ||
1758 len2 != 0)
1759 return raiseTestError("test_Z_code",
1760 "Z# code returned wrong values for None'");
Guido van Rossumfb67be22007-08-29 18:38:11 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 Py_DECREF(tuple);
1763 Py_RETURN_NONE;
Guido van Rossumfb67be22007-08-29 18:38:11 +00001764}
1765
Thomas Wouters477c8d52006-05-27 19:21:47 +00001766static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301767test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored))
Mark Dickinson081dfee2009-03-18 14:47:41 +00001768{
1769#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
1771 size_t wtextlen = 1;
Victor Stinnere3b47152011-12-09 20:49:49 +01001772 const wchar_t invalid[1] = {(wchar_t)0x110000u};
Mark Dickinson081dfee2009-03-18 14:47:41 +00001773#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
1775 size_t wtextlen = 2;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001776#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 PyObject *wide, *utf8;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 wide = PyUnicode_FromWideChar(wtext, wtextlen);
1780 if (wide == NULL)
1781 return NULL;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d");
1784 if (utf8 == NULL) {
1785 Py_DECREF(wide);
1786 return NULL;
1787 }
Mark Dickinson081dfee2009-03-18 14:47:41 +00001788
Victor Stinner8ef18872011-11-21 02:06:57 +01001789 if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 Py_DECREF(wide);
1791 Py_DECREF(utf8);
1792 return raiseTestError("test_widechar",
1793 "wide string and utf8 string "
1794 "have different length");
1795 }
1796 if (PyUnicode_Compare(wide, utf8)) {
1797 Py_DECREF(wide);
1798 Py_DECREF(utf8);
1799 if (PyErr_Occurred())
1800 return NULL;
1801 return raiseTestError("test_widechar",
1802 "wide string and utf8 string "
1803 "are different");
1804 }
Mark Dickinson081dfee2009-03-18 14:47:41 +00001805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 Py_DECREF(wide);
1807 Py_DECREF(utf8);
Victor Stinnere3b47152011-12-09 20:49:49 +01001808
1809#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
1810 wide = PyUnicode_FromWideChar(invalid, 1);
1811 if (wide == NULL)
1812 PyErr_Clear();
1813 else
1814 return raiseTestError("test_widechar",
1815 "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail");
1816
1817 wide = PyUnicode_FromUnicode(invalid, 1);
1818 if (wide == NULL)
1819 PyErr_Clear();
1820 else
1821 return raiseTestError("test_widechar",
1822 "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail");
Victor Stinnere5c05332013-03-06 00:39:03 +01001823
1824 wide = PyUnicode_FromUnicode(NULL, 1);
1825 if (wide == NULL)
1826 return NULL;
1827 PyUnicode_AS_UNICODE(wide)[0] = invalid[0];
Ezio Melotti03e667d2013-03-07 21:18:45 +02001828 if (_PyUnicode_Ready(wide) < 0) {
1829 Py_DECREF(wide);
Victor Stinnere5c05332013-03-06 00:39:03 +01001830 PyErr_Clear();
Ezio Melotti03e667d2013-03-07 21:18:45 +02001831 }
1832 else {
1833 Py_DECREF(wide);
Victor Stinnere5c05332013-03-06 00:39:03 +01001834 return raiseTestError("test_widechar",
1835 "PyUnicode_Ready() didn't fail");
Ezio Melotti03e667d2013-03-07 21:18:45 +02001836 }
Victor Stinnere3b47152011-12-09 20:49:49 +01001837#endif
1838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 Py_RETURN_NONE;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001840}
1841
1842static PyObject *
Victor Stinner46c7b3b2010-10-02 11:49:31 +00001843unicode_aswidechar(PyObject *self, PyObject *args)
Victor Stinner1c24bd02010-10-02 11:03:13 +00001844{
1845 PyObject *unicode, *result;
1846 Py_ssize_t buflen, size;
1847 wchar_t *buffer;
1848
1849 if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen))
1850 return NULL;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001851 buffer = PyMem_New(wchar_t, buflen);
Victor Stinner1c24bd02010-10-02 11:03:13 +00001852 if (buffer == NULL)
1853 return PyErr_NoMemory();
1854
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001855 size = PyUnicode_AsWideChar(unicode, buffer, buflen);
Victor Stinner1c24bd02010-10-02 11:03:13 +00001856 if (size == -1) {
1857 PyMem_Free(buffer);
1858 return NULL;
1859 }
1860
1861 if (size < buflen)
1862 buflen = size + 1;
1863 else
1864 buflen = size;
1865 result = PyUnicode_FromWideChar(buffer, buflen);
1866 PyMem_Free(buffer);
1867 if (result == NULL)
1868 return NULL;
1869
1870 return Py_BuildValue("(Nn)", result, size);
1871}
1872
1873static PyObject *
Victor Stinner46c7b3b2010-10-02 11:49:31 +00001874unicode_aswidecharstring(PyObject *self, PyObject *args)
Victor Stinner1c24bd02010-10-02 11:03:13 +00001875{
1876 PyObject *unicode, *result;
1877 Py_ssize_t size;
1878 wchar_t *buffer;
1879
1880 if (!PyArg_ParseTuple(args, "U", &unicode))
1881 return NULL;
1882
Victor Stinnerbeb4135b2010-10-07 01:02:42 +00001883 buffer = PyUnicode_AsWideCharString(unicode, &size);
Victor Stinner1c24bd02010-10-02 11:03:13 +00001884 if (buffer == NULL)
1885 return NULL;
1886
1887 result = PyUnicode_FromWideChar(buffer, size + 1);
1888 PyMem_Free(buffer);
1889 if (result == NULL)
1890 return NULL;
1891 return Py_BuildValue("(Nn)", result, size);
1892}
1893
1894static PyObject *
Serhiy Storchakacc164232016-10-02 21:29:26 +03001895unicode_asucs4(PyObject *self, PyObject *args)
1896{
1897 PyObject *unicode, *result;
1898 Py_UCS4 *buffer;
1899 int copy_null;
1900 Py_ssize_t str_len, buf_len;
1901
1902 if (!PyArg_ParseTuple(args, "Unp:unicode_asucs4", &unicode, &str_len, &copy_null)) {
1903 return NULL;
1904 }
1905
1906 buf_len = str_len + 1;
1907 buffer = PyMem_NEW(Py_UCS4, buf_len);
1908 if (buffer == NULL) {
1909 return PyErr_NoMemory();
1910 }
1911 memset(buffer, 0, sizeof(Py_UCS4)*buf_len);
1912 buffer[str_len] = 0xffffU;
1913
1914 if (!PyUnicode_AsUCS4(unicode, buffer, buf_len, copy_null)) {
1915 PyMem_FREE(buffer);
1916 return NULL;
1917 }
1918
1919 result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, buf_len);
1920 PyMem_FREE(buffer);
1921 return result;
1922}
1923
1924static PyObject *
Xiang Zhangb2110682016-12-20 22:52:33 +08001925unicode_findchar(PyObject *self, PyObject *args)
1926{
1927 PyObject *str;
1928 int direction;
1929 unsigned int ch;
1930 Py_ssize_t result;
1931 Py_ssize_t start, end;
1932
1933 if (!PyArg_ParseTuple(args, "UInni:unicode_findchar", &str, &ch,
1934 &start, &end, &direction)) {
1935 return NULL;
1936 }
1937
1938 result = PyUnicode_FindChar(str, (Py_UCS4)ch, start, end, direction);
1939 if (result == -2)
1940 return NULL;
1941 else
1942 return PyLong_FromSsize_t(result);
1943}
1944
1945static PyObject *
Serhiy Storchaka9c0e1f82016-10-08 22:45:38 +03001946unicode_copycharacters(PyObject *self, PyObject *args)
1947{
1948 PyObject *from, *to, *to_copy;
1949 Py_ssize_t from_start, to_start, how_many, copied;
1950
1951 if (!PyArg_ParseTuple(args, "UnOnn:unicode_copycharacters", &to, &to_start,
1952 &from, &from_start, &how_many)) {
1953 return NULL;
1954 }
1955
Serhiy Storchaka9c0e1f82016-10-08 22:45:38 +03001956 if (!(to_copy = PyUnicode_New(PyUnicode_GET_LENGTH(to),
1957 PyUnicode_MAX_CHAR_VALUE(to)))) {
1958 return NULL;
1959 }
1960 if (PyUnicode_Fill(to_copy, 0, PyUnicode_GET_LENGTH(to_copy), 0U) < 0) {
1961 Py_DECREF(to_copy);
1962 return NULL;
1963 }
1964
1965 if ((copied = PyUnicode_CopyCharacters(to_copy, to_start, from,
1966 from_start, how_many)) < 0) {
1967 Py_DECREF(to_copy);
1968 return NULL;
1969 }
1970
1971 return Py_BuildValue("(Nn)", to_copy, copied);
1972}
1973
1974static PyObject *
Victor Stinner42bf7752011-11-21 22:52:58 +01001975unicode_encodedecimal(PyObject *self, PyObject *args)
1976{
1977 Py_UNICODE *unicode;
1978 Py_ssize_t length;
1979 char *errors = NULL;
1980 PyObject *decimal;
1981 Py_ssize_t decimal_length, new_length;
1982 int res;
1983
1984 if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors))
1985 return NULL;
1986
1987 decimal_length = length * 7; /* len('&#8364;') */
1988 decimal = PyBytes_FromStringAndSize(NULL, decimal_length);
1989 if (decimal == NULL)
1990 return NULL;
1991
1992 res = PyUnicode_EncodeDecimal(unicode, length,
1993 PyBytes_AS_STRING(decimal),
1994 errors);
1995 if (res < 0) {
1996 Py_DECREF(decimal);
1997 return NULL;
1998 }
1999
2000 new_length = strlen(PyBytes_AS_STRING(decimal));
2001 assert(new_length <= decimal_length);
2002 res = _PyBytes_Resize(&decimal, new_length);
2003 if (res < 0)
2004 return NULL;
2005
2006 return decimal;
2007}
2008
2009static PyObject *
2010unicode_transformdecimaltoascii(PyObject *self, PyObject *args)
2011{
2012 Py_UNICODE *unicode;
2013 Py_ssize_t length;
2014 if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length))
2015 return NULL;
2016 return PyUnicode_TransformDecimalToASCII(unicode, length);
2017}
2018
2019static PyObject *
Stefan Krahe6996ed2012-11-02 14:44:20 +01002020unicode_legacy_string(PyObject *self, PyObject *args)
2021{
2022 Py_UNICODE *data;
2023 Py_ssize_t len;
2024 PyObject *u;
2025
2026 if (!PyArg_ParseTuple(args, "u#", &data, &len))
2027 return NULL;
2028
2029 u = PyUnicode_FromUnicode(NULL, len);
2030 if (u == NULL)
2031 return NULL;
2032
2033 memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE));
2034
2035 if (len > 0) { /* The empty string is always ready. */
2036 assert(!PyUnicode_IS_READY(u));
2037 }
2038
2039 return u;
2040}
2041
2042static PyObject *
Victor Stinner25e8ec42010-06-25 00:02:38 +00002043getargs_w_star(PyObject *self, PyObject *args)
2044{
2045 Py_buffer buffer;
2046 PyObject *result;
2047 char *str;
2048
2049 if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer))
2050 return NULL;
2051
2052 if (2 <= buffer.len) {
2053 str = buffer.buf;
2054 str[0] = '[';
2055 str[buffer.len-1] = ']';
2056 }
2057
2058 result = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
2059 PyBuffer_Release(&buffer);
2060 return result;
2061}
2062
2063
2064static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302065test_empty_argparse(PyObject *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson92035012008-12-27 16:00:54 +00002066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 /* Test that formats can begin with '|'. See issue #4720. */
2068 PyObject *tuple, *dict = NULL;
2069 static char *kwlist[] = {NULL};
2070 int result;
2071 tuple = PyTuple_New(0);
2072 if (!tuple)
2073 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03002074 if (!(result = PyArg_ParseTuple(tuple, "|:test_empty_argparse"))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 goto done;
Oren Milmanba7d7362017-08-29 11:58:27 +03002076 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 dict = PyDict_New();
2078 if (!dict)
2079 goto done;
2080 result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
Benjamin Peterson92035012008-12-27 16:00:54 +00002081 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 Py_DECREF(tuple);
2083 Py_XDECREF(dict);
Oren Milmanba7d7362017-08-29 11:58:27 +03002084 if (!result) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 return NULL;
Oren Milmanba7d7362017-08-29 11:58:27 +03002086 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 else {
2088 Py_RETURN_NONE;
2089 }
Benjamin Peterson92035012008-12-27 16:00:54 +00002090}
2091
2092static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00002093codec_incrementalencoder(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 const char *encoding, *errors = NULL;
2096 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
2097 &encoding, &errors))
2098 return NULL;
2099 return PyCodec_IncrementalEncoder(encoding, errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002100}
2101
Thomas Wouters477c8d52006-05-27 19:21:47 +00002102static PyObject *
2103codec_incrementaldecoder(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 const char *encoding, *errors = NULL;
2106 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
2107 &encoding, &errors))
2108 return NULL;
2109 return PyCodec_IncrementalDecoder(encoding, errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002110}
2111
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00002112
Tim Peters5b8132f2003-01-31 15:52:05 +00002113/* Simple test of _PyLong_NumBits and _PyLong_Sign. */
Tim Petersbaefd9e2003-01-28 20:37:45 +00002114static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302115test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored))
Tim Petersbaefd9e2003-01-28 20:37:45 +00002116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 struct triple {
2118 long input;
2119 size_t nbits;
2120 int sign;
2121 } testcases[] = {{0, 0, 0},
2122 {1L, 1, 1},
2123 {-1L, 1, -1},
2124 {2L, 2, 1},
2125 {-2L, 2, -1},
2126 {3L, 2, 1},
2127 {-3L, 2, -1},
2128 {4L, 3, 1},
2129 {-4L, 3, -1},
Serhiy Storchaka95949422013-08-27 19:40:23 +03002130 {0x7fffL, 15, 1}, /* one Python int digit */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 {-0x7fffL, 15, -1},
2132 {0xffffL, 16, 1},
2133 {-0xffffL, 16, -1},
2134 {0xfffffffL, 28, 1},
2135 {-0xfffffffL, 28, -1}};
Victor Stinner706768c2014-08-16 01:03:39 +02002136 size_t i;
Tim Petersbaefd9e2003-01-28 20:37:45 +00002137
Victor Stinner63941882011-09-29 00:42:28 +02002138 for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) {
Christian Heimes3205e742013-07-26 15:06:48 +02002139 size_t nbits;
2140 int sign;
2141 PyObject *plong;
2142
2143 plong = PyLong_FromLong(testcases[i].input);
Christian Heimesff369a52013-07-26 14:52:18 +02002144 if (plong == NULL)
2145 return NULL;
Christian Heimes3205e742013-07-26 15:06:48 +02002146 nbits = _PyLong_NumBits(plong);
2147 sign = _PyLong_Sign(plong);
Tim Petersbaefd9e2003-01-28 20:37:45 +00002148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 Py_DECREF(plong);
2150 if (nbits != testcases[i].nbits)
2151 return raiseTestError("test_long_numbits",
2152 "wrong result for _PyLong_NumBits");
2153 if (sign != testcases[i].sign)
2154 return raiseTestError("test_long_numbits",
2155 "wrong result for _PyLong_Sign");
2156 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002157 Py_RETURN_NONE;
Tim Petersbaefd9e2003-01-28 20:37:45 +00002158}
2159
Thomas Heller519a0422007-11-15 20:48:54 +00002160/* Example passing NULLs to PyObject_Str(NULL). */
Thomas Woutersa9773292006-04-21 09:43:23 +00002161
2162static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302163test_null_strings(PyObject *self, PyObject *Py_UNUSED(ignored))
Thomas Woutersa9773292006-04-21 09:43:23 +00002164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL);
2166 PyObject *tuple = PyTuple_Pack(2, o1, o2);
2167 Py_XDECREF(o1);
2168 Py_XDECREF(o2);
2169 return tuple;
Thomas Woutersa9773292006-04-21 09:43:23 +00002170}
2171
Jeremy Hyltonede049b2001-09-26 20:01:13 +00002172static PyObject *
2173raise_exception(PyObject *self, PyObject *args)
2174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 PyObject *exc;
2176 PyObject *exc_args, *v;
2177 int num_args, i;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00002178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
2180 &exc, &num_args))
2181 return NULL;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00002182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 exc_args = PyTuple_New(num_args);
2184 if (exc_args == NULL)
2185 return NULL;
2186 for (i = 0; i < num_args; ++i) {
2187 v = PyLong_FromLong(i);
2188 if (v == NULL) {
2189 Py_DECREF(exc_args);
2190 return NULL;
2191 }
2192 PyTuple_SET_ITEM(exc_args, i, v);
2193 }
2194 PyErr_SetObject(exc, exc_args);
2195 Py_DECREF(exc_args);
2196 return NULL;
Jeremy Hyltonede049b2001-09-26 20:01:13 +00002197}
Tim Peters91621db2001-06-12 20:10:01 +00002198
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +02002199static PyObject *
Antoine Pitrou6bc217d2015-06-23 14:31:11 +02002200set_errno(PyObject *self, PyObject *args)
2201{
2202 int new_errno;
2203
2204 if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno))
2205 return NULL;
2206
2207 errno = new_errno;
2208 Py_RETURN_NONE;
2209}
2210
2211static PyObject *
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +02002212test_set_exc_info(PyObject *self, PyObject *args)
2213{
2214 PyObject *orig_exc;
2215 PyObject *new_type, *new_value, *new_tb;
2216 PyObject *type, *value, *tb;
2217 if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info",
2218 &new_type, &new_value, &new_tb))
2219 return NULL;
2220
2221 PyErr_GetExcInfo(&type, &value, &tb);
2222
2223 Py_INCREF(new_type);
2224 Py_INCREF(new_value);
2225 Py_INCREF(new_tb);
2226 PyErr_SetExcInfo(new_type, new_value, new_tb);
2227
2228 orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None);
2229 Py_XDECREF(type);
2230 Py_XDECREF(value);
2231 Py_XDECREF(tb);
2232 return orig_exc;
2233}
Benjamin Peterson16323982010-02-03 01:13:41 +00002234
2235static int test_run_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002236
Benjamin Peterson91d58bd2009-12-13 21:30:54 +00002237static PyObject *
2238test_datetime_capi(PyObject *self, PyObject *args) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 if (PyDateTimeAPI) {
2240 if (test_run_counter) {
2241 /* Probably regrtest.py -R */
2242 Py_RETURN_NONE;
2243 }
2244 else {
2245 PyErr_SetString(PyExc_AssertionError,
2246 "PyDateTime_CAPI somehow initialized");
2247 return NULL;
2248 }
2249 }
2250 test_run_counter++;
2251 PyDateTime_IMPORT;
Paul Ganssle04af5b12018-01-24 17:29:30 -05002252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 if (PyDateTimeAPI)
2254 Py_RETURN_NONE;
2255 else
2256 return NULL;
Benjamin Peterson91d58bd2009-12-13 21:30:54 +00002257}
2258
Paul Ganssle04af5b12018-01-24 17:29:30 -05002259/* Functions exposing the C API type checking for testing */
2260#define MAKE_DATETIME_CHECK_FUNC(check_method, exact_method) \
2261 PyObject *obj; \
2262 int exact = 0; \
2263 if (!PyArg_ParseTuple(args, "O|p", &obj, &exact)) { \
2264 return NULL; \
2265 } \
2266 int rv = exact?exact_method(obj):check_method(obj); \
2267 if (rv) { \
2268 Py_RETURN_TRUE; \
2269 } else { \
2270 Py_RETURN_FALSE; \
2271 }
2272
2273static PyObject *
2274datetime_check_date(PyObject *self, PyObject *args) {
2275 MAKE_DATETIME_CHECK_FUNC(PyDate_Check, PyDate_CheckExact)
2276}
2277
2278static PyObject *
2279datetime_check_time(PyObject *self, PyObject *args) {
2280 MAKE_DATETIME_CHECK_FUNC(PyTime_Check, PyTime_CheckExact)
2281}
2282
2283static PyObject *
2284datetime_check_datetime(PyObject *self, PyObject *args) {
2285 MAKE_DATETIME_CHECK_FUNC(PyDateTime_Check, PyDateTime_CheckExact)
2286}
2287
2288static PyObject *
2289datetime_check_delta(PyObject *self, PyObject *args) {
2290 MAKE_DATETIME_CHECK_FUNC(PyDelta_Check, PyDelta_CheckExact)
2291}
2292
2293static PyObject *
2294datetime_check_tzinfo(PyObject *self, PyObject *args) {
2295 MAKE_DATETIME_CHECK_FUNC(PyTZInfo_Check, PyTZInfo_CheckExact)
2296}
2297
2298
2299/* Makes three variations on timezone representing UTC-5:
2300 1. timezone with offset and name from PyDateTimeAPI
2301 2. timezone with offset and name from PyTimeZone_FromOffsetAndName
2302 3. timezone with offset (no name) from PyTimeZone_FromOffset
2303*/
2304static PyObject *
2305make_timezones_capi(PyObject *self, PyObject *args) {
2306 PyObject *offset = PyDelta_FromDSU(0, -18000, 0);
2307 PyObject *name = PyUnicode_FromString("EST");
2308
2309 PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name);
2310 PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name);
2311 PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset);
2312
2313 Py_DecRef(offset);
2314 Py_DecRef(name);
2315
2316 PyObject *rv = PyTuple_New(3);
2317
2318 PyTuple_SET_ITEM(rv, 0, est_zone_capi);
2319 PyTuple_SET_ITEM(rv, 1, est_zone_macro);
2320 PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname);
2321
2322 return rv;
2323}
2324
2325static PyObject *
Paul Gansslea049f572018-02-22 15:15:32 -05002326get_timezones_offset_zero(PyObject *self, PyObject *args) {
2327 PyObject *offset = PyDelta_FromDSU(0, 0, 0);
2328 PyObject *name = PyUnicode_FromString("");
2329
2330 // These two should return the UTC singleton
2331 PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset);
2332 PyObject *utc_singleton_1 = PyTimeZone_FromOffsetAndName(offset, NULL);
2333
2334 // This one will return +00:00 zone, but not the UTC singleton
2335 PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name);
2336
2337 Py_DecRef(offset);
2338 Py_DecRef(name);
2339
2340 PyObject *rv = PyTuple_New(3);
2341 PyTuple_SET_ITEM(rv, 0, utc_singleton_0);
2342 PyTuple_SET_ITEM(rv, 1, utc_singleton_1);
2343 PyTuple_SET_ITEM(rv, 2, non_utc_zone);
2344
2345 return rv;
2346}
2347
2348static PyObject *
Paul Ganssle04af5b12018-01-24 17:29:30 -05002349get_timezone_utc_capi(PyObject* self, PyObject *args) {
2350 int macro = 0;
2351 if (!PyArg_ParseTuple(args, "|p", &macro)) {
2352 return NULL;
2353 }
2354 if (macro) {
Paul Ganssle58dc03c2018-01-25 08:58:07 -05002355 Py_INCREF(PyDateTime_TimeZone_UTC);
Paul Ganssle04af5b12018-01-24 17:29:30 -05002356 return PyDateTime_TimeZone_UTC;
2357 } else {
Paul Ganssle58dc03c2018-01-25 08:58:07 -05002358 Py_INCREF(PyDateTimeAPI->TimeZone_UTC);
Paul Ganssle04af5b12018-01-24 17:29:30 -05002359 return PyDateTimeAPI->TimeZone_UTC;
2360 }
2361}
2362
Paul Ganssle4d8c8c02019-04-27 15:39:40 -04002363static PyObject *
Edison A98ff4d52019-05-17 13:28:42 -07002364get_date_fromdate(PyObject *self, PyObject *args)
2365{
2366 PyObject *rv = NULL;
2367 int macro;
2368 int year, month, day;
2369
2370 if (!PyArg_ParseTuple(args, "piii", &macro, &year, &month, &day)) {
2371 return NULL;
2372 }
2373
2374 if (macro) {
2375 rv = PyDate_FromDate(year, month, day);
2376 }
2377 else {
2378 rv = PyDateTimeAPI->Date_FromDate(
2379 year, month, day,
2380 PyDateTimeAPI->DateType);
2381 }
2382 return rv;
2383}
2384
2385static PyObject *
2386get_datetime_fromdateandtime(PyObject *self, PyObject *args)
2387{
2388 PyObject *rv = NULL;
2389 int macro;
2390 int year, month, day;
2391 int hour, minute, second, microsecond;
2392
2393 if (!PyArg_ParseTuple(args, "piiiiiii",
2394 &macro,
2395 &year, &month, &day,
2396 &hour, &minute, &second, &microsecond)) {
2397 return NULL;
2398 }
2399
2400 if (macro) {
2401 rv = PyDateTime_FromDateAndTime(
2402 year, month, day,
2403 hour, minute, second, microsecond);
2404 }
2405 else {
2406 rv = PyDateTimeAPI->DateTime_FromDateAndTime(
2407 year, month, day,
2408 hour, minute, second, microsecond,
2409 Py_None,
2410 PyDateTimeAPI->DateTimeType);
2411 }
2412 return rv;
2413}
2414
2415static PyObject *
2416get_datetime_fromdateandtimeandfold(PyObject *self, PyObject *args)
2417{
2418 PyObject *rv = NULL;
2419 int macro;
2420 int year, month, day;
2421 int hour, minute, second, microsecond, fold;
2422
2423 if (!PyArg_ParseTuple(args, "piiiiiiii",
2424 &macro,
2425 &year, &month, &day,
2426 &hour, &minute, &second, &microsecond,
2427 &fold)) {
2428 return NULL;
2429 }
2430
2431 if (macro) {
2432 rv = PyDateTime_FromDateAndTimeAndFold(
2433 year, month, day,
2434 hour, minute, second, microsecond,
2435 fold);
2436 }
2437 else {
2438 rv = PyDateTimeAPI->DateTime_FromDateAndTimeAndFold(
2439 year, month, day,
2440 hour, minute, second, microsecond,
2441 Py_None,
2442 fold,
2443 PyDateTimeAPI->DateTimeType);
2444 }
2445 return rv;
2446}
2447
2448static PyObject *
2449get_time_fromtime(PyObject *self, PyObject *args)
2450{
2451 PyObject *rv = NULL;
2452 int macro;
2453 int hour, minute, second, microsecond;
2454
2455 if (!PyArg_ParseTuple(args, "piiii",
2456 &macro,
2457 &hour, &minute, &second, &microsecond)) {
2458 return NULL;
2459 }
2460
2461 if (macro) {
2462 rv = PyTime_FromTime(hour, minute, second, microsecond);
2463 }
2464 else {
2465 rv = PyDateTimeAPI->Time_FromTime(
2466 hour, minute, second, microsecond,
2467 Py_None,
2468 PyDateTimeAPI->TimeType);
2469 }
2470 return rv;
2471}
2472
2473static PyObject *
2474get_time_fromtimeandfold(PyObject *self, PyObject *args)
2475{
2476 PyObject *rv = NULL;
2477 int macro;
2478 int hour, minute, second, microsecond, fold;
2479
2480 if (!PyArg_ParseTuple(args, "piiiii",
2481 &macro,
2482 &hour, &minute, &second, &microsecond,
2483 &fold)) {
2484 return NULL;
2485 }
2486
2487 if (macro) {
2488 rv = PyTime_FromTimeAndFold(hour, minute, second, microsecond, fold);
2489 }
2490 else {
2491 rv = PyDateTimeAPI->Time_FromTimeAndFold(
2492 hour, minute, second, microsecond,
2493 Py_None,
2494 fold,
2495 PyDateTimeAPI->TimeType);
2496 }
2497 return rv;
2498}
2499
2500static PyObject *
2501get_delta_fromdsu(PyObject *self, PyObject *args)
2502{
2503 PyObject *rv = NULL;
2504 int macro;
2505 int days, seconds, microseconds;
2506
2507 if (!PyArg_ParseTuple(args, "piii",
2508 &macro,
2509 &days, &seconds, &microseconds)) {
2510 return NULL;
2511 }
2512
2513 if (macro) {
2514 rv = PyDelta_FromDSU(days, seconds, microseconds);
2515 }
2516 else {
2517 rv = PyDateTimeAPI->Delta_FromDelta(
2518 days, seconds, microseconds, 1,
2519 PyDateTimeAPI->DeltaType);
2520 }
2521
2522 return rv;
2523}
2524
2525static PyObject *
Paul Ganssle4d8c8c02019-04-27 15:39:40 -04002526get_date_fromtimestamp(PyObject* self, PyObject *args)
2527{
2528 PyObject *tsargs = NULL, *ts = NULL, *rv = NULL;
2529 int macro = 0;
2530
2531 if (!PyArg_ParseTuple(args, "O|p", &ts, &macro)) {
2532 return NULL;
2533 }
2534
2535 // Construct the argument tuple
2536 if ((tsargs = PyTuple_Pack(1, ts)) == NULL) {
2537 return NULL;
2538 }
2539
2540 // Pass along to the API function
2541 if (macro) {
2542 rv = PyDate_FromTimestamp(tsargs);
2543 }
2544 else {
2545 rv = PyDateTimeAPI->Date_FromTimestamp(
2546 (PyObject *)PyDateTimeAPI->DateType, tsargs
2547 );
2548 }
2549
2550 Py_DECREF(tsargs);
2551 return rv;
2552}
2553
2554static PyObject *
2555get_datetime_fromtimestamp(PyObject* self, PyObject *args)
2556{
2557 int macro = 0;
2558 int usetz = 0;
2559 PyObject *tsargs = NULL, *ts = NULL, *tzinfo = Py_None, *rv = NULL;
2560 if (!PyArg_ParseTuple(args, "OO|pp", &ts, &tzinfo, &usetz, &macro)) {
2561 return NULL;
2562 }
2563
2564 // Construct the argument tuple
2565 if (usetz) {
2566 tsargs = PyTuple_Pack(2, ts, tzinfo);
2567 }
2568 else {
2569 tsargs = PyTuple_Pack(1, ts);
2570 }
2571
2572 if (tsargs == NULL) {
2573 return NULL;
2574 }
2575
2576 // Pass along to the API function
2577 if (macro) {
2578 rv = PyDateTime_FromTimestamp(tsargs);
2579 }
2580 else {
2581 rv = PyDateTimeAPI->DateTime_FromTimestamp(
2582 (PyObject *)PyDateTimeAPI->DateTimeType, tsargs, NULL
2583 );
2584 }
2585
2586 Py_DECREF(tsargs);
2587 return rv;
2588}
2589
Benjamin Peterson16323982010-02-03 01:13:41 +00002590
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002591/* test_thread_state spawns a thread of its own, and that thread releases
2592 * `thread_done` when it's finished. The driver code has to know when the
2593 * thread finishes, because the thread uses a PyObject (the callable) that
2594 * may go away when the driver finishes. The former lack of this explicit
2595 * synchronization caused rare segfaults, so rare that they were seen only
2596 * on a Mac buildbot (although they were possible on any box).
2597 */
2598static PyThread_type_lock thread_done = NULL;
2599
Benjamin Petersona786b022008-08-25 21:05:21 +00002600static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002601_make_call(void *callable)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 PyObject *rc;
2604 int success;
2605 PyGILState_STATE s = PyGILState_Ensure();
Victor Stinner3466bde2016-09-05 18:16:01 -07002606 rc = _PyObject_CallNoArg((PyObject *)callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 success = (rc != NULL);
2608 Py_XDECREF(rc);
2609 PyGILState_Release(s);
2610 return success;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002611}
2612
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002613/* Same thing, but releases `thread_done` when it returns. This variant
2614 * should be called only from threads spawned by test_thread_state().
2615 */
2616static void
2617_make_call_from_thread(void *callable)
2618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 _make_call(callable);
2620 PyThread_release_lock(thread_done);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002621}
2622
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002623static PyObject *
2624test_thread_state(PyObject *self, PyObject *args)
2625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 PyObject *fn;
2627 int success = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
2630 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 if (!PyCallable_Check(fn)) {
2633 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
2634 fn->ob_type->tp_name);
2635 return NULL;
2636 }
Benjamin Petersona786b022008-08-25 21:05:21 +00002637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 /* Ensure Python is set up for threading */
2639 PyEval_InitThreads();
2640 thread_done = PyThread_allocate_lock();
2641 if (thread_done == NULL)
2642 return PyErr_NoMemory();
2643 PyThread_acquire_lock(thread_done, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 /* Start a new thread with our callback. */
2646 PyThread_start_new_thread(_make_call_from_thread, fn);
2647 /* Make the callback with the thread lock held by this thread */
2648 success &= _make_call(fn);
2649 /* Do it all again, but this time with the thread-lock released */
2650 Py_BEGIN_ALLOW_THREADS
2651 success &= _make_call(fn);
2652 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
2653 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 /* And once more with and without a thread
2656 XXX - should use a lock and work out exactly what we are trying
2657 to test <wink>
2658 */
2659 Py_BEGIN_ALLOW_THREADS
2660 PyThread_start_new_thread(_make_call_from_thread, fn);
2661 success &= _make_call(fn);
2662 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
2663 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 /* Release lock we acquired above. This is required on HP-UX. */
2666 PyThread_release_lock(thread_done);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 PyThread_free_lock(thread_done);
2669 if (!success)
2670 return NULL;
2671 Py_RETURN_NONE;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002672}
Benjamin Petersona54c9092009-01-13 02:11:23 +00002673
2674/* test Py_AddPendingCalls using threads */
2675static int _pending_callback(void *arg)
2676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 /* we assume the argument is callable object to which we own a reference */
2678 PyObject *callable = (PyObject *)arg;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01002679 PyObject *r = _PyObject_CallNoArg(callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 Py_DECREF(callable);
2681 Py_XDECREF(r);
2682 return r != NULL ? 0 : -1;
Benjamin Petersona54c9092009-01-13 02:11:23 +00002683}
2684
2685/* The following requests n callbacks to _pending_callback. It can be
2686 * run from any python thread.
2687 */
Benjamin Petersonb4588c22018-07-03 22:30:56 -07002688static PyObject *
2689pending_threadfunc(PyObject *self, PyObject *arg)
Benjamin Petersona54c9092009-01-13 02:11:23 +00002690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 PyObject *callable;
2692 int r;
2693 if (PyArg_ParseTuple(arg, "O", &callable) == 0)
2694 return NULL;
Benjamin Petersona54c9092009-01-13 02:11:23 +00002695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 /* create the reference for the callbackwhile we hold the lock */
2697 Py_INCREF(callable);
Benjamin Petersona54c9092009-01-13 02:11:23 +00002698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 Py_BEGIN_ALLOW_THREADS
2700 r = Py_AddPendingCall(&_pending_callback, callable);
2701 Py_END_ALLOW_THREADS
Benjamin Petersona54c9092009-01-13 02:11:23 +00002702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 if (r<0) {
2704 Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002705 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002707 Py_RETURN_TRUE;
Benjamin Petersona54c9092009-01-13 02:11:23 +00002708}
Mark Hammond8d98d2c2003-04-19 15:41:53 +00002709
Neal Norwitzb0d26332007-08-25 00:49:05 +00002710/* Some tests of PyUnicode_FromFormat(). This needs more tests. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002711static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02002712test_string_from_format(PyObject *self, PyObject *Py_UNUSED(ignored))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 PyObject *result;
2715 char *msg;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002716
Alexander Belopolskye239d232010-12-08 23:31:48 +00002717#define CHECK_1_FORMAT(FORMAT, TYPE) \
2718 result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \
2719 if (result == NULL) \
2720 return NULL; \
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02002721 if (!_PyUnicode_EqualToASCIIString(result, "1")) { \
Alexander Belopolskye239d232010-12-08 23:31:48 +00002722 msg = FORMAT " failed at 1"; \
2723 goto Fail; \
2724 } \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 Py_DECREF(result)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 CHECK_1_FORMAT("%d", int);
2728 CHECK_1_FORMAT("%ld", long);
2729 /* The z width modifier was added in Python 2.5. */
2730 CHECK_1_FORMAT("%zd", Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 /* The u type code was added in Python 2.5. */
2733 CHECK_1_FORMAT("%u", unsigned int);
2734 CHECK_1_FORMAT("%lu", unsigned long);
2735 CHECK_1_FORMAT("%zu", size_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 /* "%lld" and "%llu" support added in Python 2.7. */
Benjamin Petersonaf580df2016-09-06 10:46:49 -07002738 CHECK_1_FORMAT("%llu", unsigned long long);
2739 CHECK_1_FORMAT("%lld", long long);
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00002740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002742
2743 Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 Py_XDECREF(result);
2745 return raiseTestError("test_string_from_format", msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002746
2747#undef CHECK_1_FORMAT
2748}
2749
Benjamin Peterson8667a9b2010-01-09 21:45:28 +00002750
2751static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302752test_unicode_compare_with_ascii(PyObject *self, PyObject *Py_UNUSED(ignored)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4);
2754 int result;
2755 if (py_s == NULL)
2756 return NULL;
2757 result = PyUnicode_CompareWithASCIIString(py_s, "str");
2758 Py_DECREF(py_s);
2759 if (!result) {
2760 PyErr_SetString(TestError, "Python string ending in NULL "
2761 "should not compare equal to c string.");
2762 return NULL;
2763 }
2764 Py_RETURN_NONE;
Victor Stinner3e2b7172010-11-09 09:32:19 +00002765}
Benjamin Peterson8667a9b2010-01-09 21:45:28 +00002766
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002767/* This is here to provide a docstring for test_descr. */
2768static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302769test_with_docstring(PyObject *self, PyObject *Py_UNUSED(ignored))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002772}
2773
Mark Dickinson725bfd82009-05-03 20:33:40 +00002774/* Test PyOS_string_to_double. */
2775static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302776test_string_to_double(PyObject *self, PyObject *Py_UNUSED(ignored)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 double result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02002778 const char *msg;
Mark Dickinson725bfd82009-05-03 20:33:40 +00002779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780#define CHECK_STRING(STR, expected) \
2781 result = PyOS_string_to_double(STR, NULL, NULL); \
2782 if (result == -1.0 && PyErr_Occurred()) \
2783 return NULL; \
Benjamin Peterson8f4b2472016-09-07 18:09:22 -07002784 if (result != (double)expected) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 msg = "conversion of " STR " to float failed"; \
2786 goto fail; \
2787 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00002788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789#define CHECK_INVALID(STR) \
2790 result = PyOS_string_to_double(STR, NULL, NULL); \
2791 if (result == -1.0 && PyErr_Occurred()) { \
2792 if (PyErr_ExceptionMatches(PyExc_ValueError)) \
2793 PyErr_Clear(); \
2794 else \
2795 return NULL; \
2796 } \
2797 else { \
2798 msg = "conversion of " STR " didn't raise ValueError"; \
2799 goto fail; \
2800 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00002801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 CHECK_STRING("0.1", 0.1);
2803 CHECK_STRING("1.234", 1.234);
2804 CHECK_STRING("-1.35", -1.35);
2805 CHECK_STRING(".1e01", 1.0);
2806 CHECK_STRING("2.e-2", 0.02);
Mark Dickinson725bfd82009-05-03 20:33:40 +00002807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 CHECK_INVALID(" 0.1");
2809 CHECK_INVALID("\t\n-3");
2810 CHECK_INVALID(".123 ");
2811 CHECK_INVALID("3\n");
2812 CHECK_INVALID("123abc");
Mark Dickinson725bfd82009-05-03 20:33:40 +00002813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 Py_RETURN_NONE;
Mark Dickinson725bfd82009-05-03 20:33:40 +00002815 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 return raiseTestError("test_string_to_double", msg);
Mark Dickinson725bfd82009-05-03 20:33:40 +00002817#undef CHECK_STRING
2818#undef CHECK_INVALID
2819}
2820
2821
Benjamin Petersonb173f782009-05-05 22:31:58 +00002822/* Coverage testing of capsule objects. */
2823
2824static const char *capsule_name = "capsule name";
2825static char *capsule_pointer = "capsule pointer";
2826static char *capsule_context = "capsule context";
2827static const char *capsule_error = NULL;
2828static int
2829capsule_destructor_call_count = 0;
2830
2831static void
2832capsule_destructor(PyObject *o) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 capsule_destructor_call_count++;
2834 if (PyCapsule_GetContext(o) != capsule_context) {
2835 capsule_error = "context did not match in destructor!";
2836 } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
2837 capsule_error = "destructor did not match in destructor! (woah!)";
2838 } else if (PyCapsule_GetName(o) != capsule_name) {
2839 capsule_error = "name did not match in destructor!";
2840 } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
2841 capsule_error = "pointer did not match in destructor!";
2842 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00002843}
2844
2845typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 char *name;
2847 char *module;
2848 char *attribute;
Benjamin Petersonb173f782009-05-05 22:31:58 +00002849} known_capsule;
2850
2851static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02002852test_capsule(PyObject *self, PyObject *Py_UNUSED(ignored))
Benjamin Petersonb173f782009-05-05 22:31:58 +00002853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 PyObject *object;
2855 const char *error = NULL;
2856 void *pointer;
2857 void *pointer2;
2858 known_capsule known_capsules[] = {
2859 #define KNOWN_CAPSULE(module, name) { module "." name, module, name }
2860 KNOWN_CAPSULE("_socket", "CAPI"),
2861 KNOWN_CAPSULE("_curses", "_C_API"),
2862 KNOWN_CAPSULE("datetime", "datetime_CAPI"),
2863 { NULL, NULL },
2864 };
2865 known_capsule *known = &known_capsules[0];
Benjamin Petersonb173f782009-05-05 22:31:58 +00002866
2867#define FAIL(x) { error = (x); goto exit; }
2868
2869#define CHECK_DESTRUCTOR \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 if (capsule_error) { \
2871 FAIL(capsule_error); \
2872 } \
2873 else if (!capsule_destructor_call_count) { \
2874 FAIL("destructor not called!"); \
2875 } \
2876 capsule_destructor_call_count = 0; \
Benjamin Petersonb173f782009-05-05 22:31:58 +00002877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
2879 PyCapsule_SetContext(object, capsule_context);
2880 capsule_destructor(object);
2881 CHECK_DESTRUCTOR;
2882 Py_DECREF(object);
2883 CHECK_DESTRUCTOR;
Benjamin Petersonb173f782009-05-05 22:31:58 +00002884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 object = PyCapsule_New(known, "ignored", NULL);
2886 PyCapsule_SetPointer(object, capsule_pointer);
2887 PyCapsule_SetName(object, capsule_name);
2888 PyCapsule_SetDestructor(object, capsule_destructor);
2889 PyCapsule_SetContext(object, capsule_context);
2890 capsule_destructor(object);
2891 CHECK_DESTRUCTOR;
2892 /* intentionally access using the wrong name */
2893 pointer2 = PyCapsule_GetPointer(object, "the wrong name");
2894 if (!PyErr_Occurred()) {
2895 FAIL("PyCapsule_GetPointer should have failed but did not!");
2896 }
2897 PyErr_Clear();
2898 if (pointer2) {
2899 if (pointer2 == capsule_pointer) {
2900 FAIL("PyCapsule_GetPointer should not have"
2901 " returned the internal pointer!");
2902 } else {
2903 FAIL("PyCapsule_GetPointer should have "
2904 "returned NULL pointer but did not!");
2905 }
2906 }
2907 PyCapsule_SetDestructor(object, NULL);
2908 Py_DECREF(object);
2909 if (capsule_destructor_call_count) {
2910 FAIL("destructor called when it should not have been!");
2911 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00002912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 for (known = &known_capsules[0]; known->module != NULL; known++) {
2914 /* yeah, ordinarily I wouldn't do this either,
2915 but it's fine for this test harness.
2916 */
2917 static char buffer[256];
Benjamin Petersonb173f782009-05-05 22:31:58 +00002918#undef FAIL
2919#define FAIL(x) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 { \
2921 sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
2922 x, known->module, known->attribute); \
2923 error = buffer; \
2924 goto exit; \
2925 } \
Benjamin Petersonb173f782009-05-05 22:31:58 +00002926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 PyObject *module = PyImport_ImportModule(known->module);
2928 if (module) {
2929 pointer = PyCapsule_Import(known->name, 0);
2930 if (!pointer) {
2931 Py_DECREF(module);
2932 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
2933 }
2934 object = PyObject_GetAttrString(module, known->attribute);
2935 if (!object) {
2936 Py_DECREF(module);
2937 return NULL;
2938 }
2939 pointer2 = PyCapsule_GetPointer(object,
2940 "weebles wobble but they don't fall down");
2941 if (!PyErr_Occurred()) {
2942 Py_DECREF(object);
2943 Py_DECREF(module);
2944 FAIL("PyCapsule_GetPointer should have failed but did not!");
2945 }
2946 PyErr_Clear();
2947 if (pointer2) {
2948 Py_DECREF(module);
2949 Py_DECREF(object);
2950 if (pointer2 == pointer) {
2951 FAIL("PyCapsule_GetPointer should not have"
2952 " returned its internal pointer!");
2953 } else {
2954 FAIL("PyCapsule_GetPointer should have"
2955 " returned NULL pointer but did not!");
2956 }
2957 }
2958 Py_DECREF(object);
2959 Py_DECREF(module);
2960 }
2961 else
2962 PyErr_Clear();
2963 }
Benjamin Petersonb173f782009-05-05 22:31:58 +00002964
2965 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 if (error) {
2967 return raiseTestError("test_capsule", error);
2968 }
2969 Py_RETURN_NONE;
Benjamin Petersonb173f782009-05-05 22:31:58 +00002970#undef FAIL
2971}
2972
Guido van Rossumddefaf32007-01-14 03:31:43 +00002973#ifdef HAVE_GETTIMEOFDAY
2974/* Profiling of integer performance */
Martin v. Löwis1c951552008-06-13 07:48:19 +00002975static void print_delta(int test, struct timeval *s, struct timeval *e)
Guido van Rossumddefaf32007-01-14 03:31:43 +00002976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 e->tv_sec -= s->tv_sec;
2978 e->tv_usec -= s->tv_usec;
2979 if (e->tv_usec < 0) {
2980 e->tv_sec -=1;
2981 e->tv_usec += 1000000;
2982 }
2983 printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002984}
2985
2986static PyObject *
2987profile_int(PyObject *self, PyObject* args)
2988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 int i, k;
2990 struct timeval start, stop;
2991 PyObject *single, **multiple, *op1, *result;
Guido van Rossumddefaf32007-01-14 03:31:43 +00002992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 /* Test 1: Allocate and immediately deallocate
2994 many small integers */
2995 gettimeofday(&start, NULL);
2996 for(k=0; k < 20000; k++)
2997 for(i=0; i < 1000; i++) {
2998 single = PyLong_FromLong(i);
2999 Py_DECREF(single);
3000 }
3001 gettimeofday(&stop, NULL);
3002 print_delta(1, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00003003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 /* Test 2: Allocate and immediately deallocate
3005 many large integers */
3006 gettimeofday(&start, NULL);
3007 for(k=0; k < 20000; k++)
3008 for(i=0; i < 1000; i++) {
3009 single = PyLong_FromLong(i+1000000);
3010 Py_DECREF(single);
3011 }
3012 gettimeofday(&stop, NULL);
3013 print_delta(2, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00003014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 /* Test 3: Allocate a few integers, then release
3016 them all simultaneously. */
3017 multiple = malloc(sizeof(PyObject*) * 1000);
Christian Heimes7e138022013-07-26 15:03:50 +02003018 if (multiple == NULL)
3019 return PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 gettimeofday(&start, NULL);
3021 for(k=0; k < 20000; k++) {
3022 for(i=0; i < 1000; i++) {
3023 multiple[i] = PyLong_FromLong(i+1000000);
3024 }
3025 for(i=0; i < 1000; i++) {
3026 Py_DECREF(multiple[i]);
3027 }
3028 }
3029 gettimeofday(&stop, NULL);
3030 print_delta(3, &start, &stop);
Christian Heimes7e138022013-07-26 15:03:50 +02003031 free(multiple);
Guido van Rossumddefaf32007-01-14 03:31:43 +00003032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 /* Test 4: Allocate many integers, then release
3034 them all simultaneously. */
3035 multiple = malloc(sizeof(PyObject*) * 1000000);
Christian Heimes7e138022013-07-26 15:03:50 +02003036 if (multiple == NULL)
3037 return PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 gettimeofday(&start, NULL);
3039 for(k=0; k < 20; k++) {
3040 for(i=0; i < 1000000; i++) {
3041 multiple[i] = PyLong_FromLong(i+1000000);
3042 }
3043 for(i=0; i < 1000000; i++) {
3044 Py_DECREF(multiple[i]);
3045 }
3046 }
3047 gettimeofday(&stop, NULL);
3048 print_delta(4, &start, &stop);
Christian Heimes7e138022013-07-26 15:03:50 +02003049 free(multiple);
Guido van Rossumddefaf32007-01-14 03:31:43 +00003050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 /* Test 5: Allocate many integers < 32000 */
3052 multiple = malloc(sizeof(PyObject*) * 1000000);
Christian Heimes7e138022013-07-26 15:03:50 +02003053 if (multiple == NULL)
3054 return PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 gettimeofday(&start, NULL);
3056 for(k=0; k < 10; k++) {
3057 for(i=0; i < 1000000; i++) {
3058 multiple[i] = PyLong_FromLong(i+1000);
3059 }
3060 for(i=0; i < 1000000; i++) {
3061 Py_DECREF(multiple[i]);
3062 }
3063 }
3064 gettimeofday(&stop, NULL);
3065 print_delta(5, &start, &stop);
Christian Heimes7e138022013-07-26 15:03:50 +02003066 free(multiple);
Guido van Rossumddefaf32007-01-14 03:31:43 +00003067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 /* Test 6: Perform small int addition */
3069 op1 = PyLong_FromLong(1);
3070 gettimeofday(&start, NULL);
3071 for(i=0; i < 10000000; i++) {
3072 result = PyNumber_Add(op1, op1);
3073 Py_DECREF(result);
3074 }
3075 gettimeofday(&stop, NULL);
3076 Py_DECREF(op1);
3077 print_delta(6, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00003078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 /* Test 7: Perform medium int addition */
3080 op1 = PyLong_FromLong(1000);
Christian Heimes66eda262013-07-26 15:54:07 +02003081 if (op1 == NULL)
3082 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 gettimeofday(&start, NULL);
3084 for(i=0; i < 10000000; i++) {
3085 result = PyNumber_Add(op1, op1);
Christian Heimesff369a52013-07-26 14:52:18 +02003086 Py_XDECREF(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 }
3088 gettimeofday(&stop, NULL);
3089 Py_DECREF(op1);
3090 print_delta(7, &start, &stop);
Guido van Rossumddefaf32007-01-14 03:31:43 +00003091
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003092 Py_RETURN_NONE;
Guido van Rossumddefaf32007-01-14 03:31:43 +00003093}
3094#endif
3095
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003096/* To test the format of tracebacks as printed out. */
3097static PyObject *
3098traceback_print(PyObject *self, PyObject *args)
3099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 PyObject *file;
3101 PyObject *traceback;
3102 int result;
3103
3104 if (!PyArg_ParseTuple(args, "OO:traceback_print",
3105 &traceback, &file))
3106 return NULL;
3107
3108 result = PyTraceBack_Print(traceback, file);
3109 if (result < 0)
3110 return NULL;
3111 Py_RETURN_NONE;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003112}
3113
Benjamin Petersone6528212008-07-15 15:32:09 +00003114/* To test the format of exceptions as printed out. */
3115static PyObject *
3116exception_print(PyObject *self, PyObject *args)
3117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 PyObject *value;
3119 PyObject *tb;
Benjamin Petersone6528212008-07-15 15:32:09 +00003120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 if (!PyArg_ParseTuple(args, "O:exception_print",
3122 &value))
3123 return NULL;
3124 if (!PyExceptionInstance_Check(value)) {
3125 PyErr_Format(PyExc_TypeError, "an exception instance is required");
3126 return NULL;
3127 }
Benjamin Petersone6528212008-07-15 15:32:09 +00003128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 tb = PyException_GetTraceback(value);
3130 PyErr_Display((PyObject *) Py_TYPE(value), value, tb);
3131 Py_XDECREF(tb);
Benjamin Petersone6528212008-07-15 15:32:09 +00003132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 Py_RETURN_NONE;
Benjamin Petersone6528212008-07-15 15:32:09 +00003134}
3135
3136
3137
Benjamin Peterson0067bd62008-08-16 16:11:03 +00003138
3139/* reliably raise a MemoryError */
3140static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303141raise_memoryerror(PyObject *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson0067bd62008-08-16 16:11:03 +00003142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 PyErr_NoMemory();
3144 return NULL;
Benjamin Peterson0067bd62008-08-16 16:11:03 +00003145}
3146
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00003147/* Issue 6012 */
3148static PyObject *str1, *str2;
3149static int
3150failing_converter(PyObject *obj, void *arg)
3151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 /* Clone str1, then let the conversion fail. */
3153 assert(str1);
3154 str2 = str1;
3155 Py_INCREF(str2);
3156 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00003157}
3158static PyObject*
3159argparsing(PyObject *o, PyObject *args)
3160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 PyObject *res;
3162 str1 = str2 = NULL;
3163 if (!PyArg_ParseTuple(args, "O&O&",
3164 PyUnicode_FSConverter, &str1,
3165 failing_converter, &str2)) {
3166 if (!str2)
3167 /* argument converter not called? */
3168 return NULL;
3169 /* Should be 1 */
Victor Stinner0fcab4a2011-01-04 12:59:15 +00003170 res = PyLong_FromSsize_t(Py_REFCNT(str2));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 Py_DECREF(str2);
3172 PyErr_Clear();
3173 return res;
3174 }
3175 Py_RETURN_NONE;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00003176}
Benjamin Peterson0067bd62008-08-16 16:11:03 +00003177
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003178/* To test that the result of PyCode_NewEmpty has the right members. */
3179static PyObject *
3180code_newempty(PyObject *self, PyObject *args)
3181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 const char *filename;
3183 const char *funcname;
3184 int firstlineno;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 if (!PyArg_ParseTuple(args, "ssi:code_newempty",
3187 &filename, &funcname, &firstlineno))
3188 return NULL;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003191}
3192
Georg Brandl1e28a272009-12-28 08:41:01 +00003193/* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException).
3194 Run via Lib/test/test_exceptions.py */
3195static PyObject *
3196make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs)
3197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 const char *name;
3199 const char *doc = NULL;
3200 PyObject *base = NULL;
3201 PyObject *dict = NULL;
Georg Brandl1e28a272009-12-28 08:41:01 +00003202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 static char *kwlist[] = {"name", "doc", "base", "dict", NULL};
Georg Brandl1e28a272009-12-28 08:41:01 +00003204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
3206 "s|sOO:make_exception_with_doc", kwlist,
3207 &name, &doc, &base, &dict))
3208 return NULL;
Georg Brandl1e28a272009-12-28 08:41:01 +00003209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 return PyErr_NewExceptionWithDoc(name, doc, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +00003211}
3212
Antoine Pitrou5bffa792011-02-24 20:50:49 +00003213static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303214make_memoryview_from_NULL_pointer(PyObject *self, PyObject *Py_UNUSED(ignored))
Antoine Pitrou5bffa792011-02-24 20:50:49 +00003215{
3216 Py_buffer info;
3217 if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0)
3218 return NULL;
3219 return PyMemoryView_FromBuffer(&info);
3220}
Serhiy Storchaka009b8112015-03-18 21:53:15 +02003221
Stefan Krah7213fcc2015-02-01 16:19:23 +01003222static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02003223test_from_contiguous(PyObject* self, PyObject *Py_UNUSED(ignored))
Stefan Krah7213fcc2015-02-01 16:19:23 +01003224{
3225 int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1};
3226 int init[5] = {0, 1, 2, 3, 4};
3227 Py_ssize_t itemsize = sizeof(int);
3228 Py_ssize_t shape = 5;
3229 Py_ssize_t strides = 2 * itemsize;
3230 Py_buffer view = {
3231 data,
3232 NULL,
3233 5 * itemsize,
3234 itemsize,
3235 1,
3236 1,
3237 NULL,
3238 &shape,
3239 &strides,
3240 NULL,
3241 NULL
3242 };
3243 int *ptr;
3244 int i;
3245
3246 PyBuffer_FromContiguous(&view, init, view.len, 'C');
3247 ptr = view.buf;
3248 for (i = 0; i < 5; i++) {
3249 if (ptr[2*i] != i) {
3250 PyErr_SetString(TestError,
3251 "test_from_contiguous: incorrect result");
3252 return NULL;
3253 }
3254 }
3255
3256 view.buf = &data[8];
3257 view.strides[0] = -2 * itemsize;
3258
3259 PyBuffer_FromContiguous(&view, init, view.len, 'C');
3260 ptr = view.buf;
3261 for (i = 0; i < 5; i++) {
3262 if (*(ptr-2*i) != i) {
3263 PyErr_SetString(TestError,
3264 "test_from_contiguous: incorrect result");
3265 return NULL;
3266 }
3267 }
3268
3269 Py_RETURN_NONE;
3270}
Stefan Krah650c1e82015-02-03 21:43:23 +01003271
Stefan Kraha7559c02015-02-03 22:27:21 +01003272#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
Stefan Krah650c1e82015-02-03 21:43:23 +01003273extern PyTypeObject _PyBytesIOBuffer_Type;
3274
Stefan Krah5178d912015-02-03 16:57:21 +01003275static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02003276test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored))
Stefan Krah5178d912015-02-03 16:57:21 +01003277{
Stefan Krah650c1e82015-02-03 21:43:23 +01003278 PyTypeObject *type = &_PyBytesIOBuffer_Type;
Stefan Krah5178d912015-02-03 16:57:21 +01003279 PyObject *b;
3280 char *dummy[1];
3281 int ret, match;
3282
Stefan Krah650c1e82015-02-03 21:43:23 +01003283 /* PyBuffer_FillInfo() */
Stefan Krah5178d912015-02-03 16:57:21 +01003284 ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE);
3285 match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
3286 PyErr_Clear();
3287 if (ret != -1 || match == 0)
3288 goto error;
3289
Stefan Krah650c1e82015-02-03 21:43:23 +01003290 /* bytesiobuf_getbuffer() */
3291 b = type->tp_alloc(type, 0);
Stefan Krah5178d912015-02-03 16:57:21 +01003292 if (b == NULL) {
3293 return NULL;
3294 }
3295
3296 ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE);
3297 Py_DECREF(b);
3298 match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
3299 PyErr_Clear();
3300 if (ret != -1 || match == 0)
3301 goto error;
3302
3303 Py_RETURN_NONE;
3304
3305error:
3306 PyErr_SetString(TestError,
3307 "test_pep3118_obsolete_write_locks: failure");
3308 return NULL;
3309}
Stefan Kraha7559c02015-02-03 22:27:21 +01003310#endif
Antoine Pitrou5bffa792011-02-24 20:50:49 +00003311
Stefan Krah650c1e82015-02-03 21:43:23 +01003312/* This tests functions that historically supported write locks. It is
3313 wrong to call getbuffer() with view==NULL and a compliant getbufferproc
3314 is entitled to segfault in that case. */
3315static PyObject *
3316getbuffer_with_null_view(PyObject* self, PyObject *obj)
3317{
3318 if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0)
3319 return NULL;
3320
3321 Py_RETURN_NONE;
3322}
3323
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00003324/* Test that the fatal error from not having a current thread doesn't
3325 cause an infinite loop. Run via Lib/test/test_capi.py */
3326static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303327crash_no_current_thread(PyObject *self, PyObject *Py_UNUSED(ignored))
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00003328{
3329 Py_BEGIN_ALLOW_THREADS
Jeffrey Yasskinea7b7482010-05-17 16:59:23 +00003330 /* Using PyThreadState_Get() directly allows the test to pass in
3331 !pydebug mode. However, the test only actually tests anything
3332 in pydebug mode, since that's where the infinite loop was in
3333 the first place. */
3334 PyThreadState_Get();
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00003335 Py_END_ALLOW_THREADS
3336 return NULL;
3337}
3338
Antoine Pitrou2f828f22012-01-18 00:21:11 +01003339/* To run some code in a sub-interpreter. */
3340static PyObject *
3341run_in_subinterp(PyObject *self, PyObject *args)
3342{
3343 const char *code;
3344 int r;
3345 PyThreadState *substate, *mainstate;
3346
3347 if (!PyArg_ParseTuple(args, "s:run_in_subinterp",
3348 &code))
3349 return NULL;
3350
3351 mainstate = PyThreadState_Get();
3352
3353 PyThreadState_Swap(NULL);
3354
3355 substate = Py_NewInterpreter();
Brett Cannonb6855682012-02-03 12:08:03 -05003356 if (substate == NULL) {
3357 /* Since no new thread state was created, there is no exception to
3358 propagate; raise a fresh one after swapping in the old thread
3359 state. */
3360 PyThreadState_Swap(mainstate);
3361 PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed");
3362 return NULL;
3363 }
Antoine Pitrou2f828f22012-01-18 00:21:11 +01003364 r = PyRun_SimpleString(code);
3365 Py_EndInterpreter(substate);
3366
3367 PyThreadState_Swap(mainstate);
3368
3369 return PyLong_FromLong(r);
3370}
3371
Victor Stinner3c1b3792014-02-17 00:02:43 +01003372static int
3373check_time_rounding(int round)
3374{
Victor Stinner74474232015-09-02 01:43:56 +02003375 if (round != _PyTime_ROUND_FLOOR
3376 && round != _PyTime_ROUND_CEILING
Pablo Galindo2c15b292017-10-17 15:14:41 +01003377 && round != _PyTime_ROUND_HALF_EVEN
3378 && round != _PyTime_ROUND_UP) {
Victor Stinner3c1b3792014-02-17 00:02:43 +01003379 PyErr_SetString(PyExc_ValueError, "invalid rounding");
3380 return -1;
3381 }
3382 return 0;
3383}
3384
Victor Stinner5d272cc2012-03-13 13:35:55 +01003385static PyObject *
3386test_pytime_object_to_time_t(PyObject *self, PyObject *args)
3387{
3388 PyObject *obj;
3389 time_t sec;
Victor Stinner3c1b3792014-02-17 00:02:43 +01003390 int round;
3391 if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round))
Victor Stinner5d272cc2012-03-13 13:35:55 +01003392 return NULL;
Victor Stinner3c1b3792014-02-17 00:02:43 +01003393 if (check_time_rounding(round) < 0)
3394 return NULL;
3395 if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1)
Victor Stinner5d272cc2012-03-13 13:35:55 +01003396 return NULL;
3397 return _PyLong_FromTime_t(sec);
3398}
3399
3400static PyObject *
3401test_pytime_object_to_timeval(PyObject *self, PyObject *args)
3402{
3403 PyObject *obj;
3404 time_t sec;
3405 long usec;
Victor Stinner3c1b3792014-02-17 00:02:43 +01003406 int round;
3407 if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round))
Victor Stinner5d272cc2012-03-13 13:35:55 +01003408 return NULL;
Victor Stinner3c1b3792014-02-17 00:02:43 +01003409 if (check_time_rounding(round) < 0)
3410 return NULL;
3411 if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1)
Victor Stinner5d272cc2012-03-13 13:35:55 +01003412 return NULL;
3413 return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec);
3414}
3415
Victor Stinner643cd682012-03-02 22:54:03 +01003416static PyObject *
3417test_pytime_object_to_timespec(PyObject *self, PyObject *args)
3418{
3419 PyObject *obj;
3420 time_t sec;
3421 long nsec;
Victor Stinner3c1b3792014-02-17 00:02:43 +01003422 int round;
3423 if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round))
Victor Stinner643cd682012-03-02 22:54:03 +01003424 return NULL;
Victor Stinner3c1b3792014-02-17 00:02:43 +01003425 if (check_time_rounding(round) < 0)
3426 return NULL;
3427 if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1)
Victor Stinner643cd682012-03-02 22:54:03 +01003428 return NULL;
Victor Stinner5d272cc2012-03-13 13:35:55 +01003429 return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec);
Victor Stinner643cd682012-03-02 22:54:03 +01003430}
3431
Antoine Pitrou796564c2013-07-30 19:59:21 +02003432static void
3433slot_tp_del(PyObject *self)
3434{
3435 _Py_IDENTIFIER(__tp_del__);
3436 PyObject *del, *res;
3437 PyObject *error_type, *error_value, *error_traceback;
3438
3439 /* Temporarily resurrect the object. */
3440 assert(self->ob_refcnt == 0);
3441 self->ob_refcnt = 1;
3442
3443 /* Save the current exception, if any. */
3444 PyErr_Fetch(&error_type, &error_value, &error_traceback);
3445
3446 /* Execute __del__ method, if any. */
3447 del = _PyObject_LookupSpecial(self, &PyId___tp_del__);
3448 if (del != NULL) {
INADA Naoki72dccde2017-02-16 09:26:01 +09003449 res = _PyObject_CallNoArg(del);
Antoine Pitrou796564c2013-07-30 19:59:21 +02003450 if (res == NULL)
3451 PyErr_WriteUnraisable(del);
3452 else
3453 Py_DECREF(res);
3454 Py_DECREF(del);
3455 }
3456
3457 /* Restore the saved exception. */
3458 PyErr_Restore(error_type, error_value, error_traceback);
3459
3460 /* Undo the temporary resurrection; can't use DECREF here, it would
3461 * cause a recursive call.
3462 */
3463 assert(self->ob_refcnt > 0);
3464 if (--self->ob_refcnt == 0)
3465 return; /* this is the normal path out */
3466
3467 /* __del__ resurrected it! Make it look like the original Py_DECREF
3468 * never happened.
3469 */
3470 {
3471 Py_ssize_t refcnt = self->ob_refcnt;
3472 _Py_NewReference(self);
3473 self->ob_refcnt = refcnt;
3474 }
INADA Naokid8521422018-05-17 11:07:21 +09003475 assert(!PyType_IS_GC(Py_TYPE(self)) || _PyObject_GC_IS_TRACKED(self));
Antoine Pitrou796564c2013-07-30 19:59:21 +02003476 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
3477 * we need to undo that. */
3478 _Py_DEC_REFTOTAL;
3479 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
3480 * chain, so no more to do there.
3481 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
3482 * _Py_NewReference bumped tp_allocs: both of those need to be
3483 * undone.
3484 */
3485#ifdef COUNT_ALLOCS
3486 --Py_TYPE(self)->tp_frees;
3487 --Py_TYPE(self)->tp_allocs;
3488#endif
3489}
3490
3491static PyObject *
3492with_tp_del(PyObject *self, PyObject *args)
3493{
3494 PyObject *obj;
3495 PyTypeObject *tp;
3496
3497 if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj))
3498 return NULL;
3499 tp = (PyTypeObject *) obj;
3500 if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
3501 PyErr_Format(PyExc_TypeError,
3502 "heap type expected, got %R", obj);
3503 return NULL;
3504 }
3505 tp->tp_del = slot_tp_del;
3506 Py_INCREF(obj);
3507 return obj;
3508}
3509
Antoine Pitroub349e4c2014-08-06 19:31:40 -04003510static PyMethodDef ml;
3511
3512static PyObject *
3513create_cfunction(PyObject *self, PyObject *args)
3514{
3515 return PyCFunction_NewEx(&ml, self, NULL);
3516}
3517
3518static PyMethodDef ml = {
3519 "create_cfunction",
3520 create_cfunction,
3521 METH_NOARGS,
3522 NULL
3523};
3524
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -07003525static PyObject *
3526_test_incref(PyObject *ob)
3527{
3528 Py_INCREF(ob);
3529 return ob;
3530}
3531
3532static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303533test_xincref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -07003534{
3535 PyObject *obj = PyLong_FromLong(0);
3536 Py_XINCREF(_test_incref(obj));
3537 Py_DECREF(obj);
3538 Py_DECREF(obj);
3539 Py_DECREF(obj);
3540 Py_RETURN_NONE;
3541}
3542
3543static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303544test_incref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -07003545{
3546 PyObject *obj = PyLong_FromLong(0);
3547 Py_INCREF(_test_incref(obj));
3548 Py_DECREF(obj);
3549 Py_DECREF(obj);
3550 Py_DECREF(obj);
3551 Py_RETURN_NONE;
3552}
3553
3554static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303555test_xdecref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -07003556{
3557 Py_XDECREF(PyLong_FromLong(0));
3558 Py_RETURN_NONE;
3559}
3560
3561static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303562test_decref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -07003563{
3564 Py_DECREF(PyLong_FromLong(0));
3565 Py_RETURN_NONE;
3566}
Antoine Pitrou2f828f22012-01-18 00:21:11 +01003567
Victor Stinner0507bf52013-07-07 02:05:46 +02003568static PyObject *
Eddie Elizondo474eedf2018-11-13 04:09:31 -08003569test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self),
3570 PyObject *Py_UNUSED(args))
3571{
3572 PyStructSequence_Desc descr;
3573 PyStructSequence_Field descr_fields[3];
3574
3575 descr_fields[0] = (PyStructSequence_Field){"foo", "foo value"};
3576 descr_fields[1] = (PyStructSequence_Field){NULL, "some hidden value"};
3577 descr_fields[2] = (PyStructSequence_Field){0, NULL};
3578
3579 descr.name = "_testcapi.test_descr";
3580 descr.doc = "This is used to test for memory leaks in NewType";
3581 descr.fields = descr_fields;
3582 descr.n_in_sequence = 1;
3583
3584 PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
3585 assert(structseq_type != NULL);
3586 assert(PyType_Check(structseq_type));
3587 assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
3588 Py_DECREF(structseq_type);
3589
3590 Py_RETURN_NONE;
3591}
3592
3593static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303594test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored))
Christian Heimes4efdb412013-07-31 02:36:43 +02003595{
3596 PyObject *obj = PyLong_FromLong(0);
Victor Stinnerfc6a90a2014-10-09 22:15:41 +02003597 Py_IncRef(obj);
Christian Heimes4efdb412013-07-31 02:36:43 +02003598 Py_DecRef(obj);
3599 Py_DecRef(obj);
3600 Py_RETURN_NONE;
3601}
3602
3603static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303604test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored))
Victor Stinner0507bf52013-07-07 02:05:46 +02003605{
3606 void *ptr;
3607
Victor Stinnerdb067af2014-05-02 22:31:14 +02003608 ptr = PyMem_RawMalloc(0);
3609 if (ptr == NULL) {
3610 PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL");
3611 return NULL;
3612 }
3613 PyMem_RawFree(ptr);
3614
3615 ptr = PyMem_RawCalloc(0, 0);
3616 if (ptr == NULL) {
3617 PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL");
3618 return NULL;
3619 }
3620 PyMem_RawFree(ptr);
3621
Victor Stinner0507bf52013-07-07 02:05:46 +02003622 ptr = PyMem_Malloc(0);
3623 if (ptr == NULL) {
3624 PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL");
3625 return NULL;
3626 }
3627 PyMem_Free(ptr);
3628
Victor Stinnerdb067af2014-05-02 22:31:14 +02003629 ptr = PyMem_Calloc(0, 0);
3630 if (ptr == NULL) {
3631 PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL");
3632 return NULL;
3633 }
3634 PyMem_Free(ptr);
3635
Victor Stinner0507bf52013-07-07 02:05:46 +02003636 ptr = PyObject_Malloc(0);
3637 if (ptr == NULL) {
3638 PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL");
3639 return NULL;
3640 }
3641 PyObject_Free(ptr);
3642
Victor Stinnerdb067af2014-05-02 22:31:14 +02003643 ptr = PyObject_Calloc(0, 0);
3644 if (ptr == NULL) {
3645 PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL");
3646 return NULL;
3647 }
3648 PyObject_Free(ptr);
3649
Victor Stinner0507bf52013-07-07 02:05:46 +02003650 Py_RETURN_NONE;
3651}
3652
3653typedef struct {
Victor Stinnerd8f0d922014-06-02 21:57:10 +02003654 PyMemAllocatorEx alloc;
Victor Stinner0507bf52013-07-07 02:05:46 +02003655
3656 size_t malloc_size;
Victor Stinnerdb067af2014-05-02 22:31:14 +02003657 size_t calloc_nelem;
3658 size_t calloc_elsize;
Victor Stinner0507bf52013-07-07 02:05:46 +02003659 void *realloc_ptr;
3660 size_t realloc_new_size;
3661 void *free_ptr;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003662 void *ctx;
Victor Stinner0507bf52013-07-07 02:05:46 +02003663} alloc_hook_t;
3664
Victor Stinner9ed83c42017-10-31 12:18:10 -07003665static void* hook_malloc(void* ctx, size_t size)
Victor Stinner0507bf52013-07-07 02:05:46 +02003666{
3667 alloc_hook_t *hook = (alloc_hook_t *)ctx;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003668 hook->ctx = ctx;
Victor Stinner0507bf52013-07-07 02:05:46 +02003669 hook->malloc_size = size;
3670 return hook->alloc.malloc(hook->alloc.ctx, size);
3671}
3672
Victor Stinner9ed83c42017-10-31 12:18:10 -07003673static void* hook_calloc(void* ctx, size_t nelem, size_t elsize)
Victor Stinnerdb067af2014-05-02 22:31:14 +02003674{
3675 alloc_hook_t *hook = (alloc_hook_t *)ctx;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003676 hook->ctx = ctx;
Victor Stinnerdb067af2014-05-02 22:31:14 +02003677 hook->calloc_nelem = nelem;
3678 hook->calloc_elsize = elsize;
3679 return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize);
3680}
3681
Victor Stinner9ed83c42017-10-31 12:18:10 -07003682static void* hook_realloc(void* ctx, void* ptr, size_t new_size)
Victor Stinner0507bf52013-07-07 02:05:46 +02003683{
3684 alloc_hook_t *hook = (alloc_hook_t *)ctx;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003685 hook->ctx = ctx;
Victor Stinner0507bf52013-07-07 02:05:46 +02003686 hook->realloc_ptr = ptr;
3687 hook->realloc_new_size = new_size;
3688 return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size);
3689}
3690
Victor Stinner9ed83c42017-10-31 12:18:10 -07003691static void hook_free(void *ctx, void *ptr)
Victor Stinner0507bf52013-07-07 02:05:46 +02003692{
3693 alloc_hook_t *hook = (alloc_hook_t *)ctx;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003694 hook->ctx = ctx;
Victor Stinner0507bf52013-07-07 02:05:46 +02003695 hook->free_ptr = ptr;
3696 hook->alloc.free(hook->alloc.ctx, ptr);
3697}
3698
3699static PyObject *
3700test_setallocators(PyMemAllocatorDomain domain)
3701{
3702 PyObject *res = NULL;
3703 const char *error_msg;
3704 alloc_hook_t hook;
Victor Stinnerd8f0d922014-06-02 21:57:10 +02003705 PyMemAllocatorEx alloc;
Victor Stinnerdb067af2014-05-02 22:31:14 +02003706 size_t size, size2, nelem, elsize;
Victor Stinner0507bf52013-07-07 02:05:46 +02003707 void *ptr, *ptr2;
3708
Victor Stinnerdb067af2014-05-02 22:31:14 +02003709 memset(&hook, 0, sizeof(hook));
Victor Stinner0507bf52013-07-07 02:05:46 +02003710
3711 alloc.ctx = &hook;
3712 alloc.malloc = &hook_malloc;
Victor Stinnerdb067af2014-05-02 22:31:14 +02003713 alloc.calloc = &hook_calloc;
Victor Stinner0507bf52013-07-07 02:05:46 +02003714 alloc.realloc = &hook_realloc;
3715 alloc.free = &hook_free;
3716 PyMem_GetAllocator(domain, &hook.alloc);
3717 PyMem_SetAllocator(domain, &alloc);
3718
Victor Stinner9ed83c42017-10-31 12:18:10 -07003719 /* malloc, realloc, free */
Victor Stinner0507bf52013-07-07 02:05:46 +02003720 size = 42;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003721 hook.ctx = NULL;
Victor Stinner0507bf52013-07-07 02:05:46 +02003722 switch(domain)
3723 {
3724 case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break;
3725 case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break;
3726 case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break;
3727 default: ptr = NULL; break;
3728 }
3729
Victor Stinner9ed83c42017-10-31 12:18:10 -07003730#define CHECK_CTX(FUNC) \
3731 if (hook.ctx != &hook) { \
3732 error_msg = FUNC " wrong context"; \
3733 goto fail; \
3734 } \
3735 hook.ctx = NULL; /* reset for next check */
3736
Victor Stinner0507bf52013-07-07 02:05:46 +02003737 if (ptr == NULL) {
3738 error_msg = "malloc failed";
3739 goto fail;
3740 }
Victor Stinner9ed83c42017-10-31 12:18:10 -07003741 CHECK_CTX("malloc");
Victor Stinner0507bf52013-07-07 02:05:46 +02003742 if (hook.malloc_size != size) {
3743 error_msg = "malloc invalid size";
3744 goto fail;
3745 }
3746
3747 size2 = 200;
3748 switch(domain)
3749 {
3750 case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break;
3751 case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break;
3752 case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break;
Christian Heimes865d12a2013-08-02 11:10:51 +02003753 default: ptr2 = NULL; break;
Victor Stinner0507bf52013-07-07 02:05:46 +02003754 }
3755
3756 if (ptr2 == NULL) {
3757 error_msg = "realloc failed";
3758 goto fail;
3759 }
Victor Stinner9ed83c42017-10-31 12:18:10 -07003760 CHECK_CTX("realloc");
Victor Stinner0507bf52013-07-07 02:05:46 +02003761 if (hook.realloc_ptr != ptr
3762 || hook.realloc_new_size != size2) {
3763 error_msg = "realloc invalid parameters";
3764 goto fail;
3765 }
3766
3767 switch(domain)
3768 {
3769 case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break;
3770 case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break;
3771 case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break;
3772 }
3773
Victor Stinner9ed83c42017-10-31 12:18:10 -07003774 CHECK_CTX("free");
Victor Stinner0507bf52013-07-07 02:05:46 +02003775 if (hook.free_ptr != ptr2) {
3776 error_msg = "free invalid pointer";
3777 goto fail;
3778 }
3779
Victor Stinner9ed83c42017-10-31 12:18:10 -07003780 /* calloc, free */
Victor Stinnerdb067af2014-05-02 22:31:14 +02003781 nelem = 2;
3782 elsize = 5;
3783 switch(domain)
3784 {
3785 case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break;
3786 case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break;
3787 case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break;
3788 default: ptr = NULL; break;
3789 }
3790
3791 if (ptr == NULL) {
3792 error_msg = "calloc failed";
3793 goto fail;
3794 }
Victor Stinner9ed83c42017-10-31 12:18:10 -07003795 CHECK_CTX("calloc");
Victor Stinnerdb067af2014-05-02 22:31:14 +02003796 if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) {
3797 error_msg = "calloc invalid nelem or elsize";
3798 goto fail;
3799 }
3800
Victor Stinner9ed83c42017-10-31 12:18:10 -07003801 hook.free_ptr = NULL;
Victor Stinnerdb067af2014-05-02 22:31:14 +02003802 switch(domain)
3803 {
3804 case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break;
3805 case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break;
3806 case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break;
3807 }
3808
Victor Stinner9ed83c42017-10-31 12:18:10 -07003809 CHECK_CTX("calloc free");
3810 if (hook.free_ptr != ptr) {
3811 error_msg = "calloc free invalid pointer";
3812 goto fail;
3813 }
3814
Victor Stinner0507bf52013-07-07 02:05:46 +02003815 Py_INCREF(Py_None);
3816 res = Py_None;
3817 goto finally;
3818
3819fail:
3820 PyErr_SetString(PyExc_RuntimeError, error_msg);
3821
3822finally:
3823 PyMem_SetAllocator(domain, &hook.alloc);
3824 return res;
Victor Stinner9ed83c42017-10-31 12:18:10 -07003825
3826#undef CHECK_CTX
Victor Stinner0507bf52013-07-07 02:05:46 +02003827}
3828
3829static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303830test_pymem_setrawallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
Victor Stinner0507bf52013-07-07 02:05:46 +02003831{
3832 return test_setallocators(PYMEM_DOMAIN_RAW);
3833}
3834
3835static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303836test_pymem_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
Victor Stinner0507bf52013-07-07 02:05:46 +02003837{
3838 return test_setallocators(PYMEM_DOMAIN_MEM);
3839}
3840
3841static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303842test_pyobject_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
Victor Stinner0507bf52013-07-07 02:05:46 +02003843{
3844 return test_setallocators(PYMEM_DOMAIN_OBJ);
3845}
3846
xdegaye85f64302017-07-01 14:14:45 +02003847/* Most part of the following code is inherited from the pyfailmalloc project
3848 * written by Victor Stinner. */
3849static struct {
3850 int installed;
3851 PyMemAllocatorEx raw;
3852 PyMemAllocatorEx mem;
3853 PyMemAllocatorEx obj;
3854} FmHook;
3855
3856static struct {
3857 int start;
3858 int stop;
3859 Py_ssize_t count;
3860} FmData;
3861
3862static int
3863fm_nomemory(void)
3864{
3865 FmData.count++;
3866 if (FmData.count > FmData.start &&
3867 (FmData.stop <= 0 || FmData.count <= FmData.stop)) {
3868 return 1;
3869 }
3870 return 0;
3871}
3872
3873static void *
3874hook_fmalloc(void *ctx, size_t size)
3875{
3876 PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
3877 if (fm_nomemory()) {
3878 return NULL;
3879 }
3880 return alloc->malloc(alloc->ctx, size);
3881}
3882
3883static void *
3884hook_fcalloc(void *ctx, size_t nelem, size_t elsize)
3885{
3886 PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
3887 if (fm_nomemory()) {
3888 return NULL;
3889 }
3890 return alloc->calloc(alloc->ctx, nelem, elsize);
3891}
3892
3893static void *
3894hook_frealloc(void *ctx, void *ptr, size_t new_size)
3895{
3896 PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
3897 if (fm_nomemory()) {
3898 return NULL;
3899 }
3900 return alloc->realloc(alloc->ctx, ptr, new_size);
3901}
3902
3903static void
3904hook_ffree(void *ctx, void *ptr)
3905{
3906 PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
3907 alloc->free(alloc->ctx, ptr);
3908}
3909
3910static void
3911fm_setup_hooks(void)
3912{
3913 PyMemAllocatorEx alloc;
3914
3915 if (FmHook.installed) {
3916 return;
3917 }
3918 FmHook.installed = 1;
3919
3920 alloc.malloc = hook_fmalloc;
3921 alloc.calloc = hook_fcalloc;
3922 alloc.realloc = hook_frealloc;
3923 alloc.free = hook_ffree;
3924 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw);
3925 PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem);
3926 PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj);
3927
3928 alloc.ctx = &FmHook.raw;
3929 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
3930
3931 alloc.ctx = &FmHook.mem;
3932 PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc);
3933
3934 alloc.ctx = &FmHook.obj;
3935 PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc);
3936}
3937
3938static void
3939fm_remove_hooks(void)
3940{
3941 if (FmHook.installed) {
3942 FmHook.installed = 0;
3943 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw);
3944 PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem);
3945 PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj);
3946 }
3947}
3948
3949static PyObject*
3950set_nomemory(PyObject *self, PyObject *args)
3951{
3952 /* Memory allocation fails after 'start' allocation requests, and until
3953 * 'stop' allocation requests except when 'stop' is negative or equal
3954 * to 0 (default) in which case allocation failures never stop. */
3955 FmData.count = 0;
3956 FmData.stop = 0;
3957 if (!PyArg_ParseTuple(args, "i|i", &FmData.start, &FmData.stop)) {
3958 return NULL;
3959 }
3960 fm_setup_hooks();
3961 Py_RETURN_NONE;
3962}
3963
3964static PyObject*
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303965remove_mem_hooks(PyObject *self, PyObject *Py_UNUSED(ignored))
xdegaye85f64302017-07-01 14:14:45 +02003966{
3967 fm_remove_hooks();
3968 Py_RETURN_NONE;
3969}
3970
Larry Hastings44e2eaa2013-11-23 15:37:55 -08003971PyDoc_STRVAR(docstring_empty,
3972""
3973);
3974
3975PyDoc_STRVAR(docstring_no_signature,
3976"This docstring has no signature."
3977);
3978
3979PyDoc_STRVAR(docstring_with_invalid_signature,
Larry Hastings2623c8c2014-02-08 22:15:29 -08003980"docstring_with_invalid_signature($module, /, boo)\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -08003981"\n"
3982"This docstring has an invalid signature."
3983);
3984
Larry Hastings2623c8c2014-02-08 22:15:29 -08003985PyDoc_STRVAR(docstring_with_invalid_signature2,
3986"docstring_with_invalid_signature2($module, /, boo)\n"
3987"\n"
3988"--\n"
3989"\n"
3990"This docstring also has an invalid signature."
3991);
3992
Larry Hastings44e2eaa2013-11-23 15:37:55 -08003993PyDoc_STRVAR(docstring_with_signature,
Larry Hastings2623c8c2014-02-08 22:15:29 -08003994"docstring_with_signature($module, /, sig)\n"
3995"--\n"
3996"\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -08003997"This docstring has a valid signature."
3998);
3999
Zachary Ware8ef887c2015-04-13 18:22:35 -05004000PyDoc_STRVAR(docstring_with_signature_but_no_doc,
4001"docstring_with_signature_but_no_doc($module, /, sig)\n"
4002"--\n"
4003"\n"
4004);
4005
Larry Hastings44e2eaa2013-11-23 15:37:55 -08004006PyDoc_STRVAR(docstring_with_signature_and_extra_newlines,
Larry Hastings2623c8c2014-02-08 22:15:29 -08004007"docstring_with_signature_and_extra_newlines($module, /, parameter)\n"
4008"--\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -08004009"\n"
4010"\n"
4011"This docstring has a valid signature and some extra newlines."
4012);
4013
Larry Hastings16c51912014-01-07 11:53:01 -08004014PyDoc_STRVAR(docstring_with_signature_with_defaults,
Larry Hastings2623c8c2014-02-08 22:15:29 -08004015"docstring_with_signature_with_defaults(module, s='avocado',\n"
4016" b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n"
4017" local=the_number_three, sys=sys.maxsize,\n"
4018" exp=sys.maxsize - 1)\n"
4019"--\n"
Larry Hastings16c51912014-01-07 11:53:01 -08004020"\n"
4021"\n"
4022"\n"
4023"This docstring has a valid signature with parameters,\n"
4024"and the parameters take defaults of varying types."
4025);
4026
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004027typedef struct {
4028 PyThread_type_lock start_event;
4029 PyThread_type_lock exit_event;
4030 PyObject *callback;
4031} test_c_thread_t;
4032
4033static void
4034temporary_c_thread(void *data)
4035{
4036 test_c_thread_t *test_c_thread = data;
4037 PyGILState_STATE state;
4038 PyObject *res;
4039
4040 PyThread_release_lock(test_c_thread->start_event);
4041
4042 /* Allocate a Python thread state for this thread */
4043 state = PyGILState_Ensure();
4044
Victor Stinner3466bde2016-09-05 18:16:01 -07004045 res = _PyObject_CallNoArg(test_c_thread->callback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004046 Py_CLEAR(test_c_thread->callback);
4047
4048 if (res == NULL) {
4049 PyErr_Print();
4050 }
4051 else {
4052 Py_DECREF(res);
4053 }
4054
4055 /* Destroy the Python thread state for this thread */
4056 PyGILState_Release(state);
4057
4058 PyThread_release_lock(test_c_thread->exit_event);
4059
4060 PyThread_exit_thread();
4061}
4062
4063static PyObject *
4064call_in_temporary_c_thread(PyObject *self, PyObject *callback)
4065{
4066 PyObject *res = NULL;
4067 test_c_thread_t test_c_thread;
4068 long thread;
4069
4070 PyEval_InitThreads();
4071
4072 test_c_thread.start_event = PyThread_allocate_lock();
4073 test_c_thread.exit_event = PyThread_allocate_lock();
4074 test_c_thread.callback = NULL;
4075 if (!test_c_thread.start_event || !test_c_thread.exit_event) {
4076 PyErr_SetString(PyExc_RuntimeError, "could not allocate lock");
4077 goto exit;
4078 }
4079
4080 Py_INCREF(callback);
4081 test_c_thread.callback = callback;
4082
4083 PyThread_acquire_lock(test_c_thread.start_event, 1);
4084 PyThread_acquire_lock(test_c_thread.exit_event, 1);
4085
4086 thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread);
4087 if (thread == -1) {
4088 PyErr_SetString(PyExc_RuntimeError, "unable to start the thread");
4089 PyThread_release_lock(test_c_thread.start_event);
4090 PyThread_release_lock(test_c_thread.exit_event);
4091 goto exit;
4092 }
4093
4094 PyThread_acquire_lock(test_c_thread.start_event, 1);
4095 PyThread_release_lock(test_c_thread.start_event);
4096
4097 Py_BEGIN_ALLOW_THREADS
4098 PyThread_acquire_lock(test_c_thread.exit_event, 1);
4099 PyThread_release_lock(test_c_thread.exit_event);
4100 Py_END_ALLOW_THREADS
4101
4102 Py_INCREF(Py_None);
4103 res = Py_None;
4104
4105exit:
4106 Py_CLEAR(test_c_thread.callback);
4107 if (test_c_thread.start_event)
4108 PyThread_free_lock(test_c_thread.start_event);
4109 if (test_c_thread.exit_event)
4110 PyThread_free_lock(test_c_thread.exit_event);
4111 return res;
4112}
Victor Stinner13105102013-12-13 02:17:29 +01004113
Serhiy Storchakab5181342015-02-06 08:58:56 +02004114/* marshal */
4115
4116static PyObject*
4117pymarshal_write_long_to_file(PyObject* self, PyObject *args)
4118{
4119 long value;
4120 char *filename;
4121 int version;
4122 FILE *fp;
4123
4124 if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file",
4125 &value, &filename, &version))
4126 return NULL;
4127
4128 fp = fopen(filename, "wb");
4129 if (fp == NULL) {
4130 PyErr_SetFromErrno(PyExc_OSError);
4131 return NULL;
4132 }
4133
4134 PyMarshal_WriteLongToFile(value, fp, version);
4135
4136 fclose(fp);
4137 if (PyErr_Occurred())
4138 return NULL;
4139 Py_RETURN_NONE;
4140}
4141
4142static PyObject*
4143pymarshal_write_object_to_file(PyObject* self, PyObject *args)
4144{
4145 PyObject *obj;
4146 char *filename;
4147 int version;
4148 FILE *fp;
4149
4150 if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file",
4151 &obj, &filename, &version))
4152 return NULL;
4153
4154 fp = fopen(filename, "wb");
4155 if (fp == NULL) {
4156 PyErr_SetFromErrno(PyExc_OSError);
4157 return NULL;
4158 }
4159
4160 PyMarshal_WriteObjectToFile(obj, fp, version);
4161
4162 fclose(fp);
4163 if (PyErr_Occurred())
4164 return NULL;
4165 Py_RETURN_NONE;
4166}
4167
4168static PyObject*
4169pymarshal_read_short_from_file(PyObject* self, PyObject *args)
4170{
4171 int value;
4172 long pos;
4173 char *filename;
4174 FILE *fp;
4175
4176 if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename))
4177 return NULL;
4178
4179 fp = fopen(filename, "rb");
4180 if (fp == NULL) {
4181 PyErr_SetFromErrno(PyExc_OSError);
4182 return NULL;
4183 }
4184
4185 value = PyMarshal_ReadShortFromFile(fp);
4186 pos = ftell(fp);
4187
4188 fclose(fp);
4189 if (PyErr_Occurred())
4190 return NULL;
4191 return Py_BuildValue("il", value, pos);
4192}
4193
4194static PyObject*
4195pymarshal_read_long_from_file(PyObject* self, PyObject *args)
4196{
4197 long value, pos;
4198 char *filename;
4199 FILE *fp;
4200
4201 if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename))
4202 return NULL;
4203
4204 fp = fopen(filename, "rb");
4205 if (fp == NULL) {
4206 PyErr_SetFromErrno(PyExc_OSError);
4207 return NULL;
4208 }
4209
4210 value = PyMarshal_ReadLongFromFile(fp);
4211 pos = ftell(fp);
4212
4213 fclose(fp);
4214 if (PyErr_Occurred())
4215 return NULL;
4216 return Py_BuildValue("ll", value, pos);
4217}
4218
4219static PyObject*
4220pymarshal_read_last_object_from_file(PyObject* self, PyObject *args)
4221{
4222 PyObject *obj;
4223 long pos;
4224 char *filename;
4225 FILE *fp;
4226
4227 if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename))
4228 return NULL;
4229
4230 fp = fopen(filename, "rb");
4231 if (fp == NULL) {
4232 PyErr_SetFromErrno(PyExc_OSError);
4233 return NULL;
4234 }
4235
4236 obj = PyMarshal_ReadLastObjectFromFile(fp);
4237 pos = ftell(fp);
4238
4239 fclose(fp);
4240 return Py_BuildValue("Nl", obj, pos);
4241}
4242
4243static PyObject*
4244pymarshal_read_object_from_file(PyObject* self, PyObject *args)
4245{
4246 PyObject *obj;
4247 long pos;
4248 char *filename;
4249 FILE *fp;
4250
4251 if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename))
4252 return NULL;
4253
4254 fp = fopen(filename, "rb");
4255 if (fp == NULL) {
4256 PyErr_SetFromErrno(PyExc_OSError);
4257 return NULL;
4258 }
4259
4260 obj = PyMarshal_ReadObjectFromFile(fp);
4261 pos = ftell(fp);
4262
4263 fclose(fp);
4264 return Py_BuildValue("Nl", obj, pos);
4265}
4266
Victor Stinnerefde1462015-03-21 15:04:43 +01004267static PyObject*
4268return_null_without_error(PyObject *self, PyObject *args)
4269{
4270 /* invalid call: return NULL without setting an error,
4271 * _Py_CheckFunctionResult() must detect such bug at runtime. */
4272 PyErr_Clear();
4273 return NULL;
4274}
4275
4276static PyObject*
4277return_result_with_error(PyObject *self, PyObject *args)
4278{
4279 /* invalid call: return a result with an error set,
4280 * _Py_CheckFunctionResult() must detect such bug at runtime. */
4281 PyErr_SetNone(PyExc_ValueError);
4282 Py_RETURN_NONE;
4283}
4284
Victor Stinner992c43f2015-03-27 17:12:45 +01004285static PyObject *
Victor Stinner13019fd2015-04-03 13:10:54 +02004286test_pytime_fromseconds(PyObject *self, PyObject *args)
4287{
4288 int seconds;
4289 _PyTime_t ts;
4290
4291 if (!PyArg_ParseTuple(args, "i", &seconds))
4292 return NULL;
4293 ts = _PyTime_FromSeconds(seconds);
4294 return _PyTime_AsNanosecondsObject(ts);
4295}
4296
4297static PyObject *
Victor Stinner992c43f2015-03-27 17:12:45 +01004298test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
4299{
4300 PyObject *obj;
4301 int round;
4302 _PyTime_t ts;
4303
4304 if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
4305 return NULL;
4306 if (check_time_rounding(round) < 0)
4307 return NULL;
4308 if (_PyTime_FromSecondsObject(&ts, obj, round) == -1)
4309 return NULL;
4310 return _PyTime_AsNanosecondsObject(ts);
4311}
4312
Victor Stinner4bfb4602015-03-27 22:27:24 +01004313static PyObject *
4314test_pytime_assecondsdouble(PyObject *self, PyObject *args)
4315{
Victor Stinnerc29b5852017-11-02 07:28:27 -07004316 PyObject *obj;
Victor Stinner4bfb4602015-03-27 22:27:24 +01004317 _PyTime_t ts;
4318 double d;
4319
Victor Stinnerc29b5852017-11-02 07:28:27 -07004320 if (!PyArg_ParseTuple(args, "O", &obj)) {
Victor Stinner4bfb4602015-03-27 22:27:24 +01004321 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004322 }
4323 if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) {
4324 return NULL;
4325 }
Victor Stinner4bfb4602015-03-27 22:27:24 +01004326 d = _PyTime_AsSecondsDouble(ts);
4327 return PyFloat_FromDouble(d);
4328}
4329
Victor Stinner95e9cef2015-03-28 01:26:47 +01004330static PyObject *
4331test_PyTime_AsTimeval(PyObject *self, PyObject *args)
4332{
Victor Stinnerc29b5852017-11-02 07:28:27 -07004333 PyObject *obj;
Victor Stinner95e9cef2015-03-28 01:26:47 +01004334 int round;
4335 _PyTime_t t;
4336 struct timeval tv;
4337 PyObject *seconds;
4338
Victor Stinnerc29b5852017-11-02 07:28:27 -07004339 if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
Victor Stinner95e9cef2015-03-28 01:26:47 +01004340 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004341 if (check_time_rounding(round) < 0) {
Victor Stinner95e9cef2015-03-28 01:26:47 +01004342 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004343 }
4344 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
Victor Stinner95e9cef2015-03-28 01:26:47 +01004345 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004346 }
4347 if (_PyTime_AsTimeval(t, &tv, round) < 0) {
4348 return NULL;
4349 }
Victor Stinner95e9cef2015-03-28 01:26:47 +01004350
Benjamin Peterson2c134c32017-04-13 01:44:54 -07004351 seconds = PyLong_FromLongLong(tv.tv_sec);
Victor Stinnerc29b5852017-11-02 07:28:27 -07004352 if (seconds == NULL) {
Victor Stinner95e9cef2015-03-28 01:26:47 +01004353 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004354 }
Victor Stinner95e9cef2015-03-28 01:26:47 +01004355 return Py_BuildValue("Nl", seconds, tv.tv_usec);
4356}
4357
Victor Stinner34dc0f42015-03-27 18:19:03 +01004358#ifdef HAVE_CLOCK_GETTIME
4359static PyObject *
4360test_PyTime_AsTimespec(PyObject *self, PyObject *args)
4361{
Victor Stinnerc29b5852017-11-02 07:28:27 -07004362 PyObject *obj;
Victor Stinner34dc0f42015-03-27 18:19:03 +01004363 _PyTime_t t;
4364 struct timespec ts;
4365
Victor Stinnerc29b5852017-11-02 07:28:27 -07004366 if (!PyArg_ParseTuple(args, "O", &obj)) {
Victor Stinner34dc0f42015-03-27 18:19:03 +01004367 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004368 }
4369 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
Victor Stinner34dc0f42015-03-27 18:19:03 +01004370 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004371 }
4372 if (_PyTime_AsTimespec(t, &ts) == -1) {
4373 return NULL;
4374 }
Victor Stinner34dc0f42015-03-27 18:19:03 +01004375 return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec);
4376}
4377#endif
4378
Victor Stinner62d1c702015-04-01 17:47:07 +02004379static PyObject *
4380test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
4381{
Victor Stinnerc29b5852017-11-02 07:28:27 -07004382 PyObject *obj;
Victor Stinner62d1c702015-04-01 17:47:07 +02004383 int round;
4384 _PyTime_t t, ms;
4385
Victor Stinnerc29b5852017-11-02 07:28:27 -07004386 if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
Victor Stinner62d1c702015-04-01 17:47:07 +02004387 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004388 }
4389 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
Victor Stinner62d1c702015-04-01 17:47:07 +02004390 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004391 }
4392 if (check_time_rounding(round) < 0) {
4393 return NULL;
4394 }
Victor Stinner62d1c702015-04-01 17:47:07 +02004395 ms = _PyTime_AsMilliseconds(t, round);
4396 /* This conversion rely on the fact that _PyTime_t is a number of
4397 nanoseconds */
4398 return _PyTime_AsNanosecondsObject(ms);
4399}
4400
4401static PyObject *
4402test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
4403{
Victor Stinnerc29b5852017-11-02 07:28:27 -07004404 PyObject *obj;
Victor Stinner62d1c702015-04-01 17:47:07 +02004405 int round;
4406 _PyTime_t t, ms;
4407
Victor Stinnerc29b5852017-11-02 07:28:27 -07004408 if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
Victor Stinner62d1c702015-04-01 17:47:07 +02004409 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004410 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
Victor Stinner62d1c702015-04-01 17:47:07 +02004411 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07004412 }
4413 if (check_time_rounding(round) < 0) {
4414 return NULL;
4415 }
Victor Stinner62d1c702015-04-01 17:47:07 +02004416 ms = _PyTime_AsMicroseconds(t, round);
4417 /* This conversion rely on the fact that _PyTime_t is a number of
4418 nanoseconds */
4419 return _PyTime_AsNanosecondsObject(ms);
4420}
4421
Victor Stinner50856d52015-10-13 00:11:21 +02004422static PyObject*
4423get_recursion_depth(PyObject *self, PyObject *args)
4424{
Victor Stinner50b48572018-11-01 01:51:40 +01004425 PyThreadState *tstate = PyThreadState_Get();
Victor Stinner50856d52015-10-13 00:11:21 +02004426
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07004427 /* subtract one to ignore the frame of the get_recursion_depth() call */
Victor Stinner50856d52015-10-13 00:11:21 +02004428 return PyLong_FromLong(tstate->recursion_depth - 1);
4429}
4430
Victor Stinner34be8072016-03-14 12:04:26 +01004431static PyObject*
4432pymem_buffer_overflow(PyObject *self, PyObject *args)
4433{
4434 char *buffer;
4435
4436 /* Deliberate buffer overflow to check that PyMem_Free() detects
4437 the overflow when debug hooks are installed. */
4438 buffer = PyMem_Malloc(16);
Victor Stinner414b1cd2019-03-26 14:35:30 +01004439 if (buffer == NULL) {
4440 PyErr_NoMemory();
4441 return NULL;
4442 }
Victor Stinner34be8072016-03-14 12:04:26 +01004443 buffer[16] = 'x';
4444 PyMem_Free(buffer);
4445
4446 Py_RETURN_NONE;
4447}
4448
4449static PyObject*
4450pymem_api_misuse(PyObject *self, PyObject *args)
4451{
4452 char *buffer;
4453
4454 /* Deliberate misusage of Python allocators:
4455 allococate with PyMem but release with PyMem_Raw. */
4456 buffer = PyMem_Malloc(16);
4457 PyMem_RawFree(buffer);
4458
4459 Py_RETURN_NONE;
4460}
4461
Victor Stinnerc4aec362016-03-14 22:26:53 +01004462static PyObject*
Victor Stinnerad524372016-03-16 12:12:53 +01004463pymem_malloc_without_gil(PyObject *self, PyObject *args)
4464{
4465 char *buffer;
4466
4467 /* Deliberate bug to test debug hooks on Python memory allocators:
4468 call PyMem_Malloc() without holding the GIL */
4469 Py_BEGIN_ALLOW_THREADS
4470 buffer = PyMem_Malloc(10);
4471 Py_END_ALLOW_THREADS
4472
4473 PyMem_Free(buffer);
4474
4475 Py_RETURN_NONE;
4476}
4477
Victor Stinner5d39e042017-11-29 17:20:38 +01004478
4479static PyObject*
4480test_pymem_getallocatorsname(PyObject *self, PyObject *args)
4481{
Victor Stinnerb16b4e42019-05-17 15:20:52 +02004482 const char *name = _PyMem_GetCurrentAllocatorName();
Victor Stinner5d39e042017-11-29 17:20:38 +01004483 if (name == NULL) {
4484 PyErr_SetString(PyExc_RuntimeError, "cannot get allocators name");
4485 return NULL;
4486 }
4487 return PyUnicode_FromString(name);
4488}
4489
4490
Victor Stinnerad524372016-03-16 12:12:53 +01004491static PyObject*
Victor Stinner3bf0f3a2019-06-07 16:22:21 +02004492test_pyobject_is_freed(const char *test_name, PyObject *op)
Victor Stinner2b00db62019-04-11 11:33:27 +02004493{
Victor Stinner3bf0f3a2019-06-07 16:22:21 +02004494 if (!_PyObject_IsFreed(op)) {
4495 return raiseTestError(test_name, "object is not seen as freed");
4496 }
4497 Py_RETURN_NONE;
Victor Stinner2b00db62019-04-11 11:33:27 +02004498}
4499
4500
4501static PyObject*
Victor Stinner3bf0f3a2019-06-07 16:22:21 +02004502check_pyobject_uninitialized_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
Victor Stinner2b00db62019-04-11 11:33:27 +02004503{
4504 PyObject *op = (PyObject *)PyObject_Malloc(sizeof(PyObject));
4505 if (op == NULL) {
4506 return NULL;
4507 }
4508 /* Initialize reference count to avoid early crash in ceval or GC */
4509 Py_REFCNT(op) = 1;
4510 /* object fields like ob_type are uninitialized! */
Victor Stinner3bf0f3a2019-06-07 16:22:21 +02004511 return test_pyobject_is_freed("check_pyobject_uninitialized_is_freed", op);
Victor Stinner2b00db62019-04-11 11:33:27 +02004512}
4513
4514
4515static PyObject*
Victor Stinner3bf0f3a2019-06-07 16:22:21 +02004516check_pyobject_forbidden_bytes_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
Victor Stinner2b00db62019-04-11 11:33:27 +02004517{
4518 /* Allocate an incomplete PyObject structure: truncate 'ob_type' field */
4519 PyObject *op = (PyObject *)PyObject_Malloc(offsetof(PyObject, ob_type));
4520 if (op == NULL) {
4521 return NULL;
4522 }
4523 /* Initialize reference count to avoid early crash in ceval or GC */
4524 Py_REFCNT(op) = 1;
4525 /* ob_type field is after the memory block: part of "forbidden bytes"
4526 when using debug hooks on memory allocatrs! */
Victor Stinner3bf0f3a2019-06-07 16:22:21 +02004527 return test_pyobject_is_freed("check_pyobject_forbidden_bytes_is_freed", op);
Victor Stinner2b00db62019-04-11 11:33:27 +02004528}
4529
4530
4531static PyObject*
Victor Stinner3bf0f3a2019-06-07 16:22:21 +02004532check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
Victor Stinner2b00db62019-04-11 11:33:27 +02004533{
4534 PyObject *op = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type);
4535 if (op == NULL) {
4536 return NULL;
4537 }
4538 Py_TYPE(op)->tp_dealloc(op);
4539 /* Reset reference count to avoid early crash in ceval or GC */
4540 Py_REFCNT(op) = 1;
4541 /* object memory is freed! */
Victor Stinner3bf0f3a2019-06-07 16:22:21 +02004542 return test_pyobject_is_freed("check_pyobject_freed_is_freed", op);
Victor Stinner2b00db62019-04-11 11:33:27 +02004543}
4544
4545
4546static PyObject*
Victor Stinnerc4aec362016-03-14 22:26:53 +01004547pyobject_malloc_without_gil(PyObject *self, PyObject *args)
4548{
4549 char *buffer;
4550
Victor Stinnerad524372016-03-16 12:12:53 +01004551 /* Deliberate bug to test debug hooks on Python memory allocators:
4552 call PyObject_Malloc() without holding the GIL */
Victor Stinnerc4aec362016-03-14 22:26:53 +01004553 Py_BEGIN_ALLOW_THREADS
4554 buffer = PyObject_Malloc(10);
4555 Py_END_ALLOW_THREADS
4556
4557 PyObject_Free(buffer);
4558
4559 Py_RETURN_NONE;
4560}
4561
Victor Stinner10b73e12016-03-22 13:39:05 +01004562static PyObject *
4563tracemalloc_track(PyObject *self, PyObject *args)
4564{
4565 unsigned int domain;
4566 PyObject *ptr_obj;
4567 void *ptr;
4568 Py_ssize_t size;
4569 int release_gil = 0;
4570 int res;
4571
4572 if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil))
4573 return NULL;
4574 ptr = PyLong_AsVoidPtr(ptr_obj);
4575 if (PyErr_Occurred())
4576 return NULL;
4577
4578 if (release_gil) {
4579 Py_BEGIN_ALLOW_THREADS
Victor Stinner5ea4c062017-06-20 17:46:36 +02004580 res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
Victor Stinner10b73e12016-03-22 13:39:05 +01004581 Py_END_ALLOW_THREADS
4582 }
4583 else {
Victor Stinner5ea4c062017-06-20 17:46:36 +02004584 res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
Victor Stinner10b73e12016-03-22 13:39:05 +01004585 }
4586
4587 if (res < 0) {
Victor Stinner5ea4c062017-06-20 17:46:36 +02004588 PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Track error");
Victor Stinner10b73e12016-03-22 13:39:05 +01004589 return NULL;
4590 }
4591
4592 Py_RETURN_NONE;
4593}
4594
4595static PyObject *
4596tracemalloc_untrack(PyObject *self, PyObject *args)
4597{
4598 unsigned int domain;
4599 PyObject *ptr_obj;
4600 void *ptr;
4601 int res;
4602
4603 if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
4604 return NULL;
4605 ptr = PyLong_AsVoidPtr(ptr_obj);
4606 if (PyErr_Occurred())
4607 return NULL;
4608
Victor Stinner5ea4c062017-06-20 17:46:36 +02004609 res = PyTraceMalloc_Untrack(domain, (uintptr_t)ptr);
Victor Stinner10b73e12016-03-22 13:39:05 +01004610 if (res < 0) {
Victor Stinner5ea4c062017-06-20 17:46:36 +02004611 PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Untrack error");
Victor Stinner10b73e12016-03-22 13:39:05 +01004612 return NULL;
4613 }
4614
4615 Py_RETURN_NONE;
4616}
4617
4618static PyObject *
4619tracemalloc_get_traceback(PyObject *self, PyObject *args)
4620{
4621 unsigned int domain;
4622 PyObject *ptr_obj;
4623 void *ptr;
4624
4625 if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
4626 return NULL;
4627 ptr = PyLong_AsVoidPtr(ptr_obj);
4628 if (PyErr_Occurred())
4629 return NULL;
4630
Benjamin Petersonca470632016-09-06 13:47:26 -07004631 return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr);
Victor Stinner10b73e12016-03-22 13:39:05 +01004632}
4633
Victor Stinner3b6a6b42016-09-08 12:51:24 -07004634static PyObject *
4635dict_get_version(PyObject *self, PyObject *args)
4636{
4637 PyDictObject *dict;
4638 uint64_t version;
4639
4640 if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict))
4641 return NULL;
4642
4643 version = dict->ma_version_tag;
4644
4645 Py_BUILD_ASSERT(sizeof(unsigned PY_LONG_LONG) >= sizeof(version));
4646 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)version);
4647}
4648
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004649
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07004650static PyObject *
4651raise_SIGINT_then_send_None(PyObject *self, PyObject *args)
4652{
4653 PyGenObject *gen;
4654
4655 if (!PyArg_ParseTuple(args, "O!", &PyGen_Type, &gen))
4656 return NULL;
4657
4658 /* This is used in a test to check what happens if a signal arrives just
4659 as we're in the process of entering a yield from chain (see
4660 bpo-30039).
4661
4662 Needs to be done in C, because:
4663 - we don't have a Python wrapper for raise()
4664 - we need to make sure that the Python-level signal handler doesn't run
4665 *before* we enter the generator frame, which is impossible in Python
4666 because we check for signals before every bytecode operation.
4667 */
4668 raise(SIGINT);
4669 return _PyGen_Send(gen, Py_None);
4670}
4671
4672
Victor Stinner3b5cf852017-06-09 16:48:45 +02004673static int
4674fastcall_args(PyObject *args, PyObject ***stack, Py_ssize_t *nargs)
4675{
4676 if (args == Py_None) {
4677 *stack = NULL;
4678 *nargs = 0;
4679 }
4680 else if (PyTuple_Check(args)) {
Victor Stinnerd17a6932018-11-09 16:56:48 +01004681 *stack = ((PyTupleObject *)args)->ob_item;
Victor Stinner3b5cf852017-06-09 16:48:45 +02004682 *nargs = PyTuple_GET_SIZE(args);
4683 }
4684 else {
4685 PyErr_SetString(PyExc_TypeError, "args must be None or a tuple");
4686 return -1;
4687 }
4688 return 0;
4689}
4690
4691
4692static PyObject *
4693test_pyobject_fastcall(PyObject *self, PyObject *args)
4694{
4695 PyObject *func, *func_args;
4696 PyObject **stack;
4697 Py_ssize_t nargs;
4698
4699 if (!PyArg_ParseTuple(args, "OO", &func, &func_args)) {
4700 return NULL;
4701 }
4702
4703 if (fastcall_args(func_args, &stack, &nargs) < 0) {
4704 return NULL;
4705 }
4706 return _PyObject_FastCall(func, stack, nargs);
4707}
4708
4709
4710static PyObject *
4711test_pyobject_fastcalldict(PyObject *self, PyObject *args)
4712{
4713 PyObject *func, *func_args, *kwargs;
4714 PyObject **stack;
4715 Py_ssize_t nargs;
4716
4717 if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwargs)) {
4718 return NULL;
4719 }
4720
4721 if (fastcall_args(func_args, &stack, &nargs) < 0) {
4722 return NULL;
4723 }
4724
4725 if (kwargs == Py_None) {
4726 kwargs = NULL;
4727 }
4728 else if (!PyDict_Check(kwargs)) {
4729 PyErr_SetString(PyExc_TypeError, "kwnames must be None or a dict");
4730 return NULL;
4731 }
4732
4733 return _PyObject_FastCallDict(func, stack, nargs, kwargs);
4734}
4735
4736
4737static PyObject *
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004738test_pyobject_vectorcall(PyObject *self, PyObject *args)
Victor Stinner3b5cf852017-06-09 16:48:45 +02004739{
4740 PyObject *func, *func_args, *kwnames = NULL;
4741 PyObject **stack;
4742 Py_ssize_t nargs, nkw;
4743
4744 if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwnames)) {
4745 return NULL;
4746 }
4747
4748 if (fastcall_args(func_args, &stack, &nargs) < 0) {
4749 return NULL;
4750 }
4751
4752 if (kwnames == Py_None) {
4753 kwnames = NULL;
4754 }
4755 else if (PyTuple_Check(kwnames)) {
4756 nkw = PyTuple_GET_SIZE(kwnames);
4757 if (nargs < nkw) {
4758 PyErr_SetString(PyExc_ValueError, "kwnames longer than args");
4759 return NULL;
4760 }
4761 nargs -= nkw;
4762 }
4763 else {
4764 PyErr_SetString(PyExc_TypeError, "kwnames must be None or a tuple");
4765 return NULL;
4766 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004767 return _PyObject_Vectorcall(func, stack, nargs, kwnames);
4768}
4769
4770
4771static PyObject *
4772test_pyvectorcall_call(PyObject *self, PyObject *args)
4773{
4774 PyObject *func;
4775 PyObject *argstuple;
4776 PyObject *kwargs = NULL;
4777
4778 if (!PyArg_ParseTuple(args, "OO|O", &func, &argstuple, &kwargs)) {
4779 return NULL;
4780 }
4781
4782 if (!PyTuple_Check(argstuple)) {
4783 PyErr_SetString(PyExc_TypeError, "args must be a tuple");
4784 return NULL;
4785 }
4786 if (kwargs != NULL && !PyDict_Check(kwargs)) {
4787 PyErr_SetString(PyExc_TypeError, "kwargs must be a dict");
4788 return NULL;
4789 }
4790
4791 return PyVectorcall_Call(func, argstuple, kwargs);
Victor Stinner3b5cf852017-06-09 16:48:45 +02004792}
4793
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02004794
Victor Stinner64fa4492017-07-10 14:37:49 +02004795static PyObject*
4796stack_pointer(PyObject *self, PyObject *args)
4797{
4798 int v = 5;
4799 return PyLong_FromVoidPtr(&v);
4800}
4801
Victor Stinner3b5cf852017-06-09 16:48:45 +02004802
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02004803#ifdef W_STOPCODE
4804static PyObject*
4805py_w_stopcode(PyObject *self, PyObject *args)
4806{
4807 int sig, status;
4808 if (!PyArg_ParseTuple(args, "i", &sig)) {
4809 return NULL;
4810 }
4811 status = W_STOPCODE(sig);
4812 return PyLong_FromLong(status);
4813}
4814#endif
4815
4816
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09004817static PyObject *
Oren Milman0ccc0f62017-10-08 11:17:46 +03004818get_mapping_keys(PyObject* self, PyObject *obj)
4819{
4820 return PyMapping_Keys(obj);
4821}
4822
4823static PyObject *
4824get_mapping_values(PyObject* self, PyObject *obj)
4825{
4826 return PyMapping_Values(obj);
4827}
4828
4829static PyObject *
4830get_mapping_items(PyObject* self, PyObject *obj)
4831{
4832 return PyMapping_Items(obj);
4833}
4834
4835
4836static PyObject *
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09004837test_pythread_tss_key_state(PyObject *self, PyObject *args)
4838{
4839 Py_tss_t tss_key = Py_tss_NEEDS_INIT;
4840 if (PyThread_tss_is_created(&tss_key)) {
4841 return raiseTestError("test_pythread_tss_key_state",
4842 "TSS key not in an uninitialized state at "
4843 "creation time");
4844 }
4845 if (PyThread_tss_create(&tss_key) != 0) {
4846 PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_create failed");
4847 return NULL;
4848 }
4849 if (!PyThread_tss_is_created(&tss_key)) {
4850 return raiseTestError("test_pythread_tss_key_state",
4851 "PyThread_tss_create succeeded, "
4852 "but with TSS key in an uninitialized state");
4853 }
4854 if (PyThread_tss_create(&tss_key) != 0) {
4855 return raiseTestError("test_pythread_tss_key_state",
4856 "PyThread_tss_create unsuccessful with "
4857 "an already initialized key");
4858 }
4859#define CHECK_TSS_API(expr) \
4860 (void)(expr); \
4861 if (!PyThread_tss_is_created(&tss_key)) { \
4862 return raiseTestError("test_pythread_tss_key_state", \
4863 "TSS key initialization state was not " \
4864 "preserved after calling " #expr); }
4865 CHECK_TSS_API(PyThread_tss_set(&tss_key, NULL));
4866 CHECK_TSS_API(PyThread_tss_get(&tss_key));
4867#undef CHECK_TSS_API
4868 PyThread_tss_delete(&tss_key);
4869 if (PyThread_tss_is_created(&tss_key)) {
4870 return raiseTestError("test_pythread_tss_key_state",
4871 "PyThread_tss_delete called, but did not "
4872 "set the key state to uninitialized");
4873 }
4874
4875 Py_tss_t *ptr_key = PyThread_tss_alloc();
4876 if (ptr_key == NULL) {
4877 PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_alloc failed");
4878 return NULL;
4879 }
4880 if (PyThread_tss_is_created(ptr_key)) {
4881 return raiseTestError("test_pythread_tss_key_state",
4882 "TSS key not in an uninitialized state at "
4883 "allocation time");
4884 }
4885 PyThread_tss_free(ptr_key);
4886 ptr_key = NULL;
4887 Py_RETURN_NONE;
4888}
4889
4890
Yury Selivanovf23746a2018-01-22 19:11:18 -05004891static PyObject*
4892new_hamt(PyObject *self, PyObject *args)
4893{
4894 return _PyContext_NewHamtForTests();
4895}
4896
4897
jdemeyer5a306202018-10-19 23:50:06 +02004898/* def bad_get(self, obj, cls):
4899 cls()
4900 return repr(self)
4901*/
4902static PyObject*
4903bad_get(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4904{
Serhiy Storchakab1dede32018-11-20 20:45:40 +02004905 PyObject *self, *obj, *cls;
4906 if (!_PyArg_UnpackStack(args, nargs, "bad_get", 3, 3, &self, &obj, &cls)) {
jdemeyer5a306202018-10-19 23:50:06 +02004907 return NULL;
4908 }
4909
Jeroen Demeyer7f41c8e2019-07-04 12:35:31 +02004910 PyObject *res = _PyObject_CallNoArg(cls);
jdemeyer5a306202018-10-19 23:50:06 +02004911 if (res == NULL) {
4912 return NULL;
4913 }
4914 Py_DECREF(res);
4915
Serhiy Storchakab1dede32018-11-20 20:45:40 +02004916 return PyObject_Repr(self);
jdemeyer5a306202018-10-19 23:50:06 +02004917}
4918
4919
Victor Stinner3d4226a2018-08-29 22:21:32 +02004920static PyObject *
4921encode_locale_ex(PyObject *self, PyObject *args)
4922{
4923 PyObject *unicode;
4924 int current_locale = 0;
4925 wchar_t *wstr;
4926 PyObject *res = NULL;
4927 const char *errors = NULL;
4928
4929 if (!PyArg_ParseTuple(args, "U|is", &unicode, &current_locale, &errors)) {
4930 return NULL;
4931 }
4932 wstr = PyUnicode_AsWideCharString(unicode, NULL);
4933 if (wstr == NULL) {
4934 return NULL;
4935 }
4936 _Py_error_handler error_handler = _Py_GetErrorHandler(errors);
4937
4938 char *str = NULL;
4939 size_t error_pos;
4940 const char *reason = NULL;
4941 int ret = _Py_EncodeLocaleEx(wstr,
4942 &str, &error_pos, &reason,
4943 current_locale, error_handler);
4944 PyMem_Free(wstr);
4945
4946 switch(ret) {
4947 case 0:
4948 res = PyBytes_FromString(str);
4949 PyMem_RawFree(str);
4950 break;
4951 case -1:
4952 PyErr_NoMemory();
4953 break;
4954 case -2:
4955 PyErr_Format(PyExc_RuntimeError, "encode error: pos=%zu, reason=%s",
4956 error_pos, reason);
4957 break;
4958 case -3:
4959 PyErr_SetString(PyExc_ValueError, "unsupported error handler");
4960 break;
4961 default:
4962 PyErr_SetString(PyExc_ValueError, "unknow error code");
4963 break;
4964 }
4965 return res;
4966}
4967
4968
4969static PyObject *
4970decode_locale_ex(PyObject *self, PyObject *args)
4971{
4972 char *str;
4973 int current_locale = 0;
4974 PyObject *res = NULL;
4975 const char *errors = NULL;
4976
4977 if (!PyArg_ParseTuple(args, "y|is", &str, &current_locale, &errors)) {
4978 return NULL;
4979 }
4980 _Py_error_handler error_handler = _Py_GetErrorHandler(errors);
4981
4982 wchar_t *wstr = NULL;
4983 size_t wlen = 0;
4984 const char *reason = NULL;
4985 int ret = _Py_DecodeLocaleEx(str,
4986 &wstr, &wlen, &reason,
4987 current_locale, error_handler);
4988
4989 switch(ret) {
4990 case 0:
4991 res = PyUnicode_FromWideChar(wstr, wlen);
4992 PyMem_RawFree(wstr);
4993 break;
4994 case -1:
4995 PyErr_NoMemory();
4996 break;
4997 case -2:
4998 PyErr_Format(PyExc_RuntimeError, "decode error: pos=%zu, reason=%s",
4999 wlen, reason);
5000 break;
5001 case -3:
5002 PyErr_SetString(PyExc_ValueError, "unsupported error handler");
5003 break;
5004 default:
5005 PyErr_SetString(PyExc_ValueError, "unknow error code");
5006 break;
5007 }
5008 return res;
5009}
5010
5011
Victor Stinner18618e652018-10-25 17:28:11 +02005012#ifdef Py_REF_DEBUG
5013static PyObject *
5014negative_refcount(PyObject *self, PyObject *Py_UNUSED(args))
5015{
5016 PyObject *obj = PyUnicode_FromString("negative_refcount");
5017 if (obj == NULL) {
5018 return NULL;
5019 }
5020 assert(Py_REFCNT(obj) == 1);
5021
5022 Py_REFCNT(obj) = 0;
5023 /* Py_DECREF() must call _Py_NegativeRefcount() and abort Python */
5024 Py_DECREF(obj);
5025
5026 Py_RETURN_NONE;
5027}
5028#endif
5029
5030
Victor Stinneref9d9b62019-05-22 11:28:22 +02005031static PyObject*
5032test_write_unraisable_exc(PyObject *self, PyObject *args)
5033{
Victor Stinner71c52e32019-05-27 08:57:14 +02005034 PyObject *exc, *err_msg, *obj;
5035 if (!PyArg_ParseTuple(args, "OOO", &exc, &err_msg, &obj)) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02005036 return NULL;
5037 }
5038
Victor Stinner71c52e32019-05-27 08:57:14 +02005039 const char *err_msg_utf8;
5040 if (err_msg != Py_None) {
5041 err_msg_utf8 = PyUnicode_AsUTF8(err_msg);
5042 if (err_msg_utf8 == NULL) {
5043 return NULL;
5044 }
5045 }
5046 else {
5047 err_msg_utf8 = NULL;
5048 }
5049
Victor Stinneref9d9b62019-05-22 11:28:22 +02005050 PyErr_SetObject((PyObject *)Py_TYPE(exc), exc);
Victor Stinner71c52e32019-05-27 08:57:14 +02005051 _PyErr_WriteUnraisableMsg(err_msg_utf8, obj);
Victor Stinneref9d9b62019-05-22 11:28:22 +02005052 Py_RETURN_NONE;
5053}
5054
5055
Tim Peters9ea17ac2001-02-02 05:57:15 +00005056static PyMethodDef TestMethods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 {"raise_exception", raise_exception, METH_VARARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305058 {"raise_memoryerror", raise_memoryerror, METH_NOARGS},
Antoine Pitrou6bc217d2015-06-23 14:31:11 +02005059 {"set_errno", set_errno, METH_VARARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305060 {"test_config", test_config, METH_NOARGS},
5061 {"test_sizeof_c_types", test_sizeof_c_types, METH_NOARGS},
Edison A98ff4d52019-05-17 13:28:42 -07005062 {"test_datetime_capi", test_datetime_capi, METH_NOARGS},
Paul Ganssle04af5b12018-01-24 17:29:30 -05005063 {"datetime_check_date", datetime_check_date, METH_VARARGS},
5064 {"datetime_check_time", datetime_check_time, METH_VARARGS},
5065 {"datetime_check_datetime", datetime_check_datetime, METH_VARARGS},
5066 {"datetime_check_delta", datetime_check_delta, METH_VARARGS},
5067 {"datetime_check_tzinfo", datetime_check_tzinfo, METH_VARARGS},
5068 {"make_timezones_capi", make_timezones_capi, METH_NOARGS},
Paul Gansslea049f572018-02-22 15:15:32 -05005069 {"get_timezones_offset_zero", get_timezones_offset_zero, METH_NOARGS},
Paul Ganssle4d8c8c02019-04-27 15:39:40 -04005070 {"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS},
Edison A98ff4d52019-05-17 13:28:42 -07005071 {"get_date_fromdate", get_date_fromdate, METH_VARARGS},
5072 {"get_datetime_fromdateandtime", get_datetime_fromdateandtime, METH_VARARGS},
5073 {"get_datetime_fromdateandtimeandfold", get_datetime_fromdateandtimeandfold, METH_VARARGS},
5074 {"get_time_fromtime", get_time_fromtime, METH_VARARGS},
5075 {"get_time_fromtimeandfold", get_time_fromtimeandfold, METH_VARARGS},
5076 {"get_delta_fromdsu", get_delta_fromdsu, METH_VARARGS},
Paul Ganssle4d8c8c02019-04-27 15:39:40 -04005077 {"get_date_fromtimestamp", get_date_fromtimestamp, METH_VARARGS},
5078 {"get_datetime_fromtimestamp", get_datetime_fromtimestamp, METH_VARARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305079 {"test_list_api", test_list_api, METH_NOARGS},
5080 {"test_dict_iteration", test_dict_iteration, METH_NOARGS},
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02005081 {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS},
Victor Stinner3d3f2642016-12-15 17:21:23 +01005082 {"dict_hassplittable", dict_hassplittable, METH_O},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305083 {"test_lazy_hash_inheritance", test_lazy_hash_inheritance,METH_NOARGS},
5084 {"test_long_api", test_long_api, METH_NOARGS},
5085 {"test_xincref_doesnt_leak",test_xincref_doesnt_leak, METH_NOARGS},
5086 {"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS},
5087 {"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak, METH_NOARGS},
5088 {"test_decref_doesnt_leak", test_decref_doesnt_leak, METH_NOARGS},
Eddie Elizondo474eedf2018-11-13 04:09:31 -08005089 {"test_structseq_newtype_doesnt_leak",
5090 test_structseq_newtype_doesnt_leak, METH_NOARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305091 {"test_incref_decref_API", test_incref_decref_API, METH_NOARGS},
5092 {"test_long_and_overflow", test_long_and_overflow, METH_NOARGS},
5093 {"test_long_as_double", test_long_as_double, METH_NOARGS},
5094 {"test_long_as_size_t", test_long_as_size_t, METH_NOARGS},
Zackery Spytzdc247652019-06-06 14:39:23 -06005095 {"test_long_as_unsigned_long_long_mask",
5096 test_long_as_unsigned_long_long_mask, METH_NOARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305097 {"test_long_numbits", test_long_numbits, METH_NOARGS},
5098 {"test_k_code", test_k_code, METH_NOARGS},
5099 {"test_empty_argparse", test_empty_argparse, METH_NOARGS},
Larry Hastings8f904da2012-06-22 03:56:29 -07005100 {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305101 {"test_null_strings", test_null_strings, METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305103 {"test_with_docstring", test_with_docstring, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 PyDoc_STR("This is a pretty normal docstring.")},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305105 {"test_string_to_double", test_string_to_double, METH_NOARGS},
5106 {"test_unicode_compare_with_ascii", test_unicode_compare_with_ascii,
5107 METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
Stefan Krah7213fcc2015-02-01 16:19:23 +01005109 {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS},
Stefan Kraha7559c02015-02-03 22:27:21 +01005110#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
Stefan Krah5178d912015-02-03 16:57:21 +01005111 {"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS},
Stefan Kraha7559c02015-02-03 22:27:21 +01005112#endif
Stefan Krah650c1e82015-02-03 21:43:23 +01005113 {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
Serhiy Storchaka13e602e2016-05-20 22:31:14 +03005114 {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS},
Serhiy Storchakace412872016-05-08 23:36:44 +03005115 {"get_args", get_args, METH_VARARGS},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02005116 {"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs, METH_VARARGS|METH_KEYWORDS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 {"getargs_tuple", getargs_tuple, METH_VARARGS},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02005118 {"getargs_keywords", (PyCFunction)(void(*)(void))getargs_keywords,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 METH_VARARGS|METH_KEYWORDS},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02005120 {"getargs_keyword_only", (PyCFunction)(void(*)(void))getargs_keyword_only,
Larry Hastings83a9f482012-03-20 20:06:16 +00005121 METH_VARARGS|METH_KEYWORDS},
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03005122 {"getargs_positional_only_and_keywords",
Serhiy Storchaka62be7422018-11-27 13:27:31 +02005123 (PyCFunction)(void(*)(void))getargs_positional_only_and_keywords,
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03005124 METH_VARARGS|METH_KEYWORDS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 {"getargs_b", getargs_b, METH_VARARGS},
5126 {"getargs_B", getargs_B, METH_VARARGS},
5127 {"getargs_h", getargs_h, METH_VARARGS},
5128 {"getargs_H", getargs_H, METH_VARARGS},
5129 {"getargs_I", getargs_I, METH_VARARGS},
5130 {"getargs_k", getargs_k, METH_VARARGS},
5131 {"getargs_i", getargs_i, METH_VARARGS},
5132 {"getargs_l", getargs_l, METH_VARARGS},
5133 {"getargs_n", getargs_n, METH_VARARGS},
Larry Hastingsfaf91e72012-05-05 16:54:29 -07005134 {"getargs_p", getargs_p, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005135 {"getargs_L", getargs_L, METH_VARARGS},
5136 {"getargs_K", getargs_K, METH_VARARGS},
5137 {"test_longlong_api", test_longlong_api, METH_NOARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305138 {"test_long_long_and_overflow",test_long_long_and_overflow, METH_NOARGS},
5139 {"test_L_code", test_L_code, METH_NOARGS},
Serhiy Storchakaf95455d2016-05-16 10:11:47 +03005140 {"getargs_f", getargs_f, METH_VARARGS},
5141 {"getargs_d", getargs_d, METH_VARARGS},
5142 {"getargs_D", getargs_D, METH_VARARGS},
5143 {"getargs_S", getargs_S, METH_VARARGS},
5144 {"getargs_Y", getargs_Y, METH_VARARGS},
5145 {"getargs_U", getargs_U, METH_VARARGS},
Eli Bendersky906b88f2011-07-29 07:05:08 +03005146 {"getargs_c", getargs_c, METH_VARARGS},
Serhiy Storchakac8241fd2016-01-28 19:49:54 +02005147 {"getargs_C", getargs_C, METH_VARARGS},
Victor Stinner06e49dd2010-06-13 18:21:50 +00005148 {"getargs_s", getargs_s, METH_VARARGS},
5149 {"getargs_s_star", getargs_s_star, METH_VARARGS},
5150 {"getargs_s_hash", getargs_s_hash, METH_VARARGS},
5151 {"getargs_z", getargs_z, METH_VARARGS},
5152 {"getargs_z_star", getargs_z_star, METH_VARARGS},
5153 {"getargs_z_hash", getargs_z_hash, METH_VARARGS},
5154 {"getargs_y", getargs_y, METH_VARARGS},
5155 {"getargs_y_star", getargs_y_star, METH_VARARGS},
5156 {"getargs_y_hash", getargs_y_hash, METH_VARARGS},
5157 {"getargs_u", getargs_u, METH_VARARGS},
5158 {"getargs_u_hash", getargs_u_hash, METH_VARARGS},
5159 {"getargs_Z", getargs_Z, METH_VARARGS},
5160 {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS},
Victor Stinner25e8ec42010-06-25 00:02:38 +00005161 {"getargs_w_star", getargs_w_star, METH_VARARGS},
Serhiy Storchakac8241fd2016-01-28 19:49:54 +02005162 {"getargs_es", getargs_es, METH_VARARGS},
5163 {"getargs_et", getargs_et, METH_VARARGS},
5164 {"getargs_es_hash", getargs_es_hash, METH_VARARGS},
5165 {"getargs_et_hash", getargs_et_hash, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005166 {"codec_incrementalencoder",
Victor Stinner1c24bd02010-10-02 11:03:13 +00005167 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005168 {"codec_incrementaldecoder",
Victor Stinner1c24bd02010-10-02 11:03:13 +00005169 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305170 {"test_s_code", test_s_code, METH_NOARGS},
5171 {"test_u_code", test_u_code, METH_NOARGS},
5172 {"test_Z_code", test_Z_code, METH_NOARGS},
5173 {"test_widechar", test_widechar, METH_NOARGS},
Victor Stinner42bf7752011-11-21 22:52:58 +01005174 {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS},
5175 {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS},
Serhiy Storchakacc164232016-10-02 21:29:26 +03005176 {"unicode_asucs4", unicode_asucs4, METH_VARARGS},
Xiang Zhangb2110682016-12-20 22:52:33 +08005177 {"unicode_findchar", unicode_findchar, METH_VARARGS},
Serhiy Storchaka9c0e1f82016-10-08 22:45:38 +03005178 {"unicode_copycharacters", unicode_copycharacters, METH_VARARGS},
Victor Stinner42bf7752011-11-21 22:52:58 +01005179 {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS},
5180 {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS},
Stefan Krahe6996ed2012-11-02 14:44:20 +01005181 {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS},
Victor Stinner1c24bd02010-10-02 11:03:13 +00005182 {"_test_thread_state", test_thread_state, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005183 {"_pending_threadfunc", pending_threadfunc, METH_VARARGS},
Guido van Rossumddefaf32007-01-14 03:31:43 +00005184#ifdef HAVE_GETTIMEOFDAY
Victor Stinner1c24bd02010-10-02 11:03:13 +00005185 {"profile_int", profile_int, METH_NOARGS},
Guido van Rossumddefaf32007-01-14 03:31:43 +00005186#endif
Victor Stinner1c24bd02010-10-02 11:03:13 +00005187 {"traceback_print", traceback_print, METH_VARARGS},
5188 {"exception_print", exception_print, METH_VARARGS},
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +02005189 {"set_exc_info", test_set_exc_info, METH_VARARGS},
Victor Stinner1c24bd02010-10-02 11:03:13 +00005190 {"argparsing", argparsing, METH_VARARGS},
5191 {"code_newempty", code_newempty, METH_VARARGS},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02005192 {"make_exception_with_doc", (PyCFunction)(void(*)(void))make_exception_with_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005193 METH_VARARGS | METH_KEYWORDS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305194 {"make_memoryview_from_NULL_pointer", make_memoryview_from_NULL_pointer,
Antoine Pitrou5bffa792011-02-24 20:50:49 +00005195 METH_NOARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305196 {"crash_no_current_thread", crash_no_current_thread, METH_NOARGS},
Antoine Pitrou2f828f22012-01-18 00:21:11 +01005197 {"run_in_subinterp", run_in_subinterp, METH_VARARGS},
Victor Stinner5d272cc2012-03-13 13:35:55 +01005198 {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS},
5199 {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS},
Victor Stinner643cd682012-03-02 22:54:03 +01005200 {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS},
Antoine Pitrou796564c2013-07-30 19:59:21 +02005201 {"with_tp_del", with_tp_del, METH_VARARGS},
Antoine Pitroub349e4c2014-08-06 19:31:40 -04005202 {"create_cfunction", create_cfunction, METH_NOARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305203 {"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS},
5204 {"test_pymem_setrawallocators",test_pymem_setrawallocators, METH_NOARGS},
5205 {"test_pymem_setallocators",test_pymem_setallocators, METH_NOARGS},
5206 {"test_pyobject_setallocators",test_pyobject_setallocators, METH_NOARGS},
xdegaye85f64302017-07-01 14:14:45 +02005207 {"set_nomemory", (PyCFunction)set_nomemory, METH_VARARGS,
5208 PyDoc_STR("set_nomemory(start:int, stop:int = 0)")},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05305209 {"remove_mem_hooks", remove_mem_hooks, METH_NOARGS,
xdegaye85f64302017-07-01 14:14:45 +02005210 PyDoc_STR("Remove memory hooks.")},
Larry Hastings44e2eaa2013-11-23 15:37:55 -08005211 {"no_docstring",
5212 (PyCFunction)test_with_docstring, METH_NOARGS},
5213 {"docstring_empty",
5214 (PyCFunction)test_with_docstring, METH_NOARGS,
5215 docstring_empty},
5216 {"docstring_no_signature",
5217 (PyCFunction)test_with_docstring, METH_NOARGS,
5218 docstring_no_signature},
5219 {"docstring_with_invalid_signature",
5220 (PyCFunction)test_with_docstring, METH_NOARGS,
5221 docstring_with_invalid_signature},
Larry Hastings2623c8c2014-02-08 22:15:29 -08005222 {"docstring_with_invalid_signature2",
5223 (PyCFunction)test_with_docstring, METH_NOARGS,
5224 docstring_with_invalid_signature2},
Larry Hastings44e2eaa2013-11-23 15:37:55 -08005225 {"docstring_with_signature",
5226 (PyCFunction)test_with_docstring, METH_NOARGS,
5227 docstring_with_signature},
Zachary Ware8ef887c2015-04-13 18:22:35 -05005228 {"docstring_with_signature_but_no_doc",
5229 (PyCFunction)test_with_docstring, METH_NOARGS,
5230 docstring_with_signature_but_no_doc},
Larry Hastings44e2eaa2013-11-23 15:37:55 -08005231 {"docstring_with_signature_and_extra_newlines",
5232 (PyCFunction)test_with_docstring, METH_NOARGS,
5233 docstring_with_signature_and_extra_newlines},
Larry Hastings16c51912014-01-07 11:53:01 -08005234 {"docstring_with_signature_with_defaults",
5235 (PyCFunction)test_with_docstring, METH_NOARGS,
5236 docstring_with_signature_with_defaults},
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005237 {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O,
5238 PyDoc_STR("set_error_class(error_class) -> None")},
Serhiy Storchakab5181342015-02-06 08:58:56 +02005239 {"pymarshal_write_long_to_file",
5240 pymarshal_write_long_to_file, METH_VARARGS},
5241 {"pymarshal_write_object_to_file",
5242 pymarshal_write_object_to_file, METH_VARARGS},
5243 {"pymarshal_read_short_from_file",
5244 pymarshal_read_short_from_file, METH_VARARGS},
5245 {"pymarshal_read_long_from_file",
5246 pymarshal_read_long_from_file, METH_VARARGS},
5247 {"pymarshal_read_last_object_from_file",
5248 pymarshal_read_last_object_from_file, METH_VARARGS},
5249 {"pymarshal_read_object_from_file",
5250 pymarshal_read_object_from_file, METH_VARARGS},
Victor Stinnerefde1462015-03-21 15:04:43 +01005251 {"return_null_without_error",
5252 return_null_without_error, METH_NOARGS},
5253 {"return_result_with_error",
5254 return_result_with_error, METH_NOARGS},
Victor Stinner13019fd2015-04-03 13:10:54 +02005255 {"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS},
Victor Stinner4bfb4602015-03-27 22:27:24 +01005256 {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
5257 {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
Victor Stinner95e9cef2015-03-28 01:26:47 +01005258 {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS},
Victor Stinner34dc0f42015-03-27 18:19:03 +01005259#ifdef HAVE_CLOCK_GETTIME
5260 {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS},
5261#endif
Victor Stinner62d1c702015-04-01 17:47:07 +02005262 {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS},
5263 {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS},
Victor Stinner50856d52015-10-13 00:11:21 +02005264 {"get_recursion_depth", get_recursion_depth, METH_NOARGS},
Victor Stinner34be8072016-03-14 12:04:26 +01005265 {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS},
5266 {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS},
Victor Stinnerad524372016-03-16 12:12:53 +01005267 {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS},
Victor Stinner5d39e042017-11-29 17:20:38 +01005268 {"pymem_getallocatorsname", test_pymem_getallocatorsname, METH_NOARGS},
Victor Stinner3bf0f3a2019-06-07 16:22:21 +02005269 {"check_pyobject_uninitialized_is_freed", check_pyobject_uninitialized_is_freed, METH_NOARGS},
5270 {"check_pyobject_forbidden_bytes_is_freed", check_pyobject_forbidden_bytes_is_freed, METH_NOARGS},
5271 {"check_pyobject_freed_is_freed", check_pyobject_freed_is_freed, METH_NOARGS},
Victor Stinnerc4aec362016-03-14 22:26:53 +01005272 {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS},
Victor Stinner10b73e12016-03-22 13:39:05 +01005273 {"tracemalloc_track", tracemalloc_track, METH_VARARGS},
5274 {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS},
5275 {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS},
Victor Stinner3b6a6b42016-09-08 12:51:24 -07005276 {"dict_get_version", dict_get_version, METH_VARARGS},
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07005277 {"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS},
Victor Stinner3b5cf852017-06-09 16:48:45 +02005278 {"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS},
5279 {"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS},
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005280 {"pyobject_vectorcall", test_pyobject_vectorcall, METH_VARARGS},
5281 {"pyvectorcall_call", test_pyvectorcall_call, METH_VARARGS},
Victor Stinner64fa4492017-07-10 14:37:49 +02005282 {"stack_pointer", stack_pointer, METH_NOARGS},
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02005283#ifdef W_STOPCODE
5284 {"W_STOPCODE", py_w_stopcode, METH_VARARGS},
5285#endif
Oren Milman0ccc0f62017-10-08 11:17:46 +03005286 {"get_mapping_keys", get_mapping_keys, METH_O},
5287 {"get_mapping_values", get_mapping_values, METH_O},
5288 {"get_mapping_items", get_mapping_items, METH_O},
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09005289 {"test_pythread_tss_key_state", test_pythread_tss_key_state, METH_VARARGS},
Yury Selivanovf23746a2018-01-22 19:11:18 -05005290 {"hamt", new_hamt, METH_NOARGS},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02005291 {"bad_get", (PyCFunction)(void(*)(void))bad_get, METH_FASTCALL},
Victor Stinner3d4226a2018-08-29 22:21:32 +02005292 {"EncodeLocaleEx", encode_locale_ex, METH_VARARGS},
5293 {"DecodeLocaleEx", decode_locale_ex, METH_VARARGS},
Victor Stinner18618e652018-10-25 17:28:11 +02005294#ifdef Py_REF_DEBUG
5295 {"negative_refcount", negative_refcount, METH_NOARGS},
5296#endif
Victor Stinneref9d9b62019-05-22 11:28:22 +02005297 {"write_unraisable_exc", test_write_unraisable_exc, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005298 {NULL, NULL} /* sentinel */
Tim Peters9ea17ac2001-02-02 05:57:15 +00005299};
5300
Thomas Hellera4ea6032003-04-17 18:55:45 +00005301#define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
5302
Thomas Wouters89f507f2006-12-13 04:49:30 +00005303typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005304 char bool_member;
5305 char byte_member;
5306 unsigned char ubyte_member;
5307 short short_member;
5308 unsigned short ushort_member;
5309 int int_member;
5310 unsigned int uint_member;
5311 long long_member;
5312 unsigned long ulong_member;
5313 Py_ssize_t pyssizet_member;
5314 float float_member;
5315 double double_member;
5316 char inplace_member[6];
Benjamin Petersonaf580df2016-09-06 10:46:49 -07005317 long long longlong_member;
5318 unsigned long long ulonglong_member;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005319} all_structmembers;
5320
5321typedef struct {
5322 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005323 all_structmembers structmembers;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005324} test_structmembers;
5325
5326static struct PyMemberDef test_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005327 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
5328 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
5329 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
5330 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
5331 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
5332 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
5333 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
5334 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
5335 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
5336 {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL},
5337 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
5338 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
5339 {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005340 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
5341 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005342 {NULL}
Thomas Wouters89f507f2006-12-13 04:49:30 +00005343};
5344
5345
Christian Heimes1af737c2008-01-23 08:24:23 +00005346static PyObject *
5347test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
5348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005349 static char *keywords[] = {
5350 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
5351 "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET",
5352 "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005353 "T_LONGLONG", "T_ULONGLONG",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005354 NULL};
Benjamin Petersoned4aa832016-09-05 17:44:18 -07005355 static const char fmt[] = "|bbBhHiIlknfds#LK";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005356 test_structmembers *ob;
5357 const char *s = NULL;
5358 Py_ssize_t string_len = 0;
5359 ob = PyObject_New(test_structmembers, type);
5360 if (ob == NULL)
5361 return NULL;
5362 memset(&ob->structmembers, 0, sizeof(all_structmembers));
5363 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
5364 &ob->structmembers.bool_member,
5365 &ob->structmembers.byte_member,
5366 &ob->structmembers.ubyte_member,
5367 &ob->structmembers.short_member,
5368 &ob->structmembers.ushort_member,
5369 &ob->structmembers.int_member,
5370 &ob->structmembers.uint_member,
5371 &ob->structmembers.long_member,
5372 &ob->structmembers.ulong_member,
5373 &ob->structmembers.pyssizet_member,
5374 &ob->structmembers.float_member,
5375 &ob->structmembers.double_member,
5376 &s, &string_len
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005377 , &ob->structmembers.longlong_member,
5378 &ob->structmembers.ulonglong_member
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005379 )) {
5380 Py_DECREF(ob);
5381 return NULL;
5382 }
5383 if (s != NULL) {
5384 if (string_len > 5) {
5385 Py_DECREF(ob);
5386 PyErr_SetString(PyExc_ValueError, "string too long");
5387 return NULL;
5388 }
5389 strcpy(ob->structmembers.inplace_member, s);
5390 }
5391 else {
5392 strcpy(ob->structmembers.inplace_member, "");
5393 }
5394 return (PyObject *)ob;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005395}
5396
Christian Heimes1af737c2008-01-23 08:24:23 +00005397static void
5398test_structmembers_free(PyObject *ob)
5399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 PyObject_FREE(ob);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005401}
5402
5403static PyTypeObject test_structmembersType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00005404 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005405 "test_structmembersType",
5406 sizeof(test_structmembers), /* tp_basicsize */
5407 0, /* tp_itemsize */
5408 test_structmembers_free, /* destructor tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005409 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005410 0, /* tp_getattr */
5411 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005412 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005413 0, /* tp_repr */
5414 0, /* tp_as_number */
5415 0, /* tp_as_sequence */
5416 0, /* tp_as_mapping */
5417 0, /* tp_hash */
5418 0, /* tp_call */
5419 0, /* tp_str */
5420 PyObject_GenericGetAttr, /* tp_getattro */
5421 PyObject_GenericSetAttr, /* tp_setattro */
5422 0, /* tp_as_buffer */
5423 0, /* tp_flags */
5424 "Type containing all structmember types",
5425 0, /* traverseproc tp_traverse */
5426 0, /* tp_clear */
5427 0, /* tp_richcompare */
5428 0, /* tp_weaklistoffset */
5429 0, /* tp_iter */
5430 0, /* tp_iternext */
5431 0, /* tp_methods */
5432 test_members, /* tp_members */
5433 0,
5434 0,
5435 0,
5436 0,
5437 0,
5438 0,
5439 0,
5440 0,
5441 test_structmembers_new, /* tp_new */
Thomas Wouters89f507f2006-12-13 04:49:30 +00005442};
5443
5444
Benjamin Petersond51374e2014-04-09 23:55:56 -04005445typedef struct {
5446 PyObject_HEAD
5447} matmulObject;
5448
5449static PyObject *
5450matmulType_matmul(PyObject *self, PyObject *other)
5451{
5452 return Py_BuildValue("(sOO)", "matmul", self, other);
5453}
5454
5455static PyObject *
5456matmulType_imatmul(PyObject *self, PyObject *other)
5457{
5458 return Py_BuildValue("(sOO)", "imatmul", self, other);
5459}
5460
5461static void
5462matmulType_dealloc(PyObject *self)
5463{
Zachary Ware420dc562014-04-23 13:51:27 -05005464 Py_TYPE(self)->tp_free(self);
Benjamin Petersond51374e2014-04-09 23:55:56 -04005465}
5466
5467static PyNumberMethods matmulType_as_number = {
5468 0, /* nb_add */
5469 0, /* nb_subtract */
5470 0, /* nb_multiply */
5471 0, /* nb_remainde r*/
5472 0, /* nb_divmod */
5473 0, /* nb_power */
5474 0, /* nb_negative */
5475 0, /* tp_positive */
5476 0, /* tp_absolute */
5477 0, /* tp_bool */
5478 0, /* nb_invert */
5479 0, /* nb_lshift */
5480 0, /* nb_rshift */
5481 0, /* nb_and */
5482 0, /* nb_xor */
5483 0, /* nb_or */
5484 0, /* nb_int */
5485 0, /* nb_reserved */
5486 0, /* nb_float */
5487 0, /* nb_inplace_add */
5488 0, /* nb_inplace_subtract */
5489 0, /* nb_inplace_multiply */
5490 0, /* nb_inplace_remainder */
5491 0, /* nb_inplace_power */
5492 0, /* nb_inplace_lshift */
5493 0, /* nb_inplace_rshift */
5494 0, /* nb_inplace_and */
5495 0, /* nb_inplace_xor */
5496 0, /* nb_inplace_or */
5497 0, /* nb_floor_divide */
5498 0, /* nb_true_divide */
5499 0, /* nb_inplace_floor_divide */
5500 0, /* nb_inplace_true_divide */
5501 0, /* nb_index */
5502 matmulType_matmul, /* nb_matrix_multiply */
5503 matmulType_imatmul /* nb_matrix_inplace_multiply */
5504};
5505
5506static PyTypeObject matmulType = {
5507 PyVarObject_HEAD_INIT(NULL, 0)
5508 "matmulType",
5509 sizeof(matmulObject), /* tp_basicsize */
5510 0, /* tp_itemsize */
5511 matmulType_dealloc, /* destructor tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005512 0, /* tp_vectorcall_offset */
Benjamin Petersond51374e2014-04-09 23:55:56 -04005513 0, /* tp_getattr */
5514 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005515 0, /* tp_as_async */
Benjamin Petersond51374e2014-04-09 23:55:56 -04005516 0, /* tp_repr */
5517 &matmulType_as_number, /* tp_as_number */
5518 0, /* tp_as_sequence */
5519 0, /* tp_as_mapping */
5520 0, /* tp_hash */
5521 0, /* tp_call */
5522 0, /* tp_str */
5523 PyObject_GenericGetAttr, /* tp_getattro */
5524 PyObject_GenericSetAttr, /* tp_setattro */
5525 0, /* tp_as_buffer */
5526 0, /* tp_flags */
5527 "C level type with matrix operations defined",
5528 0, /* traverseproc tp_traverse */
5529 0, /* tp_clear */
5530 0, /* tp_richcompare */
5531 0, /* tp_weaklistoffset */
5532 0, /* tp_iter */
5533 0, /* tp_iternext */
5534 0, /* tp_methods */
5535 0, /* tp_members */
5536 0,
5537 0,
5538 0,
5539 0,
5540 0,
5541 0,
5542 0,
5543 0,
5544 PyType_GenericNew, /* tp_new */
5545 PyObject_Del, /* tp_free */
5546};
5547
Zackery Spytzc7f803b2019-05-31 03:46:36 -06005548typedef struct {
5549 PyObject_HEAD
5550} ipowObject;
5551
5552static PyObject *
5553ipowType_ipow(PyObject *self, PyObject *other, PyObject *mod)
5554{
5555 return Py_BuildValue("OO", other, mod);
5556}
5557
5558static PyNumberMethods ipowType_as_number = {
5559 .nb_inplace_power = ipowType_ipow
5560};
5561
5562static PyTypeObject ipowType = {
5563 PyVarObject_HEAD_INIT(NULL, 0)
5564 .tp_name = "ipowType",
5565 .tp_basicsize = sizeof(ipowObject),
5566 .tp_as_number = &ipowType_as_number,
5567 .tp_new = PyType_GenericNew
5568};
Martin v. Löwis1a214512008-06-11 05:26:20 +00005569
Yury Selivanov75445082015-05-11 22:57:16 -04005570typedef struct {
5571 PyObject_HEAD
5572 PyObject *ao_iterator;
5573} awaitObject;
5574
5575
5576static PyObject *
5577awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5578{
5579 PyObject *v;
5580 awaitObject *ao;
5581
5582 if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v))
5583 return NULL;
5584
5585 ao = (awaitObject *)type->tp_alloc(type, 0);
5586 if (ao == NULL) {
5587 return NULL;
5588 }
5589
5590 Py_INCREF(v);
5591 ao->ao_iterator = v;
5592
5593 return (PyObject *)ao;
5594}
5595
5596
5597static void
5598awaitObject_dealloc(awaitObject *ao)
5599{
5600 Py_CLEAR(ao->ao_iterator);
5601 Py_TYPE(ao)->tp_free(ao);
5602}
5603
5604
5605static PyObject *
5606awaitObject_await(awaitObject *ao)
5607{
5608 Py_INCREF(ao->ao_iterator);
5609 return ao->ao_iterator;
5610}
5611
5612static PyAsyncMethods awaitType_as_async = {
Yury Selivanov6ef05902015-05-28 11:21:31 -04005613 (unaryfunc)awaitObject_await, /* am_await */
Yury Selivanov75445082015-05-11 22:57:16 -04005614 0, /* am_aiter */
5615 0 /* am_anext */
5616};
5617
5618
5619static PyTypeObject awaitType = {
5620 PyVarObject_HEAD_INIT(NULL, 0)
5621 "awaitType",
5622 sizeof(awaitObject), /* tp_basicsize */
5623 0, /* tp_itemsize */
5624 (destructor)awaitObject_dealloc, /* destructor tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005625 0, /* tp_vectorcall_offset */
Yury Selivanov75445082015-05-11 22:57:16 -04005626 0, /* tp_getattr */
5627 0, /* tp_setattr */
5628 &awaitType_as_async, /* tp_as_async */
5629 0, /* tp_repr */
5630 0, /* tp_as_number */
5631 0, /* tp_as_sequence */
5632 0, /* tp_as_mapping */
5633 0, /* tp_hash */
5634 0, /* tp_call */
5635 0, /* tp_str */
5636 PyObject_GenericGetAttr, /* tp_getattro */
5637 PyObject_GenericSetAttr, /* tp_setattro */
5638 0, /* tp_as_buffer */
5639 0, /* tp_flags */
5640 "C level type with tp_as_async",
5641 0, /* traverseproc tp_traverse */
5642 0, /* tp_clear */
5643 0, /* tp_richcompare */
5644 0, /* tp_weaklistoffset */
5645 0, /* tp_iter */
5646 0, /* tp_iternext */
5647 0, /* tp_methods */
5648 0, /* tp_members */
5649 0,
5650 0,
5651 0,
5652 0,
5653 0,
5654 0,
5655 0,
5656 0,
5657 awaitObject_new, /* tp_new */
5658 PyObject_Del, /* tp_free */
5659};
5660
5661
xdegaye56d1f5c2017-10-26 15:09:06 +02005662static int recurse_infinitely_error_init(PyObject *, PyObject *, PyObject *);
5663
5664static PyTypeObject PyRecursingInfinitelyError_Type = {
5665 PyVarObject_HEAD_INIT(NULL, 0)
5666 "RecursingInfinitelyError", /* tp_name */
5667 sizeof(PyBaseExceptionObject), /* tp_basicsize */
5668 0, /* tp_itemsize */
5669 0, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005670 0, /* tp_vectorcall_offset */
xdegaye56d1f5c2017-10-26 15:09:06 +02005671 0, /* tp_getattr */
5672 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005673 0, /* tp_as_async */
xdegaye56d1f5c2017-10-26 15:09:06 +02005674 0, /* tp_repr */
5675 0, /* tp_as_number */
5676 0, /* tp_as_sequence */
5677 0, /* tp_as_mapping */
5678 0, /* tp_hash */
5679 0, /* tp_call */
5680 0, /* tp_str */
5681 0, /* tp_getattro */
5682 0, /* tp_setattro */
5683 0, /* tp_as_buffer */
5684 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
5685 "Instantiating this exception starts infinite recursion.", /* tp_doc */
5686 0, /* tp_traverse */
5687 0, /* tp_clear */
5688 0, /* tp_richcompare */
5689 0, /* tp_weaklistoffset */
5690 0, /* tp_iter */
5691 0, /* tp_iternext */
5692 0, /* tp_methods */
5693 0, /* tp_members */
5694 0, /* tp_getset */
5695 0, /* tp_base */
5696 0, /* tp_dict */
5697 0, /* tp_descr_get */
5698 0, /* tp_descr_set */
5699 0, /* tp_dictoffset */
5700 (initproc)recurse_infinitely_error_init, /* tp_init */
5701 0, /* tp_alloc */
5702 0, /* tp_new */
5703};
5704
5705static int
5706recurse_infinitely_error_init(PyObject *self, PyObject *args, PyObject *kwds)
5707{
5708 PyObject *type = (PyObject *)&PyRecursingInfinitelyError_Type;
5709
5710 /* Instantiating this exception starts infinite recursion. */
5711 Py_INCREF(type);
5712 PyErr_SetObject(type, NULL);
5713 return -1;
5714}
5715
5716
Jeroen Demeyer351c6742019-05-10 19:21:11 +02005717/* Test bpo-35983: create a subclass of "list" which checks that instances
5718 * are not deallocated twice */
5719
5720typedef struct {
5721 PyListObject list;
5722 int deallocated;
5723} MyListObject;
5724
5725static PyObject *
5726MyList_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5727{
5728 PyObject* op = PyList_Type.tp_new(type, args, kwds);
5729 ((MyListObject*)op)->deallocated = 0;
5730 return op;
5731}
5732
5733void
5734MyList_dealloc(MyListObject* op)
5735{
5736 if (op->deallocated) {
5737 /* We cannot raise exceptions here but we still want the testsuite
5738 * to fail when we hit this */
5739 Py_FatalError("MyList instance deallocated twice");
5740 }
5741 op->deallocated = 1;
5742 PyList_Type.tp_dealloc((PyObject *)op);
5743}
5744
5745static PyTypeObject MyList_Type = {
5746 PyVarObject_HEAD_INIT(NULL, 0)
5747 "MyList",
5748 sizeof(MyListObject),
5749 0,
5750 (destructor)MyList_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005751 0, /* tp_vectorcall_offset */
Jeroen Demeyer351c6742019-05-10 19:21:11 +02005752 0, /* tp_getattr */
5753 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005754 0, /* tp_as_async */
Jeroen Demeyer351c6742019-05-10 19:21:11 +02005755 0, /* tp_repr */
5756 0, /* tp_as_number */
5757 0, /* tp_as_sequence */
5758 0, /* tp_as_mapping */
5759 0, /* tp_hash */
5760 0, /* tp_call */
5761 0, /* tp_str */
5762 0, /* tp_getattro */
5763 0, /* tp_setattro */
5764 0, /* tp_as_buffer */
5765 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
5766 0, /* tp_doc */
5767 0, /* tp_traverse */
5768 0, /* tp_clear */
5769 0, /* tp_richcompare */
5770 0, /* tp_weaklistoffset */
5771 0, /* tp_iter */
5772 0, /* tp_iternext */
5773 0, /* tp_methods */
5774 0, /* tp_members */
5775 0, /* tp_getset */
5776 0, /* &PyList_Type */ /* tp_base */
5777 0, /* tp_dict */
5778 0, /* tp_descr_get */
5779 0, /* tp_descr_set */
5780 0, /* tp_dictoffset */
5781 0, /* tp_init */
5782 0, /* tp_alloc */
5783 MyList_new, /* tp_new */
5784};
5785
5786
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005787/* Test PEP 560 */
5788
5789typedef struct {
5790 PyObject_HEAD
5791 PyObject *item;
5792} PyGenericAliasObject;
5793
5794static void
5795generic_alias_dealloc(PyGenericAliasObject *self)
5796{
5797 Py_CLEAR(self->item);
Victor Stinnere8600892018-01-17 23:08:18 +01005798 Py_TYPE(self)->tp_free((PyObject *)self);
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005799}
5800
5801static PyObject *
5802generic_alias_mro_entries(PyGenericAliasObject *self, PyObject *bases)
5803{
5804 return PyTuple_Pack(1, self->item);
5805}
5806
5807static PyMethodDef generic_alias_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02005808 {"__mro_entries__", (PyCFunction)(void(*)(void))generic_alias_mro_entries, METH_O, NULL},
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005809 {NULL} /* sentinel */
5810};
5811
Benjamin Peterson97ae32c2018-07-03 22:39:09 -07005812static PyTypeObject GenericAlias_Type = {
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005813 PyVarObject_HEAD_INIT(NULL, 0)
5814 "GenericAlias",
5815 sizeof(PyGenericAliasObject),
5816 0,
5817 .tp_dealloc = (destructor)generic_alias_dealloc,
5818 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
5819 .tp_methods = generic_alias_methods,
5820};
5821
5822static PyObject *
5823generic_alias_new(PyObject *item)
5824{
5825 PyGenericAliasObject *o = PyObject_New(PyGenericAliasObject, &GenericAlias_Type);
5826 if (o == NULL) {
5827 return NULL;
5828 }
5829 Py_INCREF(item);
5830 o->item = item;
5831 return (PyObject*) o;
5832}
5833
5834typedef struct {
5835 PyObject_HEAD
5836} PyGenericObject;
5837
5838static PyObject *
Serhiy Storchakace5b0e92018-01-05 00:21:41 +02005839generic_class_getitem(PyObject *type, PyObject *item)
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005840{
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005841 return generic_alias_new(item);
5842}
5843
5844static PyMethodDef generic_methods[] = {
Serhiy Storchakace5b0e92018-01-05 00:21:41 +02005845 {"__class_getitem__", generic_class_getitem, METH_O|METH_CLASS, NULL},
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005846 {NULL} /* sentinel */
5847};
5848
Benjamin Peterson97ae32c2018-07-03 22:39:09 -07005849static PyTypeObject Generic_Type = {
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02005850 PyVarObject_HEAD_INIT(NULL, 0)
5851 "Generic",
5852 sizeof(PyGenericObject),
5853 0,
5854 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
5855 .tp_methods = generic_methods,
5856};
5857
5858
Jeroen Demeyereb65e242019-05-28 14:42:53 +02005859/* Test PEP 590 */
5860
Jeroen Demeyer735e8af2019-05-30 12:43:19 +02005861typedef struct {
5862 PyObject_HEAD
5863 vectorcallfunc vectorcall;
5864} MethodDescriptorObject;
5865
5866static PyObject *
5867MethodDescriptor_vectorcall(PyObject *callable, PyObject *const *args,
5868 size_t nargsf, PyObject *kwnames)
5869{
5870 /* True if using the vectorcall function in MethodDescriptorObject
5871 * but False for MethodDescriptor2Object */
5872 MethodDescriptorObject *md = (MethodDescriptorObject *)callable;
5873 return PyBool_FromLong(md->vectorcall != NULL);
5874}
5875
5876static PyObject *
5877MethodDescriptor_new(PyTypeObject* type, PyObject* args, PyObject *kw)
5878{
Petr Viktorine584cbf2019-06-03 01:08:14 +02005879 MethodDescriptorObject *op = (MethodDescriptorObject *)type->tp_alloc(type, 0);
Jeroen Demeyer735e8af2019-05-30 12:43:19 +02005880 op->vectorcall = MethodDescriptor_vectorcall;
5881 return (PyObject *)op;
5882}
5883
Jeroen Demeyereb65e242019-05-28 14:42:53 +02005884static PyObject *
5885func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
5886{
5887 if (obj == Py_None || obj == NULL) {
5888 Py_INCREF(func);
5889 return func;
5890 }
5891 return PyMethod_New(func, obj);
5892}
5893
5894static PyObject *
5895nop_descr_get(PyObject *func, PyObject *obj, PyObject *type)
5896{
5897 Py_INCREF(func);
5898 return func;
5899}
5900
Jeroen Demeyer735e8af2019-05-30 12:43:19 +02005901static PyObject *
5902call_return_args(PyObject *self, PyObject *args, PyObject *kwargs)
5903{
5904 Py_INCREF(args);
5905 return args;
5906}
5907
Jeroen Demeyereb65e242019-05-28 14:42:53 +02005908static PyTypeObject MethodDescriptorBase_Type = {
5909 PyVarObject_HEAD_INIT(NULL, 0)
5910 "MethodDescriptorBase",
Jeroen Demeyer735e8af2019-05-30 12:43:19 +02005911 sizeof(MethodDescriptorObject),
5912 .tp_new = MethodDescriptor_new,
5913 .tp_call = PyVectorcall_Call,
5914 .tp_vectorcall_offset = offsetof(MethodDescriptorObject, vectorcall),
5915 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
5916 Py_TPFLAGS_METHOD_DESCRIPTOR | _Py_TPFLAGS_HAVE_VECTORCALL,
Jeroen Demeyereb65e242019-05-28 14:42:53 +02005917 .tp_descr_get = func_descr_get,
5918};
5919
5920static PyTypeObject MethodDescriptorDerived_Type = {
5921 PyVarObject_HEAD_INIT(NULL, 0)
5922 "MethodDescriptorDerived",
5923 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
5924};
5925
5926static PyTypeObject MethodDescriptorNopGet_Type = {
5927 PyVarObject_HEAD_INIT(NULL, 0)
5928 "MethodDescriptorNopGet",
5929 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
Jeroen Demeyer735e8af2019-05-30 12:43:19 +02005930 .tp_call = call_return_args,
Jeroen Demeyereb65e242019-05-28 14:42:53 +02005931 .tp_descr_get = nop_descr_get,
5932};
5933
Jeroen Demeyer735e8af2019-05-30 12:43:19 +02005934typedef struct {
5935 MethodDescriptorObject base;
5936 vectorcallfunc vectorcall;
5937} MethodDescriptor2Object;
5938
5939static PyObject *
5940MethodDescriptor2_new(PyTypeObject* type, PyObject* args, PyObject *kw)
5941{
5942 MethodDescriptor2Object *op = PyObject_New(MethodDescriptor2Object, type);
5943 op->base.vectorcall = NULL;
5944 op->vectorcall = MethodDescriptor_vectorcall;
5945 return (PyObject *)op;
5946}
5947
5948static PyTypeObject MethodDescriptor2_Type = {
5949 PyVarObject_HEAD_INIT(NULL, 0)
5950 "MethodDescriptor2",
5951 sizeof(MethodDescriptor2Object),
5952 .tp_new = MethodDescriptor2_new,
5953 .tp_call = PyVectorcall_Call,
5954 .tp_vectorcall_offset = offsetof(MethodDescriptor2Object, vectorcall),
5955 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | _Py_TPFLAGS_HAVE_VECTORCALL,
5956};
5957
Jeroen Demeyereb65e242019-05-28 14:42:53 +02005958
Martin v. Löwis1a214512008-06-11 05:26:20 +00005959static struct PyModuleDef _testcapimodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005960 PyModuleDef_HEAD_INIT,
5961 "_testcapi",
5962 NULL,
5963 -1,
5964 TestMethods,
5965 NULL,
5966 NULL,
5967 NULL,
5968 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00005969};
5970
Nick Coghland5cacbb2015-05-23 22:24:10 +10005971/* Per PEP 489, this module will not be converted to multi-phase initialization
5972 */
5973
Mark Hammond62b1ab12002-07-23 06:31:15 +00005974PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00005975PyInit__testcapi(void)
Tim Peters9ea17ac2001-02-02 05:57:15 +00005976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005977 PyObject *m;
Tim Peters9ea17ac2001-02-02 05:57:15 +00005978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005979 m = PyModule_Create(&_testcapimodule);
5980 if (m == NULL)
5981 return NULL;
Tim Peters9ea17ac2001-02-02 05:57:15 +00005982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005983 Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
Hirokazu Yamamoto8ebab5d2008-12-31 06:05:46 +00005984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005985 Py_TYPE(&test_structmembersType)=&PyType_Type;
5986 Py_INCREF(&test_structmembersType);
5987 /* don't use a name starting with "test", since we don't want
5988 test_capi to automatically call this */
5989 PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
Benjamin Petersond51374e2014-04-09 23:55:56 -04005990 if (PyType_Ready(&matmulType) < 0)
5991 return NULL;
5992 Py_INCREF(&matmulType);
5993 PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType);
Zackery Spytzc7f803b2019-05-31 03:46:36 -06005994 if (PyType_Ready(&ipowType) < 0) {
5995 return NULL;
5996 }
5997 Py_INCREF(&ipowType);
5998 PyModule_AddObject(m, "ipowType", (PyObject *)&ipowType);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005999
Yury Selivanov75445082015-05-11 22:57:16 -04006000 if (PyType_Ready(&awaitType) < 0)
6001 return NULL;
6002 Py_INCREF(&awaitType);
6003 PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType);
6004
Jeroen Demeyer351c6742019-05-10 19:21:11 +02006005 MyList_Type.tp_base = &PyList_Type;
6006 if (PyType_Ready(&MyList_Type) < 0)
6007 return NULL;
6008 Py_INCREF(&MyList_Type);
6009 PyModule_AddObject(m, "MyList", (PyObject *)&MyList_Type);
6010
Jeroen Demeyereb65e242019-05-28 14:42:53 +02006011 if (PyType_Ready(&MethodDescriptorBase_Type) < 0)
6012 return NULL;
6013 Py_INCREF(&MethodDescriptorBase_Type);
6014 PyModule_AddObject(m, "MethodDescriptorBase", (PyObject *)&MethodDescriptorBase_Type);
6015
6016 MethodDescriptorDerived_Type.tp_base = &MethodDescriptorBase_Type;
6017 if (PyType_Ready(&MethodDescriptorDerived_Type) < 0)
6018 return NULL;
6019 Py_INCREF(&MethodDescriptorDerived_Type);
6020 PyModule_AddObject(m, "MethodDescriptorDerived", (PyObject *)&MethodDescriptorDerived_Type);
6021
6022 MethodDescriptorNopGet_Type.tp_base = &MethodDescriptorBase_Type;
6023 if (PyType_Ready(&MethodDescriptorNopGet_Type) < 0)
6024 return NULL;
6025 Py_INCREF(&MethodDescriptorNopGet_Type);
6026 PyModule_AddObject(m, "MethodDescriptorNopGet", (PyObject *)&MethodDescriptorNopGet_Type);
6027
Jeroen Demeyer735e8af2019-05-30 12:43:19 +02006028 MethodDescriptor2_Type.tp_base = &MethodDescriptorBase_Type;
6029 if (PyType_Ready(&MethodDescriptor2_Type) < 0)
6030 return NULL;
6031 Py_INCREF(&MethodDescriptor2_Type);
6032 PyModule_AddObject(m, "MethodDescriptor2", (PyObject *)&MethodDescriptor2_Type);
6033
Serhiy Storchaka45700fb2017-12-16 11:25:56 +02006034 if (PyType_Ready(&GenericAlias_Type) < 0)
6035 return NULL;
6036 Py_INCREF(&GenericAlias_Type);
6037 PyModule_AddObject(m, "GenericAlias", (PyObject *)&GenericAlias_Type);
6038
6039 if (PyType_Ready(&Generic_Type) < 0)
6040 return NULL;
6041 Py_INCREF(&Generic_Type);
6042 PyModule_AddObject(m, "Generic", (PyObject *)&Generic_Type);
6043
xdegaye56d1f5c2017-10-26 15:09:06 +02006044 PyRecursingInfinitelyError_Type.tp_base = (PyTypeObject *)PyExc_Exception;
6045 if (PyType_Ready(&PyRecursingInfinitelyError_Type) < 0) {
6046 return NULL;
6047 }
6048 Py_INCREF(&PyRecursingInfinitelyError_Type);
6049 PyModule_AddObject(m, "RecursingInfinitelyError",
6050 (PyObject *)&PyRecursingInfinitelyError_Type);
6051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006052 PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
6053 PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
6054 PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX));
6055 PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX));
6056 PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN));
6057 PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX));
6058 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
6059 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
6060 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
6061 PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX));
6062 PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN));
6063 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
6064 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
6065 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
6066 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
6067 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
6068 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
6069 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
6070 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
6071 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
6072 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
6073 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
Victor Stinner4237d342015-09-10 10:10:39 +02006074 PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006075 Py_INCREF(&PyInstanceMethod_Type);
6076 PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
Thomas Hellera4ea6032003-04-17 18:55:45 +00006077
Larry Hastings2a727912014-01-16 11:32:01 -08006078 PyModule_AddIntConstant(m, "the_number_three", 3);
Victor Stinner5d39e042017-11-29 17:20:38 +01006079#ifdef WITH_PYMALLOC
6080 PyModule_AddObject(m, "WITH_PYMALLOC", Py_True);
6081#else
6082 PyModule_AddObject(m, "WITH_PYMALLOC", Py_False);
6083#endif
Larry Hastings2a727912014-01-16 11:32:01 -08006084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006085 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
6086 Py_INCREF(TestError);
6087 PyModule_AddObject(m, "error", TestError);
6088 return m;
Tim Peters9ea17ac2001-02-02 05:57:15 +00006089}