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