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#include "datetime.h" 12#include "marshal.h" 13 14#ifdef WITH_THREAD 15#include "pythread.h" 16#endif /* WITH_THREAD */ 17static PyObject *TestError; /* set to exception object in init */ 18 19/* Raise TestError with test_name + ": " + msg, and return NULL. */ 20 21static PyObject * 22raiseTestError(const char* test_name, const char* msg) 23{ 24 char buf[2048]; 25 26 if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50) 27 PyErr_SetString(TestError, "internal error msg too large"); 28 else { 29 PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg); 30 PyErr_SetString(TestError, buf); 31 } 32 return NULL; 33} 34 35/* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). 36 37 The ones derived from autoconf on the UNIX-like OSes can be relied 38 upon (in the absence of sloppy cross-compiling), but the Windows 39 platforms have these hardcoded. Better safe than sorry. 40*/ 41static PyObject* 42sizeof_error(const char* fatname, const char* typname, 43 int expected, int got) 44{ 45 char buf[1024]; 46 PyOS_snprintf(buf, sizeof(buf), 47 "%.200s #define == %d but sizeof(%.200s) == %d", 48 fatname, expected, typname, got); 49 PyErr_SetString(TestError, buf); 50 return (PyObject*)NULL; 51} 52 53static PyObject* 54test_config(PyObject *self) 55{ 56#define CHECK_SIZEOF(FATNAME, TYPE) \ 57 if (FATNAME != sizeof(TYPE)) \ 58 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) 59 60 CHECK_SIZEOF(SIZEOF_SHORT, short); 61 CHECK_SIZEOF(SIZEOF_INT, int); 62 CHECK_SIZEOF(SIZEOF_LONG, long); 63 CHECK_SIZEOF(SIZEOF_VOID_P, void*); 64 CHECK_SIZEOF(SIZEOF_TIME_T, time_t); 65#ifdef HAVE_LONG_LONG 66 CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); 67#endif 68 69#undef CHECK_SIZEOF 70 71 Py_INCREF(Py_None); 72 return Py_None; 73} 74 75static PyObject* 76test_list_api(PyObject *self) 77{ 78 PyObject* list; 79 int i; 80 81 /* SF bug 132008: PyList_Reverse segfaults */ 82#define NLIST 30 83 list = PyList_New(NLIST); 84 if (list == (PyObject*)NULL) 85 return (PyObject*)NULL; 86 /* list = range(NLIST) */ 87 for (i = 0; i < NLIST; ++i) { 88 PyObject* anint = PyInt_FromLong(i); 89 if (anint == (PyObject*)NULL) { 90 Py_DECREF(list); 91 return (PyObject*)NULL; 92 } 93 PyList_SET_ITEM(list, i, anint); 94 } 95 /* list.reverse(), via PyList_Reverse() */ 96 i = PyList_Reverse(list); /* should not blow up! */ 97 if (i != 0) { 98 Py_DECREF(list); 99 return (PyObject*)NULL; 100 } 101 /* Check that list == range(29, -1, -1) now */ 102 for (i = 0; i < NLIST; ++i) { 103 PyObject* anint = PyList_GET_ITEM(list, i); 104 if (PyInt_AS_LONG(anint) != NLIST-1-i) { 105 PyErr_SetString(TestError, 106 "test_list_api: reverse screwed up"); 107 Py_DECREF(list); 108 return (PyObject*)NULL; 109 } 110 } 111 Py_DECREF(list); 112#undef NLIST 113 114 Py_INCREF(Py_None); 115 return Py_None; 116} 117 118static int 119test_dict_inner(int count) 120{ 121 Py_ssize_t pos = 0, iterations = 0; 122 int i; 123 PyObject *dict = PyDict_New(); 124 PyObject *v, *k; 125 126 if (dict == NULL) 127 return -1; 128 129 for (i = 0; i < count; i++) { 130 v = PyInt_FromLong(i); 131 PyDict_SetItem(dict, v, v); 132 Py_DECREF(v); 133 } 134 135 while (PyDict_Next(dict, &pos, &k, &v)) { 136 PyObject *o; 137 iterations++; 138 139 i = PyInt_AS_LONG(v) + 1; 140 o = PyInt_FromLong(i); 141 if (o == NULL) 142 return -1; 143 if (PyDict_SetItem(dict, k, o) < 0) { 144 Py_DECREF(o); 145 return -1; 146 } 147 Py_DECREF(o); 148 } 149 150 Py_DECREF(dict); 151 152 if (iterations != count) { 153 PyErr_SetString( 154 TestError, 155 "test_dict_iteration: dict iteration went wrong "); 156 return -1; 157 } else { 158 return 0; 159 } 160} 161 162static PyObject* 163test_dict_iteration(PyObject* self) 164{ 165 int i; 166 167 for (i = 0; i < 200; i++) { 168 if (test_dict_inner(i) < 0) { 169 return NULL; 170 } 171 } 172 173 Py_INCREF(Py_None); 174 return Py_None; 175} 176 177 178/* Issue #4701: Check that PyObject_Hash implicitly calls 179 * PyType_Ready if it hasn't already been called 180 */ 181static PyTypeObject _HashInheritanceTester_Type = { 182 PyObject_HEAD_INIT(NULL) 183 0, /* Number of items for varobject */ 184 "hashinheritancetester", /* Name of this type */ 185 sizeof(PyObject), /* Basic object size */ 186 0, /* Item size for varobject */ 187 (destructor)PyObject_Del, /* tp_dealloc */ 188 0, /* tp_print */ 189 0, /* tp_getattr */ 190 0, /* tp_setattr */ 191 0, /* tp_compare */ 192 0, /* tp_repr */ 193 0, /* tp_as_number */ 194 0, /* tp_as_sequence */ 195 0, /* tp_as_mapping */ 196 0, /* tp_hash */ 197 0, /* tp_call */ 198 0, /* tp_str */ 199 PyObject_GenericGetAttr, /* tp_getattro */ 200 0, /* tp_setattro */ 201 0, /* tp_as_buffer */ 202 Py_TPFLAGS_DEFAULT, /* tp_flags */ 203 0, /* tp_doc */ 204 0, /* tp_traverse */ 205 0, /* tp_clear */ 206 0, /* tp_richcompare */ 207 0, /* tp_weaklistoffset */ 208 0, /* tp_iter */ 209 0, /* tp_iternext */ 210 0, /* tp_methods */ 211 0, /* tp_members */ 212 0, /* tp_getset */ 213 0, /* tp_base */ 214 0, /* tp_dict */ 215 0, /* tp_descr_get */ 216 0, /* tp_descr_set */ 217 0, /* tp_dictoffset */ 218 0, /* tp_init */ 219 0, /* tp_alloc */ 220 PyType_GenericNew, /* tp_new */ 221}; 222 223static PyObject* 224test_lazy_hash_inheritance(PyObject* self) 225{ 226 PyTypeObject *type; 227 PyObject *obj; 228 long hash; 229 230 type = &_HashInheritanceTester_Type; 231 232 if (type->tp_dict != NULL) 233 /* The type has already been initialized. This probably means 234 -R is being used. */ 235 Py_RETURN_NONE; 236 237 238 obj = PyObject_New(PyObject, type); 239 if (obj == NULL) { 240 PyErr_Clear(); 241 PyErr_SetString( 242 TestError, 243 "test_lazy_hash_inheritance: failed to create object"); 244 return NULL; 245 } 246 247 if (type->tp_dict != NULL) { 248 PyErr_SetString( 249 TestError, 250 "test_lazy_hash_inheritance: type initialised too soon"); 251 Py_DECREF(obj); 252 return NULL; 253 } 254 255 hash = PyObject_Hash(obj); 256 if ((hash == -1) && PyErr_Occurred()) { 257 PyErr_Clear(); 258 PyErr_SetString( 259 TestError, 260 "test_lazy_hash_inheritance: could not hash object"); 261 Py_DECREF(obj); 262 return NULL; 263 } 264 265 if (type->tp_dict == NULL) { 266 PyErr_SetString( 267 TestError, 268 "test_lazy_hash_inheritance: type not initialised by hash()"); 269 Py_DECREF(obj); 270 return NULL; 271 } 272 273 if (type->tp_hash != PyType_Type.tp_hash) { 274 PyErr_SetString( 275 TestError, 276 "test_lazy_hash_inheritance: unexpected hash function"); 277 Py_DECREF(obj); 278 return NULL; 279 } 280 281 Py_DECREF(obj); 282 283 Py_RETURN_NONE; 284} 285 286 287/* Issue #7385: Check that memoryview() does not crash 288 * when bf_getbuffer returns an error 289 */ 290 291static int 292broken_buffer_getbuffer(PyObject *self, Py_buffer *view, int flags) 293{ 294 PyErr_SetString( 295 TestError, 296 "test_broken_memoryview: expected error in bf_getbuffer"); 297 return -1; 298} 299 300static PyBufferProcs memoryviewtester_as_buffer = { 301 0, /* bf_getreadbuffer */ 302 0, /* bf_getwritebuffer */ 303 0, /* bf_getsegcount */ 304 0, /* bf_getcharbuffer */ 305 (getbufferproc)broken_buffer_getbuffer, /* bf_getbuffer */ 306 0, /* bf_releasebuffer */ 307}; 308 309static PyTypeObject _MemoryViewTester_Type = { 310 PyObject_HEAD_INIT(NULL) 311 0, /* Number of items for varobject */ 312 "memoryviewtester", /* Name of this type */ 313 sizeof(PyObject), /* Basic object size */ 314 0, /* Item size for varobject */ 315 (destructor)PyObject_Del, /* tp_dealloc */ 316 0, /* tp_print */ 317 0, /* tp_getattr */ 318 0, /* tp_setattr */ 319 0, /* tp_compare */ 320 0, /* tp_repr */ 321 0, /* tp_as_number */ 322 0, /* tp_as_sequence */ 323 0, /* tp_as_mapping */ 324 0, /* tp_hash */ 325 0, /* tp_call */ 326 0, /* tp_str */ 327 PyObject_GenericGetAttr, /* tp_getattro */ 328 0, /* tp_setattro */ 329 &memoryviewtester_as_buffer, /* tp_as_buffer */ 330 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */ 331 0, /* tp_doc */ 332 0, /* tp_traverse */ 333 0, /* tp_clear */ 334 0, /* tp_richcompare */ 335 0, /* tp_weaklistoffset */ 336 0, /* tp_iter */ 337 0, /* tp_iternext */ 338 0, /* tp_methods */ 339 0, /* tp_members */ 340 0, /* tp_getset */ 341 0, /* tp_base */ 342 0, /* tp_dict */ 343 0, /* tp_descr_get */ 344 0, /* tp_descr_set */ 345 0, /* tp_dictoffset */ 346 0, /* tp_init */ 347 0, /* tp_alloc */ 348 PyType_GenericNew, /* tp_new */ 349}; 350 351static PyObject* 352test_broken_memoryview(PyObject* self) 353{ 354 PyObject *obj = PyObject_New(PyObject, &_MemoryViewTester_Type); 355 PyObject *res; 356 357 if (obj == NULL) { 358 PyErr_Clear(); 359 PyErr_SetString( 360 TestError, 361 "test_broken_memoryview: failed to create object"); 362 return NULL; 363 } 364 365 res = PyMemoryView_FromObject(obj); 366 if (res || !PyErr_Occurred()){ 367 PyErr_SetString( 368 TestError, 369 "test_broken_memoryview: memoryview() didn't raise an Exception"); 370 Py_XDECREF(res); 371 Py_DECREF(obj); 372 return NULL; 373 } 374 375 PyErr_Clear(); 376 Py_DECREF(obj); 377 Py_RETURN_NONE; 378} 379 380static PyObject * 381test_to_contiguous(PyObject* self, PyObject *noargs) 382{ 383 int data[9] = {0, -1, 1, -1, 2, -1, 3, -1, 4}; 384 int result[5]; 385 Py_ssize_t itemsize = sizeof(int); 386 Py_ssize_t shape = 5; 387 Py_ssize_t strides = 2 * itemsize; 388 Py_buffer view = { 389 data, 390 NULL, 391 5 * itemsize, 392 itemsize, 393 1, 394 1, 395 NULL, 396 &shape, 397 &strides, 398 NULL, 399 {0, 0}, 400 NULL 401 }; 402 int i; 403 404 PyBuffer_ToContiguous(result, &view, view.len, 'C'); 405 for (i = 0; i < 5; i++) { 406 if (result[i] != i) { 407 PyErr_SetString(TestError, 408 "test_to_contiguous: incorrect result"); 409 return NULL; 410 } 411 } 412 413 view.buf = &data[8]; 414 view.strides[0] = -2 * itemsize; 415 416 PyBuffer_ToContiguous(result, &view, view.len, 'C'); 417 for (i = 0; i < 5; i++) { 418 if (result[i] != 4-i) { 419 PyErr_SetString(TestError, 420 "test_to_contiguous: incorrect result"); 421 return NULL; 422 } 423 } 424 425 Py_RETURN_NONE; 426} 427 428static PyObject * 429test_from_contiguous(PyObject* self, PyObject *noargs) 430{ 431 int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; 432 int init[5] = {0, 1, 2, 3, 4}; 433 Py_ssize_t itemsize = sizeof(int); 434 Py_ssize_t shape = 5; 435 Py_ssize_t strides = 2 * itemsize; 436 Py_buffer view = { 437 data, 438 NULL, 439 5 * itemsize, 440 itemsize, 441 1, 442 1, 443 NULL, 444 &shape, 445 &strides, 446 NULL, 447 {0, 0}, 448 NULL 449 }; 450 int *ptr; 451 int i; 452 453 PyBuffer_FromContiguous(&view, init, view.len, 'C'); 454 ptr = view.buf; 455 for (i = 0; i < 5; i++) { 456 if (ptr[2*i] != i) { 457 PyErr_SetString(TestError, 458 "test_from_contiguous: incorrect result"); 459 return NULL; 460 } 461 } 462 463 view.buf = &data[8]; 464 view.strides[0] = -2 * itemsize; 465 466 PyBuffer_FromContiguous(&view, init, view.len, 'C'); 467 ptr = view.buf; 468 for (i = 0; i < 5; i++) { 469 if (*(ptr-2*i) != i) { 470 PyErr_SetString(TestError, 471 "test_from_contiguous: incorrect result"); 472 return NULL; 473 } 474 } 475 476 Py_RETURN_NONE; 477} 478 479 480/* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) 481 PyLong_{As, From}{Unsigned,}LongLong(). 482 483 Note that the meat of the test is contained in testcapi_long.h. 484 This is revolting, but delicate code duplication is worse: "almost 485 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous 486 dependence on type names makes it impossible to use a parameterized 487 function. A giant macro would be even worse than this. A C++ template 488 would be perfect. 489 490 The "report an error" functions are deliberately not part of the #include 491 file: if the test fails, you can set a breakpoint in the appropriate 492 error function directly, and crawl back from there in the debugger. 493*/ 494 495#define UNBIND(X) Py_DECREF(X); (X) = NULL 496 497static PyObject * 498raise_test_long_error(const char* msg) 499{ 500 return raiseTestError("test_long_api", msg); 501} 502 503#define TESTNAME test_long_api_inner 504#define TYPENAME long 505#define F_S_TO_PY PyLong_FromLong 506#define F_PY_TO_S PyLong_AsLong 507#define F_U_TO_PY PyLong_FromUnsignedLong 508#define F_PY_TO_U PyLong_AsUnsignedLong 509 510#include "testcapi_long.h" 511 512static PyObject * 513test_long_api(PyObject* self) 514{ 515 return TESTNAME(raise_test_long_error); 516} 517 518#undef TESTNAME 519#undef TYPENAME 520#undef F_S_TO_PY 521#undef F_PY_TO_S 522#undef F_U_TO_PY 523#undef F_PY_TO_U 524 525#ifdef HAVE_LONG_LONG 526 527static PyObject * 528raise_test_longlong_error(const char* msg) 529{ 530 return raiseTestError("test_longlong_api", msg); 531} 532 533#define TESTNAME test_longlong_api_inner 534#define TYPENAME PY_LONG_LONG 535#define F_S_TO_PY PyLong_FromLongLong 536#define F_PY_TO_S PyLong_AsLongLong 537#define F_U_TO_PY PyLong_FromUnsignedLongLong 538#define F_PY_TO_U PyLong_AsUnsignedLongLong 539 540#include "testcapi_long.h" 541 542static PyObject * 543test_longlong_api(PyObject* self, PyObject *args) 544{ 545 return TESTNAME(raise_test_longlong_error); 546} 547 548#undef TESTNAME 549#undef TYPENAME 550#undef F_S_TO_PY 551#undef F_PY_TO_S 552#undef F_U_TO_PY 553#undef F_PY_TO_U 554 555/* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG 556 is tested by test_long_api_inner. This test will concentrate on proper 557 handling of overflow. 558*/ 559 560static PyObject * 561test_long_and_overflow(PyObject *self) 562{ 563 PyObject *num, *one, *temp; 564 long value; 565 int overflow; 566 567 /* Test that overflow is set properly for a large value. */ 568 /* num is a number larger than LONG_MAX even on 64-bit platforms */ 569 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); 570 if (num == NULL) 571 return NULL; 572 overflow = 1234; 573 value = PyLong_AsLongAndOverflow(num, &overflow); 574 Py_DECREF(num); 575 if (value == -1 && PyErr_Occurred()) 576 return NULL; 577 if (value != -1) 578 return raiseTestError("test_long_and_overflow", 579 "return value was not set to -1"); 580 if (overflow != 1) 581 return raiseTestError("test_long_and_overflow", 582 "overflow was not set to 1"); 583 584 /* Same again, with num = LONG_MAX + 1 */ 585 num = PyLong_FromLong(LONG_MAX); 586 if (num == NULL) 587 return NULL; 588 one = PyLong_FromLong(1L); 589 if (one == NULL) { 590 Py_DECREF(num); 591 return NULL; 592 } 593 temp = PyNumber_Add(num, one); 594 Py_DECREF(one); 595 Py_DECREF(num); 596 num = temp; 597 if (num == NULL) 598 return NULL; 599 overflow = 0; 600 value = PyLong_AsLongAndOverflow(num, &overflow); 601 Py_DECREF(num); 602 if (value == -1 && PyErr_Occurred()) 603 return NULL; 604 if (value != -1) 605 return raiseTestError("test_long_and_overflow", 606 "return value was not set to -1"); 607 if (overflow != 1) 608 return raiseTestError("test_long_and_overflow", 609 "overflow was not set to 1"); 610 611 /* Test that overflow is set properly for a large negative value. */ 612 /* num is a number smaller than LONG_MIN even on 64-bit platforms */ 613 num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); 614 if (num == NULL) 615 return NULL; 616 overflow = 1234; 617 value = PyLong_AsLongAndOverflow(num, &overflow); 618 Py_DECREF(num); 619 if (value == -1 && PyErr_Occurred()) 620 return NULL; 621 if (value != -1) 622 return raiseTestError("test_long_and_overflow", 623 "return value was not set to -1"); 624 if (overflow != -1) 625 return raiseTestError("test_long_and_overflow", 626 "overflow was not set to -1"); 627 628 /* Same again, with num = LONG_MIN - 1 */ 629 num = PyLong_FromLong(LONG_MIN); 630 if (num == NULL) 631 return NULL; 632 one = PyLong_FromLong(1L); 633 if (one == NULL) { 634 Py_DECREF(num); 635 return NULL; 636 } 637 temp = PyNumber_Subtract(num, one); 638 Py_DECREF(one); 639 Py_DECREF(num); 640 num = temp; 641 if (num == NULL) 642 return NULL; 643 overflow = 0; 644 value = PyLong_AsLongAndOverflow(num, &overflow); 645 Py_DECREF(num); 646 if (value == -1 && PyErr_Occurred()) 647 return NULL; 648 if (value != -1) 649 return raiseTestError("test_long_and_overflow", 650 "return value was not set to -1"); 651 if (overflow != -1) 652 return raiseTestError("test_long_and_overflow", 653 "overflow was not set to -1"); 654 655 /* Test that overflow is cleared properly for small values. */ 656 num = PyLong_FromString("FF", NULL, 16); 657 if (num == NULL) 658 return NULL; 659 overflow = 1234; 660 value = PyLong_AsLongAndOverflow(num, &overflow); 661 Py_DECREF(num); 662 if (value == -1 && PyErr_Occurred()) 663 return NULL; 664 if (value != 0xFF) 665 return raiseTestError("test_long_and_overflow", 666 "expected return value 0xFF"); 667 if (overflow != 0) 668 return raiseTestError("test_long_and_overflow", 669 "overflow was not cleared"); 670 671 num = PyLong_FromString("-FF", NULL, 16); 672 if (num == NULL) 673 return NULL; 674 overflow = 0; 675 value = PyLong_AsLongAndOverflow(num, &overflow); 676 Py_DECREF(num); 677 if (value == -1 && PyErr_Occurred()) 678 return NULL; 679 if (value != -0xFF) 680 return raiseTestError("test_long_and_overflow", 681 "expected return value 0xFF"); 682 if (overflow != 0) 683 return raiseTestError("test_long_and_overflow", 684 "overflow was set incorrectly"); 685 686 num = PyLong_FromLong(LONG_MAX); 687 if (num == NULL) 688 return NULL; 689 overflow = 1234; 690 value = PyLong_AsLongAndOverflow(num, &overflow); 691 Py_DECREF(num); 692 if (value == -1 && PyErr_Occurred()) 693 return NULL; 694 if (value != LONG_MAX) 695 return raiseTestError("test_long_and_overflow", 696 "expected return value LONG_MAX"); 697 if (overflow != 0) 698 return raiseTestError("test_long_and_overflow", 699 "overflow was not cleared"); 700 701 num = PyLong_FromLong(LONG_MIN); 702 if (num == NULL) 703 return NULL; 704 overflow = 0; 705 value = PyLong_AsLongAndOverflow(num, &overflow); 706 Py_DECREF(num); 707 if (value == -1 && PyErr_Occurred()) 708 return NULL; 709 if (value != LONG_MIN) 710 return raiseTestError("test_long_and_overflow", 711 "expected return value LONG_MIN"); 712 if (overflow != 0) 713 return raiseTestError("test_long_and_overflow", 714 "overflow was not cleared"); 715 716 Py_INCREF(Py_None); 717 return Py_None; 718} 719 720/* Test the PyLong_AsLongLongAndOverflow API. General conversion to 721 PY_LONG_LONG is tested by test_long_api_inner. This test will 722 concentrate on proper handling of overflow. 723*/ 724 725static PyObject * 726test_long_long_and_overflow(PyObject *self) 727{ 728 PyObject *num, *one, *temp; 729 PY_LONG_LONG value; 730 int overflow; 731 732 /* Test that overflow is set properly for a large value. */ 733 /* num is a number larger than PY_LLONG_MAX on a typical machine. */ 734 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); 735 if (num == NULL) 736 return NULL; 737 overflow = 1234; 738 value = PyLong_AsLongLongAndOverflow(num, &overflow); 739 Py_DECREF(num); 740 if (value == -1 && PyErr_Occurred()) 741 return NULL; 742 if (value != -1) 743 return raiseTestError("test_long_long_and_overflow", 744 "return value was not set to -1"); 745 if (overflow != 1) 746 return raiseTestError("test_long_long_and_overflow", 747 "overflow was not set to 1"); 748 749 /* Same again, with num = PY_LLONG_MAX + 1 */ 750 num = PyLong_FromLongLong(PY_LLONG_MAX); 751 if (num == NULL) 752 return NULL; 753 one = PyLong_FromLong(1L); 754 if (one == NULL) { 755 Py_DECREF(num); 756 return NULL; 757 } 758 temp = PyNumber_Add(num, one); 759 Py_DECREF(one); 760 Py_DECREF(num); 761 num = temp; 762 if (num == NULL) 763 return NULL; 764 overflow = 0; 765 value = PyLong_AsLongLongAndOverflow(num, &overflow); 766 Py_DECREF(num); 767 if (value == -1 && PyErr_Occurred()) 768 return NULL; 769 if (value != -1) 770 return raiseTestError("test_long_long_and_overflow", 771 "return value was not set to -1"); 772 if (overflow != 1) 773 return raiseTestError("test_long_long_and_overflow", 774 "overflow was not set to 1"); 775 776 /* Test that overflow is set properly for a large negative value. */ 777 /* num is a number smaller than PY_LLONG_MIN on a typical platform */ 778 num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); 779 if (num == NULL) 780 return NULL; 781 overflow = 1234; 782 value = PyLong_AsLongLongAndOverflow(num, &overflow); 783 Py_DECREF(num); 784 if (value == -1 && PyErr_Occurred()) 785 return NULL; 786 if (value != -1) 787 return raiseTestError("test_long_long_and_overflow", 788 "return value was not set to -1"); 789 if (overflow != -1) 790 return raiseTestError("test_long_long_and_overflow", 791 "overflow was not set to -1"); 792 793 /* Same again, with num = PY_LLONG_MIN - 1 */ 794 num = PyLong_FromLongLong(PY_LLONG_MIN); 795 if (num == NULL) 796 return NULL; 797 one = PyLong_FromLong(1L); 798 if (one == NULL) { 799 Py_DECREF(num); 800 return NULL; 801 } 802 temp = PyNumber_Subtract(num, one); 803 Py_DECREF(one); 804 Py_DECREF(num); 805 num = temp; 806 if (num == NULL) 807 return NULL; 808 overflow = 0; 809 value = PyLong_AsLongLongAndOverflow(num, &overflow); 810 Py_DECREF(num); 811 if (value == -1 && PyErr_Occurred()) 812 return NULL; 813 if (value != -1) 814 return raiseTestError("test_long_long_and_overflow", 815 "return value was not set to -1"); 816 if (overflow != -1) 817 return raiseTestError("test_long_long_and_overflow", 818 "overflow was not set to -1"); 819 820 /* Test that overflow is cleared properly for small values. */ 821 num = PyLong_FromString("FF", NULL, 16); 822 if (num == NULL) 823 return NULL; 824 overflow = 1234; 825 value = PyLong_AsLongLongAndOverflow(num, &overflow); 826 Py_DECREF(num); 827 if (value == -1 && PyErr_Occurred()) 828 return NULL; 829 if (value != 0xFF) 830 return raiseTestError("test_long_long_and_overflow", 831 "expected return value 0xFF"); 832 if (overflow != 0) 833 return raiseTestError("test_long_long_and_overflow", 834 "overflow was not cleared"); 835 836 num = PyLong_FromString("-FF", NULL, 16); 837 if (num == NULL) 838 return NULL; 839 overflow = 0; 840 value = PyLong_AsLongLongAndOverflow(num, &overflow); 841 Py_DECREF(num); 842 if (value == -1 && PyErr_Occurred()) 843 return NULL; 844 if (value != -0xFF) 845 return raiseTestError("test_long_long_and_overflow", 846 "expected return value 0xFF"); 847 if (overflow != 0) 848 return raiseTestError("test_long_long_and_overflow", 849 "overflow was set incorrectly"); 850 851 num = PyLong_FromLongLong(PY_LLONG_MAX); 852 if (num == NULL) 853 return NULL; 854 overflow = 1234; 855 value = PyLong_AsLongLongAndOverflow(num, &overflow); 856 Py_DECREF(num); 857 if (value == -1 && PyErr_Occurred()) 858 return NULL; 859 if (value != PY_LLONG_MAX) 860 return raiseTestError("test_long_long_and_overflow", 861 "expected return value PY_LLONG_MAX"); 862 if (overflow != 0) 863 return raiseTestError("test_long_long_and_overflow", 864 "overflow was not cleared"); 865 866 num = PyLong_FromLongLong(PY_LLONG_MIN); 867 if (num == NULL) 868 return NULL; 869 overflow = 0; 870 value = PyLong_AsLongLongAndOverflow(num, &overflow); 871 Py_DECREF(num); 872 if (value == -1 && PyErr_Occurred()) 873 return NULL; 874 if (value != PY_LLONG_MIN) 875 return raiseTestError("test_long_long_and_overflow", 876 "expected return value PY_LLONG_MIN"); 877 if (overflow != 0) 878 return raiseTestError("test_long_long_and_overflow", 879 "overflow was not cleared"); 880 881 Py_INCREF(Py_None); 882 return Py_None; 883} 884 885/* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG 886 for both long and int arguments. The test may leak a little memory if 887 it fails. 888*/ 889static PyObject * 890test_L_code(PyObject *self) 891{ 892 PyObject *tuple, *num; 893 PY_LONG_LONG value; 894 895 tuple = PyTuple_New(1); 896 if (tuple == NULL) 897 return NULL; 898 899 num = PyLong_FromLong(42); 900 if (num == NULL) 901 return NULL; 902 903 PyTuple_SET_ITEM(tuple, 0, num); 904 905 value = -1; 906 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) 907 return NULL; 908 if (value != 42) 909 return raiseTestError("test_L_code", 910 "L code returned wrong value for long 42"); 911 912 Py_DECREF(num); 913 num = PyInt_FromLong(42); 914 if (num == NULL) 915 return NULL; 916 917 PyTuple_SET_ITEM(tuple, 0, num); 918 919 value = -1; 920 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) 921 return NULL; 922 if (value != 42) 923 return raiseTestError("test_L_code", 924 "L code returned wrong value for int 42"); 925 926 Py_DECREF(tuple); 927 Py_INCREF(Py_None); 928 return Py_None; 929} 930 931#endif /* ifdef HAVE_LONG_LONG */ 932 933static PyObject * 934return_none(void *unused) 935{ 936 Py_RETURN_NONE; 937} 938 939static PyObject * 940raise_error(void *unused) 941{ 942 PyErr_SetNone(PyExc_ValueError); 943 return NULL; 944} 945 946static int 947test_buildvalue_N_error(const char *fmt) 948{ 949 PyObject *arg, *res; 950 951 arg = PyList_New(0); 952 if (arg == NULL) { 953 return -1; 954 } 955 956 Py_INCREF(arg); 957 res = Py_BuildValue(fmt, return_none, NULL, arg); 958 if (res == NULL) { 959 return -1; 960 } 961 Py_DECREF(res); 962 if (Py_REFCNT(arg) != 1) { 963 PyErr_Format(TestError, "test_buildvalue_N: " 964 "arg was not decrefed in successful " 965 "Py_BuildValue(\"%s\")", fmt); 966 return -1; 967 } 968 969 Py_INCREF(arg); 970 res = Py_BuildValue(fmt, raise_error, NULL, arg); 971 if (res != NULL || !PyErr_Occurred()) { 972 PyErr_Format(TestError, "test_buildvalue_N: " 973 "Py_BuildValue(\"%s\") didn't complain", fmt); 974 return -1; 975 } 976 PyErr_Clear(); 977 if (Py_REFCNT(arg) != 1) { 978 PyErr_Format(TestError, "test_buildvalue_N: " 979 "arg was not decrefed in failed " 980 "Py_BuildValue(\"%s\")", fmt); 981 return -1; 982 } 983 Py_DECREF(arg); 984 return 0; 985} 986 987static PyObject * 988test_buildvalue_N(PyObject *self, PyObject *noargs) 989{ 990 PyObject *arg, *res; 991 992 arg = PyList_New(0); 993 if (arg == NULL) { 994 return NULL; 995 } 996 Py_INCREF(arg); 997 res = Py_BuildValue("N", arg); 998 if (res == NULL) { 999 return NULL; 1000 } 1001 if (res != arg) { 1002 return raiseTestError("test_buildvalue_N", 1003 "Py_BuildValue(\"N\") returned wrong result"); 1004 } 1005 if (Py_REFCNT(arg) != 2) { 1006 return raiseTestError("test_buildvalue_N", 1007 "arg was not decrefed in Py_BuildValue(\"N\")"); 1008 } 1009 Py_DECREF(res); 1010 Py_DECREF(arg); 1011 1012 if (test_buildvalue_N_error("O&N") < 0) 1013 return NULL; 1014 if (test_buildvalue_N_error("(O&N)") < 0) 1015 return NULL; 1016 if (test_buildvalue_N_error("[O&N]") < 0) 1017 return NULL; 1018 if (test_buildvalue_N_error("{O&N}") < 0) 1019 return NULL; 1020 if (test_buildvalue_N_error("{()O&(())N}") < 0) 1021 return NULL; 1022 1023 Py_RETURN_NONE; 1024} 1025 1026 1027static PyObject * 1028get_args(PyObject *self, PyObject *args) 1029{ 1030 if (args == NULL) { 1031 args = Py_None; 1032 } 1033 Py_INCREF(args); 1034 return args; 1035} 1036 1037static PyObject * 1038get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs) 1039{ 1040 if (kwargs == NULL) { 1041 kwargs = Py_None; 1042 } 1043 Py_INCREF(kwargs); 1044 return kwargs; 1045} 1046 1047/* Test tuple argument processing */ 1048static PyObject * 1049getargs_tuple(PyObject *self, PyObject *args) 1050{ 1051 int a, b, c; 1052 if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) 1053 return NULL; 1054 return Py_BuildValue("iii", a, b, c); 1055} 1056 1057/* test PyArg_ParseTupleAndKeywords */ 1058static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) 1059{ 1060 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; 1061 static char *fmt="(ii)i|(i(ii))(iii)i"; 1062 int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; 1063 1064 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, 1065 &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], 1066 &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) 1067 return NULL; 1068 return Py_BuildValue("iiiiiiiiii", 1069 int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], 1070 int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]); 1071} 1072 1073/* Functions to call PyArg_ParseTuple with integer format codes, 1074 and return the result. 1075*/ 1076static PyObject * 1077getargs_b(PyObject *self, PyObject *args) 1078{ 1079 unsigned char value; 1080 if (!PyArg_ParseTuple(args, "b", &value)) 1081 return NULL; 1082 return PyLong_FromUnsignedLong((unsigned long)value); 1083} 1084 1085static PyObject * 1086getargs_B(PyObject *self, PyObject *args) 1087{ 1088 unsigned char value; 1089 if (!PyArg_ParseTuple(args, "B", &value)) 1090 return NULL; 1091 return PyLong_FromUnsignedLong((unsigned long)value); 1092} 1093 1094static PyObject * 1095getargs_h(PyObject *self, PyObject *args) 1096{ 1097 short value; 1098 if (!PyArg_ParseTuple(args, "h", &value)) 1099 return NULL; 1100 return PyLong_FromLong((long)value); 1101} 1102 1103static PyObject * 1104getargs_H(PyObject *self, PyObject *args) 1105{ 1106 unsigned short value; 1107 if (!PyArg_ParseTuple(args, "H", &value)) 1108 return NULL; 1109 return PyLong_FromUnsignedLong((unsigned long)value); 1110} 1111 1112static PyObject * 1113getargs_I(PyObject *self, PyObject *args) 1114{ 1115 unsigned int value; 1116 if (!PyArg_ParseTuple(args, "I", &value)) 1117 return NULL; 1118 return PyLong_FromUnsignedLong((unsigned long)value); 1119} 1120 1121static PyObject * 1122getargs_k(PyObject *self, PyObject *args) 1123{ 1124 unsigned long value; 1125 if (!PyArg_ParseTuple(args, "k", &value)) 1126 return NULL; 1127 return PyLong_FromUnsignedLong(value); 1128} 1129 1130static PyObject * 1131getargs_i(PyObject *self, PyObject *args) 1132{ 1133 int value; 1134 if (!PyArg_ParseTuple(args, "i", &value)) 1135 return NULL; 1136 return PyLong_FromLong((long)value); 1137} 1138 1139static PyObject * 1140getargs_l(PyObject *self, PyObject *args) 1141{ 1142 long value; 1143 if (!PyArg_ParseTuple(args, "l", &value)) 1144 return NULL; 1145 return PyLong_FromLong(value); 1146} 1147 1148static PyObject * 1149getargs_n(PyObject *self, PyObject *args) 1150{ 1151 Py_ssize_t value; 1152 if (!PyArg_ParseTuple(args, "n", &value)) 1153 return NULL; 1154 return PyInt_FromSsize_t(value); 1155} 1156 1157#ifdef HAVE_LONG_LONG 1158static PyObject * 1159getargs_L(PyObject *self, PyObject *args) 1160{ 1161 PY_LONG_LONG value; 1162 if (!PyArg_ParseTuple(args, "L", &value)) 1163 return NULL; 1164 return PyLong_FromLongLong(value); 1165} 1166 1167static PyObject * 1168getargs_K(PyObject *self, PyObject *args) 1169{ 1170 unsigned PY_LONG_LONG value; 1171 if (!PyArg_ParseTuple(args, "K", &value)) 1172 return NULL; 1173 return PyLong_FromUnsignedLongLong(value); 1174} 1175#endif 1176 1177/* This function not only tests the 'k' getargs code, but also the 1178 PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */ 1179static PyObject * 1180test_k_code(PyObject *self) 1181{ 1182 PyObject *tuple, *num; 1183 unsigned long value; 1184 1185 tuple = PyTuple_New(1); 1186 if (tuple == NULL) 1187 return NULL; 1188 1189 /* a number larger than ULONG_MAX even on 64-bit platforms */ 1190 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); 1191 if (num == NULL) 1192 return NULL; 1193 1194 value = PyInt_AsUnsignedLongMask(num); 1195 if (value != ULONG_MAX) 1196 return raiseTestError("test_k_code", 1197 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); 1198 1199 PyTuple_SET_ITEM(tuple, 0, num); 1200 1201 value = 0; 1202 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) 1203 return NULL; 1204 if (value != ULONG_MAX) 1205 return raiseTestError("test_k_code", 1206 "k code returned wrong value for long 0xFFF...FFF"); 1207 1208 Py_DECREF(num); 1209 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); 1210 if (num == NULL) 1211 return NULL; 1212 1213 value = PyInt_AsUnsignedLongMask(num); 1214 if (value != (unsigned long)-0x42) 1215 return raiseTestError("test_k_code", 1216 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); 1217 1218 PyTuple_SET_ITEM(tuple, 0, num); 1219 1220 value = 0; 1221 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) 1222 return NULL; 1223 if (value != (unsigned long)-0x42) 1224 return raiseTestError("test_k_code", 1225 "k code returned wrong value for long -0xFFF..000042"); 1226 1227 Py_DECREF(tuple); 1228 Py_INCREF(Py_None); 1229 return Py_None; 1230} 1231 1232static PyObject * 1233getargs_f(PyObject *self, PyObject *args) 1234{ 1235 float f; 1236 if (!PyArg_ParseTuple(args, "f", &f)) 1237 return NULL; 1238 return PyFloat_FromDouble(f); 1239} 1240 1241static PyObject * 1242getargs_d(PyObject *self, PyObject *args) 1243{ 1244 double d; 1245 if (!PyArg_ParseTuple(args, "d", &d)) 1246 return NULL; 1247 return PyFloat_FromDouble(d); 1248} 1249 1250static PyObject * 1251getargs_D(PyObject *self, PyObject *args) 1252{ 1253 Py_complex cval; 1254 if (!PyArg_ParseTuple(args, "D", &cval)) 1255 return NULL; 1256 return PyComplex_FromCComplex(cval); 1257} 1258 1259static PyObject * 1260getargs_S(PyObject *self, PyObject *args) 1261{ 1262 PyObject *obj; 1263 if (!PyArg_ParseTuple(args, "S", &obj)) 1264 return NULL; 1265 Py_INCREF(obj); 1266 return obj; 1267} 1268 1269static PyObject * 1270getargs_Y(PyObject *self, PyObject *args) 1271{ 1272 PyObject *obj; 1273 if (!PyArg_ParseTuple(args, "Y", &obj)) 1274 return NULL; 1275 Py_INCREF(obj); 1276 return obj; 1277} 1278 1279#ifdef Py_USING_UNICODE 1280static PyObject * 1281getargs_U(PyObject *self, PyObject *args) 1282{ 1283 PyObject *obj; 1284 if (!PyArg_ParseTuple(args, "U", &obj)) 1285 return NULL; 1286 Py_INCREF(obj); 1287 return obj; 1288} 1289#endif 1290 1291static PyObject * 1292getargs_c(PyObject *self, PyObject *args) 1293{ 1294 char c; 1295 if (!PyArg_ParseTuple(args, "c", &c)) 1296 return NULL; 1297 return PyInt_FromLong((unsigned char)c); 1298} 1299 1300static PyObject * 1301getargs_s(PyObject *self, PyObject *args) 1302{ 1303 const char *str; 1304 if (!PyArg_ParseTuple(args, "s", &str)) 1305 return NULL; 1306 return PyBytes_FromString(str); 1307} 1308 1309static PyObject * 1310getargs_s_star(PyObject *self, PyObject *args) 1311{ 1312 Py_buffer buffer; 1313 PyObject *bytes; 1314 if (!PyArg_ParseTuple(args, "s*", &buffer)) 1315 return NULL; 1316 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); 1317 PyBuffer_Release(&buffer); 1318 return bytes; 1319} 1320 1321static PyObject * 1322getargs_s_hash(PyObject *self, PyObject *args) 1323{ 1324 const char *str; 1325 int size; 1326 if (!PyArg_ParseTuple(args, "s#", &str, &size)) 1327 return NULL; 1328 return PyBytes_FromStringAndSize(str, size); 1329} 1330 1331static PyObject * 1332getargs_t_hash(PyObject *self, PyObject *args) 1333{ 1334 const char *str; 1335 int size; 1336 if (!PyArg_ParseTuple(args, "t#", &str, &size)) 1337 return NULL; 1338 return PyBytes_FromStringAndSize(str, size); 1339} 1340 1341static PyObject * 1342getargs_z(PyObject *self, PyObject *args) 1343{ 1344 const char *str; 1345 if (!PyArg_ParseTuple(args, "z", &str)) 1346 return NULL; 1347 if (str != NULL) 1348 return PyBytes_FromString(str); 1349 else 1350 Py_RETURN_NONE; 1351} 1352 1353static PyObject * 1354getargs_z_star(PyObject *self, PyObject *args) 1355{ 1356 Py_buffer buffer; 1357 PyObject *bytes; 1358 if (!PyArg_ParseTuple(args, "z*", &buffer)) 1359 return NULL; 1360 if (buffer.buf != NULL) 1361 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); 1362 else { 1363 Py_INCREF(Py_None); 1364 bytes = Py_None; 1365 } 1366 PyBuffer_Release(&buffer); 1367 return bytes; 1368} 1369 1370static PyObject * 1371getargs_z_hash(PyObject *self, PyObject *args) 1372{ 1373 const char *str; 1374 int size; 1375 if (!PyArg_ParseTuple(args, "z#", &str, &size)) 1376 return NULL; 1377 if (str != NULL) 1378 return PyBytes_FromStringAndSize(str, size); 1379 else 1380 Py_RETURN_NONE; 1381} 1382 1383static PyObject * 1384getargs_w(PyObject *self, PyObject *args) 1385{ 1386 char *str; 1387 Py_ssize_t size; 1388 1389 if (!PyArg_ParseTuple(args, "wn", &str, &size)) 1390 return NULL; 1391 1392 if (2 <= size) { 1393 str[0] = '['; 1394 str[size-1] = ']'; 1395 } 1396 1397 return PyBytes_FromStringAndSize(str, size); 1398} 1399 1400static PyObject * 1401getargs_w_hash(PyObject *self, PyObject *args) 1402{ 1403 char *str; 1404 int size; 1405 1406 if (!PyArg_ParseTuple(args, "w#", &str, &size)) 1407 return NULL; 1408 1409 if (2 <= size) { 1410 str[0] = '['; 1411 str[size-1] = ']'; 1412 } 1413 1414 return PyBytes_FromStringAndSize(str, size); 1415} 1416 1417static PyObject * 1418getargs_w_star(PyObject *self, PyObject *args) 1419{ 1420 Py_buffer buffer; 1421 PyObject *result; 1422 char *str; 1423 1424 if (!PyArg_ParseTuple(args, "w*", &buffer)) 1425 return NULL; 1426 1427 if (2 <= buffer.len) { 1428 str = buffer.buf; 1429 str[0] = '['; 1430 str[buffer.len-1] = ']'; 1431 } 1432 1433 result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); 1434 PyBuffer_Release(&buffer); 1435 return result; 1436} 1437 1438#ifdef Py_USING_UNICODE 1439 1440static int 1441_ustrlen(const Py_UNICODE *u) 1442{ 1443 int i = 0; 1444 const Py_UNICODE *v = u; 1445 while (*v != 0) { i++; v++; } 1446 return i; 1447} 1448 1449static PyObject * 1450getargs_u(PyObject *self, PyObject *args) 1451{ 1452 const Py_UNICODE *str; 1453 int size; 1454 if (!PyArg_ParseTuple(args, "u", &str)) 1455 return NULL; 1456 size = _ustrlen(str); 1457 return PyUnicode_FromUnicode(str, size); 1458} 1459 1460static PyObject * 1461getargs_u_hash(PyObject *self, PyObject *args) 1462{ 1463 const Py_UNICODE *str; 1464 int size; 1465 if (!PyArg_ParseTuple(args, "u#", &str, &size)) 1466 return NULL; 1467 return PyUnicode_FromUnicode(str, size); 1468} 1469 1470#endif 1471 1472static PyObject * 1473getargs_es(PyObject *self, PyObject *args) 1474{ 1475 PyObject *arg, *result; 1476 const char *encoding = NULL; 1477 char *str; 1478 1479 if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) 1480 return NULL; 1481 if (!PyArg_Parse(arg, "es", encoding, &str)) 1482 return NULL; 1483 result = PyBytes_FromString(str); 1484 PyMem_Free(str); 1485 return result; 1486} 1487 1488static PyObject * 1489getargs_et(PyObject *self, PyObject *args) 1490{ 1491 PyObject *arg, *result; 1492 const char *encoding = NULL; 1493 char *str; 1494 1495 if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) 1496 return NULL; 1497 if (!PyArg_Parse(arg, "et", encoding, &str)) 1498 return NULL; 1499 result = PyBytes_FromString(str); 1500 PyMem_Free(str); 1501 return result; 1502} 1503 1504static PyObject * 1505getargs_es_hash(PyObject *self, PyObject *args) 1506{ 1507 PyObject *arg, *result; 1508 const char *encoding = NULL; 1509 PyByteArrayObject *buffer = NULL; 1510 char *str = NULL; 1511 int size; 1512 1513 if (!PyArg_ParseTuple(args, "O|sO!", 1514 &arg, &encoding, &PyByteArray_Type, &buffer)) 1515 return NULL; 1516 if (buffer != NULL) { 1517 str = PyByteArray_AS_STRING(buffer); 1518 size = PyByteArray_GET_SIZE(buffer); 1519 } 1520 if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) 1521 return NULL; 1522 result = PyBytes_FromStringAndSize(str, size); 1523 if (buffer == NULL) 1524 PyMem_Free(str); 1525 return result; 1526} 1527 1528static PyObject * 1529getargs_et_hash(PyObject *self, PyObject *args) 1530{ 1531 PyObject *arg, *result; 1532 const char *encoding = NULL; 1533 PyByteArrayObject *buffer = NULL; 1534 char *str = NULL; 1535 int size; 1536 1537 if (!PyArg_ParseTuple(args, "O|sO!", 1538 &arg, &encoding, &PyByteArray_Type, &buffer)) 1539 return NULL; 1540 if (buffer != NULL) { 1541 str = PyByteArray_AS_STRING(buffer); 1542 size = PyByteArray_GET_SIZE(buffer); 1543 } 1544 if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) 1545 return NULL; 1546 result = PyBytes_FromStringAndSize(str, size); 1547 if (buffer == NULL) 1548 PyMem_Free(str); 1549 return result; 1550} 1551 1552#ifdef Py_USING_UNICODE 1553 1554static volatile int x; 1555 1556/* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case 1557 of an error. 1558*/ 1559static PyObject * 1560test_u_code(PyObject *self) 1561{ 1562 PyObject *tuple, *obj; 1563 Py_UNICODE *value; 1564 int len; 1565 1566 /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ 1567 /* Just use the macro and check that it compiles */ 1568 x = Py_UNICODE_ISSPACE(25); 1569 1570 tuple = PyTuple_New(1); 1571 if (tuple == NULL) 1572 return NULL; 1573 1574 obj = PyUnicode_Decode("test", strlen("test"), 1575 "ascii", NULL); 1576 if (obj == NULL) 1577 return NULL; 1578 1579 PyTuple_SET_ITEM(tuple, 0, obj); 1580 1581 value = 0; 1582 if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) 1583 return NULL; 1584 if (value != PyUnicode_AS_UNICODE(obj)) 1585 return raiseTestError("test_u_code", 1586 "u code returned wrong value for u'test'"); 1587 value = 0; 1588 if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) 1589 return NULL; 1590 if (value != PyUnicode_AS_UNICODE(obj) || 1591 len != PyUnicode_GET_SIZE(obj)) 1592 return raiseTestError("test_u_code", 1593 "u# code returned wrong values for u'test'"); 1594 1595 Py_DECREF(tuple); 1596 Py_INCREF(Py_None); 1597 return Py_None; 1598} 1599 1600static PyObject * 1601test_widechar(PyObject *self) 1602{ 1603#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) 1604 const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; 1605 size_t wtextlen = 1; 1606#else 1607 const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; 1608 size_t wtextlen = 2; 1609#endif 1610 PyObject *wide, *utf8; 1611 1612 wide = PyUnicode_FromWideChar(wtext, wtextlen); 1613 if (wide == NULL) 1614 return NULL; 1615 1616 utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); 1617 if (utf8 == NULL) { 1618 Py_DECREF(wide); 1619 return NULL; 1620 } 1621 1622 if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) { 1623 Py_DECREF(wide); 1624 Py_DECREF(utf8); 1625 return raiseTestError("test_widechar", 1626 "wide string and utf8 string have different length"); 1627 } 1628 if (PyUnicode_Compare(wide, utf8)) { 1629 Py_DECREF(wide); 1630 Py_DECREF(utf8); 1631 if (PyErr_Occurred()) 1632 return NULL; 1633 return raiseTestError("test_widechar", 1634 "wide string and utf8 string are differents"); 1635 } 1636 1637 Py_DECREF(wide); 1638 Py_DECREF(utf8); 1639 Py_RETURN_NONE; 1640} 1641 1642static PyObject * 1643unicode_encodedecimal(PyObject *self, PyObject *args) 1644{ 1645 Py_UNICODE *unicode; 1646 int length; 1647 char *errors = NULL; 1648 PyObject *decimal; 1649 Py_ssize_t decimal_length, new_length; 1650 int res; 1651 1652 if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) 1653 return NULL; 1654 1655 decimal_length = length * 10; /* len('') */ 1656 decimal = PyBytes_FromStringAndSize(NULL, decimal_length); 1657 if (decimal == NULL) 1658 return NULL; 1659 1660 res = PyUnicode_EncodeDecimal(unicode, length, 1661 PyBytes_AS_STRING(decimal), 1662 errors); 1663 if (res < 0) { 1664 Py_DECREF(decimal); 1665 return NULL; 1666 } 1667 1668 new_length = strlen(PyBytes_AS_STRING(decimal)); 1669 assert(new_length <= decimal_length); 1670 res = _PyBytes_Resize(&decimal, new_length); 1671 if (res < 0) 1672 return NULL; 1673 1674 return decimal; 1675} 1676 1677static PyObject * 1678test_empty_argparse(PyObject *self) 1679{ 1680 /* Test that formats can begin with '|'. See issue #4720. */ 1681 PyObject *tuple, *dict = NULL; 1682 static char *kwlist[] = {NULL}; 1683 int result; 1684 tuple = PyTuple_New(0); 1685 if (!tuple) 1686 return NULL; 1687 if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) 1688 goto done; 1689 dict = PyDict_New(); 1690 if (!dict) 1691 goto done; 1692 result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); 1693 done: 1694 Py_DECREF(tuple); 1695 Py_XDECREF(dict); 1696 if (result < 0) 1697 return NULL; 1698 else { 1699 Py_RETURN_NONE; 1700 } 1701} 1702 1703static PyObject * 1704codec_incrementalencoder(PyObject *self, PyObject *args) 1705{ 1706 const char *encoding, *errors = NULL; 1707 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", 1708 &encoding, &errors)) 1709 return NULL; 1710 return PyCodec_IncrementalEncoder(encoding, errors); 1711} 1712 1713static PyObject * 1714codec_incrementaldecoder(PyObject *self, PyObject *args) 1715{ 1716 const char *encoding, *errors = NULL; 1717 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", 1718 &encoding, &errors)) 1719 return NULL; 1720 return PyCodec_IncrementalDecoder(encoding, errors); 1721} 1722 1723#endif 1724 1725/* Simple test of _PyLong_NumBits and _PyLong_Sign. */ 1726static PyObject * 1727test_long_numbits(PyObject *self) 1728{ 1729 struct triple { 1730 long input; 1731 size_t nbits; 1732 int sign; 1733 } testcases[] = {{0, 0, 0}, 1734 {1L, 1, 1}, 1735 {-1L, 1, -1}, 1736 {2L, 2, 1}, 1737 {-2L, 2, -1}, 1738 {3L, 2, 1}, 1739 {-3L, 2, -1}, 1740 {4L, 3, 1}, 1741 {-4L, 3, -1}, 1742 {0x7fffL, 15, 1}, /* one Python long digit */ 1743 {-0x7fffL, 15, -1}, 1744 {0xffffL, 16, 1}, 1745 {-0xffffL, 16, -1}, 1746 {0xfffffffL, 28, 1}, 1747 {-0xfffffffL, 28, -1}}; 1748 int i; 1749 1750 for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { 1751 PyObject *plong = PyLong_FromLong(testcases[i].input); 1752 size_t nbits = _PyLong_NumBits(plong); 1753 int sign = _PyLong_Sign(plong); 1754 1755 Py_DECREF(plong); 1756 if (nbits != testcases[i].nbits) 1757 return raiseTestError("test_long_numbits", 1758 "wrong result for _PyLong_NumBits"); 1759 if (sign != testcases[i].sign) 1760 return raiseTestError("test_long_numbits", 1761 "wrong result for _PyLong_Sign"); 1762 } 1763 Py_INCREF(Py_None); 1764 return Py_None; 1765} 1766 1767/* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */ 1768 1769static PyObject * 1770test_null_strings(PyObject *self) 1771{ 1772 PyObject *o1 = PyObject_Str(NULL); 1773#ifdef Py_USING_UNICODE 1774 PyObject *o2 = PyObject_Unicode(NULL); 1775#else 1776 PyObject *o2 = PyObject_Str(NULL); 1777#endif 1778 PyObject *tuple = PyTuple_Pack(2, o1, o2); 1779 Py_XDECREF(o1); 1780 Py_XDECREF(o2); 1781 return tuple; 1782} 1783 1784static PyObject * 1785raise_exception(PyObject *self, PyObject *args) 1786{ 1787 PyObject *exc; 1788 PyObject *exc_args, *v; 1789 int num_args, i; 1790 1791 if (!PyArg_ParseTuple(args, "Oi:raise_exception", 1792 &exc, &num_args)) 1793 return NULL; 1794 if (!PyExceptionClass_Check(exc)) { 1795 PyErr_Format(PyExc_TypeError, "an exception class is required"); 1796 return NULL; 1797 } 1798 1799 exc_args = PyTuple_New(num_args); 1800 if (exc_args == NULL) 1801 return NULL; 1802 for (i = 0; i < num_args; ++i) { 1803 v = PyInt_FromLong(i); 1804 if (v == NULL) { 1805 Py_DECREF(exc_args); 1806 return NULL; 1807 } 1808 PyTuple_SET_ITEM(exc_args, i, v); 1809 } 1810 PyErr_SetObject(exc, exc_args); 1811 Py_DECREF(exc_args); 1812 return NULL; 1813} 1814 1815static PyObject * 1816set_errno(PyObject *self, PyObject *args) 1817{ 1818 int new_errno; 1819 1820 if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno)) 1821 return NULL; 1822 1823 errno = new_errno; 1824 Py_RETURN_NONE; 1825} 1826 1827#if defined(Py_USING_UNICODE) && !defined(Py_BUILD_CORE) 1828static int test_run_counter = 0; 1829 1830static PyObject * 1831test_datetime_capi(PyObject *self, PyObject *args) { 1832 if (PyDateTimeAPI) { 1833 if (test_run_counter) { 1834 /* Probably regrtest.py -R */ 1835 Py_RETURN_NONE; 1836 } 1837 else { 1838 PyErr_SetString(PyExc_AssertionError, 1839 "PyDateTime_CAPI somehow initialized"); 1840 return NULL; 1841 } 1842 } 1843 test_run_counter++; 1844 PyDateTime_IMPORT; 1845 if (PyDateTimeAPI) 1846 Py_RETURN_NONE; 1847 else 1848 return NULL; 1849} 1850#endif 1851 1852 1853#ifdef WITH_THREAD 1854 1855/* test_thread_state spawns a thread of its own, and that thread releases 1856 * `thread_done` when it's finished. The driver code has to know when the 1857 * thread finishes, because the thread uses a PyObject (the callable) that 1858 * may go away when the driver finishes. The former lack of this explicit 1859 * synchronization caused rare segfaults, so rare that they were seen only 1860 * on a Mac buildbot (although they were possible on any box). 1861 */ 1862static PyThread_type_lock thread_done = NULL; 1863 1864static int 1865_make_call(void *callable) 1866{ 1867 PyObject *rc; 1868 int success; 1869 PyGILState_STATE s = PyGILState_Ensure(); 1870 rc = PyObject_CallFunction((PyObject *)callable, ""); 1871 success = (rc != NULL); 1872 Py_XDECREF(rc); 1873 PyGILState_Release(s); 1874 return success; 1875} 1876 1877/* Same thing, but releases `thread_done` when it returns. This variant 1878 * should be called only from threads spawned by test_thread_state(). 1879 */ 1880static void 1881_make_call_from_thread(void *callable) 1882{ 1883 _make_call(callable); 1884 PyThread_release_lock(thread_done); 1885} 1886 1887static PyObject * 1888test_thread_state(PyObject *self, PyObject *args) 1889{ 1890 PyObject *fn; 1891 int success = 1; 1892 1893 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) 1894 return NULL; 1895 1896 if (!PyCallable_Check(fn)) { 1897 PyErr_Format(PyExc_TypeError, "'%s' object is not callable", 1898 fn->ob_type->tp_name); 1899 return NULL; 1900 } 1901 1902 /* Ensure Python is set up for threading */ 1903 PyEval_InitThreads(); 1904 thread_done = PyThread_allocate_lock(); 1905 if (thread_done == NULL) 1906 return PyErr_NoMemory(); 1907 PyThread_acquire_lock(thread_done, 1); 1908 1909 /* Start a new thread with our callback. */ 1910 PyThread_start_new_thread(_make_call_from_thread, fn); 1911 /* Make the callback with the thread lock held by this thread */ 1912 success &= _make_call(fn); 1913 /* Do it all again, but this time with the thread-lock released */ 1914 Py_BEGIN_ALLOW_THREADS 1915 success &= _make_call(fn); 1916 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ 1917 Py_END_ALLOW_THREADS 1918 1919 /* And once more with and without a thread 1920 XXX - should use a lock and work out exactly what we are trying 1921 to test <wink> 1922 */ 1923 Py_BEGIN_ALLOW_THREADS 1924 PyThread_start_new_thread(_make_call_from_thread, fn); 1925 success &= _make_call(fn); 1926 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ 1927 Py_END_ALLOW_THREADS 1928 1929 /* Release lock we acquired above. This is required on HP-UX. */ 1930 PyThread_release_lock(thread_done); 1931 1932 PyThread_free_lock(thread_done); 1933 if (!success) 1934 return NULL; 1935 Py_RETURN_NONE; 1936} 1937 1938/* test Py_AddPendingCalls using threads */ 1939static int _pending_callback(void *arg) 1940{ 1941 /* we assume the argument is callable object to which we own a reference */ 1942 PyObject *callable = (PyObject *)arg; 1943 PyObject *r = PyObject_CallObject(callable, NULL); 1944 Py_DECREF(callable); 1945 Py_XDECREF(r); 1946 return r != NULL ? 0 : -1; 1947} 1948 1949/* The following requests n callbacks to _pending_callback. It can be 1950 * run from any python thread. 1951 */ 1952PyObject *pending_threadfunc(PyObject *self, PyObject *arg) 1953{ 1954 PyObject *callable; 1955 int r; 1956 if (PyArg_ParseTuple(arg, "O", &callable) == 0) 1957 return NULL; 1958 1959 /* create the reference for the callbackwhile we hold the lock */ 1960 Py_INCREF(callable); 1961 1962 Py_BEGIN_ALLOW_THREADS 1963 r = Py_AddPendingCall(&_pending_callback, callable); 1964 Py_END_ALLOW_THREADS 1965 1966 if (r<0) { 1967 Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ 1968 Py_INCREF(Py_False); 1969 return Py_False; 1970 } 1971 Py_INCREF(Py_True); 1972 return Py_True; 1973} 1974#endif 1975 1976/* Some tests of PyString_FromFormat(). This needs more tests. */ 1977static PyObject * 1978test_string_from_format(PyObject *self, PyObject *args) 1979{ 1980 PyObject *result; 1981 char *msg; 1982 1983#define CHECK_1_FORMAT(FORMAT, TYPE) \ 1984 result = PyString_FromFormat(FORMAT, (TYPE)1); \ 1985 if (result == NULL) \ 1986 return NULL; \ 1987 if (strcmp(PyString_AsString(result), "1")) { \ 1988 msg = FORMAT " failed at 1"; \ 1989 goto Fail; \ 1990 } \ 1991 Py_DECREF(result) 1992 1993 CHECK_1_FORMAT("%d", int); 1994 CHECK_1_FORMAT("%ld", long); 1995 /* The z width modifier was added in Python 2.5. */ 1996 CHECK_1_FORMAT("%zd", Py_ssize_t); 1997 1998 /* The u type code was added in Python 2.5. */ 1999 CHECK_1_FORMAT("%u", unsigned int); 2000 CHECK_1_FORMAT("%lu", unsigned long); 2001 CHECK_1_FORMAT("%zu", size_t); 2002 2003 /* "%lld" and "%llu" support added in Python 2.7. */ 2004#ifdef HAVE_LONG_LONG 2005 CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG); 2006 CHECK_1_FORMAT("%lld", PY_LONG_LONG); 2007#endif 2008 2009 Py_RETURN_NONE; 2010 2011 Fail: 2012 Py_XDECREF(result); 2013 return raiseTestError("test_string_from_format", msg); 2014 2015#undef CHECK_1_FORMAT 2016} 2017 2018/* Coverage testing of capsule objects. */ 2019 2020static const char *capsule_name = "capsule name"; 2021static char *capsule_pointer = "capsule pointer"; 2022static char *capsule_context = "capsule context"; 2023static const char *capsule_error = NULL; 2024static int 2025capsule_destructor_call_count = 0; 2026 2027static void 2028capsule_destructor(PyObject *o) { 2029 capsule_destructor_call_count++; 2030 if (PyCapsule_GetContext(o) != capsule_context) { 2031 capsule_error = "context did not match in destructor!"; 2032 } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { 2033 capsule_error = "destructor did not match in destructor! (woah!)"; 2034 } else if (PyCapsule_GetName(o) != capsule_name) { 2035 capsule_error = "name did not match in destructor!"; 2036 } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { 2037 capsule_error = "pointer did not match in destructor!"; 2038 } 2039} 2040 2041typedef struct { 2042 char *name; 2043 char *module; 2044 char *attribute; 2045} known_capsule; 2046 2047static PyObject * 2048test_capsule(PyObject *self, PyObject *args) 2049{ 2050 PyObject *object; 2051 const char *error = NULL; 2052 void *pointer; 2053 void *pointer2; 2054 known_capsule known_capsules[] = { 2055 #define KNOWN_CAPSULE(module, name) { module "." name, module, name } 2056 KNOWN_CAPSULE("_socket", "CAPI"), 2057 KNOWN_CAPSULE("_curses", "_C_API"), 2058 KNOWN_CAPSULE("datetime", "datetime_CAPI"), 2059 { NULL, NULL }, 2060 }; 2061 known_capsule *known = &known_capsules[0]; 2062 2063#define FAIL(x) { error = (x); goto exit; } 2064 2065#define CHECK_DESTRUCTOR \ 2066 if (capsule_error) { \ 2067 FAIL(capsule_error); \ 2068 } \ 2069 else if (!capsule_destructor_call_count) { \ 2070 FAIL("destructor not called!"); \ 2071 } \ 2072 capsule_destructor_call_count = 0; \ 2073 2074 object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); 2075 PyCapsule_SetContext(object, capsule_context); 2076 capsule_destructor(object); 2077 CHECK_DESTRUCTOR; 2078 Py_DECREF(object); 2079 CHECK_DESTRUCTOR; 2080 2081 object = PyCapsule_New(known, "ignored", NULL); 2082 PyCapsule_SetPointer(object, capsule_pointer); 2083 PyCapsule_SetName(object, capsule_name); 2084 PyCapsule_SetDestructor(object, capsule_destructor); 2085 PyCapsule_SetContext(object, capsule_context); 2086 capsule_destructor(object); 2087 CHECK_DESTRUCTOR; 2088 /* intentionally access using the wrong name */ 2089 pointer2 = PyCapsule_GetPointer(object, "the wrong name"); 2090 if (!PyErr_Occurred()) { 2091 FAIL("PyCapsule_GetPointer should have failed but did not!"); 2092 } 2093 PyErr_Clear(); 2094 if (pointer2) { 2095 if (pointer2 == capsule_pointer) { 2096 FAIL("PyCapsule_GetPointer should not have" 2097 " returned the internal pointer!"); 2098 } else { 2099 FAIL("PyCapsule_GetPointer should have " 2100 "returned NULL pointer but did not!"); 2101 } 2102 } 2103 PyCapsule_SetDestructor(object, NULL); 2104 Py_DECREF(object); 2105 if (capsule_destructor_call_count) { 2106 FAIL("destructor called when it should not have been!"); 2107 } 2108 2109 for (known = &known_capsules[0]; known->module != NULL; known++) { 2110 /* yeah, ordinarily I wouldn't do this either, 2111 but it's fine for this test harness. 2112 */ 2113 static char buffer[256]; 2114#undef FAIL 2115#define FAIL(x) \ 2116 { \ 2117 sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ 2118 x, known->module, known->attribute); \ 2119 error = buffer; \ 2120 goto exit; \ 2121 } \ 2122 2123 PyObject *module = PyImport_ImportModule(known->module); 2124 if (module) { 2125 pointer = PyCapsule_Import(known->name, 0); 2126 if (!pointer) { 2127 Py_DECREF(module); 2128 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); 2129 } 2130 object = PyObject_GetAttrString(module, known->attribute); 2131 if (!object) { 2132 Py_DECREF(module); 2133 return NULL; 2134 } 2135 pointer2 = PyCapsule_GetPointer(object, 2136 "weebles wobble but they don't fall down"); 2137 if (!PyErr_Occurred()) { 2138 Py_DECREF(object); 2139 Py_DECREF(module); 2140 FAIL("PyCapsule_GetPointer should have failed but did not!"); 2141 } 2142 PyErr_Clear(); 2143 if (pointer2) { 2144 Py_DECREF(module); 2145 Py_DECREF(object); 2146 if (pointer2 == pointer) { 2147 FAIL("PyCapsule_GetPointer should not have" 2148 " returned its internal pointer!"); 2149 } else { 2150 FAIL("PyCapsule_GetPointer should have" 2151 " returned NULL pointer but did not!"); 2152 } 2153 } 2154 Py_DECREF(object); 2155 Py_DECREF(module); 2156 } 2157 else 2158 PyErr_Clear(); 2159 } 2160 2161 exit: 2162 if (error) { 2163 return raiseTestError("test_capsule", error); 2164 } 2165 Py_RETURN_NONE; 2166#undef FAIL 2167} 2168 2169/* This is here to provide a docstring for test_descr. */ 2170static PyObject * 2171test_with_docstring(PyObject *self) 2172{ 2173 Py_RETURN_NONE; 2174} 2175 2176/* To test the format of tracebacks as printed out. */ 2177static PyObject * 2178traceback_print(PyObject *self, PyObject *args) 2179{ 2180 PyObject *file; 2181 PyObject *traceback; 2182 int result; 2183 2184 if (!PyArg_ParseTuple(args, "OO:traceback_print", 2185 &traceback, &file)) 2186 return NULL; 2187 2188 result = PyTraceBack_Print(traceback, file); 2189 if (result < 0) 2190 return NULL; 2191 Py_RETURN_NONE; 2192} 2193 2194/* To test that the result of PyCode_NewEmpty has the right members. */ 2195static PyObject * 2196code_newempty(PyObject *self, PyObject *args) 2197{ 2198 const char *filename; 2199 const char *funcname; 2200 int firstlineno; 2201 2202 if (!PyArg_ParseTuple(args, "ssi:code_newempty", 2203 &filename, &funcname, &firstlineno)) 2204 return NULL; 2205 2206 return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); 2207} 2208 2209/* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). 2210 Run via Lib/test/test_exceptions.py */ 2211static PyObject * 2212make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) 2213{ 2214 char *name; 2215 char *doc = NULL; 2216 PyObject *base = NULL; 2217 PyObject *dict = NULL; 2218 2219 static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; 2220 2221 if (!PyArg_ParseTupleAndKeywords(args, kwargs, 2222 "s|sOO:make_exception_with_doc", kwlist, 2223 &name, &doc, &base, &dict)) 2224 return NULL; 2225 2226 return PyErr_NewExceptionWithDoc(name, doc, base, dict); 2227} 2228 2229static PyObject * 2230sequence_delitem(PyObject *self, PyObject *args) 2231{ 2232 PyObject *seq; 2233 Py_ssize_t i; 2234 2235 if (!PyArg_ParseTuple(args, "On", &seq, &i)) 2236 return NULL; 2237 if (PySequence_DelItem(seq, i) < 0) 2238 return NULL; 2239 Py_RETURN_NONE; 2240} 2241 2242#ifdef WITH_THREAD 2243typedef struct { 2244 PyThread_type_lock start_event; 2245 PyThread_type_lock exit_event; 2246 PyObject *callback; 2247} test_c_thread_t; 2248 2249static void 2250temporary_c_thread(void *data) 2251{ 2252 test_c_thread_t *test_c_thread = data; 2253 PyGILState_STATE state; 2254 PyObject *res; 2255 2256 PyThread_release_lock(test_c_thread->start_event); 2257 2258 /* Allocate a Python thread state for this thread */ 2259 state = PyGILState_Ensure(); 2260 2261 res = PyObject_CallFunction(test_c_thread->callback, "", NULL); 2262 Py_CLEAR(test_c_thread->callback); 2263 2264 if (res == NULL) { 2265 PyErr_Print(); 2266 } 2267 else { 2268 Py_DECREF(res); 2269 } 2270 2271 /* Destroy the Python thread state for this thread */ 2272 PyGILState_Release(state); 2273 2274 PyThread_release_lock(test_c_thread->exit_event); 2275 2276 PyThread_exit_thread(); 2277} 2278 2279static PyObject * 2280call_in_temporary_c_thread(PyObject *self, PyObject *callback) 2281{ 2282 PyObject *res = NULL; 2283 test_c_thread_t test_c_thread; 2284 long thread; 2285 2286 PyEval_InitThreads(); 2287 2288 test_c_thread.start_event = PyThread_allocate_lock(); 2289 test_c_thread.exit_event = PyThread_allocate_lock(); 2290 test_c_thread.callback = NULL; 2291 if (!test_c_thread.start_event || !test_c_thread.exit_event) { 2292 PyErr_SetString(PyExc_RuntimeError, "could not allocate lock"); 2293 goto exit; 2294 } 2295 2296 Py_INCREF(callback); 2297 test_c_thread.callback = callback; 2298 2299 PyThread_acquire_lock(test_c_thread.start_event, 1); 2300 PyThread_acquire_lock(test_c_thread.exit_event, 1); 2301 2302 thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread); 2303 if (thread == -1) { 2304 PyErr_SetString(PyExc_RuntimeError, "unable to start the thread"); 2305 PyThread_release_lock(test_c_thread.start_event); 2306 PyThread_release_lock(test_c_thread.exit_event); 2307 goto exit; 2308 } 2309 2310 PyThread_acquire_lock(test_c_thread.start_event, 1); 2311 PyThread_release_lock(test_c_thread.start_event); 2312 2313 Py_BEGIN_ALLOW_THREADS 2314 PyThread_acquire_lock(test_c_thread.exit_event, 1); 2315 PyThread_release_lock(test_c_thread.exit_event); 2316 Py_END_ALLOW_THREADS 2317 2318 Py_INCREF(Py_None); 2319 res = Py_None; 2320 2321exit: 2322 Py_CLEAR(test_c_thread.callback); 2323 if (test_c_thread.start_event) 2324 PyThread_free_lock(test_c_thread.start_event); 2325 if (test_c_thread.exit_event) 2326 PyThread_free_lock(test_c_thread.exit_event); 2327 return res; 2328} 2329#endif /* WITH_THREAD */ 2330 2331/* marshal */ 2332 2333static PyObject* 2334pymarshal_write_long_to_file(PyObject* self, PyObject *args) 2335{ 2336 long value; 2337 char *filename; 2338 int version; 2339 FILE *fp; 2340 2341 if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file", 2342 &value, &filename, &version)) 2343 return NULL; 2344 2345 fp = fopen(filename, "wb"); 2346 if (fp == NULL) { 2347 PyErr_SetFromErrno(PyExc_OSError); 2348 return NULL; 2349 } 2350 2351 PyMarshal_WriteLongToFile(value, fp, version); 2352 2353 fclose(fp); 2354 if (PyErr_Occurred()) 2355 return NULL; 2356 Py_RETURN_NONE; 2357} 2358 2359static PyObject* 2360pymarshal_write_object_to_file(PyObject* self, PyObject *args) 2361{ 2362 PyObject *obj; 2363 char *filename; 2364 int version; 2365 FILE *fp; 2366 2367 if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file", 2368 &obj, &filename, &version)) 2369 return NULL; 2370 2371 fp = fopen(filename, "wb"); 2372 if (fp == NULL) { 2373 PyErr_SetFromErrno(PyExc_OSError); 2374 return NULL; 2375 } 2376 2377 PyMarshal_WriteObjectToFile(obj, fp, version); 2378 2379 fclose(fp); 2380 if (PyErr_Occurred()) 2381 return NULL; 2382 Py_RETURN_NONE; 2383} 2384 2385static PyObject* 2386pymarshal_read_short_from_file(PyObject* self, PyObject *args) 2387{ 2388 int value; 2389 long pos; 2390 char *filename; 2391 FILE *fp; 2392 2393 if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename)) 2394 return NULL; 2395 2396 fp = fopen(filename, "rb"); 2397 if (fp == NULL) { 2398 PyErr_SetFromErrno(PyExc_OSError); 2399 return NULL; 2400 } 2401 2402 value = PyMarshal_ReadShortFromFile(fp); 2403 pos = ftell(fp); 2404 2405 fclose(fp); 2406 if (PyErr_Occurred()) 2407 return NULL; 2408 return Py_BuildValue("il", value, pos); 2409} 2410 2411static PyObject* 2412pymarshal_read_long_from_file(PyObject* self, PyObject *args) 2413{ 2414 long value, pos; 2415 char *filename; 2416 FILE *fp; 2417 2418 if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename)) 2419 return NULL; 2420 2421 fp = fopen(filename, "rb"); 2422 if (fp == NULL) { 2423 PyErr_SetFromErrno(PyExc_OSError); 2424 return NULL; 2425 } 2426 2427 value = PyMarshal_ReadLongFromFile(fp); 2428 pos = ftell(fp); 2429 2430 fclose(fp); 2431 if (PyErr_Occurred()) 2432 return NULL; 2433 return Py_BuildValue("ll", value, pos); 2434} 2435 2436static PyObject* 2437pymarshal_read_last_object_from_file(PyObject* self, PyObject *args) 2438{ 2439 PyObject *obj; 2440 long pos; 2441 char *filename; 2442 FILE *fp; 2443 2444 if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename)) 2445 return NULL; 2446 2447 fp = fopen(filename, "rb"); 2448 if (fp == NULL) { 2449 PyErr_SetFromErrno(PyExc_OSError); 2450 return NULL; 2451 } 2452 2453 obj = PyMarshal_ReadLastObjectFromFile(fp); 2454 pos = ftell(fp); 2455 2456 fclose(fp); 2457 return Py_BuildValue("Nl", obj, pos); 2458} 2459 2460static PyObject* 2461pymarshal_read_object_from_file(PyObject* self, PyObject *args) 2462{ 2463 PyObject *obj; 2464 long pos; 2465 char *filename; 2466 FILE *fp; 2467 2468 if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename)) 2469 return NULL; 2470 2471 fp = fopen(filename, "rb"); 2472 if (fp == NULL) { 2473 PyErr_SetFromErrno(PyExc_OSError); 2474 return NULL; 2475 } 2476 2477 obj = PyMarshal_ReadObjectFromFile(fp); 2478 pos = ftell(fp); 2479 2480 fclose(fp); 2481 return Py_BuildValue("Nl", obj, pos); 2482} 2483 2484 2485static PyMethodDef TestMethods[] = { 2486 {"raise_exception", raise_exception, METH_VARARGS}, 2487 {"set_errno", set_errno, METH_VARARGS}, 2488 {"test_config", (PyCFunction)test_config, METH_NOARGS}, 2489#if defined(Py_USING_UNICODE) && !defined(Py_BUILD_CORE) 2490 {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, 2491#endif 2492 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, 2493 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, 2494 {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, 2495 {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS}, 2496 {"test_to_contiguous", (PyCFunction)test_to_contiguous, METH_NOARGS}, 2497 {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS}, 2498 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, 2499 {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, 2500 METH_NOARGS}, 2501 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, 2502 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, 2503#ifdef Py_USING_UNICODE 2504 {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, 2505#endif 2506 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, 2507 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, 2508 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, 2509 PyDoc_STR("This is a pretty normal docstring.")}, 2510 2511 {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS}, 2512 {"get_args", get_args, METH_VARARGS}, 2513 {"get_kwargs", (PyCFunction)get_kwargs, METH_VARARGS|METH_KEYWORDS}, 2514 {"getargs_tuple", getargs_tuple, METH_VARARGS}, 2515 {"getargs_keywords", (PyCFunction)getargs_keywords, 2516 METH_VARARGS|METH_KEYWORDS}, 2517 {"getargs_b", getargs_b, METH_VARARGS}, 2518 {"getargs_B", getargs_B, METH_VARARGS}, 2519 {"getargs_h", getargs_h, METH_VARARGS}, 2520 {"getargs_H", getargs_H, METH_VARARGS}, 2521 {"getargs_I", getargs_I, METH_VARARGS}, 2522 {"getargs_k", getargs_k, METH_VARARGS}, 2523 {"getargs_i", getargs_i, METH_VARARGS}, 2524 {"getargs_l", getargs_l, METH_VARARGS}, 2525 {"getargs_n", getargs_n, METH_VARARGS}, 2526#ifdef HAVE_LONG_LONG 2527 {"getargs_L", getargs_L, METH_VARARGS}, 2528 {"getargs_K", getargs_K, METH_VARARGS}, 2529 {"test_longlong_api", test_longlong_api, METH_NOARGS}, 2530 {"test_long_long_and_overflow", 2531 (PyCFunction)test_long_long_and_overflow, METH_NOARGS}, 2532 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, 2533#endif 2534 {"getargs_f", getargs_f, METH_VARARGS}, 2535 {"getargs_d", getargs_d, METH_VARARGS}, 2536 {"getargs_D", getargs_D, METH_VARARGS}, 2537 {"getargs_S", getargs_S, METH_VARARGS}, 2538 {"getargs_Y", getargs_Y, METH_VARARGS}, 2539#ifdef Py_USING_UNICODE 2540 {"getargs_U", getargs_U, METH_VARARGS}, 2541#endif 2542 {"getargs_c", getargs_c, METH_VARARGS}, 2543 {"getargs_s", getargs_s, METH_VARARGS}, 2544 {"getargs_s_star", getargs_s_star, METH_VARARGS}, 2545 {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, 2546 {"getargs_t_hash", getargs_t_hash, METH_VARARGS}, 2547 {"getargs_z", getargs_z, METH_VARARGS}, 2548 {"getargs_z_star", getargs_z_star, METH_VARARGS}, 2549 {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, 2550 {"getargs_w", getargs_w, METH_VARARGS}, 2551 {"getargs_w_star", getargs_w_star, METH_VARARGS}, 2552 {"getargs_w_hash", getargs_w_hash, METH_VARARGS}, 2553#ifdef Py_USING_UNICODE 2554 {"getargs_u", getargs_u, METH_VARARGS}, 2555 {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, 2556#endif 2557 {"getargs_es", getargs_es, METH_VARARGS}, 2558 {"getargs_et", getargs_et, METH_VARARGS}, 2559 {"getargs_es_hash", getargs_es_hash, METH_VARARGS}, 2560 {"getargs_et_hash", getargs_et_hash, METH_VARARGS}, 2561#ifdef Py_USING_UNICODE 2562 {"codec_incrementalencoder", 2563 (PyCFunction)codec_incrementalencoder, METH_VARARGS}, 2564 {"codec_incrementaldecoder", 2565 (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, 2566 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, 2567 {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, 2568 {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, 2569#endif 2570#ifdef WITH_THREAD 2571 {"_test_thread_state", test_thread_state, METH_VARARGS}, 2572 {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, 2573#endif 2574 {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, 2575 {"traceback_print", traceback_print, METH_VARARGS}, 2576 {"code_newempty", code_newempty, METH_VARARGS}, 2577 {"make_exception_with_doc", (PyCFunction)make_exception_with_doc, 2578 METH_VARARGS | METH_KEYWORDS}, 2579 {"sequence_delitem", (PyCFunction)sequence_delitem, METH_VARARGS}, 2580#ifdef WITH_THREAD 2581 {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, 2582 PyDoc_STR("set_error_class(error_class) -> None")}, 2583#endif 2584 {"pymarshal_write_long_to_file", 2585 pymarshal_write_long_to_file, METH_VARARGS}, 2586 {"pymarshal_write_object_to_file", 2587 pymarshal_write_object_to_file, METH_VARARGS}, 2588 {"pymarshal_read_short_from_file", 2589 pymarshal_read_short_from_file, METH_VARARGS}, 2590 {"pymarshal_read_long_from_file", 2591 pymarshal_read_long_from_file, METH_VARARGS}, 2592 {"pymarshal_read_last_object_from_file", 2593 pymarshal_read_last_object_from_file, METH_VARARGS}, 2594 {"pymarshal_read_object_from_file", 2595 pymarshal_read_object_from_file, METH_VARARGS}, 2596 {NULL, NULL} /* sentinel */ 2597}; 2598 2599#define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} 2600 2601typedef struct { 2602 char bool_member; 2603 char byte_member; 2604 unsigned char ubyte_member; 2605 short short_member; 2606 unsigned short ushort_member; 2607 int int_member; 2608 unsigned int uint_member; 2609 long long_member; 2610 unsigned long ulong_member; 2611 float float_member; 2612 double double_member; 2613 char inplace_member[6]; 2614#ifdef HAVE_LONG_LONG 2615 PY_LONG_LONG longlong_member; 2616 unsigned PY_LONG_LONG ulonglong_member; 2617#endif 2618} all_structmembers; 2619 2620typedef struct { 2621 PyObject_HEAD 2622 all_structmembers structmembers; 2623} test_structmembers; 2624 2625static struct PyMemberDef test_members[] = { 2626 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, 2627 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, 2628 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, 2629 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, 2630 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, 2631 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, 2632 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, 2633 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, 2634 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, 2635 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, 2636 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, 2637 {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL}, 2638#ifdef HAVE_LONG_LONG 2639 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, 2640 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, 2641#endif 2642 {NULL} 2643}; 2644 2645 2646static PyObject * 2647test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) 2648{ 2649 static char *keywords[] = { 2650 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", 2651 "T_INT", "T_UINT", "T_LONG", "T_ULONG", 2652 "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", 2653#ifdef HAVE_LONG_LONG 2654 "T_LONGLONG", "T_ULONGLONG", 2655#endif 2656 NULL}; 2657 static char *fmt = "|bbBhHiIlkfds#" 2658#ifdef HAVE_LONG_LONG 2659 "LK" 2660#endif 2661 ; 2662 test_structmembers *ob; 2663 const char *s = NULL; 2664 int string_len = 0; 2665 ob = PyObject_New(test_structmembers, type); 2666 if (ob == NULL) 2667 return NULL; 2668 memset(&ob->structmembers, 0, sizeof(all_structmembers)); 2669 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, 2670 &ob->structmembers.bool_member, 2671 &ob->structmembers.byte_member, 2672 &ob->structmembers.ubyte_member, 2673 &ob->structmembers.short_member, 2674 &ob->structmembers.ushort_member, 2675 &ob->structmembers.int_member, 2676 &ob->structmembers.uint_member, 2677 &ob->structmembers.long_member, 2678 &ob->structmembers.ulong_member, 2679 &ob->structmembers.float_member, 2680 &ob->structmembers.double_member, 2681 &s, &string_len 2682#ifdef HAVE_LONG_LONG 2683 , &ob->structmembers.longlong_member, 2684 &ob->structmembers.ulonglong_member 2685#endif 2686 )) { 2687 Py_DECREF(ob); 2688 return NULL; 2689 } 2690 if (s != NULL) { 2691 if (string_len > 5) { 2692 Py_DECREF(ob); 2693 PyErr_SetString(PyExc_ValueError, "string too long"); 2694 return NULL; 2695 } 2696 strcpy(ob->structmembers.inplace_member, s); 2697 } 2698 else { 2699 strcpy(ob->structmembers.inplace_member, ""); 2700 } 2701 return (PyObject *)ob; 2702} 2703 2704static void 2705test_structmembers_free(PyObject *ob) 2706{ 2707 PyObject_FREE(ob); 2708} 2709 2710static PyTypeObject test_structmembersType = { 2711 PyVarObject_HEAD_INIT(NULL, 0) 2712 "test_structmembersType", 2713 sizeof(test_structmembers), /* tp_basicsize */ 2714 0, /* tp_itemsize */ 2715 test_structmembers_free, /* destructor tp_dealloc */ 2716 0, /* tp_print */ 2717 0, /* tp_getattr */ 2718 0, /* tp_setattr */ 2719 0, /* tp_compare */ 2720 0, /* tp_repr */ 2721 0, /* tp_as_number */ 2722 0, /* tp_as_sequence */ 2723 0, /* tp_as_mapping */ 2724 0, /* tp_hash */ 2725 0, /* tp_call */ 2726 0, /* tp_str */ 2727 PyObject_GenericGetAttr, /* tp_getattro */ 2728 PyObject_GenericSetAttr, /* tp_setattro */ 2729 0, /* tp_as_buffer */ 2730 0, /* tp_flags */ 2731 "Type containing all structmember types", 2732 0, /* traverseproc tp_traverse */ 2733 0, /* tp_clear */ 2734 0, /* tp_richcompare */ 2735 0, /* tp_weaklistoffset */ 2736 0, /* tp_iter */ 2737 0, /* tp_iternext */ 2738 0, /* tp_methods */ 2739 test_members, /* tp_members */ 2740 0, 2741 0, 2742 0, 2743 0, 2744 0, 2745 0, 2746 0, 2747 0, 2748 test_structmembers_new, /* tp_new */ 2749}; 2750 2751 2752PyMODINIT_FUNC 2753init_testcapi(void) 2754{ 2755 PyObject *m; 2756 2757 m = Py_InitModule("_testcapi", TestMethods); 2758 if (m == NULL) 2759 return; 2760 2761 Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; 2762 2763 Py_TYPE(&test_structmembersType)=&PyType_Type; 2764 Py_INCREF(&test_structmembersType); 2765 /* don't use a name starting with "test", since we don't want 2766 test_capi to automatically call this */ 2767 PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); 2768 2769 PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX)); 2770 PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN)); 2771 PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX)); 2772 PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX)); 2773 PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN)); 2774 PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX)); 2775 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); 2776 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); 2777 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); 2778 PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX)); 2779 PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN)); 2780 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); 2781 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); 2782 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); 2783 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); 2784 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); 2785 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); 2786 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); 2787 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); 2788 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX)); 2789 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN)); 2790 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head))); 2791 2792 TestError = PyErr_NewException("_testcapi.error", NULL, NULL); 2793 Py_INCREF(TestError); 2794 PyModule_AddObject(m, "error", TestError); 2795} 2796