| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1 | /* | 
 | 2 |  * C Extension module to test Python interpreter C APIs. | 
 | 3 |  * | 
 | 4 |  * The 'test_*' functions exported by this module are run as part of the | 
 | 5 |  * standard Python regression test, via Lib/test/test_capi.py. | 
 | 6 |  */ | 
 | 7 |  | 
 | 8 | #include "Python.h" | 
 | 9 |  | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 10 | #ifdef WITH_THREAD | 
 | 11 | #include "pythread.h" | 
 | 12 | #endif /* WITH_THREAD */ | 
 | 13 |  | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 14 | static PyObject *TestError;	/* set to exception object in init */ | 
 | 15 |  | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 16 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ | 
 | 17 |  | 
 | 18 | static PyObject * | 
 | 19 | raiseTestError(const char* test_name, const char* msg) | 
 | 20 | { | 
 | 21 | 	char buf[2048]; | 
 | 22 |  | 
 | 23 | 	if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50) | 
 | 24 | 		PyErr_SetString(TestError, "internal error msg too large"); | 
 | 25 | 	else { | 
| Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 26 | 		PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg); | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 27 | 		PyErr_SetString(TestError, buf); | 
 | 28 | 	} | 
 | 29 | 	return NULL; | 
 | 30 | } | 
 | 31 |  | 
| Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 32 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 33 |  | 
 | 34 |    The ones derived from autoconf on the UNIX-like OSes can be relied | 
 | 35 |    upon (in the absence of sloppy cross-compiling), but the Windows | 
 | 36 |    platforms have these hardcoded.  Better safe than sorry. | 
 | 37 | */ | 
 | 38 | static PyObject* | 
 | 39 | sizeof_error(const char* fatname, const char* typename, | 
 | 40 |         int expected, int got) | 
 | 41 | { | 
 | 42 | 	char buf[1024]; | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 43 | 	PyOS_snprintf(buf, sizeof(buf), | 
| Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 44 | 		"%.200s #define == %d but sizeof(%.200s) == %d", | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 45 | 		fatname, expected, typename, got); | 
 | 46 | 	PyErr_SetString(TestError, buf); | 
 | 47 | 	return (PyObject*)NULL; | 
 | 48 | } | 
 | 49 |  | 
 | 50 | static PyObject* | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 51 | test_config(PyObject *self) | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 52 | { | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 53 | #define CHECK_SIZEOF(FATNAME, TYPE) \ | 
 | 54 | 	    if (FATNAME != sizeof(TYPE)) \ | 
 | 55 |     	    	return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) | 
 | 56 |  | 
| Tim Peters | 208efe5 | 2001-06-26 22:40:47 +0000 | [diff] [blame] | 57 | 	CHECK_SIZEOF(SIZEOF_SHORT, short); | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 58 | 	CHECK_SIZEOF(SIZEOF_INT, int); | 
 | 59 | 	CHECK_SIZEOF(SIZEOF_LONG, long); | 
 | 60 | 	CHECK_SIZEOF(SIZEOF_VOID_P, void*); | 
 | 61 | 	CHECK_SIZEOF(SIZEOF_TIME_T, time_t); | 
 | 62 | #ifdef HAVE_LONG_LONG | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 63 | 	CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 64 | #endif | 
 | 65 |  | 
 | 66 | #undef CHECK_SIZEOF | 
 | 67 |  | 
 | 68 | 	Py_INCREF(Py_None); | 
 | 69 | 	return Py_None; | 
 | 70 | } | 
 | 71 |  | 
| Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 72 | static PyObject* | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 73 | test_list_api(PyObject *self) | 
| Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 74 | { | 
 | 75 | 	PyObject* list; | 
 | 76 | 	int i; | 
| Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 77 |  | 
 | 78 | 	/* SF bug 132008:  PyList_Reverse segfaults */ | 
 | 79 | #define NLIST 30 | 
 | 80 | 	list = PyList_New(NLIST); | 
 | 81 | 	if (list == (PyObject*)NULL) | 
 | 82 | 		return (PyObject*)NULL; | 
 | 83 | 	/* list = range(NLIST) */ | 
 | 84 | 	for (i = 0; i < NLIST; ++i) { | 
 | 85 | 		PyObject* anint = PyInt_FromLong(i); | 
 | 86 | 		if (anint == (PyObject*)NULL) { | 
 | 87 | 			Py_DECREF(list); | 
 | 88 | 			return (PyObject*)NULL; | 
 | 89 | 		} | 
 | 90 | 		PyList_SET_ITEM(list, i, anint); | 
 | 91 | 	} | 
 | 92 | 	/* list.reverse(), via PyList_Reverse() */ | 
 | 93 | 	i = PyList_Reverse(list);   /* should not blow up! */ | 
 | 94 | 	if (i != 0) { | 
 | 95 | 		Py_DECREF(list); | 
 | 96 | 		return (PyObject*)NULL; | 
 | 97 | 	} | 
 | 98 | 	/* Check that list == range(29, -1, -1) now */ | 
 | 99 | 	for (i = 0; i < NLIST; ++i) { | 
 | 100 | 		PyObject* anint = PyList_GET_ITEM(list, i); | 
 | 101 | 		if (PyInt_AS_LONG(anint) != NLIST-1-i) { | 
 | 102 | 			PyErr_SetString(TestError, | 
 | 103 | 			                "test_list_api: reverse screwed up"); | 
 | 104 | 			Py_DECREF(list); | 
 | 105 | 			return (PyObject*)NULL; | 
 | 106 | 		} | 
 | 107 | 	} | 
 | 108 | 	Py_DECREF(list); | 
 | 109 | #undef NLIST | 
 | 110 |  | 
 | 111 | 	Py_INCREF(Py_None); | 
 | 112 | 	return Py_None; | 
 | 113 | } | 
 | 114 |  | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 115 | static int | 
 | 116 | test_dict_inner(int count) | 
 | 117 | { | 
 | 118 | 	int pos = 0, iterations = 0, i; | 
 | 119 | 	PyObject *dict = PyDict_New(); | 
 | 120 | 	PyObject *v, *k; | 
 | 121 |  | 
 | 122 | 	if (dict == NULL) | 
 | 123 | 		return -1; | 
 | 124 |  | 
 | 125 | 	for (i = 0; i < count; i++) { | 
 | 126 | 		v = PyInt_FromLong(i); | 
 | 127 | 		PyDict_SetItem(dict, v, v); | 
 | 128 | 		Py_DECREF(v); | 
 | 129 | 	} | 
 | 130 |  | 
 | 131 | 	while (PyDict_Next(dict, &pos, &k, &v)) { | 
 | 132 | 		PyObject *o; | 
 | 133 | 		iterations++; | 
 | 134 |  | 
 | 135 | 		i = PyInt_AS_LONG(v) + 1; | 
 | 136 | 		o = PyInt_FromLong(i); | 
 | 137 | 		if (o == NULL) | 
 | 138 | 			return -1; | 
 | 139 | 		if (PyDict_SetItem(dict, k, o) < 0) { | 
 | 140 | 			Py_DECREF(o); | 
 | 141 | 			return -1; | 
 | 142 | 		} | 
 | 143 | 		Py_DECREF(o); | 
 | 144 | 	} | 
 | 145 |  | 
 | 146 | 	Py_DECREF(dict); | 
 | 147 |  | 
 | 148 | 	if (iterations != count) { | 
 | 149 | 		PyErr_SetString( | 
 | 150 | 			TestError, | 
 | 151 | 			"test_dict_iteration: dict iteration went wrong "); | 
 | 152 | 		return -1; | 
 | 153 | 	} else { | 
 | 154 | 		return 0; | 
 | 155 | 	} | 
 | 156 | } | 
 | 157 |  | 
 | 158 | static PyObject* | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 159 | test_dict_iteration(PyObject* self) | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 160 | { | 
 | 161 | 	int i; | 
 | 162 |  | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 163 | 	for (i = 0; i < 200; i++) { | 
 | 164 | 		if (test_dict_inner(i) < 0) { | 
 | 165 | 			return NULL; | 
 | 166 | 		} | 
 | 167 | 	} | 
 | 168 |  | 
 | 169 | 	Py_INCREF(Py_None); | 
 | 170 | 	return Py_None; | 
 | 171 | } | 
 | 172 |  | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 173 |  | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 174 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) | 
| Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 175 |    PyLong_{As, From}{Unsigned,}LongLong(). | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 176 |  | 
 | 177 |    Note that the meat of the test is contained in testcapi_long.h. | 
 | 178 |    This is revolting, but delicate code duplication is worse:  "almost | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 179 |    exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 180 |    dependence on type names makes it impossible to use a parameterized | 
 | 181 |    function.  A giant macro would be even worse than this.  A C++ template | 
 | 182 |    would be perfect. | 
 | 183 |  | 
 | 184 |    The "report an error" functions are deliberately not part of the #include | 
 | 185 |    file:  if the test fails, you can set a breakpoint in the appropriate | 
 | 186 |    error function directly, and crawl back from there in the debugger. | 
 | 187 | */ | 
 | 188 |  | 
 | 189 | #define UNBIND(X)  Py_DECREF(X); (X) = NULL | 
 | 190 |  | 
 | 191 | static PyObject * | 
 | 192 | raise_test_long_error(const char* msg) | 
 | 193 | { | 
 | 194 | 	return raiseTestError("test_long_api", msg); | 
 | 195 | } | 
 | 196 |  | 
 | 197 | #define TESTNAME	test_long_api_inner | 
 | 198 | #define TYPENAME	long | 
 | 199 | #define F_S_TO_PY	PyLong_FromLong | 
 | 200 | #define F_PY_TO_S	PyLong_AsLong | 
 | 201 | #define F_U_TO_PY	PyLong_FromUnsignedLong | 
 | 202 | #define F_PY_TO_U	PyLong_AsUnsignedLong | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 203 |  | 
 | 204 | #include "testcapi_long.h" | 
 | 205 |  | 
 | 206 | static PyObject * | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 207 | test_long_api(PyObject* self) | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 208 | { | 
| Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 209 | 	return TESTNAME(raise_test_long_error); | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 210 | } | 
 | 211 |  | 
 | 212 | #undef TESTNAME | 
 | 213 | #undef TYPENAME | 
 | 214 | #undef F_S_TO_PY | 
 | 215 | #undef F_PY_TO_S | 
 | 216 | #undef F_U_TO_PY | 
 | 217 | #undef F_PY_TO_U | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 218 |  | 
 | 219 | #ifdef HAVE_LONG_LONG | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 220 |  | 
 | 221 | static PyObject * | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 222 | raise_test_longlong_error(const char* msg) | 
 | 223 | { | 
 | 224 | 	return raiseTestError("test_longlong_api", msg); | 
 | 225 | } | 
 | 226 |  | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 227 | #define TESTNAME	test_longlong_api_inner | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 228 | #define TYPENAME	PY_LONG_LONG | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 229 | #define F_S_TO_PY	PyLong_FromLongLong | 
 | 230 | #define F_PY_TO_S	PyLong_AsLongLong | 
 | 231 | #define F_U_TO_PY	PyLong_FromUnsignedLongLong | 
 | 232 | #define F_PY_TO_U	PyLong_AsUnsignedLongLong | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 233 |  | 
 | 234 | #include "testcapi_long.h" | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 235 |  | 
 | 236 | static PyObject * | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 237 | test_longlong_api(PyObject* self) | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 238 | { | 
| Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 239 | 	return TESTNAME(raise_test_longlong_error); | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 240 | } | 
 | 241 |  | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 242 | #undef TESTNAME | 
 | 243 | #undef TYPENAME | 
 | 244 | #undef F_S_TO_PY | 
 | 245 | #undef F_PY_TO_S | 
 | 246 | #undef F_U_TO_PY | 
 | 247 | #undef F_PY_TO_U | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 248 |  | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 249 | /* Test the L code for PyArg_ParseTuple.  This should deliver a PY_LONG_LONG | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 250 |    for both long and int arguments.  The test may leak a little memory if | 
 | 251 |    it fails. | 
 | 252 | */ | 
 | 253 | static PyObject * | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 254 | test_L_code(PyObject *self) | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 255 | { | 
 | 256 | 	PyObject *tuple, *num; | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 257 | 	PY_LONG_LONG value; | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 258 |  | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 259 |         tuple = PyTuple_New(1); | 
 | 260 |         if (tuple == NULL) | 
 | 261 |         	return NULL; | 
 | 262 |  | 
 | 263 |         num = PyLong_FromLong(42); | 
 | 264 |         if (num == NULL) | 
 | 265 |         	return NULL; | 
 | 266 |  | 
 | 267 |         PyTuple_SET_ITEM(tuple, 0, num); | 
 | 268 |  | 
 | 269 |         value = -1; | 
 | 270 |         if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) | 
 | 271 |         	return NULL; | 
 | 272 |         if (value != 42) | 
 | 273 |         	return raiseTestError("test_L_code", | 
 | 274 | 			"L code returned wrong value for long 42"); | 
 | 275 |  | 
 | 276 | 	Py_DECREF(num); | 
 | 277 |         num = PyInt_FromLong(42); | 
 | 278 |         if (num == NULL) | 
 | 279 |         	return NULL; | 
 | 280 |  | 
 | 281 |         PyTuple_SET_ITEM(tuple, 0, num); | 
 | 282 |  | 
 | 283 | 	value = -1; | 
 | 284 |         if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) | 
 | 285 |         	return NULL; | 
 | 286 |         if (value != 42) | 
 | 287 |         	return raiseTestError("test_L_code", | 
 | 288 | 			"L code returned wrong value for int 42"); | 
 | 289 |  | 
 | 290 | 	Py_DECREF(tuple); | 
 | 291 | 	Py_INCREF(Py_None); | 
 | 292 | 	return Py_None; | 
 | 293 | } | 
 | 294 |  | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 295 | #endif	/* ifdef HAVE_LONG_LONG */ | 
 | 296 |  | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 297 | /* Functions to call PyArg_ParseTuple with integer format codes, | 
 | 298 |    and return the result. | 
 | 299 | */ | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 300 | static PyObject * | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 301 | getargs_b(PyObject *self, PyObject *args) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 302 | { | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 303 | 	unsigned char value; | 
 | 304 | 	if (!PyArg_ParseTuple(args, "b", &value)) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 305 | 		return NULL; | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 306 | 	return PyLong_FromUnsignedLong((unsigned long)value); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 307 | } | 
 | 308 |  | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 309 | static PyObject * | 
 | 310 | getargs_B(PyObject *self, PyObject *args) | 
 | 311 | { | 
 | 312 | 	unsigned char value; | 
 | 313 | 	if (!PyArg_ParseTuple(args, "B", &value)) | 
 | 314 | 		return NULL; | 
 | 315 | 	return PyLong_FromUnsignedLong((unsigned long)value); | 
 | 316 | } | 
 | 317 |  | 
 | 318 | static PyObject * | 
 | 319 | getargs_H(PyObject *self, PyObject *args) | 
 | 320 | { | 
 | 321 | 	unsigned short value; | 
 | 322 | 	if (!PyArg_ParseTuple(args, "H", &value)) | 
 | 323 | 		return NULL; | 
 | 324 | 	return PyLong_FromUnsignedLong((unsigned long)value); | 
 | 325 | } | 
 | 326 |  | 
 | 327 | static PyObject * | 
 | 328 | getargs_I(PyObject *self, PyObject *args) | 
 | 329 | { | 
 | 330 | 	unsigned int value; | 
 | 331 | 	if (!PyArg_ParseTuple(args, "I", &value)) | 
 | 332 | 		return NULL; | 
 | 333 | 	return PyLong_FromUnsignedLong((unsigned long)value); | 
 | 334 | } | 
 | 335 |  | 
 | 336 | static PyObject * | 
 | 337 | getargs_k(PyObject *self, PyObject *args) | 
 | 338 | { | 
 | 339 | 	unsigned long value; | 
 | 340 | 	if (!PyArg_ParseTuple(args, "k", &value)) | 
 | 341 | 		return NULL; | 
 | 342 | 	return PyLong_FromUnsignedLong(value); | 
 | 343 | } | 
 | 344 |  | 
 | 345 | static PyObject * | 
 | 346 | getargs_i(PyObject *self, PyObject *args) | 
 | 347 | { | 
 | 348 | 	int value; | 
 | 349 | 	if (!PyArg_ParseTuple(args, "i", &value)) | 
 | 350 | 		return NULL; | 
 | 351 | 	return PyLong_FromLong((long)value); | 
 | 352 | } | 
 | 353 |  | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 354 | static PyObject * | 
 | 355 | getargs_l(PyObject *self, PyObject *args) | 
 | 356 | { | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 357 | 	long value; | 
 | 358 | 	if (!PyArg_ParseTuple(args, "l", &value)) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 359 | 		return NULL; | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 360 | 	return PyLong_FromLong(value); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 361 | } | 
 | 362 |  | 
 | 363 | #ifdef HAVE_LONG_LONG | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 364 | static PyObject * | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 365 | getargs_L(PyObject *self, PyObject *args) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 366 | { | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 367 | 	PY_LONG_LONG value; | 
 | 368 | 	if (!PyArg_ParseTuple(args, "L", &value)) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 369 | 		return NULL; | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 370 | 	return PyLong_FromLongLong(value); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 371 | } | 
 | 372 |  | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 373 | static PyObject * | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 374 | getargs_K(PyObject *self, PyObject *args) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 375 | { | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 376 | 	unsigned PY_LONG_LONG value; | 
 | 377 | 	if (!PyArg_ParseTuple(args, "K", &value)) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 378 | 		return NULL; | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 379 | 	return PyLong_FromUnsignedLongLong(value); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 380 | } | 
 | 381 | #endif | 
 | 382 |  | 
 | 383 | /* This function not only tests the 'k' getargs code, but also the | 
 | 384 |    PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */ | 
 | 385 | static PyObject * | 
 | 386 | test_k_code(PyObject *self) | 
 | 387 | { | 
 | 388 | 	PyObject *tuple, *num; | 
 | 389 | 	unsigned long value; | 
 | 390 |  | 
 | 391 |         tuple = PyTuple_New(1); | 
 | 392 |         if (tuple == NULL) | 
 | 393 |         	return NULL; | 
 | 394 |  | 
| Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 395 | 	/* a number larger than ULONG_MAX even on 64-bit platforms */ | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 396 |         num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); | 
 | 397 |         if (num == NULL) | 
 | 398 |         	return NULL; | 
 | 399 |  | 
 | 400 | 	value = PyInt_AsUnsignedLongMask(num); | 
| Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 401 | 	if (value != ULONG_MAX) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 402 |         	return raiseTestError("test_k_code", | 
 | 403 | 	    "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); | 
 | 404 |  | 
 | 405 |         PyTuple_SET_ITEM(tuple, 0, num); | 
 | 406 |  | 
 | 407 |         value = -1; | 
 | 408 |         if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) | 
 | 409 |         	return NULL; | 
| Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 410 |         if (value != ULONG_MAX) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 411 |         	return raiseTestError("test_k_code", | 
 | 412 | 			"k code returned wrong value for long 0xFFF...FFF"); | 
 | 413 |  | 
 | 414 | 	Py_DECREF(num); | 
 | 415 |         num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); | 
 | 416 |         if (num == NULL) | 
 | 417 |         	return NULL; | 
 | 418 |  | 
 | 419 | 	value = PyInt_AsUnsignedLongMask(num); | 
 | 420 | 	if (value != (unsigned long)-0x42) | 
 | 421 |         	return raiseTestError("test_k_code", | 
 | 422 | 	    "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); | 
 | 423 |  | 
 | 424 |         PyTuple_SET_ITEM(tuple, 0, num); | 
 | 425 |  | 
 | 426 | 	value = -1; | 
 | 427 |         if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) | 
 | 428 |         	return NULL; | 
 | 429 |         if (value != (unsigned long)-0x42) | 
 | 430 |         	return raiseTestError("test_k_code", | 
 | 431 | 			"k code returned wrong value for long -0xFFF..000042"); | 
 | 432 |  | 
 | 433 | 	Py_DECREF(tuple); | 
 | 434 | 	Py_INCREF(Py_None); | 
 | 435 | 	return Py_None; | 
 | 436 | } | 
 | 437 |  | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 438 | #ifdef Py_USING_UNICODE | 
 | 439 |  | 
 | 440 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case | 
 | 441 |    of an error. | 
 | 442 | */ | 
 | 443 | static PyObject * | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 444 | test_u_code(PyObject *self) | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 445 | { | 
 | 446 | 	PyObject *tuple, *obj; | 
 | 447 | 	Py_UNICODE *value; | 
 | 448 | 	int len; | 
 | 449 |  | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 450 |         tuple = PyTuple_New(1); | 
 | 451 |         if (tuple == NULL) | 
 | 452 |         	return NULL; | 
 | 453 |  | 
 | 454 |         obj = PyUnicode_Decode("test", strlen("test"), | 
 | 455 | 			       "ascii", NULL); | 
 | 456 |         if (obj == NULL) | 
 | 457 |         	return NULL; | 
 | 458 |  | 
 | 459 |         PyTuple_SET_ITEM(tuple, 0, obj); | 
 | 460 |  | 
 | 461 |         value = 0; | 
 | 462 |         if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) | 
 | 463 |         	return NULL; | 
 | 464 |         if (value != PyUnicode_AS_UNICODE(obj)) | 
 | 465 |         	return raiseTestError("test_u_code", | 
 | 466 | 			"u code returned wrong value for u'test'"); | 
 | 467 |         value = 0; | 
 | 468 |         if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) | 
 | 469 |         	return NULL; | 
 | 470 |         if (value != PyUnicode_AS_UNICODE(obj) || | 
 | 471 | 	    len != PyUnicode_GET_SIZE(obj)) | 
 | 472 |         	return raiseTestError("test_u_code", | 
 | 473 | 			"u# code returned wrong values for u'test'"); | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 474 |  | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 475 | 	Py_DECREF(tuple); | 
 | 476 | 	Py_INCREF(Py_None); | 
 | 477 | 	return Py_None; | 
 | 478 | } | 
 | 479 |  | 
 | 480 | #endif | 
 | 481 |  | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 482 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 483 | static PyObject * | 
 | 484 | test_long_numbits(PyObject *self) | 
 | 485 | { | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 486 | 	struct triple { | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 487 | 		long input; | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 488 | 		size_t nbits; | 
 | 489 | 		int sign; | 
 | 490 | 	} testcases[] = {{0, 0, 0}, | 
 | 491 | 			 {1L, 1, 1}, | 
 | 492 | 			 {-1L, 1, -1}, | 
 | 493 | 			 {2L, 2, 1}, | 
 | 494 | 			 {-2L, 2, -1}, | 
 | 495 | 			 {3L, 2, 1}, | 
 | 496 | 			 {-3L, 2, -1}, | 
 | 497 | 			 {4L, 3, 1}, | 
 | 498 | 			 {-4L, 3, -1}, | 
 | 499 | 			 {0x7fffL, 15, 1},	/* one Python long digit */ | 
 | 500 | 			 {-0x7fffL, 15, -1}, | 
 | 501 | 			 {0xffffL, 16, 1}, | 
 | 502 | 			 {-0xffffL, 16, -1}, | 
 | 503 | 			 {0xfffffffL, 28, 1}, | 
 | 504 | 			 {-0xfffffffL, 28, -1}}; | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 505 | 	int i; | 
 | 506 |  | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 507 | 	for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { | 
 | 508 | 		PyObject *plong = PyLong_FromLong(testcases[i].input); | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 509 | 		size_t nbits = _PyLong_NumBits(plong); | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 510 | 		int sign = _PyLong_Sign(plong); | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 511 |  | 
 | 512 | 		Py_DECREF(plong); | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 513 | 		if (nbits != testcases[i].nbits) | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 514 | 			return raiseTestError("test_long_numbits", | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 515 | 					"wrong result for _PyLong_NumBits"); | 
 | 516 | 		if (sign != testcases[i].sign) | 
 | 517 | 			return raiseTestError("test_long_numbits", | 
 | 518 | 					"wrong result for _PyLong_Sign"); | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 519 | 	} | 
 | 520 | 	Py_INCREF(Py_None); | 
 | 521 | 	return Py_None; | 
 | 522 | } | 
 | 523 |  | 
| Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 524 | static PyObject * | 
 | 525 | raise_exception(PyObject *self, PyObject *args) | 
 | 526 | { | 
 | 527 | 	PyObject *exc; | 
 | 528 | 	PyObject *exc_args, *v; | 
 | 529 | 	int num_args, i; | 
 | 530 |  | 
 | 531 | 	if (!PyArg_ParseTuple(args, "Oi:raise_exception", | 
 | 532 | 			      &exc, &num_args)) | 
 | 533 | 		return NULL; | 
 | 534 |  | 
 | 535 | 	exc_args = PyTuple_New(num_args); | 
 | 536 | 	if (exc_args == NULL) | 
 | 537 | 		return NULL; | 
 | 538 | 	for (i = 0; i < num_args; ++i) { | 
 | 539 | 		v = PyInt_FromLong(i); | 
 | 540 | 		if (v == NULL) { | 
 | 541 | 			Py_DECREF(exc_args); | 
 | 542 | 			return NULL; | 
 | 543 | 		} | 
 | 544 | 		PyTuple_SET_ITEM(exc_args, i, v); | 
 | 545 | 	} | 
 | 546 | 	PyErr_SetObject(exc, exc_args); | 
| Michael W. Hudson | f02bcee | 2003-08-15 13:03:30 +0000 | [diff] [blame] | 547 | 	Py_DECREF(exc_args); | 
| Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 548 | 	return NULL; | 
 | 549 | } | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 550 |  | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 551 | #ifdef WITH_THREAD | 
 | 552 |  | 
 | 553 | void _make_call(void *callable) | 
 | 554 | { | 
 | 555 | 	PyObject *rc; | 
 | 556 | 	PyGILState_STATE s = PyGILState_Ensure(); | 
 | 557 | 	rc = PyObject_CallFunction(callable, ""); | 
 | 558 | 	Py_XDECREF(rc); | 
 | 559 | 	PyGILState_Release(s); | 
 | 560 | } | 
 | 561 |  | 
 | 562 | static PyObject * | 
 | 563 | test_thread_state(PyObject *self, PyObject *args) | 
 | 564 | { | 
 | 565 | 	PyObject *fn; | 
 | 566 | 	if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) | 
 | 567 | 		return NULL; | 
 | 568 | 	/* Ensure Python is setup for threading */ | 
 | 569 | 	PyEval_InitThreads(); | 
 | 570 | 	/* Start a new thread for our callback. */ | 
 | 571 | 	PyThread_start_new_thread( _make_call, fn); | 
 | 572 | 	/* Make the callback with the thread lock held by this thread */ | 
 | 573 | 	_make_call(fn); | 
 | 574 | 	/* Do it all again, but this time with the thread-lock released */ | 
 | 575 | 	Py_BEGIN_ALLOW_THREADS | 
 | 576 | 	_make_call(fn); | 
 | 577 | 	Py_END_ALLOW_THREADS | 
 | 578 | 	/* And once more with and without a thread | 
 | 579 | 	   XXX - should use a lock and work out exactly what we are trying  | 
 | 580 | 	   to test <wink>  | 
 | 581 | 	*/ | 
 | 582 | 	Py_BEGIN_ALLOW_THREADS | 
 | 583 | 	PyThread_start_new_thread( _make_call, fn); | 
 | 584 | 	_make_call(fn); | 
 | 585 | 	Py_END_ALLOW_THREADS | 
 | 586 | 	Py_INCREF(Py_None); | 
 | 587 | 	return Py_None; | 
 | 588 | } | 
 | 589 | #endif | 
 | 590 |  | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 591 | static PyMethodDef TestMethods[] = { | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 592 | 	{"raise_exception",	raise_exception,		 METH_VARARGS}, | 
 | 593 | 	{"test_config",		(PyCFunction)test_config,	 METH_NOARGS}, | 
 | 594 | 	{"test_list_api",	(PyCFunction)test_list_api,	 METH_NOARGS}, | 
 | 595 | 	{"test_dict_iteration",	(PyCFunction)test_dict_iteration,METH_NOARGS}, | 
 | 596 | 	{"test_long_api",	(PyCFunction)test_long_api,	 METH_NOARGS}, | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 597 | 	{"test_long_numbits",	(PyCFunction)test_long_numbits,	 METH_NOARGS}, | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 598 | 	{"test_k_code",		(PyCFunction)test_k_code,	 METH_NOARGS}, | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 599 |  | 
 | 600 | 	{"getargs_b",		(PyCFunction)getargs_b,		 METH_VARARGS}, | 
 | 601 | 	{"getargs_B",		(PyCFunction)getargs_B,		 METH_VARARGS}, | 
 | 602 | 	{"getargs_H",		(PyCFunction)getargs_H,		 METH_VARARGS}, | 
 | 603 | 	{"getargs_I",		(PyCFunction)getargs_I,		 METH_VARARGS}, | 
 | 604 | 	{"getargs_k",		(PyCFunction)getargs_k,		 METH_VARARGS}, | 
 | 605 | 	{"getargs_i",		(PyCFunction)getargs_i,		 METH_VARARGS}, | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 606 | 	{"getargs_l",		(PyCFunction)getargs_l,		 METH_VARARGS}, | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 607 | #ifdef HAVE_LONG_LONG | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 608 | 	{"getargs_L",		(PyCFunction)getargs_L,		 METH_VARARGS}, | 
 | 609 | 	{"getargs_K",		(PyCFunction)getargs_K,		 METH_VARARGS}, | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 610 | 	{"test_longlong_api",	(PyCFunction)test_longlong_api,	 METH_NOARGS}, | 
 | 611 | 	{"test_L_code",		(PyCFunction)test_L_code,	 METH_NOARGS}, | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 612 | #endif | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 613 | #ifdef Py_USING_UNICODE | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 614 | 	{"test_u_code",		(PyCFunction)test_u_code,	 METH_NOARGS}, | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 615 | #endif | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 616 | #ifdef WITH_THREAD | 
 | 617 | 	{"_test_thread_state", (PyCFunction)test_thread_state, METH_VARARGS}, | 
 | 618 | #endif | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 619 | 	{NULL, NULL} /* sentinel */ | 
 | 620 | }; | 
 | 621 |  | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 622 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} | 
 | 623 |  | 
| Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 624 | PyMODINIT_FUNC | 
| Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 625 | init_testcapi(void) | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 626 | { | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 627 | 	PyObject *m; | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 628 |  | 
| Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 629 | 	m = Py_InitModule("_testcapi", TestMethods); | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 630 |  | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 631 | 	PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX)); | 
 | 632 | 	PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX)); | 
 | 633 | 	PyModule_AddObject(m, "UINT_MAX",  PyLong_FromUnsignedLong(UINT_MAX)); | 
 | 634 | 	PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); | 
 | 635 | 	PyModule_AddObject(m, "INT_MIN", PyInt_FromLong(INT_MIN)); | 
 | 636 | 	PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN)); | 
 | 637 | 	PyModule_AddObject(m, "INT_MAX", PyInt_FromLong(INT_MAX)); | 
 | 638 | 	PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX)); | 
 | 639 |  | 
| Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 640 | 	TestError = PyErr_NewException("_testcapi.error", NULL, NULL); | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 641 | 	Py_INCREF(TestError); | 
 | 642 | 	PyModule_AddObject(m, "error", TestError); | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 643 | } |