Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 1 | #include <Python.h> |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 2 | #include "pythread.h" |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 3 | #include <inttypes.h> |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 4 | #include <stdio.h> |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame^] | 5 | #include <wchar.h> |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 6 | |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 7 | /********************************************************* |
| 8 | * Embedded interpreter tests that need a custom exe |
| 9 | * |
| 10 | * Executed via 'EmbeddingTests' in Lib/test/test_capi.py |
| 11 | *********************************************************/ |
| 12 | |
| 13 | static void _testembed_Py_Initialize(void) |
| 14 | { |
| 15 | /* HACK: the "./" at front avoids a search along the PATH in |
| 16 | Modules/getpath.c */ |
| 17 | Py_SetProgramName(L"./_testembed"); |
| 18 | Py_Initialize(); |
| 19 | } |
| 20 | |
| 21 | |
| 22 | /***************************************************** |
Martin Panter | 8f26565 | 2016-04-19 04:03:41 +0000 | [diff] [blame] | 23 | * Test repeated initialisation and subinterpreters |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 24 | *****************************************************/ |
| 25 | |
| 26 | static void print_subinterp(void) |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 27 | { |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 28 | /* Output information about the interpreter in the format |
| 29 | expected in Lib/test/test_capi.py (test_subinterps). */ |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 30 | PyThreadState *ts = PyThreadState_Get(); |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 31 | PyInterpreterState *interp = ts->interp; |
| 32 | int64_t id = PyInterpreterState_GetID(interp); |
Eric Snow | d1c3c13 | 2017-05-24 17:19:47 -0700 | [diff] [blame] | 33 | printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ", |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 34 | id, (uintptr_t)interp, (uintptr_t)ts); |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 35 | fflush(stdout); |
| 36 | PyRun_SimpleString( |
| 37 | "import sys;" |
| 38 | "print('id(modules) =', id(sys.modules));" |
| 39 | "sys.stdout.flush()" |
| 40 | ); |
| 41 | } |
| 42 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 43 | static int test_repeated_init_and_subinterpreters(void) |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 44 | { |
| 45 | PyThreadState *mainstate, *substate; |
| 46 | PyGILState_STATE gilstate; |
| 47 | int i, j; |
| 48 | |
Ned Deily | 939231b | 2016-08-16 00:17:42 -0400 | [diff] [blame] | 49 | for (i=0; i<15; i++) { |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 50 | printf("--- Pass %d ---\n", i); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 51 | _testembed_Py_Initialize(); |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 52 | mainstate = PyThreadState_Get(); |
| 53 | |
| 54 | PyEval_InitThreads(); |
| 55 | PyEval_ReleaseThread(mainstate); |
| 56 | |
| 57 | gilstate = PyGILState_Ensure(); |
| 58 | print_subinterp(); |
| 59 | PyThreadState_Swap(NULL); |
| 60 | |
| 61 | for (j=0; j<3; j++) { |
| 62 | substate = Py_NewInterpreter(); |
| 63 | print_subinterp(); |
| 64 | Py_EndInterpreter(substate); |
| 65 | } |
| 66 | |
| 67 | PyThreadState_Swap(mainstate); |
| 68 | print_subinterp(); |
| 69 | PyGILState_Release(gilstate); |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 70 | |
| 71 | PyEval_RestoreThread(mainstate); |
| 72 | Py_Finalize(); |
| 73 | } |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 74 | return 0; |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /***************************************************** |
| 78 | * Test forcing a particular IO encoding |
| 79 | *****************************************************/ |
| 80 | |
| 81 | static void check_stdio_details(const char *encoding, const char * errors) |
| 82 | { |
| 83 | /* Output info for the test case to check */ |
| 84 | if (encoding) { |
| 85 | printf("Expected encoding: %s\n", encoding); |
| 86 | } else { |
| 87 | printf("Expected encoding: default\n"); |
| 88 | } |
| 89 | if (errors) { |
| 90 | printf("Expected errors: %s\n", errors); |
| 91 | } else { |
| 92 | printf("Expected errors: default\n"); |
| 93 | } |
| 94 | fflush(stdout); |
| 95 | /* Force the given IO encoding */ |
| 96 | Py_SetStandardStreamEncoding(encoding, errors); |
| 97 | _testembed_Py_Initialize(); |
| 98 | PyRun_SimpleString( |
| 99 | "import sys;" |
| 100 | "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));" |
| 101 | "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));" |
| 102 | "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));" |
| 103 | "sys.stdout.flush()" |
| 104 | ); |
| 105 | Py_Finalize(); |
| 106 | } |
| 107 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 108 | static int test_forced_io_encoding(void) |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 109 | { |
| 110 | /* Check various combinations */ |
| 111 | printf("--- Use defaults ---\n"); |
| 112 | check_stdio_details(NULL, NULL); |
| 113 | printf("--- Set errors only ---\n"); |
Victor Stinner | b2bef62 | 2014-03-18 02:38:12 +0100 | [diff] [blame] | 114 | check_stdio_details(NULL, "ignore"); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 115 | printf("--- Set encoding only ---\n"); |
| 116 | check_stdio_details("latin-1", NULL); |
| 117 | printf("--- Set encoding and errors ---\n"); |
Victor Stinner | b2bef62 | 2014-03-18 02:38:12 +0100 | [diff] [blame] | 118 | check_stdio_details("latin-1", "replace"); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 119 | |
| 120 | /* Check calling after initialization fails */ |
| 121 | Py_Initialize(); |
| 122 | |
| 123 | if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) { |
| 124 | printf("Unexpected success calling Py_SetStandardStreamEncoding"); |
| 125 | } |
| 126 | Py_Finalize(); |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 127 | return 0; |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 128 | } |
| 129 | |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 130 | /********************************************************* |
| 131 | * Test parts of the C-API that work before initialization |
| 132 | *********************************************************/ |
| 133 | |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame^] | 134 | /* The pre-initialization tests tend to break by segfaulting, so explicitly |
| 135 | * flushed progress messages make the broken API easier to find when they fail. |
| 136 | */ |
| 137 | #define _Py_EMBED_PREINIT_CHECK(msg) \ |
| 138 | do {printf(msg); fflush(stdout);} while (0); |
| 139 | |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 140 | static int test_pre_initialization_api(void) |
| 141 | { |
Nick Coghlan | 4274609 | 2017-11-26 14:19:13 +1000 | [diff] [blame] | 142 | /* Leading "./" ensures getpath.c can still find the standard library */ |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame^] | 143 | _Py_EMBED_PREINIT_CHECK("Checking Py_DecodeLocale\n"); |
Nick Coghlan | 4274609 | 2017-11-26 14:19:13 +1000 | [diff] [blame] | 144 | wchar_t *program = Py_DecodeLocale("./spam", NULL); |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 145 | if (program == NULL) { |
| 146 | fprintf(stderr, "Fatal error: cannot decode program name\n"); |
| 147 | return 1; |
| 148 | } |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame^] | 149 | _Py_EMBED_PREINIT_CHECK("Checking Py_SetProgramName\n"); |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 150 | Py_SetProgramName(program); |
| 151 | |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame^] | 152 | _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n"); |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 153 | Py_Initialize(); |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame^] | 154 | _Py_EMBED_PREINIT_CHECK("Check sys module contents\n"); |
| 155 | PyRun_SimpleString("import sys; " |
| 156 | "print('sys.executable:', sys.executable)"); |
| 157 | _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n"); |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 158 | Py_Finalize(); |
| 159 | |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame^] | 160 | _Py_EMBED_PREINIT_CHECK("Freeing memory allocated by Py_DecodeLocale\n"); |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 161 | PyMem_RawFree(program); |
| 162 | return 0; |
| 163 | } |
| 164 | |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame^] | 165 | |
| 166 | /* bpo-33042: Ensure embedding apps can predefine sys module options */ |
| 167 | static int test_pre_initialization_sys_options(void) |
| 168 | { |
| 169 | /* We allocate a couple of the option dynamically, and then delete |
| 170 | * them before calling Py_Initialize. This ensures the interpreter isn't |
| 171 | * relying on the caller to keep the passed in strings alive. |
| 172 | */ |
| 173 | wchar_t *static_warnoption = L"once"; |
| 174 | wchar_t *static_xoption = L"also_not_an_option=2"; |
| 175 | size_t warnoption_len = wcslen(static_warnoption); |
| 176 | size_t xoption_len = wcslen(static_xoption); |
| 177 | wchar_t *dynamic_once_warnoption = calloc(warnoption_len+1, sizeof(wchar_t)); |
| 178 | wchar_t *dynamic_xoption = calloc(xoption_len+1, sizeof(wchar_t)); |
| 179 | wcsncpy(dynamic_once_warnoption, static_warnoption, warnoption_len+1); |
| 180 | wcsncpy(dynamic_xoption, static_xoption, xoption_len+1); |
| 181 | |
| 182 | _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption\n"); |
| 183 | PySys_AddWarnOption(L"default"); |
| 184 | _Py_EMBED_PREINIT_CHECK("Checking PySys_ResetWarnOptions\n"); |
| 185 | PySys_ResetWarnOptions(); |
| 186 | _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption linked list\n"); |
| 187 | PySys_AddWarnOption(dynamic_once_warnoption); |
| 188 | PySys_AddWarnOption(L"module"); |
| 189 | PySys_AddWarnOption(L"default"); |
| 190 | _Py_EMBED_PREINIT_CHECK("Checking PySys_AddXOption\n"); |
| 191 | PySys_AddXOption(L"not_an_option=1"); |
| 192 | PySys_AddXOption(dynamic_xoption); |
| 193 | |
| 194 | /* Delete the dynamic options early */ |
| 195 | free(dynamic_once_warnoption); |
| 196 | dynamic_once_warnoption = NULL; |
| 197 | free(dynamic_xoption); |
| 198 | dynamic_xoption = NULL; |
| 199 | |
| 200 | _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n"); |
| 201 | _testembed_Py_Initialize(); |
| 202 | _Py_EMBED_PREINIT_CHECK("Check sys module contents\n"); |
| 203 | PyRun_SimpleString("import sys; " |
| 204 | "print('sys.warnoptions:', sys.warnoptions); " |
| 205 | "print('sys._xoptions:', sys._xoptions); " |
| 206 | "warnings = sys.modules['warnings']; " |
| 207 | "latest_filters = [f[0] for f in warnings.filters[:3]]; " |
| 208 | "print('warnings.filters[:3]:', latest_filters)"); |
| 209 | _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n"); |
| 210 | Py_Finalize(); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /* bpo-20891: Avoid race condition when initialising the GIL */ |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 217 | static void bpo20891_thread(void *lockp) |
| 218 | { |
| 219 | PyThread_type_lock lock = *((PyThread_type_lock*)lockp); |
| 220 | |
| 221 | PyGILState_STATE state = PyGILState_Ensure(); |
| 222 | if (!PyGILState_Check()) { |
| 223 | fprintf(stderr, "PyGILState_Check failed!"); |
| 224 | abort(); |
| 225 | } |
| 226 | |
| 227 | PyGILState_Release(state); |
| 228 | |
| 229 | PyThread_release_lock(lock); |
| 230 | |
| 231 | PyThread_exit_thread(); |
| 232 | } |
| 233 | |
| 234 | static int test_bpo20891(void) |
| 235 | { |
| 236 | /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before |
| 237 | calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must |
| 238 | call PyEval_InitThreads() for us in this case. */ |
| 239 | PyThread_type_lock lock = PyThread_allocate_lock(); |
| 240 | if (!lock) { |
| 241 | fprintf(stderr, "PyThread_allocate_lock failed!"); |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | _testembed_Py_Initialize(); |
| 246 | |
| 247 | unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock); |
| 248 | if (thrd == PYTHREAD_INVALID_THREAD_ID) { |
| 249 | fprintf(stderr, "PyThread_start_new_thread failed!"); |
| 250 | return 1; |
| 251 | } |
| 252 | PyThread_acquire_lock(lock, WAIT_LOCK); |
| 253 | |
| 254 | Py_BEGIN_ALLOW_THREADS |
| 255 | /* wait until the thread exit */ |
| 256 | PyThread_acquire_lock(lock, WAIT_LOCK); |
| 257 | Py_END_ALLOW_THREADS |
| 258 | |
| 259 | PyThread_free_lock(lock); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 264 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 265 | /* ********************************************************* |
| 266 | * List of test cases and the function that implements it. |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 267 | * |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 268 | * Names are compared case-sensitively with the first |
| 269 | * argument. If no match is found, or no first argument was |
| 270 | * provided, the names of all test cases are printed and |
| 271 | * the exit code will be -1. |
| 272 | * |
| 273 | * The int returned from test functions is used as the exit |
| 274 | * code, and test_capi treats all non-zero exit codes as a |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 275 | * failed test. |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 276 | *********************************************************/ |
| 277 | struct TestCase |
| 278 | { |
| 279 | const char *name; |
| 280 | int (*func)(void); |
| 281 | }; |
| 282 | |
| 283 | static struct TestCase TestCases[] = { |
| 284 | { "forced_io_encoding", test_forced_io_encoding }, |
| 285 | { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters }, |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 286 | { "pre_initialization_api", test_pre_initialization_api }, |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame^] | 287 | { "pre_initialization_sys_options", test_pre_initialization_sys_options }, |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 288 | { "bpo20891", test_bpo20891 }, |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 289 | { NULL, NULL } |
| 290 | }; |
| 291 | |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 292 | int main(int argc, char *argv[]) |
| 293 | { |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 294 | if (argc > 1) { |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 295 | for (struct TestCase *tc = TestCases; tc && tc->name; tc++) { |
| 296 | if (strcmp(argv[1], tc->name) == 0) |
| 297 | return (*tc->func)(); |
| 298 | } |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 299 | } |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 300 | |
| 301 | /* No match found, or no test name provided, so display usage */ |
| 302 | printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n" |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame^] | 303 | "Normally executed via 'EmbeddingTests' in Lib/test/test_embed.py\n\n" |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 304 | "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]); |
| 305 | for (struct TestCase *tc = TestCases; tc && tc->name; tc++) { |
| 306 | printf(" %s\n", tc->name); |
| 307 | } |
| 308 | |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame^] | 309 | /* Non-zero exit code will cause test_embed.py tests to fail. |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 310 | This is intentional. */ |
| 311 | return -1; |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 312 | } |