PageRenderTime 56ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib_pypy/_testcapimodule.c

https://bitbucket.org/dac_io/pypy
C | 1898 lines | 1563 code | 211 blank | 124 comment | 345 complexity | 698e4985c119ca8654fac0a3780aa942 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /* Verbatim copy of Modules/_testcapimodule.c from CPython 2.7 */
  2. /*
  3. * C Extension module to test Python interpreter C APIs.
  4. *
  5. * The 'test_*' functions exported by this module are run as part of the
  6. * standard Python regression test, via Lib/test/test_capi.py.
  7. */
  8. #include "Python.h"
  9. #include <float.h>
  10. #include "structmember.h"
  11. #include "datetime.h"
  12. #ifdef WITH_THREAD
  13. #include "pythread.h"
  14. #endif /* WITH_THREAD */
  15. static PyObject *TestError; /* set to exception object in init */
  16. /* Raise TestError with test_name + ": " + msg, and return NULL. */
  17. static PyObject *
  18. raiseTestError(const char* test_name, const char* msg)
  19. {
  20. char buf[2048];
  21. if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
  22. PyErr_SetString(TestError, "internal error msg too large");
  23. else {
  24. PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
  25. PyErr_SetString(TestError, buf);
  26. }
  27. return NULL;
  28. }
  29. /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
  30. The ones derived from autoconf on the UNIX-like OSes can be relied
  31. upon (in the absence of sloppy cross-compiling), but the Windows
  32. platforms have these hardcoded. Better safe than sorry.
  33. */
  34. static PyObject*
  35. sizeof_error(const char* fatname, const char* typname,
  36. int expected, int got)
  37. {
  38. char buf[1024];
  39. PyOS_snprintf(buf, sizeof(buf),
  40. "%.200s #define == %d but sizeof(%.200s) == %d",
  41. fatname, expected, typname, got);
  42. PyErr_SetString(TestError, buf);
  43. return (PyObject*)NULL;
  44. }
  45. static PyObject*
  46. test_config(PyObject *self)
  47. {
  48. #define CHECK_SIZEOF(FATNAME, TYPE) \
  49. if (FATNAME != sizeof(TYPE)) \
  50. return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
  51. CHECK_SIZEOF(SIZEOF_SHORT, short);
  52. CHECK_SIZEOF(SIZEOF_INT, int);
  53. CHECK_SIZEOF(SIZEOF_LONG, long);
  54. CHECK_SIZEOF(SIZEOF_VOID_P, void*);
  55. CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
  56. #ifdef HAVE_LONG_LONG
  57. CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
  58. #endif
  59. #undef CHECK_SIZEOF
  60. Py_INCREF(Py_None);
  61. return Py_None;
  62. }
  63. static PyObject*
  64. test_list_api(PyObject *self)
  65. {
  66. PyObject* list;
  67. int i;
  68. /* SF bug 132008: PyList_Reverse segfaults */
  69. #define NLIST 30
  70. list = PyList_New(NLIST);
  71. if (list == (PyObject*)NULL)
  72. return (PyObject*)NULL;
  73. /* list = range(NLIST) */
  74. for (i = 0; i < NLIST; ++i) {
  75. PyObject* anint = PyInt_FromLong(i);
  76. if (anint == (PyObject*)NULL) {
  77. Py_DECREF(list);
  78. return (PyObject*)NULL;
  79. }
  80. PyList_SET_ITEM(list, i, anint);
  81. }
  82. /* list.reverse(), via PyList_Reverse() */
  83. i = PyList_Reverse(list); /* should not blow up! */
  84. if (i != 0) {
  85. Py_DECREF(list);
  86. return (PyObject*)NULL;
  87. }
  88. /* Check that list == range(29, -1, -1) now */
  89. for (i = 0; i < NLIST; ++i) {
  90. PyObject* anint = PyList_GET_ITEM(list, i);
  91. if (PyInt_AS_LONG(anint) != NLIST-1-i) {
  92. PyErr_SetString(TestError,
  93. "test_list_api: reverse screwed up");
  94. Py_DECREF(list);
  95. return (PyObject*)NULL;
  96. }
  97. }
  98. Py_DECREF(list);
  99. #undef NLIST
  100. Py_INCREF(Py_None);
  101. return Py_None;
  102. }
  103. static int
  104. test_dict_inner(int count)
  105. {
  106. Py_ssize_t pos = 0, iterations = 0;
  107. int i;
  108. PyObject *dict = PyDict_New();
  109. PyObject *v, *k;
  110. if (dict == NULL)
  111. return -1;
  112. for (i = 0; i < count; i++) {
  113. v = PyInt_FromLong(i);
  114. PyDict_SetItem(dict, v, v);
  115. Py_DECREF(v);
  116. }
  117. while (PyDict_Next(dict, &pos, &k, &v)) {
  118. PyObject *o;
  119. iterations++;
  120. i = PyInt_AS_LONG(v) + 1;
  121. o = PyInt_FromLong(i);
  122. if (o == NULL)
  123. return -1;
  124. if (PyDict_SetItem(dict, k, o) < 0) {
  125. Py_DECREF(o);
  126. return -1;
  127. }
  128. Py_DECREF(o);
  129. }
  130. Py_DECREF(dict);
  131. if (iterations != count) {
  132. PyErr_SetString(
  133. TestError,
  134. "test_dict_iteration: dict iteration went wrong ");
  135. return -1;
  136. } else {
  137. return 0;
  138. }
  139. }
  140. static PyObject*
  141. test_dict_iteration(PyObject* self)
  142. {
  143. int i;
  144. for (i = 0; i < 200; i++) {
  145. if (test_dict_inner(i) < 0) {
  146. return NULL;
  147. }
  148. }
  149. Py_INCREF(Py_None);
  150. return Py_None;
  151. }
  152. /* Issue #4701: Check that PyObject_Hash implicitly calls
  153. * PyType_Ready if it hasn't already been called
  154. */
  155. static PyTypeObject _HashInheritanceTester_Type = {
  156. PyObject_HEAD_INIT(NULL)
  157. 0, /* Number of items for varobject */
  158. "hashinheritancetester", /* Name of this type */
  159. sizeof(PyObject), /* Basic object size */
  160. 0, /* Item size for varobject */
  161. (destructor)PyObject_Del, /* tp_dealloc */
  162. 0, /* tp_print */
  163. 0, /* tp_getattr */
  164. 0, /* tp_setattr */
  165. 0, /* tp_compare */
  166. 0, /* tp_repr */
  167. 0, /* tp_as_number */
  168. 0, /* tp_as_sequence */
  169. 0, /* tp_as_mapping */
  170. 0, /* tp_hash */
  171. 0, /* tp_call */
  172. 0, /* tp_str */
  173. PyObject_GenericGetAttr, /* tp_getattro */
  174. 0, /* tp_setattro */
  175. 0, /* tp_as_buffer */
  176. Py_TPFLAGS_DEFAULT, /* tp_flags */
  177. 0, /* tp_doc */
  178. 0, /* tp_traverse */
  179. 0, /* tp_clear */
  180. 0, /* tp_richcompare */
  181. 0, /* tp_weaklistoffset */
  182. 0, /* tp_iter */
  183. 0, /* tp_iternext */
  184. 0, /* tp_methods */
  185. 0, /* tp_members */
  186. 0, /* tp_getset */
  187. 0, /* tp_base */
  188. 0, /* tp_dict */
  189. 0, /* tp_descr_get */
  190. 0, /* tp_descr_set */
  191. 0, /* tp_dictoffset */
  192. 0, /* tp_init */
  193. 0, /* tp_alloc */
  194. PyType_GenericNew, /* tp_new */
  195. };
  196. static PyObject*
  197. test_lazy_hash_inheritance(PyObject* self)
  198. {
  199. PyTypeObject *type;
  200. PyObject *obj;
  201. long hash;
  202. type = &_HashInheritanceTester_Type;
  203. if (type->tp_dict != NULL)
  204. /* The type has already been initialized. This probably means
  205. -R is being used. */
  206. Py_RETURN_NONE;
  207. obj = PyObject_New(PyObject, type);
  208. if (obj == NULL) {
  209. PyErr_Clear();
  210. PyErr_SetString(
  211. TestError,
  212. "test_lazy_hash_inheritance: failed to create object");
  213. return NULL;
  214. }
  215. if (type->tp_dict != NULL) {
  216. PyErr_SetString(
  217. TestError,
  218. "test_lazy_hash_inheritance: type initialised too soon");
  219. Py_DECREF(obj);
  220. return NULL;
  221. }
  222. hash = PyObject_Hash(obj);
  223. if ((hash == -1) && PyErr_Occurred()) {
  224. PyErr_Clear();
  225. PyErr_SetString(
  226. TestError,
  227. "test_lazy_hash_inheritance: could not hash object");
  228. Py_DECREF(obj);
  229. return NULL;
  230. }
  231. if (type->tp_dict == NULL) {
  232. PyErr_SetString(
  233. TestError,
  234. "test_lazy_hash_inheritance: type not initialised by hash()");
  235. Py_DECREF(obj);
  236. return NULL;
  237. }
  238. if (type->tp_hash != PyType_Type.tp_hash) {
  239. PyErr_SetString(
  240. TestError,
  241. "test_lazy_hash_inheritance: unexpected hash function");
  242. Py_DECREF(obj);
  243. return NULL;
  244. }
  245. Py_DECREF(obj);
  246. Py_RETURN_NONE;
  247. }
  248. /* Issue #7385: Check that memoryview() does not crash
  249. * when bf_getbuffer returns an error
  250. */
  251. static int
  252. broken_buffer_getbuffer(PyObject *self, Py_buffer *view, int flags)
  253. {
  254. PyErr_SetString(
  255. TestError,
  256. "test_broken_memoryview: expected error in bf_getbuffer");
  257. return -1;
  258. }
  259. static PyBufferProcs memoryviewtester_as_buffer = {
  260. 0, /* bf_getreadbuffer */
  261. 0, /* bf_getwritebuffer */
  262. 0, /* bf_getsegcount */
  263. 0, /* bf_getcharbuffer */
  264. (getbufferproc)broken_buffer_getbuffer, /* bf_getbuffer */
  265. 0, /* bf_releasebuffer */
  266. };
  267. static PyTypeObject _MemoryViewTester_Type = {
  268. PyObject_HEAD_INIT(NULL)
  269. 0, /* Number of items for varobject */
  270. "memoryviewtester", /* Name of this type */
  271. sizeof(PyObject), /* Basic object size */
  272. 0, /* Item size for varobject */
  273. (destructor)PyObject_Del, /* tp_dealloc */
  274. 0, /* tp_print */
  275. 0, /* tp_getattr */
  276. 0, /* tp_setattr */
  277. 0, /* tp_compare */
  278. 0, /* tp_repr */
  279. 0, /* tp_as_number */
  280. 0, /* tp_as_sequence */
  281. 0, /* tp_as_mapping */
  282. 0, /* tp_hash */
  283. 0, /* tp_call */
  284. 0, /* tp_str */
  285. PyObject_GenericGetAttr, /* tp_getattro */
  286. 0, /* tp_setattro */
  287. &memoryviewtester_as_buffer, /* tp_as_buffer */
  288. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */
  289. 0, /* tp_doc */
  290. 0, /* tp_traverse */
  291. 0, /* tp_clear */
  292. 0, /* tp_richcompare */
  293. 0, /* tp_weaklistoffset */
  294. 0, /* tp_iter */
  295. 0, /* tp_iternext */
  296. 0, /* tp_methods */
  297. 0, /* tp_members */
  298. 0, /* tp_getset */
  299. 0, /* tp_base */
  300. 0, /* tp_dict */
  301. 0, /* tp_descr_get */
  302. 0, /* tp_descr_set */
  303. 0, /* tp_dictoffset */
  304. 0, /* tp_init */
  305. 0, /* tp_alloc */
  306. PyType_GenericNew, /* tp_new */
  307. };
  308. static PyObject*
  309. test_broken_memoryview(PyObject* self)
  310. {
  311. PyObject *obj = PyObject_New(PyObject, &_MemoryViewTester_Type);
  312. PyObject *res;
  313. if (obj == NULL) {
  314. PyErr_Clear();
  315. PyErr_SetString(
  316. TestError,
  317. "test_broken_memoryview: failed to create object");
  318. return NULL;
  319. }
  320. res = PyMemoryView_FromObject(obj);
  321. if (res || !PyErr_Occurred()){
  322. PyErr_SetString(
  323. TestError,
  324. "test_broken_memoryview: memoryview() didn't raise an Exception");
  325. Py_XDECREF(res);
  326. Py_DECREF(obj);
  327. return NULL;
  328. }
  329. PyErr_Clear();
  330. Py_DECREF(obj);
  331. Py_RETURN_NONE;
  332. }
  333. /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
  334. PyLong_{As, From}{Unsigned,}LongLong().
  335. Note that the meat of the test is contained in testcapi_long.h.
  336. This is revolting, but delicate code duplication is worse: "almost
  337. exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
  338. dependence on type names makes it impossible to use a parameterized
  339. function. A giant macro would be even worse than this. A C++ template
  340. would be perfect.
  341. The "report an error" functions are deliberately not part of the #include
  342. file: if the test fails, you can set a breakpoint in the appropriate
  343. error function directly, and crawl back from there in the debugger.
  344. */
  345. #define UNBIND(X) Py_DECREF(X); (X) = NULL
  346. static PyObject *
  347. raise_test_long_error(const char* msg)
  348. {
  349. return raiseTestError("test_long_api", msg);
  350. }
  351. #define TESTNAME test_long_api_inner
  352. #define TYPENAME long
  353. #define F_S_TO_PY PyLong_FromLong
  354. #define F_PY_TO_S PyLong_AsLong
  355. #define F_U_TO_PY PyLong_FromUnsignedLong
  356. #define F_PY_TO_U PyLong_AsUnsignedLong
  357. #include "testcapi_long.h"
  358. static PyObject *
  359. test_long_api(PyObject* self)
  360. {
  361. return TESTNAME(raise_test_long_error);
  362. }
  363. #undef TESTNAME
  364. #undef TYPENAME
  365. #undef F_S_TO_PY
  366. #undef F_PY_TO_S
  367. #undef F_U_TO_PY
  368. #undef F_PY_TO_U
  369. #ifdef HAVE_LONG_LONG
  370. static PyObject *
  371. raise_test_longlong_error(const char* msg)
  372. {
  373. return raiseTestError("test_longlong_api", msg);
  374. }
  375. #define TESTNAME test_longlong_api_inner
  376. #define TYPENAME PY_LONG_LONG
  377. #define F_S_TO_PY PyLong_FromLongLong
  378. #define F_PY_TO_S PyLong_AsLongLong
  379. #define F_U_TO_PY PyLong_FromUnsignedLongLong
  380. #define F_PY_TO_U PyLong_AsUnsignedLongLong
  381. #include "testcapi_long.h"
  382. static PyObject *
  383. test_longlong_api(PyObject* self, PyObject *args)
  384. {
  385. return TESTNAME(raise_test_longlong_error);
  386. }
  387. #undef TESTNAME
  388. #undef TYPENAME
  389. #undef F_S_TO_PY
  390. #undef F_PY_TO_S
  391. #undef F_U_TO_PY
  392. #undef F_PY_TO_U
  393. /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG
  394. is tested by test_long_api_inner. This test will concentrate on proper
  395. handling of overflow.
  396. */
  397. static PyObject *
  398. test_long_and_overflow(PyObject *self)
  399. {
  400. PyObject *num, *one, *temp;
  401. long value;
  402. int overflow;
  403. /* Test that overflow is set properly for a large value. */
  404. /* num is a number larger than LONG_MAX even on 64-bit platforms */
  405. num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
  406. if (num == NULL)
  407. return NULL;
  408. overflow = 1234;
  409. value = PyLong_AsLongAndOverflow(num, &overflow);
  410. Py_DECREF(num);
  411. if (value == -1 && PyErr_Occurred())
  412. return NULL;
  413. if (value != -1)
  414. return raiseTestError("test_long_and_overflow",
  415. "return value was not set to -1");
  416. if (overflow != 1)
  417. return raiseTestError("test_long_and_overflow",
  418. "overflow was not set to 1");
  419. /* Same again, with num = LONG_MAX + 1 */
  420. num = PyLong_FromLong(LONG_MAX);
  421. if (num == NULL)
  422. return NULL;
  423. one = PyLong_FromLong(1L);
  424. if (one == NULL) {
  425. Py_DECREF(num);
  426. return NULL;
  427. }
  428. temp = PyNumber_Add(num, one);
  429. Py_DECREF(one);
  430. Py_DECREF(num);
  431. num = temp;
  432. if (num == NULL)
  433. return NULL;
  434. overflow = 0;
  435. value = PyLong_AsLongAndOverflow(num, &overflow);
  436. Py_DECREF(num);
  437. if (value == -1 && PyErr_Occurred())
  438. return NULL;
  439. if (value != -1)
  440. return raiseTestError("test_long_and_overflow",
  441. "return value was not set to -1");
  442. if (overflow != 1)
  443. return raiseTestError("test_long_and_overflow",
  444. "overflow was not set to 1");
  445. /* Test that overflow is set properly for a large negative value. */
  446. /* num is a number smaller than LONG_MIN even on 64-bit platforms */
  447. num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
  448. if (num == NULL)
  449. return NULL;
  450. overflow = 1234;
  451. value = PyLong_AsLongAndOverflow(num, &overflow);
  452. Py_DECREF(num);
  453. if (value == -1 && PyErr_Occurred())
  454. return NULL;
  455. if (value != -1)
  456. return raiseTestError("test_long_and_overflow",
  457. "return value was not set to -1");
  458. if (overflow != -1)
  459. return raiseTestError("test_long_and_overflow",
  460. "overflow was not set to -1");
  461. /* Same again, with num = LONG_MIN - 1 */
  462. num = PyLong_FromLong(LONG_MIN);
  463. if (num == NULL)
  464. return NULL;
  465. one = PyLong_FromLong(1L);
  466. if (one == NULL) {
  467. Py_DECREF(num);
  468. return NULL;
  469. }
  470. temp = PyNumber_Subtract(num, one);
  471. Py_DECREF(one);
  472. Py_DECREF(num);
  473. num = temp;
  474. if (num == NULL)
  475. return NULL;
  476. overflow = 0;
  477. value = PyLong_AsLongAndOverflow(num, &overflow);
  478. Py_DECREF(num);
  479. if (value == -1 && PyErr_Occurred())
  480. return NULL;
  481. if (value != -1)
  482. return raiseTestError("test_long_and_overflow",
  483. "return value was not set to -1");
  484. if (overflow != -1)
  485. return raiseTestError("test_long_and_overflow",
  486. "overflow was not set to -1");
  487. /* Test that overflow is cleared properly for small values. */
  488. num = PyLong_FromString("FF", NULL, 16);
  489. if (num == NULL)
  490. return NULL;
  491. overflow = 1234;
  492. value = PyLong_AsLongAndOverflow(num, &overflow);
  493. Py_DECREF(num);
  494. if (value == -1 && PyErr_Occurred())
  495. return NULL;
  496. if (value != 0xFF)
  497. return raiseTestError("test_long_and_overflow",
  498. "expected return value 0xFF");
  499. if (overflow != 0)
  500. return raiseTestError("test_long_and_overflow",
  501. "overflow was not cleared");
  502. num = PyLong_FromString("-FF", NULL, 16);
  503. if (num == NULL)
  504. return NULL;
  505. overflow = 0;
  506. value = PyLong_AsLongAndOverflow(num, &overflow);
  507. Py_DECREF(num);
  508. if (value == -1 && PyErr_Occurred())
  509. return NULL;
  510. if (value != -0xFF)
  511. return raiseTestError("test_long_and_overflow",
  512. "expected return value 0xFF");
  513. if (overflow != 0)
  514. return raiseTestError("test_long_and_overflow",
  515. "overflow was set incorrectly");
  516. num = PyLong_FromLong(LONG_MAX);
  517. if (num == NULL)
  518. return NULL;
  519. overflow = 1234;
  520. value = PyLong_AsLongAndOverflow(num, &overflow);
  521. Py_DECREF(num);
  522. if (value == -1 && PyErr_Occurred())
  523. return NULL;
  524. if (value != LONG_MAX)
  525. return raiseTestError("test_long_and_overflow",
  526. "expected return value LONG_MAX");
  527. if (overflow != 0)
  528. return raiseTestError("test_long_and_overflow",
  529. "overflow was not cleared");
  530. num = PyLong_FromLong(LONG_MIN);
  531. if (num == NULL)
  532. return NULL;
  533. overflow = 0;
  534. value = PyLong_AsLongAndOverflow(num, &overflow);
  535. Py_DECREF(num);
  536. if (value == -1 && PyErr_Occurred())
  537. return NULL;
  538. if (value != LONG_MIN)
  539. return raiseTestError("test_long_and_overflow",
  540. "expected return value LONG_MIN");
  541. if (overflow != 0)
  542. return raiseTestError("test_long_and_overflow",
  543. "overflow was not cleared");
  544. Py_INCREF(Py_None);
  545. return Py_None;
  546. }
  547. /* Test the PyLong_AsLongLongAndOverflow API. General conversion to
  548. PY_LONG_LONG is tested by test_long_api_inner. This test will
  549. concentrate on proper handling of overflow.
  550. */
  551. static PyObject *
  552. test_long_long_and_overflow(PyObject *self)
  553. {
  554. PyObject *num, *one, *temp;
  555. PY_LONG_LONG value;
  556. int overflow;
  557. /* Test that overflow is set properly for a large value. */
  558. /* num is a number larger than PY_LLONG_MAX on a typical machine. */
  559. num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
  560. if (num == NULL)
  561. return NULL;
  562. overflow = 1234;
  563. value = PyLong_AsLongLongAndOverflow(num, &overflow);
  564. Py_DECREF(num);
  565. if (value == -1 && PyErr_Occurred())
  566. return NULL;
  567. if (value != -1)
  568. return raiseTestError("test_long_long_and_overflow",
  569. "return value was not set to -1");
  570. if (overflow != 1)
  571. return raiseTestError("test_long_long_and_overflow",
  572. "overflow was not set to 1");
  573. /* Same again, with num = PY_LLONG_MAX + 1 */
  574. num = PyLong_FromLongLong(PY_LLONG_MAX);
  575. if (num == NULL)
  576. return NULL;
  577. one = PyLong_FromLong(1L);
  578. if (one == NULL) {
  579. Py_DECREF(num);
  580. return NULL;
  581. }
  582. temp = PyNumber_Add(num, one);
  583. Py_DECREF(one);
  584. Py_DECREF(num);
  585. num = temp;
  586. if (num == NULL)
  587. return NULL;
  588. overflow = 0;
  589. value = PyLong_AsLongLongAndOverflow(num, &overflow);
  590. Py_DECREF(num);
  591. if (value == -1 && PyErr_Occurred())
  592. return NULL;
  593. if (value != -1)
  594. return raiseTestError("test_long_long_and_overflow",
  595. "return value was not set to -1");
  596. if (overflow != 1)
  597. return raiseTestError("test_long_long_and_overflow",
  598. "overflow was not set to 1");
  599. /* Test that overflow is set properly for a large negative value. */
  600. /* num is a number smaller than PY_LLONG_MIN on a typical platform */
  601. num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
  602. if (num == NULL)
  603. return NULL;
  604. overflow = 1234;
  605. value = PyLong_AsLongLongAndOverflow(num, &overflow);
  606. Py_DECREF(num);
  607. if (value == -1 && PyErr_Occurred())
  608. return NULL;
  609. if (value != -1)
  610. return raiseTestError("test_long_long_and_overflow",
  611. "return value was not set to -1");
  612. if (overflow != -1)
  613. return raiseTestError("test_long_long_and_overflow",
  614. "overflow was not set to -1");
  615. /* Same again, with num = PY_LLONG_MIN - 1 */
  616. num = PyLong_FromLongLong(PY_LLONG_MIN);
  617. if (num == NULL)
  618. return NULL;
  619. one = PyLong_FromLong(1L);
  620. if (one == NULL) {
  621. Py_DECREF(num);
  622. return NULL;
  623. }
  624. temp = PyNumber_Subtract(num, one);
  625. Py_DECREF(one);
  626. Py_DECREF(num);
  627. num = temp;
  628. if (num == NULL)
  629. return NULL;
  630. overflow = 0;
  631. value = PyLong_AsLongLongAndOverflow(num, &overflow);
  632. Py_DECREF(num);
  633. if (value == -1 && PyErr_Occurred())
  634. return NULL;
  635. if (value != -1)
  636. return raiseTestError("test_long_long_and_overflow",
  637. "return value was not set to -1");
  638. if (overflow != -1)
  639. return raiseTestError("test_long_long_and_overflow",
  640. "overflow was not set to -1");
  641. /* Test that overflow is cleared properly for small values. */
  642. num = PyLong_FromString("FF", NULL, 16);
  643. if (num == NULL)
  644. return NULL;
  645. overflow = 1234;
  646. value = PyLong_AsLongLongAndOverflow(num, &overflow);
  647. Py_DECREF(num);
  648. if (value == -1 && PyErr_Occurred())
  649. return NULL;
  650. if (value != 0xFF)
  651. return raiseTestError("test_long_long_and_overflow",
  652. "expected return value 0xFF");
  653. if (overflow != 0)
  654. return raiseTestError("test_long_long_and_overflow",
  655. "overflow was not cleared");
  656. num = PyLong_FromString("-FF", NULL, 16);
  657. if (num == NULL)
  658. return NULL;
  659. overflow = 0;
  660. value = PyLong_AsLongLongAndOverflow(num, &overflow);
  661. Py_DECREF(num);
  662. if (value == -1 && PyErr_Occurred())
  663. return NULL;
  664. if (value != -0xFF)
  665. return raiseTestError("test_long_long_and_overflow",
  666. "expected return value 0xFF");
  667. if (overflow != 0)
  668. return raiseTestError("test_long_long_and_overflow",
  669. "overflow was set incorrectly");
  670. num = PyLong_FromLongLong(PY_LLONG_MAX);
  671. if (num == NULL)
  672. return NULL;
  673. overflow = 1234;
  674. value = PyLong_AsLongLongAndOverflow(num, &overflow);
  675. Py_DECREF(num);
  676. if (value == -1 && PyErr_Occurred())
  677. return NULL;
  678. if (value != PY_LLONG_MAX)
  679. return raiseTestError("test_long_long_and_overflow",
  680. "expected return value PY_LLONG_MAX");
  681. if (overflow != 0)
  682. return raiseTestError("test_long_long_and_overflow",
  683. "overflow was not cleared");
  684. num = PyLong_FromLongLong(PY_LLONG_MIN);
  685. if (num == NULL)
  686. return NULL;
  687. overflow = 0;
  688. value = PyLong_AsLongLongAndOverflow(num, &overflow);
  689. Py_DECREF(num);
  690. if (value == -1 && PyErr_Occurred())
  691. return NULL;
  692. if (value != PY_LLONG_MIN)
  693. return raiseTestError("test_long_long_and_overflow",
  694. "expected return value PY_LLONG_MIN");
  695. if (overflow != 0)
  696. return raiseTestError("test_long_long_and_overflow",
  697. "overflow was not cleared");
  698. Py_INCREF(Py_None);
  699. return Py_None;
  700. }
  701. /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
  702. for both long and int arguments. The test may leak a little memory if
  703. it fails.
  704. */
  705. static PyObject *
  706. test_L_code(PyObject *self)
  707. {
  708. PyObject *tuple, *num;
  709. PY_LONG_LONG value;
  710. tuple = PyTuple_New(1);
  711. if (tuple == NULL)
  712. return NULL;
  713. num = PyLong_FromLong(42);
  714. if (num == NULL)
  715. return NULL;
  716. PyTuple_SET_ITEM(tuple, 0, num);
  717. value = -1;
  718. if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
  719. return NULL;
  720. if (value != 42)
  721. return raiseTestError("test_L_code",
  722. "L code returned wrong value for long 42");
  723. Py_DECREF(num);
  724. num = PyInt_FromLong(42);
  725. if (num == NULL)
  726. return NULL;
  727. PyTuple_SET_ITEM(tuple, 0, num);
  728. value = -1;
  729. if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
  730. return NULL;
  731. if (value != 42)
  732. return raiseTestError("test_L_code",
  733. "L code returned wrong value for int 42");
  734. Py_DECREF(tuple);
  735. Py_INCREF(Py_None);
  736. return Py_None;
  737. }
  738. #endif /* ifdef HAVE_LONG_LONG */
  739. /* Test tuple argument processing */
  740. static PyObject *
  741. getargs_tuple(PyObject *self, PyObject *args)
  742. {
  743. int a, b, c;
  744. if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
  745. return NULL;
  746. return Py_BuildValue("iii", a, b, c);
  747. }
  748. /* test PyArg_ParseTupleAndKeywords */
  749. static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
  750. {
  751. static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
  752. static char *fmt="(ii)i|(i(ii))(iii)i";
  753. int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  754. if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
  755. &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
  756. &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
  757. return NULL;
  758. return Py_BuildValue("iiiiiiiiii",
  759. int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
  760. int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
  761. }
  762. /* Functions to call PyArg_ParseTuple with integer format codes,
  763. and return the result.
  764. */
  765. static PyObject *
  766. getargs_b(PyObject *self, PyObject *args)
  767. {
  768. unsigned char value;
  769. if (!PyArg_ParseTuple(args, "b", &value))
  770. return NULL;
  771. return PyLong_FromUnsignedLong((unsigned long)value);
  772. }
  773. static PyObject *
  774. getargs_B(PyObject *self, PyObject *args)
  775. {
  776. unsigned char value;
  777. if (!PyArg_ParseTuple(args, "B", &value))
  778. return NULL;
  779. return PyLong_FromUnsignedLong((unsigned long)value);
  780. }
  781. static PyObject *
  782. getargs_h(PyObject *self, PyObject *args)
  783. {
  784. short value;
  785. if (!PyArg_ParseTuple(args, "h", &value))
  786. return NULL;
  787. return PyLong_FromLong((long)value);
  788. }
  789. static PyObject *
  790. getargs_H(PyObject *self, PyObject *args)
  791. {
  792. unsigned short value;
  793. if (!PyArg_ParseTuple(args, "H", &value))
  794. return NULL;
  795. return PyLong_FromUnsignedLong((unsigned long)value);
  796. }
  797. static PyObject *
  798. getargs_I(PyObject *self, PyObject *args)
  799. {
  800. unsigned int value;
  801. if (!PyArg_ParseTuple(args, "I", &value))
  802. return NULL;
  803. return PyLong_FromUnsignedLong((unsigned long)value);
  804. }
  805. static PyObject *
  806. getargs_k(PyObject *self, PyObject *args)
  807. {
  808. unsigned long value;
  809. if (!PyArg_ParseTuple(args, "k", &value))
  810. return NULL;
  811. return PyLong_FromUnsignedLong(value);
  812. }
  813. static PyObject *
  814. getargs_i(PyObject *self, PyObject *args)
  815. {
  816. int value;
  817. if (!PyArg_ParseTuple(args, "i", &value))
  818. return NULL;
  819. return PyLong_FromLong((long)value);
  820. }
  821. static PyObject *
  822. getargs_l(PyObject *self, PyObject *args)
  823. {
  824. long value;
  825. if (!PyArg_ParseTuple(args, "l", &value))
  826. return NULL;
  827. return PyLong_FromLong(value);
  828. }
  829. static PyObject *
  830. getargs_n(PyObject *self, PyObject *args)
  831. {
  832. Py_ssize_t value;
  833. if (!PyArg_ParseTuple(args, "n", &value))
  834. return NULL;
  835. return PyInt_FromSsize_t(value);
  836. }
  837. #ifdef HAVE_LONG_LONG
  838. static PyObject *
  839. getargs_L(PyObject *self, PyObject *args)
  840. {
  841. PY_LONG_LONG value;
  842. if (!PyArg_ParseTuple(args, "L", &value))
  843. return NULL;
  844. return PyLong_FromLongLong(value);
  845. }
  846. static PyObject *
  847. getargs_K(PyObject *self, PyObject *args)
  848. {
  849. unsigned PY_LONG_LONG value;
  850. if (!PyArg_ParseTuple(args, "K", &value))
  851. return NULL;
  852. return PyLong_FromUnsignedLongLong(value);
  853. }
  854. #endif
  855. /* This function not only tests the 'k' getargs code, but also the
  856. PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
  857. static PyObject *
  858. test_k_code(PyObject *self)
  859. {
  860. PyObject *tuple, *num;
  861. unsigned long value;
  862. tuple = PyTuple_New(1);
  863. if (tuple == NULL)
  864. return NULL;
  865. /* a number larger than ULONG_MAX even on 64-bit platforms */
  866. num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
  867. if (num == NULL)
  868. return NULL;
  869. value = PyInt_AsUnsignedLongMask(num);
  870. if (value != ULONG_MAX)
  871. return raiseTestError("test_k_code",
  872. "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
  873. PyTuple_SET_ITEM(tuple, 0, num);
  874. value = 0;
  875. if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
  876. return NULL;
  877. if (value != ULONG_MAX)
  878. return raiseTestError("test_k_code",
  879. "k code returned wrong value for long 0xFFF...FFF");
  880. Py_DECREF(num);
  881. num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
  882. if (num == NULL)
  883. return NULL;
  884. value = PyInt_AsUnsignedLongMask(num);
  885. if (value != (unsigned long)-0x42)
  886. return raiseTestError("test_k_code",
  887. "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
  888. PyTuple_SET_ITEM(tuple, 0, num);
  889. value = 0;
  890. if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
  891. return NULL;
  892. if (value != (unsigned long)-0x42)
  893. return raiseTestError("test_k_code",
  894. "k code returned wrong value for long -0xFFF..000042");
  895. Py_DECREF(tuple);
  896. Py_INCREF(Py_None);
  897. return Py_None;
  898. }
  899. #ifdef Py_USING_UNICODE
  900. static volatile int x;
  901. /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
  902. of an error.
  903. */
  904. static PyObject *
  905. test_u_code(PyObject *self)
  906. {
  907. PyObject *tuple, *obj;
  908. Py_UNICODE *value;
  909. int len;
  910. /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
  911. /* Just use the macro and check that it compiles */
  912. x = Py_UNICODE_ISSPACE(25);
  913. tuple = PyTuple_New(1);
  914. if (tuple == NULL)
  915. return NULL;
  916. obj = PyUnicode_Decode("test", strlen("test"),
  917. "ascii", NULL);
  918. if (obj == NULL)
  919. return NULL;
  920. PyTuple_SET_ITEM(tuple, 0, obj);
  921. value = 0;
  922. if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
  923. return NULL;
  924. if (value != PyUnicode_AS_UNICODE(obj))
  925. return raiseTestError("test_u_code",
  926. "u code returned wrong value for u'test'");
  927. value = 0;
  928. if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
  929. return NULL;
  930. if (value != PyUnicode_AS_UNICODE(obj) ||
  931. len != PyUnicode_GET_SIZE(obj))
  932. return raiseTestError("test_u_code",
  933. "u# code returned wrong values for u'test'");
  934. Py_DECREF(tuple);
  935. Py_INCREF(Py_None);
  936. return Py_None;
  937. }
  938. static PyObject *
  939. test_widechar(PyObject *self)
  940. {
  941. #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
  942. const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
  943. size_t wtextlen = 1;
  944. #else
  945. const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
  946. size_t wtextlen = 2;
  947. #endif
  948. PyObject *wide, *utf8;
  949. wide = PyUnicode_FromWideChar(wtext, wtextlen);
  950. if (wide == NULL)
  951. return NULL;
  952. utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d");
  953. if (utf8 == NULL) {
  954. Py_DECREF(wide);
  955. return NULL;
  956. }
  957. if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) {
  958. Py_DECREF(wide);
  959. Py_DECREF(utf8);
  960. return raiseTestError("test_widechar",
  961. "wide string and utf8 string have different length");
  962. }
  963. if (PyUnicode_Compare(wide, utf8)) {
  964. Py_DECREF(wide);
  965. Py_DECREF(utf8);
  966. if (PyErr_Occurred())
  967. return NULL;
  968. return raiseTestError("test_widechar",
  969. "wide string and utf8 string are differents");
  970. }
  971. Py_DECREF(wide);
  972. Py_DECREF(utf8);
  973. Py_RETURN_NONE;
  974. }
  975. static PyObject *
  976. test_empty_argparse(PyObject *self)
  977. {
  978. /* Test that formats can begin with '|'. See issue #4720. */
  979. PyObject *tuple, *dict = NULL;
  980. static char *kwlist[] = {NULL};
  981. int result;
  982. tuple = PyTuple_New(0);
  983. if (!tuple)
  984. return NULL;
  985. if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0)
  986. goto done;
  987. dict = PyDict_New();
  988. if (!dict)
  989. goto done;
  990. result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
  991. done:
  992. Py_DECREF(tuple);
  993. Py_XDECREF(dict);
  994. if (result < 0)
  995. return NULL;
  996. else {
  997. Py_RETURN_NONE;
  998. }
  999. }
  1000. static PyObject *
  1001. codec_incrementalencoder(PyObject *self, PyObject *args)
  1002. {
  1003. const char *encoding, *errors = NULL;
  1004. if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
  1005. &encoding, &errors))
  1006. return NULL;
  1007. return PyCodec_IncrementalEncoder(encoding, errors);
  1008. }
  1009. static PyObject *
  1010. codec_incrementaldecoder(PyObject *self, PyObject *args)
  1011. {
  1012. const char *encoding, *errors = NULL;
  1013. if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
  1014. &encoding, &errors))
  1015. return NULL;
  1016. return PyCodec_IncrementalDecoder(encoding, errors);
  1017. }
  1018. #endif
  1019. /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
  1020. static PyObject *
  1021. test_long_numbits(PyObject *self)
  1022. {
  1023. struct triple {
  1024. long input;
  1025. size_t nbits;
  1026. int sign;
  1027. } testcases[] = {{0, 0, 0},
  1028. {1L, 1, 1},
  1029. {-1L, 1, -1},
  1030. {2L, 2, 1},
  1031. {-2L, 2, -1},
  1032. {3L, 2, 1},
  1033. {-3L, 2, -1},
  1034. {4L, 3, 1},
  1035. {-4L, 3, -1},
  1036. {0x7fffL, 15, 1}, /* one Python long digit */
  1037. {-0x7fffL, 15, -1},
  1038. {0xffffL, 16, 1},
  1039. {-0xffffL, 16, -1},
  1040. {0xfffffffL, 28, 1},
  1041. {-0xfffffffL, 28, -1}};
  1042. int i;
  1043. for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
  1044. PyObject *plong = PyLong_FromLong(testcases[i].input);
  1045. size_t nbits = _PyLong_NumBits(plong);
  1046. int sign = _PyLong_Sign(plong);
  1047. Py_DECREF(plong);
  1048. if (nbits != testcases[i].nbits)
  1049. return raiseTestError("test_long_numbits",
  1050. "wrong result for _PyLong_NumBits");
  1051. if (sign != testcases[i].sign)
  1052. return raiseTestError("test_long_numbits",
  1053. "wrong result for _PyLong_Sign");
  1054. }
  1055. Py_INCREF(Py_None);
  1056. return Py_None;
  1057. }
  1058. /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
  1059. static PyObject *
  1060. test_null_strings(PyObject *self)
  1061. {
  1062. PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL);
  1063. PyObject *tuple = PyTuple_Pack(2, o1, o2);
  1064. Py_XDECREF(o1);
  1065. Py_XDECREF(o2);
  1066. return tuple;
  1067. }
  1068. static PyObject *
  1069. raise_exception(PyObject *self, PyObject *args)
  1070. {
  1071. PyObject *exc;
  1072. PyObject *exc_args, *v;
  1073. int num_args, i;
  1074. if (!PyArg_ParseTuple(args, "Oi:raise_exception",
  1075. &exc, &num_args))
  1076. return NULL;
  1077. if (!PyExceptionClass_Check(exc)) {
  1078. PyErr_Format(PyExc_TypeError, "an exception class is required");
  1079. return NULL;
  1080. }
  1081. exc_args = PyTuple_New(num_args);
  1082. if (exc_args == NULL)
  1083. return NULL;
  1084. for (i = 0; i < num_args; ++i) {
  1085. v = PyInt_FromLong(i);
  1086. if (v == NULL) {
  1087. Py_DECREF(exc_args);
  1088. return NULL;
  1089. }
  1090. PyTuple_SET_ITEM(exc_args, i, v);
  1091. }
  1092. PyErr_SetObject(exc, exc_args);
  1093. Py_DECREF(exc_args);
  1094. return NULL;
  1095. }
  1096. static int test_run_counter = 0;
  1097. static PyObject *
  1098. test_datetime_capi(PyObject *self, PyObject *args) {
  1099. if (PyDateTimeAPI) {
  1100. if (test_run_counter) {
  1101. /* Probably regrtest.py -R */
  1102. Py_RETURN_NONE;
  1103. }
  1104. else {
  1105. PyErr_SetString(PyExc_AssertionError,
  1106. "PyDateTime_CAPI somehow initialized");
  1107. return NULL;
  1108. }
  1109. }
  1110. test_run_counter++;
  1111. PyDateTime_IMPORT;
  1112. if (PyDateTimeAPI)
  1113. Py_RETURN_NONE;
  1114. else
  1115. return NULL;
  1116. }
  1117. #ifdef WITH_THREAD
  1118. /* test_thread_state spawns a thread of its own, and that thread releases
  1119. * `thread_done` when it's finished. The driver code has to know when the
  1120. * thread finishes, because the thread uses a PyObject (the callable) that
  1121. * may go away when the driver finishes. The former lack of this explicit
  1122. * synchronization caused rare segfaults, so rare that they were seen only
  1123. * on a Mac buildbot (although they were possible on any box).
  1124. */
  1125. static PyThread_type_lock thread_done = NULL;
  1126. static int
  1127. _make_call(void *callable)
  1128. {
  1129. PyObject *rc;
  1130. int success;
  1131. PyGILState_STATE s = PyGILState_Ensure();
  1132. rc = PyObject_CallFunction((PyObject *)callable, "");
  1133. success = (rc != NULL);
  1134. Py_XDECREF(rc);
  1135. PyGILState_Release(s);
  1136. return success;
  1137. }
  1138. /* Same thing, but releases `thread_done` when it returns. This variant
  1139. * should be called only from threads spawned by test_thread_state().
  1140. */
  1141. static void
  1142. _make_call_from_thread(void *callable)
  1143. {
  1144. _make_call(callable);
  1145. PyThread_release_lock(thread_done);
  1146. }
  1147. static PyObject *
  1148. test_thread_state(PyObject *self, PyObject *args)
  1149. {
  1150. PyObject *fn;
  1151. int success = 1;
  1152. if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
  1153. return NULL;
  1154. if (!PyCallable_Check(fn)) {
  1155. PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
  1156. fn->ob_type->tp_name);
  1157. return NULL;
  1158. }
  1159. /* Ensure Python is set up for threading */
  1160. PyEval_InitThreads();
  1161. thread_done = PyThread_allocate_lock();
  1162. if (thread_done == NULL)
  1163. return PyErr_NoMemory();
  1164. PyThread_acquire_lock(thread_done, 1);
  1165. /* Start a new thread with our callback. */
  1166. PyThread_start_new_thread(_make_call_from_thread, fn);
  1167. /* Make the callback with the thread lock held by this thread */
  1168. success &= _make_call(fn);
  1169. /* Do it all again, but this time with the thread-lock released */
  1170. Py_BEGIN_ALLOW_THREADS
  1171. success &= _make_call(fn);
  1172. PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
  1173. Py_END_ALLOW_THREADS
  1174. /* And once more with and without a thread
  1175. XXX - should use a lock and work out exactly what we are trying
  1176. to test <wink>
  1177. */
  1178. Py_BEGIN_ALLOW_THREADS
  1179. PyThread_start_new_thread(_make_call_from_thread, fn);
  1180. success &= _make_call(fn);
  1181. PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
  1182. Py_END_ALLOW_THREADS
  1183. /* Release lock we acquired above. This is required on HP-UX. */
  1184. PyThread_release_lock(thread_done);
  1185. PyThread_free_lock(thread_done);
  1186. if (!success)
  1187. return NULL;
  1188. Py_RETURN_NONE;
  1189. }
  1190. /* test Py_AddPendingCalls using threads */
  1191. static int _pending_callback(void *arg)
  1192. {
  1193. /* we assume the argument is callable object to which we own a reference */
  1194. PyObject *callable = (PyObject *)arg;
  1195. PyObject *r = PyObject_CallObject(callable, NULL);
  1196. Py_DECREF(callable);
  1197. Py_XDECREF(r);
  1198. return r != NULL ? 0 : -1;
  1199. }
  1200. /* The following requests n callbacks to _pending_callback. It can be
  1201. * run from any python thread.
  1202. */
  1203. PyObject *pending_threadfunc(PyObject *self, PyObject *arg)
  1204. {
  1205. PyObject *callable;
  1206. int r;
  1207. if (PyArg_ParseTuple(arg, "O", &callable) == 0)
  1208. return NULL;
  1209. /* create the reference for the callbackwhile we hold the lock */
  1210. Py_INCREF(callable);
  1211. Py_BEGIN_ALLOW_THREADS
  1212. r = Py_AddPendingCall(&_pending_callback, callable);
  1213. Py_END_ALLOW_THREADS
  1214. if (r<0) {
  1215. Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
  1216. Py_INCREF(Py_False);
  1217. return Py_False;
  1218. }
  1219. Py_INCREF(Py_True);
  1220. return Py_True;
  1221. }
  1222. #endif
  1223. /* Some tests of PyString_FromFormat(). This needs more tests. */
  1224. static PyObject *
  1225. test_string_from_format(PyObject *self, PyObject *args)
  1226. {
  1227. PyObject *result;
  1228. char *msg;
  1229. #define CHECK_1_FORMAT(FORMAT, TYPE) \
  1230. result = PyString_FromFormat(FORMAT, (TYPE)1); \
  1231. if (result == NULL) \
  1232. return NULL; \
  1233. if (strcmp(PyString_AsString(result), "1")) { \
  1234. msg = FORMAT " failed at 1"; \
  1235. goto Fail; \
  1236. } \
  1237. Py_DECREF(result)
  1238. CHECK_1_FORMAT("%d", int);
  1239. CHECK_1_FORMAT("%ld", long);
  1240. /* The z width modifier was added in Python 2.5. */
  1241. CHECK_1_FORMAT("%zd", Py_ssize_t);
  1242. /* The u type code was added in Python 2.5. */
  1243. CHECK_1_FORMAT("%u", unsigned int);
  1244. CHECK_1_FORMAT("%lu", unsigned long);
  1245. CHECK_1_FORMAT("%zu", size_t);
  1246. /* "%lld" and "%llu" support added in Python 2.7. */
  1247. #ifdef HAVE_LONG_LONG
  1248. CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG);
  1249. CHECK_1_FORMAT("%lld", PY_LONG_LONG);
  1250. #endif
  1251. Py_RETURN_NONE;
  1252. Fail:
  1253. Py_XDECREF(result);
  1254. return raiseTestError("test_string_from_format", msg);
  1255. #undef CHECK_1_FORMAT
  1256. }
  1257. /* Coverage testing of capsule objects. */
  1258. static const char *capsule_name = "capsule name";
  1259. static char *capsule_pointer = "capsule pointer";
  1260. static char *capsule_context = "capsule context";
  1261. static const char *capsule_error = NULL;
  1262. static int
  1263. capsule_destructor_call_count = 0;
  1264. static void
  1265. capsule_destructor(PyObject *o) {
  1266. capsule_destructor_call_count++;
  1267. if (PyCapsule_GetContext(o) != capsule_context) {
  1268. capsule_error = "context did not match in destructor!";
  1269. } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
  1270. capsule_error = "destructor did not match in destructor! (woah!)";
  1271. } else if (PyCapsule_GetName(o) != capsule_name) {
  1272. capsule_error = "name did not match in destructor!";
  1273. } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
  1274. capsule_error = "pointer did not match in destructor!";
  1275. }
  1276. }
  1277. typedef struct {
  1278. char *name;
  1279. char *module;
  1280. char *attribute;
  1281. } known_capsule;
  1282. static PyObject *
  1283. test_capsule(PyObject *self, PyObject *args)
  1284. {
  1285. PyObject *object;
  1286. const char *error = NULL;
  1287. void *pointer;
  1288. void *pointer2;
  1289. known_capsule known_capsules[] = {
  1290. #define KNOWN_CAPSULE(module, name) { module "." name, module, name }
  1291. KNOWN_CAPSULE("_socket", "CAPI"),
  1292. KNOWN_CAPSULE("_curses", "_C_API"),
  1293. KNOWN_CAPSULE("datetime", "datetime_CAPI"),
  1294. { NULL, NULL },
  1295. };
  1296. known_capsule *known = &known_capsules[0];
  1297. #define FAIL(x) { error = (x); goto exit; }
  1298. #define CHECK_DESTRUCTOR \
  1299. if (capsule_error) { \
  1300. FAIL(capsule_error); \
  1301. } \
  1302. else if (!capsule_destructor_call_count) { \
  1303. FAIL("destructor not called!"); \
  1304. } \
  1305. capsule_destructor_call_count = 0; \
  1306. object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
  1307. PyCapsule_SetContext(object, capsule_context);
  1308. capsule_destructor(object);
  1309. CHECK_DESTRUCTOR;
  1310. Py_DECREF(object);
  1311. CHECK_DESTRUCTOR;
  1312. object = PyCapsule_New(known, "ignored", NULL);
  1313. PyCapsule_SetPointer(object, capsule_pointer);
  1314. PyCapsule_SetName(object, capsule_name);
  1315. PyCapsule_SetDestructor(object, capsule_destructor);
  1316. PyCapsule_SetContext(object, capsule_context);
  1317. capsule_destructor(object);
  1318. CHECK_DESTRUCTOR;
  1319. /* intentionally access using the wrong name */
  1320. pointer2 = PyCapsule_GetPointer(object, "the wrong name");
  1321. if (!PyErr_Occurred()) {
  1322. FAIL("PyCapsule_GetPointer should have failed but did not!");
  1323. }
  1324. PyErr_Clear();
  1325. if (pointer2) {
  1326. if (pointer2 == capsule_pointer) {
  1327. FAIL("PyCapsule_GetPointer should not have"
  1328. " returned the internal pointer!");
  1329. } else {
  1330. FAIL("PyCapsule_GetPointer should have "
  1331. "returned NULL pointer but did not!");
  1332. }
  1333. }
  1334. PyCapsule_SetDestructor(object, NULL);
  1335. Py_DECREF(object);
  1336. if (capsule_destructor_call_count) {
  1337. FAIL("destructor called when it should not have been!");
  1338. }
  1339. for (known = &known_capsules[0]; known->module != NULL; known++) {
  1340. /* yeah, ordinarily I wouldn't do this either,
  1341. but it's fine for this test harness.
  1342. */
  1343. static char buffer[256];
  1344. #undef FAIL
  1345. #define FAIL(x) \
  1346. { \
  1347. sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
  1348. x, known->module, known->attribute); \
  1349. error = buffer; \
  1350. goto exit; \
  1351. } \
  1352. PyObject *module = PyImport_ImportModule(known->module);
  1353. if (module) {
  1354. pointer = PyCapsule_Import(known->name, 0);
  1355. if (!pointer) {
  1356. Py_DECREF(module);
  1357. FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
  1358. }
  1359. object = PyObject_GetAttrString(module, known->attribute);
  1360. if (!object) {
  1361. Py_DECREF(module);
  1362. return NULL;
  1363. }
  1364. pointer2 = PyCapsule_GetPointer(object,
  1365. "weebles wobble but they don't fall down");
  1366. if (!PyErr_Occurred()) {
  1367. Py_DECREF(object);
  1368. Py_DECREF(module);
  1369. FAIL("PyCapsule_GetPointer should have failed but did not!");
  1370. }
  1371. PyErr_Clear();
  1372. if (pointer2) {
  1373. Py_DECREF(module);
  1374. Py_DECREF(object);
  1375. if (pointer2 == pointer) {
  1376. FAIL("PyCapsule_GetPointer should not have"
  1377. " returned its internal pointer!");
  1378. } else {
  1379. FAIL("PyCapsule_GetPointer should have"
  1380. " returned NULL pointer but did not!");
  1381. }
  1382. }
  1383. Py_DECREF(object);
  1384. Py_DECREF(module);
  1385. }
  1386. else
  1387. PyErr_Clear();
  1388. }
  1389. exit:
  1390. if (error) {
  1391. return raiseTestError("test_capsule", error);
  1392. }
  1393. Py_RETURN_NONE;
  1394. #undef FAIL
  1395. }
  1396. /* This is here to provide a docstring for test_descr. */
  1397. static PyObject *
  1398. test_with_docstring(PyObject *self)
  1399. {
  1400. Py_RETURN_NONE;
  1401. }
  1402. /* To test the format of tracebacks as printed out. */
  1403. static PyObject *
  1404. traceback_print(PyObject *self, PyObject *args)
  1405. {
  1406. PyObject *file;
  1407. PyObject *traceback;
  1408. int result;
  1409. if (!PyArg_ParseTuple(args, "OO:traceback_print",
  1410. &traceback, &file))
  1411. return NULL;
  1412. result = PyTraceBack_Print(traceback, file);
  1413. if (result < 0)
  1414. return NULL;
  1415. Py_RETURN_NONE;
  1416. }
  1417. /* To test that the result of PyCode_NewEmpty has the right members. */
  1418. static PyObject *
  1419. code_newempty(PyObject *self, PyObject *args)
  1420. {
  1421. const char *filename;
  1422. const char *funcname;
  1423. int firstlineno;
  1424. if (!PyArg_ParseTuple(args, "ssi:code_newempty",
  1425. &filename, &funcname, &firstlineno))
  1426. return NULL;
  1427. return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno);
  1428. }
  1429. /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException).
  1430. Run via Lib/test/test_exceptions.py */
  1431. static PyObject *
  1432. make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs)
  1433. {
  1434. char *name;
  1435. char *doc = NULL;
  1436. PyObject *base = NULL;
  1437. PyObject *dict = NULL;
  1438. static char *kwlist[] = {"name", "doc", "base", "dict", NULL};
  1439. if (!PyArg_ParseTupleAndKeywords(args, kwargs,
  1440. "s|sOO:make_exception_with_doc", kwlist,
  1441. &name, &doc, &base, &dict))
  1442. return NULL;
  1443. return PyErr_NewExceptionWithDoc(name, doc, base, dict);
  1444. }
  1445. static PyMethodDef TestMethods[] = {
  1446. {"raise_exception", raise_exception, METH_VARARGS},
  1447. {"test_config", (PyCFunction)test_config, METH_NOARGS},
  1448. {"test_datetime_capi", test_datetime_capi, METH_NOARGS},
  1449. {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
  1450. {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
  1451. {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS},
  1452. {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS},
  1453. {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
  1454. {"test_long_and_overflow", (PyCFunction)test_long_and_overflow,
  1455. METH_NOARGS},
  1456. {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
  1457. {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
  1458. {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
  1459. {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
  1460. {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
  1461. {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
  1462. PyDoc_STR("This is a pretty normal docstring.")},
  1463. {"getargs_tuple", getargs_tuple, METH_VARARGS},
  1464. {"getargs_keywords", (PyCFunction)getargs_keywords,
  1465. METH_VARARGS|METH_KEYWORDS},
  1466. {"getargs_b", getargs_b, METH_VARARGS},
  1467. {"getargs_B", getargs_B, METH_VARARGS},
  1468. {"getargs_h", getargs_h, ME

Large files files are truncated, but you can click here to view the full file