Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 1 | #include <Python.h> |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 2 | #include <inttypes.h> |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 5 | /********************************************************* |
| 6 | * Embedded interpreter tests that need a custom exe |
| 7 | * |
| 8 | * Executed via 'EmbeddingTests' in Lib/test/test_capi.py |
| 9 | *********************************************************/ |
| 10 | |
| 11 | static void _testembed_Py_Initialize(void) |
| 12 | { |
| 13 | /* HACK: the "./" at front avoids a search along the PATH in |
| 14 | Modules/getpath.c */ |
| 15 | Py_SetProgramName(L"./_testembed"); |
| 16 | Py_Initialize(); |
| 17 | } |
| 18 | |
| 19 | |
| 20 | /***************************************************** |
Martin Panter | 8f26565 | 2016-04-19 04:03:41 +0000 | [diff] [blame] | 21 | * Test repeated initialisation and subinterpreters |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 22 | *****************************************************/ |
| 23 | |
| 24 | static void print_subinterp(void) |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 25 | { |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 26 | /* Output information about the interpreter in the format |
| 27 | expected in Lib/test/test_capi.py (test_subinterps). */ |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 28 | PyThreadState *ts = PyThreadState_Get(); |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 29 | PyInterpreterState *interp = ts->interp; |
| 30 | int64_t id = PyInterpreterState_GetID(interp); |
Eric Snow | d1c3c13 | 2017-05-24 17:19:47 -0700 | [diff] [blame] | 31 | printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ", |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 32 | id, (uintptr_t)interp, (uintptr_t)ts); |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 33 | fflush(stdout); |
| 34 | PyRun_SimpleString( |
| 35 | "import sys;" |
| 36 | "print('id(modules) =', id(sys.modules));" |
| 37 | "sys.stdout.flush()" |
| 38 | ); |
| 39 | } |
| 40 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 41 | static int test_repeated_init_and_subinterpreters(void) |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 42 | { |
| 43 | PyThreadState *mainstate, *substate; |
| 44 | PyGILState_STATE gilstate; |
| 45 | int i, j; |
| 46 | |
Ned Deily | 939231b | 2016-08-16 00:17:42 -0400 | [diff] [blame] | 47 | for (i=0; i<15; i++) { |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 48 | printf("--- Pass %d ---\n", i); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 49 | _testembed_Py_Initialize(); |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 50 | mainstate = PyThreadState_Get(); |
| 51 | |
| 52 | PyEval_InitThreads(); |
| 53 | PyEval_ReleaseThread(mainstate); |
| 54 | |
| 55 | gilstate = PyGILState_Ensure(); |
| 56 | print_subinterp(); |
| 57 | PyThreadState_Swap(NULL); |
| 58 | |
| 59 | for (j=0; j<3; j++) { |
| 60 | substate = Py_NewInterpreter(); |
| 61 | print_subinterp(); |
| 62 | Py_EndInterpreter(substate); |
| 63 | } |
| 64 | |
| 65 | PyThreadState_Swap(mainstate); |
| 66 | print_subinterp(); |
| 67 | PyGILState_Release(gilstate); |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 68 | |
| 69 | PyEval_RestoreThread(mainstate); |
| 70 | Py_Finalize(); |
| 71 | } |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 72 | return 0; |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /***************************************************** |
| 76 | * Test forcing a particular IO encoding |
| 77 | *****************************************************/ |
| 78 | |
| 79 | static void check_stdio_details(const char *encoding, const char * errors) |
| 80 | { |
| 81 | /* Output info for the test case to check */ |
| 82 | if (encoding) { |
| 83 | printf("Expected encoding: %s\n", encoding); |
| 84 | } else { |
| 85 | printf("Expected encoding: default\n"); |
| 86 | } |
| 87 | if (errors) { |
| 88 | printf("Expected errors: %s\n", errors); |
| 89 | } else { |
| 90 | printf("Expected errors: default\n"); |
| 91 | } |
| 92 | fflush(stdout); |
| 93 | /* Force the given IO encoding */ |
| 94 | Py_SetStandardStreamEncoding(encoding, errors); |
| 95 | _testembed_Py_Initialize(); |
| 96 | PyRun_SimpleString( |
| 97 | "import sys;" |
| 98 | "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));" |
| 99 | "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));" |
| 100 | "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));" |
| 101 | "sys.stdout.flush()" |
| 102 | ); |
| 103 | Py_Finalize(); |
| 104 | } |
| 105 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 106 | static int test_forced_io_encoding(void) |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 107 | { |
| 108 | /* Check various combinations */ |
| 109 | printf("--- Use defaults ---\n"); |
| 110 | check_stdio_details(NULL, NULL); |
| 111 | printf("--- Set errors only ---\n"); |
Victor Stinner | b2bef62 | 2014-03-18 02:38:12 +0100 | [diff] [blame] | 112 | check_stdio_details(NULL, "ignore"); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 113 | printf("--- Set encoding only ---\n"); |
| 114 | check_stdio_details("latin-1", NULL); |
| 115 | printf("--- Set encoding and errors ---\n"); |
Victor Stinner | b2bef62 | 2014-03-18 02:38:12 +0100 | [diff] [blame] | 116 | check_stdio_details("latin-1", "replace"); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 117 | |
| 118 | /* Check calling after initialization fails */ |
| 119 | Py_Initialize(); |
| 120 | |
| 121 | if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) { |
| 122 | printf("Unexpected success calling Py_SetStandardStreamEncoding"); |
| 123 | } |
| 124 | Py_Finalize(); |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 125 | return 0; |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 126 | } |
| 127 | |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame^] | 128 | |
| 129 | /********************************************************* |
| 130 | * Test parts of the C-API that work before initialization |
| 131 | *********************************************************/ |
| 132 | |
| 133 | static int test_pre_initialization_api(void) |
| 134 | { |
| 135 | wchar_t *program = Py_DecodeLocale("spam", NULL); |
| 136 | if (program == NULL) { |
| 137 | fprintf(stderr, "Fatal error: cannot decode program name\n"); |
| 138 | return 1; |
| 139 | } |
| 140 | Py_SetProgramName(program); |
| 141 | |
| 142 | Py_Initialize(); |
| 143 | Py_Finalize(); |
| 144 | |
| 145 | PyMem_RawFree(program); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 150 | /* ********************************************************* |
| 151 | * List of test cases and the function that implements it. |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 152 | * |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 153 | * Names are compared case-sensitively with the first |
| 154 | * argument. If no match is found, or no first argument was |
| 155 | * provided, the names of all test cases are printed and |
| 156 | * the exit code will be -1. |
| 157 | * |
| 158 | * The int returned from test functions is used as the exit |
| 159 | * code, and test_capi treats all non-zero exit codes as a |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 160 | * failed test. |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 161 | *********************************************************/ |
| 162 | struct TestCase |
| 163 | { |
| 164 | const char *name; |
| 165 | int (*func)(void); |
| 166 | }; |
| 167 | |
| 168 | static struct TestCase TestCases[] = { |
| 169 | { "forced_io_encoding", test_forced_io_encoding }, |
| 170 | { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters }, |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame^] | 171 | { "pre_initialization_api", test_pre_initialization_api }, |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 172 | { NULL, NULL } |
| 173 | }; |
| 174 | |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 175 | int main(int argc, char *argv[]) |
| 176 | { |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 177 | if (argc > 1) { |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 178 | for (struct TestCase *tc = TestCases; tc && tc->name; tc++) { |
| 179 | if (strcmp(argv[1], tc->name) == 0) |
| 180 | return (*tc->func)(); |
| 181 | } |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 182 | } |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 183 | |
| 184 | /* No match found, or no test name provided, so display usage */ |
| 185 | printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n" |
| 186 | "Normally executed via 'EmbeddingTests' in Lib/test/test_capi.py\n\n" |
| 187 | "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]); |
| 188 | for (struct TestCase *tc = TestCases; tc && tc->name; tc++) { |
| 189 | printf(" %s\n", tc->name); |
| 190 | } |
| 191 | |
| 192 | /* Non-zero exit code will cause test_capi.py tests to fail. |
| 193 | This is intentional. */ |
| 194 | return -1; |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 195 | } |