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.

173 lines
5.0 KiB

  1. #include <Python.h>
  2. #include <stdio.h>
  3. /*********************************************************
  4. * Embedded interpreter tests that need a custom exe
  5. *
  6. * Executed via 'EmbeddingTests' in Lib/test/test_capi.py
  7. *********************************************************/
  8. static void _testembed_Py_Initialize(void)
  9. {
  10. /* HACK: the "./" at front avoids a search along the PATH in
  11. Modules/getpath.c */
  12. Py_SetProgramName(L"./_testembed");
  13. Py_Initialize();
  14. }
  15. /*****************************************************
  16. * Test repeated initialisation and subinterpreters
  17. *****************************************************/
  18. static void print_subinterp(void)
  19. {
  20. /* Just output some debug stuff */
  21. PyThreadState *ts = PyThreadState_Get();
  22. printf("interp %p, thread state %p: ", ts->interp, ts);
  23. fflush(stdout);
  24. PyRun_SimpleString(
  25. "import sys;"
  26. "print('id(modules) =', id(sys.modules));"
  27. "sys.stdout.flush()"
  28. );
  29. }
  30. static int test_repeated_init_and_subinterpreters(void)
  31. {
  32. PyThreadState *mainstate, *substate;
  33. #ifdef WITH_THREAD
  34. PyGILState_STATE gilstate;
  35. #endif
  36. int i, j;
  37. for (i=0; i<15; i++) {
  38. printf("--- Pass %d ---\n", i);
  39. _testembed_Py_Initialize();
  40. mainstate = PyThreadState_Get();
  41. #ifdef WITH_THREAD
  42. PyEval_InitThreads();
  43. PyEval_ReleaseThread(mainstate);
  44. gilstate = PyGILState_Ensure();
  45. #endif
  46. print_subinterp();
  47. PyThreadState_Swap(NULL);
  48. for (j=0; j<3; j++) {
  49. substate = Py_NewInterpreter();
  50. print_subinterp();
  51. Py_EndInterpreter(substate);
  52. }
  53. PyThreadState_Swap(mainstate);
  54. print_subinterp();
  55. #ifdef WITH_THREAD
  56. PyGILState_Release(gilstate);
  57. #endif
  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. * List of test cases and the function that implements it.
  113. *
  114. * Names are compared case-sensitively with the first
  115. * argument. If no match is found, or no first argument was
  116. * provided, the names of all test cases are printed and
  117. * the exit code will be -1.
  118. *
  119. * The int returned from test functions is used as the exit
  120. * code, and test_capi treats all non-zero exit codes as a
  121. * failed test.
  122. *********************************************************/
  123. struct TestCase
  124. {
  125. const char *name;
  126. int (*func)(void);
  127. };
  128. static struct TestCase TestCases[] = {
  129. { "forced_io_encoding", test_forced_io_encoding },
  130. { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
  131. { NULL, NULL }
  132. };
  133. int main(int argc, char *argv[])
  134. {
  135. if (argc > 1) {
  136. for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
  137. if (strcmp(argv[1], tc->name) == 0)
  138. return (*tc->func)();
  139. }
  140. }
  141. /* No match found, or no test name provided, so display usage */
  142. printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
  143. "Normally executed via 'EmbeddingTests' in Lib/test/test_capi.py\n\n"
  144. "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
  145. for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
  146. printf(" %s\n", tc->name);
  147. }
  148. /* Non-zero exit code will cause test_capi.py tests to fail.
  149. This is intentional. */
  150. return -1;
  151. }