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.

172 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. PyGILState_STATE gilstate;
  39. int i, j;
  40. for (i=0; i<15; i++) {
  41. printf("--- Pass %d ---\n", i);
  42. _testembed_Py_Initialize();
  43. mainstate = PyThreadState_Get();
  44. PyEval_InitThreads();
  45. PyEval_ReleaseThread(mainstate);
  46. gilstate = PyGILState_Ensure();
  47. print_subinterp();
  48. PyThreadState_Swap(NULL);
  49. for (j=0; j<3; j++) {
  50. substate = Py_NewInterpreter();
  51. print_subinterp();
  52. Py_EndInterpreter(substate);
  53. }
  54. PyThreadState_Swap(mainstate);
  55. print_subinterp();
  56. PyGILState_Release(gilstate);
  57. PyEval_RestoreThread(mainstate);
  58. Py_Finalize();
  59. }
  60. return 0;
  61. }
  62. /*****************************************************
  63. * Test forcing a particular IO encoding
  64. *****************************************************/
  65. static void check_stdio_details(const char *encoding, const char * errors)
  66. {
  67. /* Output info for the test case to check */
  68. if (encoding) {
  69. printf("Expected encoding: %s\n", encoding);
  70. } else {
  71. printf("Expected encoding: default\n");
  72. }
  73. if (errors) {
  74. printf("Expected errors: %s\n", errors);
  75. } else {
  76. printf("Expected errors: default\n");
  77. }
  78. fflush(stdout);
  79. /* Force the given IO encoding */
  80. Py_SetStandardStreamEncoding(encoding, errors);
  81. _testembed_Py_Initialize();
  82. PyRun_SimpleString(
  83. "import sys;"
  84. "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
  85. "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
  86. "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
  87. "sys.stdout.flush()"
  88. );
  89. Py_Finalize();
  90. }
  91. static int test_forced_io_encoding(void)
  92. {
  93. /* Check various combinations */
  94. printf("--- Use defaults ---\n");
  95. check_stdio_details(NULL, NULL);
  96. printf("--- Set errors only ---\n");
  97. check_stdio_details(NULL, "ignore");
  98. printf("--- Set encoding only ---\n");
  99. check_stdio_details("latin-1", NULL);
  100. printf("--- Set encoding and errors ---\n");
  101. check_stdio_details("latin-1", "replace");
  102. /* Check calling after initialization fails */
  103. Py_Initialize();
  104. if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) {
  105. printf("Unexpected success calling Py_SetStandardStreamEncoding");
  106. }
  107. Py_Finalize();
  108. return 0;
  109. }
  110. /* *********************************************************
  111. * List of test cases and the function that implements it.
  112. *
  113. * Names are compared case-sensitively with the first
  114. * argument. If no match is found, or no first argument was
  115. * provided, the names of all test cases are printed and
  116. * the exit code will be -1.
  117. *
  118. * The int returned from test functions is used as the exit
  119. * code, and test_capi treats all non-zero exit codes as a
  120. * failed test.
  121. *********************************************************/
  122. struct TestCase
  123. {
  124. const char *name;
  125. int (*func)(void);
  126. };
  127. static struct TestCase TestCases[] = {
  128. { "forced_io_encoding", test_forced_io_encoding },
  129. { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
  130. { NULL, NULL }
  131. };
  132. int main(int argc, char *argv[])
  133. {
  134. if (argc > 1) {
  135. for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
  136. if (strcmp(argv[1], tc->name) == 0)
  137. return (*tc->func)();
  138. }
  139. }
  140. /* No match found, or no test name provided, so display usage */
  141. printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
  142. "Normally executed via 'EmbeddingTests' in Lib/test/test_capi.py\n\n"
  143. "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
  144. for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
  145. printf(" %s\n", tc->name);
  146. }
  147. /* Non-zero exit code will cause test_capi.py tests to fail.
  148. This is intentional. */
  149. return -1;
  150. }