You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
7.1 KiB

  1. #include <Python.h>
  2. #include "pythread.h"
  3. #include <inttypes.h>
  4. #include <stdio.h>
  5. /*********************************************************
  6. * Embedded interpreter tests that need a custom exe
  7. *
  8. * Executed via 'EmbeddingTests' in Lib/test/test_capi.py
  9. *********************************************************/
  10. static void _testembed_Py_Initialize(void)
  11. {
  12. /* HACK: the "./" at front avoids a search along the PATH in
  13. Modules/getpath.c */
  14. Py_SetProgramName(L"./_testembed");
  15. Py_Initialize();
  16. }
  17. /*****************************************************
  18. * Test repeated initialisation and subinterpreters
  19. *****************************************************/
  20. static void print_subinterp(void)
  21. {
  22. /* Output information about the interpreter in the format
  23. expected in Lib/test/test_capi.py (test_subinterps). */
  24. PyThreadState *ts = PyThreadState_Get();
  25. PyInterpreterState *interp = ts->interp;
  26. int64_t id = PyInterpreterState_GetID(interp);
  27. printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ",
  28. id, (uintptr_t)interp, (uintptr_t)ts);
  29. fflush(stdout);
  30. PyRun_SimpleString(
  31. "import sys;"
  32. "print('id(modules) =', id(sys.modules));"
  33. "sys.stdout.flush()"
  34. );
  35. }
  36. static int test_repeated_init_and_subinterpreters(void)
  37. {
  38. PyThreadState *mainstate, *substate;
  39. PyGILState_STATE gilstate;
  40. int i, j;
  41. for (i=0; i<15; i++) {
  42. printf("--- Pass %d ---\n", i);
  43. _testembed_Py_Initialize();
  44. mainstate = PyThreadState_Get();
  45. PyEval_InitThreads();
  46. PyEval_ReleaseThread(mainstate);
  47. gilstate = PyGILState_Ensure();
  48. print_subinterp();
  49. PyThreadState_Swap(NULL);
  50. for (j=0; j<3; j++) {
  51. substate = Py_NewInterpreter();
  52. print_subinterp();
  53. Py_EndInterpreter(substate);
  54. }
  55. PyThreadState_Swap(mainstate);
  56. print_subinterp();
  57. PyGILState_Release(gilstate);
  58. PyEval_RestoreThread(mainstate);
  59. Py_Finalize();
  60. }
  61. return 0;
  62. }
  63. /*****************************************************
  64. * Test forcing a particular IO encoding
  65. *****************************************************/
  66. static void check_stdio_details(const char *encoding, const char * errors)
  67. {
  68. /* Output info for the test case to check */
  69. if (encoding) {
  70. printf("Expected encoding: %s\n", encoding);
  71. } else {
  72. printf("Expected encoding: default\n");
  73. }
  74. if (errors) {
  75. printf("Expected errors: %s\n", errors);
  76. } else {
  77. printf("Expected errors: default\n");
  78. }
  79. fflush(stdout);
  80. /* Force the given IO encoding */
  81. Py_SetStandardStreamEncoding(encoding, errors);
  82. _testembed_Py_Initialize();
  83. PyRun_SimpleString(
  84. "import sys;"
  85. "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
  86. "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
  87. "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
  88. "sys.stdout.flush()"
  89. );
  90. Py_Finalize();
  91. }
  92. static int test_forced_io_encoding(void)
  93. {
  94. /* Check various combinations */
  95. printf("--- Use defaults ---\n");
  96. check_stdio_details(NULL, NULL);
  97. printf("--- Set errors only ---\n");
  98. check_stdio_details(NULL, "ignore");
  99. printf("--- Set encoding only ---\n");
  100. check_stdio_details("latin-1", NULL);
  101. printf("--- Set encoding and errors ---\n");
  102. check_stdio_details("latin-1", "replace");
  103. /* Check calling after initialization fails */
  104. Py_Initialize();
  105. if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) {
  106. printf("Unexpected success calling Py_SetStandardStreamEncoding");
  107. }
  108. Py_Finalize();
  109. return 0;
  110. }
  111. /*********************************************************
  112. * Test parts of the C-API that work before initialization
  113. *********************************************************/
  114. static int test_pre_initialization_api(void)
  115. {
  116. /* Leading "./" ensures getpath.c can still find the standard library */
  117. wchar_t *program = Py_DecodeLocale("./spam", NULL);
  118. if (program == NULL) {
  119. fprintf(stderr, "Fatal error: cannot decode program name\n");
  120. return 1;
  121. }
  122. Py_SetProgramName(program);
  123. Py_Initialize();
  124. Py_Finalize();
  125. PyMem_RawFree(program);
  126. return 0;
  127. }
  128. static void bpo20891_thread(void *lockp)
  129. {
  130. PyThread_type_lock lock = *((PyThread_type_lock*)lockp);
  131. PyGILState_STATE state = PyGILState_Ensure();
  132. if (!PyGILState_Check()) {
  133. fprintf(stderr, "PyGILState_Check failed!");
  134. abort();
  135. }
  136. PyGILState_Release(state);
  137. PyThread_release_lock(lock);
  138. PyThread_exit_thread();
  139. }
  140. static int test_bpo20891(void)
  141. {
  142. /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
  143. calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
  144. call PyEval_InitThreads() for us in this case. */
  145. PyThread_type_lock lock = PyThread_allocate_lock();
  146. if (!lock) {
  147. fprintf(stderr, "PyThread_allocate_lock failed!");
  148. return 1;
  149. }
  150. _testembed_Py_Initialize();
  151. unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock);
  152. if (thrd == PYTHREAD_INVALID_THREAD_ID) {
  153. fprintf(stderr, "PyThread_start_new_thread failed!");
  154. return 1;
  155. }
  156. PyThread_acquire_lock(lock, WAIT_LOCK);
  157. Py_BEGIN_ALLOW_THREADS
  158. /* wait until the thread exit */
  159. PyThread_acquire_lock(lock, WAIT_LOCK);
  160. Py_END_ALLOW_THREADS
  161. PyThread_free_lock(lock);
  162. return 0;
  163. }
  164. /* *********************************************************
  165. * List of test cases and the function that implements it.
  166. *
  167. * Names are compared case-sensitively with the first
  168. * argument. If no match is found, or no first argument was
  169. * provided, the names of all test cases are printed and
  170. * the exit code will be -1.
  171. *
  172. * The int returned from test functions is used as the exit
  173. * code, and test_capi treats all non-zero exit codes as a
  174. * failed test.
  175. *********************************************************/
  176. struct TestCase
  177. {
  178. const char *name;
  179. int (*func)(void);
  180. };
  181. static struct TestCase TestCases[] = {
  182. { "forced_io_encoding", test_forced_io_encoding },
  183. { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
  184. { "pre_initialization_api", test_pre_initialization_api },
  185. { "bpo20891", test_bpo20891 },
  186. { NULL, NULL }
  187. };
  188. int main(int argc, char *argv[])
  189. {
  190. if (argc > 1) {
  191. for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
  192. if (strcmp(argv[1], tc->name) == 0)
  193. return (*tc->func)();
  194. }
  195. }
  196. /* No match found, or no test name provided, so display usage */
  197. printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
  198. "Normally executed via 'EmbeddingTests' in Lib/test/test_capi.py\n\n"
  199. "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
  200. for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
  201. printf(" %s\n", tc->name);
  202. }
  203. /* Non-zero exit code will cause test_capi.py tests to fail.
  204. This is intentional. */
  205. return -1;
  206. }