symbian-qemu-0.9.1-12/python-2.6.1/Modules/_testcapimodule.c
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     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 #include <float.h>
       
    10 #include "structmember.h"
       
    11 
       
    12 #ifdef WITH_THREAD
       
    13 #include "pythread.h"
       
    14 #endif /* WITH_THREAD */
       
    15 static PyObject *TestError;	/* set to exception object in init */
       
    16 
       
    17 /* Raise TestError with test_name + ": " + msg, and return NULL. */
       
    18 
       
    19 static PyObject *
       
    20 raiseTestError(const char* test_name, const char* msg)
       
    21 {
       
    22 	char buf[2048];
       
    23 
       
    24 	if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
       
    25 		PyErr_SetString(TestError, "internal error msg too large");
       
    26 	else {
       
    27 		PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
       
    28 		PyErr_SetString(TestError, buf);
       
    29 	}
       
    30 	return NULL;
       
    31 }
       
    32 
       
    33 /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
       
    34 
       
    35    The ones derived from autoconf on the UNIX-like OSes can be relied
       
    36    upon (in the absence of sloppy cross-compiling), but the Windows
       
    37    platforms have these hardcoded.  Better safe than sorry.
       
    38 */
       
    39 static PyObject*
       
    40 sizeof_error(const char* fatname, const char* typname,
       
    41         int expected, int got)
       
    42 {
       
    43 	char buf[1024];
       
    44 	PyOS_snprintf(buf, sizeof(buf),
       
    45 		"%.200s #define == %d but sizeof(%.200s) == %d",
       
    46 		fatname, expected, typname, got);
       
    47 	PyErr_SetString(TestError, buf);
       
    48 	return (PyObject*)NULL;
       
    49 }
       
    50 
       
    51 static PyObject*
       
    52 test_config(PyObject *self)
       
    53 {
       
    54 #define CHECK_SIZEOF(FATNAME, TYPE) \
       
    55 	    if (FATNAME != sizeof(TYPE)) \
       
    56     	    	return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
       
    57 
       
    58 	CHECK_SIZEOF(SIZEOF_SHORT, short);
       
    59 	CHECK_SIZEOF(SIZEOF_INT, int);
       
    60 	CHECK_SIZEOF(SIZEOF_LONG, long);
       
    61 	CHECK_SIZEOF(SIZEOF_VOID_P, void*);
       
    62 	CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
       
    63 #ifdef HAVE_LONG_LONG
       
    64 	CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
       
    65 #endif
       
    66 
       
    67 #undef CHECK_SIZEOF
       
    68 
       
    69 	Py_INCREF(Py_None);
       
    70 	return Py_None;
       
    71 }
       
    72 
       
    73 static PyObject*
       
    74 test_list_api(PyObject *self)
       
    75 {
       
    76 	PyObject* list;
       
    77 	int i;
       
    78 
       
    79 	/* SF bug 132008:  PyList_Reverse segfaults */
       
    80 #define NLIST 30
       
    81 	list = PyList_New(NLIST);
       
    82 	if (list == (PyObject*)NULL)
       
    83 		return (PyObject*)NULL;
       
    84 	/* list = range(NLIST) */
       
    85 	for (i = 0; i < NLIST; ++i) {
       
    86 		PyObject* anint = PyInt_FromLong(i);
       
    87 		if (anint == (PyObject*)NULL) {
       
    88 			Py_DECREF(list);
       
    89 			return (PyObject*)NULL;
       
    90 		}
       
    91 		PyList_SET_ITEM(list, i, anint);
       
    92 	}
       
    93 	/* list.reverse(), via PyList_Reverse() */
       
    94 	i = PyList_Reverse(list);   /* should not blow up! */
       
    95 	if (i != 0) {
       
    96 		Py_DECREF(list);
       
    97 		return (PyObject*)NULL;
       
    98 	}
       
    99 	/* Check that list == range(29, -1, -1) now */
       
   100 	for (i = 0; i < NLIST; ++i) {
       
   101 		PyObject* anint = PyList_GET_ITEM(list, i);
       
   102 		if (PyInt_AS_LONG(anint) != NLIST-1-i) {
       
   103 			PyErr_SetString(TestError,
       
   104 			                "test_list_api: reverse screwed up");
       
   105 			Py_DECREF(list);
       
   106 			return (PyObject*)NULL;
       
   107 		}
       
   108 	}
       
   109 	Py_DECREF(list);
       
   110 #undef NLIST
       
   111 
       
   112 	Py_INCREF(Py_None);
       
   113 	return Py_None;
       
   114 }
       
   115 
       
   116 static int
       
   117 test_dict_inner(int count)
       
   118 {
       
   119 	Py_ssize_t pos = 0, iterations = 0;
       
   120 	int i;
       
   121 	PyObject *dict = PyDict_New();
       
   122 	PyObject *v, *k;
       
   123 
       
   124 	if (dict == NULL)
       
   125 		return -1;
       
   126 
       
   127 	for (i = 0; i < count; i++) {
       
   128 		v = PyInt_FromLong(i);
       
   129 		PyDict_SetItem(dict, v, v);
       
   130 		Py_DECREF(v);
       
   131 	}
       
   132 
       
   133 	while (PyDict_Next(dict, &pos, &k, &v)) {
       
   134 		PyObject *o;
       
   135 		iterations++;
       
   136 
       
   137 		i = PyInt_AS_LONG(v) + 1;
       
   138 		o = PyInt_FromLong(i);
       
   139 		if (o == NULL)
       
   140 			return -1;
       
   141 		if (PyDict_SetItem(dict, k, o) < 0) {
       
   142 			Py_DECREF(o);
       
   143 			return -1;
       
   144 		}
       
   145 		Py_DECREF(o);
       
   146 	}
       
   147 
       
   148 	Py_DECREF(dict);
       
   149 
       
   150 	if (iterations != count) {
       
   151 		PyErr_SetString(
       
   152 			TestError,
       
   153 			"test_dict_iteration: dict iteration went wrong ");
       
   154 		return -1;
       
   155 	} else {
       
   156 		return 0;
       
   157 	}
       
   158 }
       
   159 
       
   160 static PyObject*
       
   161 test_dict_iteration(PyObject* self)
       
   162 {
       
   163 	int i;
       
   164 
       
   165 	for (i = 0; i < 200; i++) {
       
   166 		if (test_dict_inner(i) < 0) {
       
   167 			return NULL;
       
   168 		}
       
   169 	}
       
   170 
       
   171 	Py_INCREF(Py_None);
       
   172 	return Py_None;
       
   173 }
       
   174 
       
   175 
       
   176 /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
       
   177    PyLong_{As, From}{Unsigned,}LongLong().
       
   178 
       
   179    Note that the meat of the test is contained in testcapi_long.h.
       
   180    This is revolting, but delicate code duplication is worse:  "almost
       
   181    exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
       
   182    dependence on type names makes it impossible to use a parameterized
       
   183    function.  A giant macro would be even worse than this.  A C++ template
       
   184    would be perfect.
       
   185 
       
   186    The "report an error" functions are deliberately not part of the #include
       
   187    file:  if the test fails, you can set a breakpoint in the appropriate
       
   188    error function directly, and crawl back from there in the debugger.
       
   189 */
       
   190 
       
   191 #define UNBIND(X)  Py_DECREF(X); (X) = NULL
       
   192 
       
   193 static PyObject *
       
   194 raise_test_long_error(const char* msg)
       
   195 {
       
   196 	return raiseTestError("test_long_api", msg);
       
   197 }
       
   198 
       
   199 #define TESTNAME	test_long_api_inner
       
   200 #define TYPENAME	long
       
   201 #define F_S_TO_PY	PyLong_FromLong
       
   202 #define F_PY_TO_S	PyLong_AsLong
       
   203 #define F_U_TO_PY	PyLong_FromUnsignedLong
       
   204 #define F_PY_TO_U	PyLong_AsUnsignedLong
       
   205 
       
   206 #include "testcapi_long.h"
       
   207 
       
   208 static PyObject *
       
   209 test_long_api(PyObject* self)
       
   210 {
       
   211 	return TESTNAME(raise_test_long_error);
       
   212 }
       
   213 
       
   214 #undef TESTNAME
       
   215 #undef TYPENAME
       
   216 #undef F_S_TO_PY
       
   217 #undef F_PY_TO_S
       
   218 #undef F_U_TO_PY
       
   219 #undef F_PY_TO_U
       
   220 
       
   221 #ifdef HAVE_LONG_LONG
       
   222 
       
   223 static PyObject *
       
   224 raise_test_longlong_error(const char* msg)
       
   225 {
       
   226 	return raiseTestError("test_longlong_api", msg);
       
   227 }
       
   228 
       
   229 #define TESTNAME	test_longlong_api_inner
       
   230 #define TYPENAME	PY_LONG_LONG
       
   231 #define F_S_TO_PY	PyLong_FromLongLong
       
   232 #define F_PY_TO_S	PyLong_AsLongLong
       
   233 #define F_U_TO_PY	PyLong_FromUnsignedLongLong
       
   234 #define F_PY_TO_U	PyLong_AsUnsignedLongLong
       
   235 
       
   236 #include "testcapi_long.h"
       
   237 
       
   238 static PyObject *
       
   239 test_longlong_api(PyObject* self, PyObject *args)
       
   240 {
       
   241 	return TESTNAME(raise_test_longlong_error);
       
   242 }
       
   243 
       
   244 #undef TESTNAME
       
   245 #undef TYPENAME
       
   246 #undef F_S_TO_PY
       
   247 #undef F_PY_TO_S
       
   248 #undef F_U_TO_PY
       
   249 #undef F_PY_TO_U
       
   250 
       
   251 /* Test the L code for PyArg_ParseTuple.  This should deliver a PY_LONG_LONG
       
   252    for both long and int arguments.  The test may leak a little memory if
       
   253    it fails.
       
   254 */
       
   255 static PyObject *
       
   256 test_L_code(PyObject *self)
       
   257 {
       
   258 	PyObject *tuple, *num;
       
   259 	PY_LONG_LONG value;
       
   260 
       
   261         tuple = PyTuple_New(1);
       
   262         if (tuple == NULL)
       
   263         	return NULL;
       
   264 
       
   265         num = PyLong_FromLong(42);
       
   266         if (num == NULL)
       
   267         	return NULL;
       
   268 
       
   269         PyTuple_SET_ITEM(tuple, 0, num);
       
   270 
       
   271         value = -1;
       
   272         if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
       
   273         	return NULL;
       
   274         if (value != 42)
       
   275         	return raiseTestError("test_L_code",
       
   276 			"L code returned wrong value for long 42");
       
   277 
       
   278 	Py_DECREF(num);
       
   279         num = PyInt_FromLong(42);
       
   280         if (num == NULL)
       
   281         	return NULL;
       
   282 
       
   283         PyTuple_SET_ITEM(tuple, 0, num);
       
   284 
       
   285 	value = -1;
       
   286         if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
       
   287         	return NULL;
       
   288         if (value != 42)
       
   289         	return raiseTestError("test_L_code",
       
   290 			"L code returned wrong value for int 42");
       
   291 
       
   292 	Py_DECREF(tuple);
       
   293 	Py_INCREF(Py_None);
       
   294 	return Py_None;
       
   295 }
       
   296 
       
   297 #endif	/* ifdef HAVE_LONG_LONG */
       
   298 
       
   299 /* Test tuple argument processing */
       
   300 static PyObject *
       
   301 getargs_tuple(PyObject *self, PyObject *args)
       
   302 {
       
   303 	int a, b, c;
       
   304 	if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
       
   305 		return NULL;
       
   306 	return Py_BuildValue("iii", a, b, c);
       
   307 }
       
   308 
       
   309 /* test PyArg_ParseTupleAndKeywords */
       
   310 static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
       
   311 {
       
   312 	static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
       
   313 	static char *fmt="(ii)i|(i(ii))(iii)i";
       
   314 	int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
       
   315 
       
   316 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
       
   317 		&int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
       
   318 		&int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
       
   319 		return NULL;
       
   320 	return Py_BuildValue("iiiiiiiiii",
       
   321 		int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
       
   322 		int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
       
   323 }
       
   324 
       
   325 /* Functions to call PyArg_ParseTuple with integer format codes,
       
   326    and return the result.
       
   327 */
       
   328 static PyObject *
       
   329 getargs_b(PyObject *self, PyObject *args)
       
   330 {
       
   331 	unsigned char value;
       
   332 	if (!PyArg_ParseTuple(args, "b", &value))
       
   333 		return NULL;
       
   334 	return PyLong_FromUnsignedLong((unsigned long)value);
       
   335 }
       
   336 
       
   337 static PyObject *
       
   338 getargs_B(PyObject *self, PyObject *args)
       
   339 {
       
   340 	unsigned char value;
       
   341 	if (!PyArg_ParseTuple(args, "B", &value))
       
   342 		return NULL;
       
   343 	return PyLong_FromUnsignedLong((unsigned long)value);
       
   344 }
       
   345 
       
   346 static PyObject *
       
   347 getargs_H(PyObject *self, PyObject *args)
       
   348 {
       
   349 	unsigned short value;
       
   350 	if (!PyArg_ParseTuple(args, "H", &value))
       
   351 		return NULL;
       
   352 	return PyLong_FromUnsignedLong((unsigned long)value);
       
   353 }
       
   354 
       
   355 static PyObject *
       
   356 getargs_I(PyObject *self, PyObject *args)
       
   357 {
       
   358 	unsigned int value;
       
   359 	if (!PyArg_ParseTuple(args, "I", &value))
       
   360 		return NULL;
       
   361 	return PyLong_FromUnsignedLong((unsigned long)value);
       
   362 }
       
   363 
       
   364 static PyObject *
       
   365 getargs_k(PyObject *self, PyObject *args)
       
   366 {
       
   367 	unsigned long value;
       
   368 	if (!PyArg_ParseTuple(args, "k", &value))
       
   369 		return NULL;
       
   370 	return PyLong_FromUnsignedLong(value);
       
   371 }
       
   372 
       
   373 static PyObject *
       
   374 getargs_i(PyObject *self, PyObject *args)
       
   375 {
       
   376 	int value;
       
   377 	if (!PyArg_ParseTuple(args, "i", &value))
       
   378 		return NULL;
       
   379 	return PyLong_FromLong((long)value);
       
   380 }
       
   381 
       
   382 static PyObject *
       
   383 getargs_l(PyObject *self, PyObject *args)
       
   384 {
       
   385 	long value;
       
   386 	if (!PyArg_ParseTuple(args, "l", &value))
       
   387 		return NULL;
       
   388 	return PyLong_FromLong(value);
       
   389 }
       
   390 
       
   391 static PyObject *
       
   392 getargs_n(PyObject *self, PyObject *args)
       
   393 {
       
   394 	Py_ssize_t value;
       
   395 	if (!PyArg_ParseTuple(args, "n", &value))
       
   396 	return NULL;
       
   397 	return PyInt_FromSsize_t(value);
       
   398 }
       
   399 
       
   400 #ifdef HAVE_LONG_LONG
       
   401 static PyObject *
       
   402 getargs_L(PyObject *self, PyObject *args)
       
   403 {
       
   404 	PY_LONG_LONG value;
       
   405 	if (!PyArg_ParseTuple(args, "L", &value))
       
   406 		return NULL;
       
   407 	return PyLong_FromLongLong(value);
       
   408 }
       
   409 
       
   410 static PyObject *
       
   411 getargs_K(PyObject *self, PyObject *args)
       
   412 {
       
   413 	unsigned PY_LONG_LONG value;
       
   414 	if (!PyArg_ParseTuple(args, "K", &value))
       
   415 		return NULL;
       
   416 	return PyLong_FromUnsignedLongLong(value);
       
   417 }
       
   418 #endif
       
   419 
       
   420 /* This function not only tests the 'k' getargs code, but also the
       
   421    PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
       
   422 static PyObject *
       
   423 test_k_code(PyObject *self)
       
   424 {
       
   425 	PyObject *tuple, *num;
       
   426 	unsigned long value;
       
   427 
       
   428         tuple = PyTuple_New(1);
       
   429         if (tuple == NULL)
       
   430         	return NULL;
       
   431 
       
   432 	/* a number larger than ULONG_MAX even on 64-bit platforms */
       
   433         num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
       
   434         if (num == NULL)
       
   435         	return NULL;
       
   436 
       
   437 	value = PyInt_AsUnsignedLongMask(num);
       
   438 	if (value != ULONG_MAX)
       
   439         	return raiseTestError("test_k_code",
       
   440 	    "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
       
   441 
       
   442         PyTuple_SET_ITEM(tuple, 0, num);
       
   443 
       
   444         value = 0;
       
   445         if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
       
   446         	return NULL;
       
   447         if (value != ULONG_MAX)
       
   448         	return raiseTestError("test_k_code",
       
   449 			"k code returned wrong value for long 0xFFF...FFF");
       
   450 
       
   451 	Py_DECREF(num);
       
   452         num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
       
   453         if (num == NULL)
       
   454         	return NULL;
       
   455 
       
   456 	value = PyInt_AsUnsignedLongMask(num);
       
   457 	if (value != (unsigned long)-0x42)
       
   458         	return raiseTestError("test_k_code",
       
   459 	    "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
       
   460 
       
   461         PyTuple_SET_ITEM(tuple, 0, num);
       
   462 
       
   463 	value = 0;
       
   464         if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
       
   465         	return NULL;
       
   466         if (value != (unsigned long)-0x42)
       
   467         	return raiseTestError("test_k_code",
       
   468 			"k code returned wrong value for long -0xFFF..000042");
       
   469 
       
   470 	Py_DECREF(tuple);
       
   471 	Py_INCREF(Py_None);
       
   472 	return Py_None;
       
   473 }
       
   474 
       
   475 #ifdef Py_USING_UNICODE
       
   476 
       
   477 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
       
   478    of an error.
       
   479 */
       
   480 static PyObject *
       
   481 test_u_code(PyObject *self)
       
   482 {
       
   483 	PyObject *tuple, *obj;
       
   484 	Py_UNICODE *value;
       
   485 	int len;
       
   486 
       
   487 	/* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
       
   488 	/* Just use the macro and check that it compiles */
       
   489 	int x = Py_UNICODE_ISSPACE(25);
       
   490 
       
   491         tuple = PyTuple_New(1);
       
   492         if (tuple == NULL)
       
   493         	return NULL;
       
   494 
       
   495         obj = PyUnicode_Decode("test", strlen("test"),
       
   496 			       "ascii", NULL);
       
   497         if (obj == NULL)
       
   498         	return NULL;
       
   499 
       
   500         PyTuple_SET_ITEM(tuple, 0, obj);
       
   501 
       
   502         value = 0;
       
   503         if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
       
   504         	return NULL;
       
   505         if (value != PyUnicode_AS_UNICODE(obj))
       
   506         	return raiseTestError("test_u_code",
       
   507 			"u code returned wrong value for u'test'");
       
   508         value = 0;
       
   509         if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
       
   510         	return NULL;
       
   511         if (value != PyUnicode_AS_UNICODE(obj) ||
       
   512 	    len != PyUnicode_GET_SIZE(obj))
       
   513         	return raiseTestError("test_u_code",
       
   514 			"u# code returned wrong values for u'test'");
       
   515 
       
   516 	Py_DECREF(tuple);
       
   517 	Py_INCREF(Py_None);
       
   518 	return Py_None;
       
   519 }
       
   520 
       
   521 static PyObject *
       
   522 codec_incrementalencoder(PyObject *self, PyObject *args)
       
   523 {
       
   524 	const char *encoding, *errors = NULL;
       
   525 	if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
       
   526 			      &encoding, &errors))
       
   527 		return NULL;
       
   528 	return PyCodec_IncrementalEncoder(encoding, errors);
       
   529 }
       
   530 
       
   531 static PyObject *
       
   532 codec_incrementaldecoder(PyObject *self, PyObject *args)
       
   533 {
       
   534 	const char *encoding, *errors = NULL;
       
   535 	if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
       
   536 			      &encoding, &errors))
       
   537 		return NULL;
       
   538 	return PyCodec_IncrementalDecoder(encoding, errors);
       
   539 }
       
   540 
       
   541 #endif
       
   542 
       
   543 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
       
   544 static PyObject *
       
   545 test_long_numbits(PyObject *self)
       
   546 {
       
   547 	struct triple {
       
   548 		long input;
       
   549 		size_t nbits;
       
   550 		int sign;
       
   551 	} testcases[] = {{0, 0, 0},
       
   552 			 {1L, 1, 1},
       
   553 			 {-1L, 1, -1},
       
   554 			 {2L, 2, 1},
       
   555 			 {-2L, 2, -1},
       
   556 			 {3L, 2, 1},
       
   557 			 {-3L, 2, -1},
       
   558 			 {4L, 3, 1},
       
   559 			 {-4L, 3, -1},
       
   560 			 {0x7fffL, 15, 1},	/* one Python long digit */
       
   561 			 {-0x7fffL, 15, -1},
       
   562 			 {0xffffL, 16, 1},
       
   563 			 {-0xffffL, 16, -1},
       
   564 			 {0xfffffffL, 28, 1},
       
   565 			 {-0xfffffffL, 28, -1}};
       
   566 	int i;
       
   567 
       
   568 	for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
       
   569 		PyObject *plong = PyLong_FromLong(testcases[i].input);
       
   570 		size_t nbits = _PyLong_NumBits(plong);
       
   571 		int sign = _PyLong_Sign(plong);
       
   572 
       
   573 		Py_DECREF(plong);
       
   574 		if (nbits != testcases[i].nbits)
       
   575 			return raiseTestError("test_long_numbits",
       
   576 					"wrong result for _PyLong_NumBits");
       
   577 		if (sign != testcases[i].sign)
       
   578 			return raiseTestError("test_long_numbits",
       
   579 					"wrong result for _PyLong_Sign");
       
   580 	}
       
   581 	Py_INCREF(Py_None);
       
   582 	return Py_None;
       
   583 }
       
   584 
       
   585 /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
       
   586 
       
   587 static PyObject *
       
   588 test_null_strings(PyObject *self)
       
   589 {
       
   590 	PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL);
       
   591 	PyObject *tuple = PyTuple_Pack(2, o1, o2);
       
   592 	Py_XDECREF(o1);
       
   593 	Py_XDECREF(o2);
       
   594 	return tuple;
       
   595 }
       
   596 
       
   597 static PyObject *
       
   598 raise_exception(PyObject *self, PyObject *args)
       
   599 {
       
   600 	PyObject *exc;
       
   601 	PyObject *exc_args, *v;
       
   602 	int num_args, i;
       
   603 
       
   604 	if (!PyArg_ParseTuple(args, "Oi:raise_exception",
       
   605 			      &exc, &num_args))
       
   606 		return NULL;
       
   607 	if (!PyExceptionClass_Check(exc)) {
       
   608 		PyErr_Format(PyExc_TypeError, "an exception class is required");
       
   609 		return NULL;
       
   610 	}
       
   611 
       
   612 	exc_args = PyTuple_New(num_args);
       
   613 	if (exc_args == NULL)
       
   614 		return NULL;
       
   615 	for (i = 0; i < num_args; ++i) {
       
   616 		v = PyInt_FromLong(i);
       
   617 		if (v == NULL) {
       
   618 			Py_DECREF(exc_args);
       
   619 			return NULL;
       
   620 		}
       
   621 		PyTuple_SET_ITEM(exc_args, i, v);
       
   622 	}
       
   623 	PyErr_SetObject(exc, exc_args);
       
   624 	Py_DECREF(exc_args);
       
   625 	return NULL;
       
   626 }
       
   627 
       
   628 #ifdef WITH_THREAD
       
   629 
       
   630 /* test_thread_state spawns a thread of its own, and that thread releases
       
   631  * `thread_done` when it's finished.  The driver code has to know when the
       
   632  * thread finishes, because the thread uses a PyObject (the callable) that
       
   633  * may go away when the driver finishes.  The former lack of this explicit
       
   634  * synchronization caused rare segfaults, so rare that they were seen only
       
   635  * on a Mac buildbot (although they were possible on any box).
       
   636  */
       
   637 static PyThread_type_lock thread_done = NULL;
       
   638 
       
   639 static int
       
   640 _make_call(void *callable)
       
   641 {
       
   642 	PyObject *rc;
       
   643 	int success;
       
   644 	PyGILState_STATE s = PyGILState_Ensure();
       
   645 	rc = PyObject_CallFunction((PyObject *)callable, "");
       
   646 	success = (rc != NULL);
       
   647 	Py_XDECREF(rc);
       
   648 	PyGILState_Release(s);
       
   649 	return success;
       
   650 }
       
   651 
       
   652 /* Same thing, but releases `thread_done` when it returns.  This variant
       
   653  * should be called only from threads spawned by test_thread_state().
       
   654  */
       
   655 static void
       
   656 _make_call_from_thread(void *callable)
       
   657 {
       
   658 	_make_call(callable);
       
   659 	PyThread_release_lock(thread_done);
       
   660 }
       
   661 
       
   662 static PyObject *
       
   663 test_thread_state(PyObject *self, PyObject *args)
       
   664 {
       
   665 	PyObject *fn;
       
   666 	int success = 1;
       
   667 
       
   668 	if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
       
   669 		return NULL;
       
   670 
       
   671 	if (!PyCallable_Check(fn)) {
       
   672 		PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
       
   673 			fn->ob_type->tp_name);
       
   674 		return NULL;
       
   675 	}
       
   676 
       
   677 	/* Ensure Python is set up for threading */
       
   678 	PyEval_InitThreads();
       
   679 	thread_done = PyThread_allocate_lock();
       
   680 	if (thread_done == NULL)
       
   681 		return PyErr_NoMemory();
       
   682 	PyThread_acquire_lock(thread_done, 1);
       
   683 
       
   684 	/* Start a new thread with our callback. */
       
   685 	PyThread_start_new_thread(_make_call_from_thread, fn);
       
   686 	/* Make the callback with the thread lock held by this thread */
       
   687 	success &= _make_call(fn);
       
   688 	/* Do it all again, but this time with the thread-lock released */
       
   689 	Py_BEGIN_ALLOW_THREADS
       
   690 	success &= _make_call(fn);
       
   691 	PyThread_acquire_lock(thread_done, 1);  /* wait for thread to finish */
       
   692 	Py_END_ALLOW_THREADS
       
   693 
       
   694 	/* And once more with and without a thread
       
   695 	   XXX - should use a lock and work out exactly what we are trying
       
   696 	   to test <wink>
       
   697 	*/
       
   698 	Py_BEGIN_ALLOW_THREADS
       
   699 	PyThread_start_new_thread(_make_call_from_thread, fn);
       
   700 	success &= _make_call(fn);
       
   701 	PyThread_acquire_lock(thread_done, 1);  /* wait for thread to finish */
       
   702 	Py_END_ALLOW_THREADS
       
   703 
       
   704 	/* Release lock we acquired above.  This is required on HP-UX. */
       
   705 	PyThread_release_lock(thread_done);
       
   706 
       
   707 	PyThread_free_lock(thread_done);
       
   708 	if (!success)
       
   709 		return NULL;
       
   710 	Py_RETURN_NONE;
       
   711 }
       
   712 #endif
       
   713 
       
   714 /* Some tests of PyString_FromFormat().  This needs more tests. */
       
   715 static PyObject *
       
   716 test_string_from_format(PyObject *self, PyObject *args)
       
   717 {
       
   718 	PyObject *result;
       
   719 	char *msg;
       
   720 
       
   721 #define CHECK_1_FORMAT(FORMAT, TYPE) 			\
       
   722 	result = PyString_FromFormat(FORMAT, (TYPE)1);	\
       
   723 	if (result == NULL)				\
       
   724 		return NULL;				\
       
   725 	if (strcmp(PyString_AsString(result), "1")) {	\
       
   726 		msg = FORMAT " failed at 1";		\
       
   727 		goto Fail;				\
       
   728 	}						\
       
   729 	Py_DECREF(result)
       
   730 
       
   731 	CHECK_1_FORMAT("%d", int);
       
   732 	CHECK_1_FORMAT("%ld", long);
       
   733 	/* The z width modifier was added in Python 2.5. */
       
   734 	CHECK_1_FORMAT("%zd", Py_ssize_t);
       
   735 
       
   736 	/* The u type code was added in Python 2.5. */
       
   737 	CHECK_1_FORMAT("%u", unsigned int);
       
   738 	CHECK_1_FORMAT("%lu", unsigned long);
       
   739 	CHECK_1_FORMAT("%zu", size_t);
       
   740 
       
   741 	Py_RETURN_NONE;
       
   742 
       
   743  Fail:
       
   744  	Py_XDECREF(result);
       
   745 	return raiseTestError("test_string_from_format", msg);
       
   746 
       
   747 #undef CHECK_1_FORMAT
       
   748 }
       
   749 
       
   750 /* This is here to provide a docstring for test_descr. */
       
   751 static PyObject *
       
   752 test_with_docstring(PyObject *self)
       
   753 {
       
   754 	Py_RETURN_NONE;
       
   755 }
       
   756 
       
   757 /* To test the format of tracebacks as printed out. */
       
   758 static PyObject *
       
   759 traceback_print(PyObject *self, PyObject *args)
       
   760 {
       
   761 	PyObject *file;
       
   762 	PyObject *traceback;
       
   763 	int result;
       
   764 	
       
   765 	if (!PyArg_ParseTuple(args, "OO:traceback_print",
       
   766 				&traceback, &file))
       
   767 		return NULL;
       
   768 		
       
   769 	result = PyTraceBack_Print(traceback, file);
       
   770 	if (result < 0)
       
   771 		return NULL;
       
   772 	Py_RETURN_NONE;
       
   773 }
       
   774 
       
   775 static PyMethodDef TestMethods[] = {
       
   776 	{"raise_exception",	raise_exception,		 METH_VARARGS},
       
   777 	{"test_config",		(PyCFunction)test_config,	 METH_NOARGS},
       
   778 	{"test_list_api",	(PyCFunction)test_list_api,	 METH_NOARGS},
       
   779 	{"test_dict_iteration",	(PyCFunction)test_dict_iteration,METH_NOARGS},
       
   780 	{"test_long_api",	(PyCFunction)test_long_api,	 METH_NOARGS},
       
   781 	{"test_long_numbits",	(PyCFunction)test_long_numbits,	 METH_NOARGS},
       
   782 	{"test_k_code",		(PyCFunction)test_k_code,	 METH_NOARGS},
       
   783 	{"test_null_strings",	(PyCFunction)test_null_strings,	 METH_NOARGS},
       
   784 	{"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
       
   785 	{"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
       
   786 	 PyDoc_STR("This is a pretty normal docstring.")},
       
   787 
       
   788 	{"getargs_tuple",	getargs_tuple,			 METH_VARARGS},
       
   789 	{"getargs_keywords", (PyCFunction)getargs_keywords, 
       
   790 	  METH_VARARGS|METH_KEYWORDS},
       
   791 	{"getargs_b",		getargs_b,			 METH_VARARGS},
       
   792 	{"getargs_B",		getargs_B,			 METH_VARARGS},
       
   793 	{"getargs_H",		getargs_H,			 METH_VARARGS},
       
   794 	{"getargs_I",		getargs_I,			 METH_VARARGS},
       
   795 	{"getargs_k",		getargs_k,			 METH_VARARGS},
       
   796 	{"getargs_i",		getargs_i,			 METH_VARARGS},
       
   797 	{"getargs_l",		getargs_l,			 METH_VARARGS},
       
   798 	{"getargs_n",		getargs_n, 			 METH_VARARGS},
       
   799 #ifdef HAVE_LONG_LONG
       
   800 	{"getargs_L",		getargs_L,			 METH_VARARGS},
       
   801 	{"getargs_K",		getargs_K,			 METH_VARARGS},
       
   802 	{"test_longlong_api",	test_longlong_api,		 METH_NOARGS},
       
   803 	{"test_L_code",		(PyCFunction)test_L_code,	 METH_NOARGS},
       
   804 	{"codec_incrementalencoder",
       
   805 	 (PyCFunction)codec_incrementalencoder,	 METH_VARARGS},
       
   806 	{"codec_incrementaldecoder",
       
   807 	 (PyCFunction)codec_incrementaldecoder,	 METH_VARARGS},
       
   808 #endif
       
   809 #ifdef Py_USING_UNICODE
       
   810 	{"test_u_code",		(PyCFunction)test_u_code,	 METH_NOARGS},
       
   811 #endif
       
   812 #ifdef WITH_THREAD
       
   813 	{"_test_thread_state",  test_thread_state, 		 METH_VARARGS},
       
   814 #endif
       
   815 	{"traceback_print", traceback_print, 	         METH_VARARGS},
       
   816 	{NULL, NULL} /* sentinel */
       
   817 };
       
   818 
       
   819 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
       
   820 
       
   821 typedef struct {
       
   822 	char bool_member;
       
   823 	char byte_member;
       
   824 	unsigned char ubyte_member;
       
   825 	short short_member;
       
   826 	unsigned short ushort_member;
       
   827 	int int_member;
       
   828 	unsigned int uint_member;
       
   829 	long long_member;
       
   830 	unsigned long ulong_member;
       
   831 	float float_member;
       
   832 	double double_member;
       
   833 #ifdef HAVE_LONG_LONG
       
   834 	PY_LONG_LONG longlong_member;
       
   835 	unsigned PY_LONG_LONG ulonglong_member;
       
   836 #endif
       
   837 } all_structmembers;
       
   838 
       
   839 typedef struct {
       
   840     PyObject_HEAD
       
   841 	all_structmembers structmembers;
       
   842 } test_structmembers;
       
   843 
       
   844 static struct PyMemberDef test_members[] = {
       
   845 	{"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
       
   846 	{"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
       
   847 	{"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
       
   848 	{"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
       
   849 	{"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
       
   850 	{"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
       
   851 	{"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
       
   852 	{"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
       
   853 	{"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
       
   854 	{"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
       
   855 	{"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
       
   856 #ifdef HAVE_LONG_LONG
       
   857 	{"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
       
   858 	{"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
       
   859 #endif
       
   860 	{NULL}
       
   861 };
       
   862 
       
   863 
       
   864 static PyObject *
       
   865 test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
       
   866 {
       
   867 	static char *keywords[] = {
       
   868 		"T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
       
   869 		"T_INT", "T_UINT", "T_LONG", "T_ULONG",
       
   870 		"T_FLOAT", "T_DOUBLE",
       
   871 #ifdef HAVE_LONG_LONG	
       
   872 		"T_LONGLONG", "T_ULONGLONG",
       
   873 #endif
       
   874 		NULL};
       
   875 	static char *fmt = "|bbBhHiIlkfd"
       
   876 #ifdef HAVE_LONG_LONG
       
   877 		"LK"
       
   878 #endif
       
   879 		;
       
   880 	test_structmembers *ob;
       
   881 	ob = PyObject_New(test_structmembers, type);
       
   882 	if (ob == NULL)
       
   883 		return NULL;
       
   884 	memset(&ob->structmembers, 0, sizeof(all_structmembers));
       
   885 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
       
   886 					 &ob->structmembers.bool_member,
       
   887 					 &ob->structmembers.byte_member,
       
   888 					 &ob->structmembers.ubyte_member,
       
   889 					 &ob->structmembers.short_member,
       
   890 					 &ob->structmembers.ushort_member,
       
   891 					 &ob->structmembers.int_member,
       
   892 					 &ob->structmembers.uint_member, 
       
   893 					 &ob->structmembers.long_member,
       
   894 					 &ob->structmembers.ulong_member,
       
   895 					 &ob->structmembers.float_member,
       
   896 					 &ob->structmembers.double_member
       
   897 #ifdef HAVE_LONG_LONG
       
   898 					 , &ob->structmembers.longlong_member,
       
   899 					 &ob->structmembers.ulonglong_member
       
   900 #endif
       
   901 		)) {
       
   902 		Py_DECREF(ob);
       
   903 		return NULL;
       
   904 	}
       
   905 	return (PyObject *)ob;
       
   906 }
       
   907 
       
   908 static void
       
   909 test_structmembers_free(PyObject *ob)
       
   910 {
       
   911 	PyObject_FREE(ob);
       
   912 }
       
   913 
       
   914 static PyTypeObject test_structmembersType = {
       
   915     PyVarObject_HEAD_INIT(NULL, 0)
       
   916 	"test_structmembersType",
       
   917 	sizeof(test_structmembers),	/* tp_basicsize */
       
   918 	0,				/* tp_itemsize */
       
   919 	test_structmembers_free,	/* destructor tp_dealloc */
       
   920 	0,				/* tp_print */
       
   921 	0,				/* tp_getattr */
       
   922 	0,				/* tp_setattr */
       
   923 	0,				/* tp_compare */
       
   924 	0,				/* tp_repr */
       
   925 	0,				/* tp_as_number */
       
   926 	0,				/* tp_as_sequence */
       
   927 	0,				/* tp_as_mapping */
       
   928 	0,				/* tp_hash */
       
   929 	0,				/* tp_call */
       
   930 	0,				/* tp_str */
       
   931 	PyObject_GenericGetAttr,	/* tp_getattro */
       
   932 	PyObject_GenericSetAttr,	/* tp_setattro */
       
   933 	0,				/* tp_as_buffer */
       
   934 	0,				/* tp_flags */
       
   935 	"Type containing all structmember types",
       
   936 	0,				/* traverseproc tp_traverse */
       
   937 	0,				/* tp_clear */
       
   938 	0,				/* tp_richcompare */
       
   939 	0,				/* tp_weaklistoffset */
       
   940 	0,				/* tp_iter */
       
   941 	0,				/* tp_iternext */
       
   942 	0,				/* tp_methods */
       
   943 	test_members,			/* tp_members */
       
   944 	0,
       
   945 	0,
       
   946 	0,
       
   947 	0,
       
   948 	0,
       
   949 	0,
       
   950 	0,
       
   951 	0,
       
   952 	test_structmembers_new,	       	/* tp_new */
       
   953 };
       
   954 
       
   955 
       
   956 PyMODINIT_FUNC
       
   957 init_testcapi(void)
       
   958 {
       
   959 	PyObject *m;
       
   960 
       
   961 	m = Py_InitModule("_testcapi", TestMethods);
       
   962 	if (m == NULL)
       
   963 		return;
       
   964 
       
   965 	Py_TYPE(&test_structmembersType)=&PyType_Type;
       
   966 	Py_INCREF(&test_structmembersType);
       
   967 	PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType);
       
   968 
       
   969 	PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX));
       
   970 	PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN));
       
   971 	PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX));
       
   972 	PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX));
       
   973 	PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN));
       
   974 	PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX));
       
   975 	PyModule_AddObject(m, "INT_MAX",  PyLong_FromLong(INT_MAX));
       
   976 	PyModule_AddObject(m, "INT_MIN",  PyLong_FromLong(INT_MIN));
       
   977 	PyModule_AddObject(m, "UINT_MAX",  PyLong_FromUnsignedLong(UINT_MAX));
       
   978 	PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX));
       
   979 	PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN));
       
   980 	PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
       
   981 	PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
       
   982 	PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
       
   983 	PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
       
   984 	PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
       
   985 	PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
       
   986 	PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
       
   987 	PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
       
   988 	PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX));
       
   989 	PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN));
       
   990 	PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head)));
       
   991 
       
   992 	TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
       
   993 	Py_INCREF(TestError);
       
   994 	PyModule_AddObject(m, "error", TestError);
       
   995 }