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.

178 lines
5.2 KiB

  1. #include <Python.h>
  2. #include <inttypes.h>
  3. #include <stdio.h>
  4. /*********************************************************
  5. * Embedded interpreter tests that need a custom exe
  6. *
  7. * Executed via 'EmbeddingTests' in Lib/test/test_capi.py
  8. *********************************************************/
  9. static void _testembed_Py_Initialize(void)
  10. {
  11. /* HACK: the "./" at front avoids a search along the PATH in
  12. Modules/getpath.c */
  13. Py_SetProgramName(L"./_testembed");
  14. Py_Initialize();
  15. }
  16. /*****************************************************
  17. * Test repeated initialisation and subinterpreters
  18. *****************************************************/
  19. static void print_subinterp(void)
  20. {
  21. /* Output information about the interpreter in the format
  22. expected in Lib/test/test_capi.py (test_subinterps). */
  23. PyThreadState *ts = PyThreadState_Get();
  24. PyInterpreterState *interp = ts->interp;
  25. int64_t id = PyInterpreterState_GetID(interp);
  26. printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ",
  27. id, (uintptr_t)interp, (uintptr_t)ts);
  28. fflush(stdout);
  29. PyRun_SimpleString(
  30. "import sys;"
  31. "print('id(modules) =', id(sys.modules));"
  32. "sys.stdout.flush()"
  33. );
  34. }
  35. static int test_repeated_init_and_subinterpreters(void)
  36. {
  37. PyThreadState *mainstate, *substate;
  38. #ifdef WITH_THREAD
  39. PyGILState_STATE gilstate;
  40. #endif
  41. int i, j;
  42. for (i=0; i<15; i++) {
  43. printf("--- Pass %d ---\n", i);
  44. _testembed_Py_Initialize();
  45. mainstate = PyThreadState_Get();
  46. #ifdef WITH_THREAD
  47. PyEval_InitThreads();
  48. PyEval_ReleaseThread(mainstate);
  49. gilstate = PyGILState_Ensure();
  50. #endif
  51. print_subinterp();
  52. PyThreadState_Swap(NULL);
  53. for (j=0; j<3; j++) {
  54. substate = Py_NewInterpreter();
  55. print_subinterp();
  56. Py_EndInterpreter(substate);
  57. }
  58. PyThreadState_Swap(mainstate);
  59. print_subinterp();
  60. #ifdef WITH_THREAD
  61. PyGILState_Release(gilstate);
  62. #endif
  63. PyEval_RestoreThread(mainstate);
  64. Py_Finalize();
  65. }
  66. return 0;
  67. }
  68. /*****************************************************
  69. * Test forcing a particular IO encoding
  70. *****************************************************/
  71. static void check_stdio_details(const char *encoding, const char * errors)
  72. {
  73. /* Output info for the test case to check */
  74. if (encoding) {
  75. printf("Expected encoding: %s\n", encoding);
  76. } else {
  77. printf("Expected encoding: default\n");
  78. }
  79. if (errors) {
  80. printf("Expected errors: %s\n", errors);
  81. } else {
  82. printf("Expected errors: default\n");
  83. }
  84. fflush(stdout);
  85. /* Force the given IO encoding */
  86. Py_SetStandardStreamEncoding(encoding, errors);
  87. _testembed_Py_Initialize();
  88. PyRun_SimpleString(
  89. "import sys;"
  90. "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
  91. "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
  92. "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
  93. "sys.stdout.flush()"
  94. );
  95. Py_Finalize();
  96. }
  97. static int test_forced_io_encoding(void)
  98. {
  99. /* Check various combinations */
  100. printf("--- Use defaults ---\n");
  101. check_stdio_details(NULL, NULL);
  102. printf("--- Set errors only ---\n");
  103. check_stdio_details(NULL, "ignore");
  104. printf("--- Set encoding only ---\n");
  105. check_stdio_details("latin-1", NULL);
  106. printf("--- Set encoding and errors ---\n");
  107. check_stdio_details("latin-1", "replace");
  108. /* Check calling after initialization fails */
  109. Py_Initialize();
  110. if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) {
  111. printf("Unexpected success calling Py_SetStandardStreamEncoding");
  112. }
  113. Py_Finalize();
  114. return 0;
  115. }
  116. /* *********************************************************
  117. * List of test cases and the function that implements it.
  118. *
  119. * Names are compared case-sensitively with the first
  120. * argument. If no match is found, or no first argument was
  121. * provided, the names of all test cases are printed and
  122. * the exit code will be -1.
  123. *
  124. * The int returned from test functions is used as the exit
  125. * code, and test_capi treats all non-zero exit codes as a
  126. * failed test.
  127. *********************************************************/
  128. struct TestCase
  129. {
  130. const char *name;
  131. int (*func)(void);
  132. };
  133. static struct TestCase TestCases[] = {
  134. { "forced_io_encoding", test_forced_io_encoding },
  135. { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
  136. { NULL, NULL }
  137. };
  138. int main(int argc, char *argv[])
  139. {
  140. if (argc > 1) {
  141. for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
  142. if (strcmp(argv[1], tc->name) == 0)
  143. return (*tc->func)();
  144. }
  145. }
  146. /* No match found, or no test name provided, so display usage */
  147. printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
  148. "Normally executed via 'EmbeddingTests' in Lib/test/test_capi.py\n\n"
  149. "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
  150. for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
  151. printf(" %s\n", tc->name);
  152. }
  153. /* Non-zero exit code will cause test_capi.py tests to fail.
  154. This is intentional. */
  155. return -1;
  156. }