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.

140 lines
3.8 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 void 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. }
  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 void 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. }
  109. /* Different embedding tests */
  110. int main(int argc, char *argv[])
  111. {
  112. /* TODO: Check the argument string to allow for more test cases */
  113. if (argc > 1) {
  114. /* For now: assume "forced_io_encoding */
  115. test_forced_io_encoding();
  116. } else {
  117. /* Run the original embedding test case by default */
  118. test_repeated_init_and_subinterpreters();
  119. }
  120. return 0;
  121. }