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> |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [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 | |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [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 */ |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [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 | } |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [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 | |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [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(); |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [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 | |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [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 | |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 165 | |
| 166 | /* bpo-33042: Ensure embedding apps can predefine sys module options */ |
| 167 | static int test_pre_initialization_sys_options(void) |
| 168 | { |
Nick Coghlan | 69f5c73 | 2018-03-30 15:36:42 +1000 | [diff] [blame] | 169 | /* We allocate a couple of the options dynamically, and then delete |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 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 | */ |
Nick Coghlan | 69f5c73 | 2018-03-30 15:36:42 +1000 | [diff] [blame] | 173 | const wchar_t *static_warnoption = L"once"; |
| 174 | const wchar_t *static_xoption = L"also_not_an_option=2"; |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 175 | size_t warnoption_len = wcslen(static_warnoption); |
| 176 | size_t xoption_len = wcslen(static_xoption); |
Nick Coghlan | 69f5c73 | 2018-03-30 15:36:42 +1000 | [diff] [blame] | 177 | wchar_t *dynamic_once_warnoption = \ |
| 178 | (wchar_t *) calloc(warnoption_len+1, sizeof(wchar_t)); |
| 179 | wchar_t *dynamic_xoption = \ |
| 180 | (wchar_t *) calloc(xoption_len+1, sizeof(wchar_t)); |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 181 | wcsncpy(dynamic_once_warnoption, static_warnoption, warnoption_len+1); |
| 182 | wcsncpy(dynamic_xoption, static_xoption, xoption_len+1); |
| 183 | |
| 184 | _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption\n"); |
| 185 | PySys_AddWarnOption(L"default"); |
| 186 | _Py_EMBED_PREINIT_CHECK("Checking PySys_ResetWarnOptions\n"); |
| 187 | PySys_ResetWarnOptions(); |
| 188 | _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption linked list\n"); |
| 189 | PySys_AddWarnOption(dynamic_once_warnoption); |
| 190 | PySys_AddWarnOption(L"module"); |
| 191 | PySys_AddWarnOption(L"default"); |
| 192 | _Py_EMBED_PREINIT_CHECK("Checking PySys_AddXOption\n"); |
| 193 | PySys_AddXOption(L"not_an_option=1"); |
| 194 | PySys_AddXOption(dynamic_xoption); |
| 195 | |
| 196 | /* Delete the dynamic options early */ |
| 197 | free(dynamic_once_warnoption); |
| 198 | dynamic_once_warnoption = NULL; |
| 199 | free(dynamic_xoption); |
| 200 | dynamic_xoption = NULL; |
| 201 | |
| 202 | _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n"); |
| 203 | _testembed_Py_Initialize(); |
| 204 | _Py_EMBED_PREINIT_CHECK("Check sys module contents\n"); |
| 205 | PyRun_SimpleString("import sys; " |
| 206 | "print('sys.warnoptions:', sys.warnoptions); " |
| 207 | "print('sys._xoptions:', sys._xoptions); " |
| 208 | "warnings = sys.modules['warnings']; " |
| 209 | "latest_filters = [f[0] for f in warnings.filters[:3]]; " |
| 210 | "print('warnings.filters[:3]:', latest_filters)"); |
| 211 | _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n"); |
| 212 | Py_Finalize(); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /* bpo-20891: Avoid race condition when initialising the GIL */ |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 219 | static void bpo20891_thread(void *lockp) |
| 220 | { |
| 221 | PyThread_type_lock lock = *((PyThread_type_lock*)lockp); |
| 222 | |
| 223 | PyGILState_STATE state = PyGILState_Ensure(); |
| 224 | if (!PyGILState_Check()) { |
| 225 | fprintf(stderr, "PyGILState_Check failed!"); |
| 226 | abort(); |
| 227 | } |
| 228 | |
| 229 | PyGILState_Release(state); |
| 230 | |
| 231 | PyThread_release_lock(lock); |
| 232 | |
| 233 | PyThread_exit_thread(); |
| 234 | } |
| 235 | |
| 236 | static int test_bpo20891(void) |
| 237 | { |
| 238 | /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before |
| 239 | calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must |
| 240 | call PyEval_InitThreads() for us in this case. */ |
| 241 | PyThread_type_lock lock = PyThread_allocate_lock(); |
| 242 | if (!lock) { |
| 243 | fprintf(stderr, "PyThread_allocate_lock failed!"); |
| 244 | return 1; |
| 245 | } |
| 246 | |
| 247 | _testembed_Py_Initialize(); |
| 248 | |
| 249 | unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock); |
| 250 | if (thrd == PYTHREAD_INVALID_THREAD_ID) { |
| 251 | fprintf(stderr, "PyThread_start_new_thread failed!"); |
| 252 | return 1; |
| 253 | } |
| 254 | PyThread_acquire_lock(lock, WAIT_LOCK); |
| 255 | |
| 256 | Py_BEGIN_ALLOW_THREADS |
| 257 | /* wait until the thread exit */ |
| 258 | PyThread_acquire_lock(lock, WAIT_LOCK); |
| 259 | Py_END_ALLOW_THREADS |
| 260 | |
| 261 | PyThread_free_lock(lock); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Victor Stinner | 209abf7 | 2018-06-22 19:14:51 +0200 | [diff] [blame] | 266 | static int test_initialize_twice(void) |
| 267 | { |
| 268 | _testembed_Py_Initialize(); |
| 269 | |
| 270 | /* bpo-33932: Calling Py_Initialize() twice should do nothing |
| 271 | * (and not crash!). */ |
| 272 | Py_Initialize(); |
| 273 | |
| 274 | Py_Finalize(); |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 279 | static int test_initialize_pymain(void) |
| 280 | { |
| 281 | wchar_t *argv[] = {L"PYTHON", L"-c", |
| 282 | L"import sys; print(f'Py_Main() after Py_Initialize: sys.argv={sys.argv}')", |
| 283 | L"arg2"}; |
| 284 | _testembed_Py_Initialize(); |
| 285 | |
| 286 | /* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */ |
| 287 | Py_Main(Py_ARRAY_LENGTH(argv), argv); |
| 288 | |
| 289 | Py_Finalize(); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 294 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 295 | static void |
| 296 | dump_config(void) |
| 297 | { |
| 298 | #define ASSERT_EQUAL(a, b) \ |
| 299 | if ((a) != (b)) { \ |
| 300 | printf("ERROR: %s != %s (%i != %i)\n", #a, #b, (a), (b)); \ |
| 301 | exit(1); \ |
| 302 | } |
| 303 | #define ASSERT_STR_EQUAL(a, b) \ |
| 304 | if ((a) == NULL || (b == NULL) || wcscmp((a), (b)) != 0) { \ |
| 305 | printf("ERROR: %s != %s ('%ls' != '%ls')\n", #a, #b, (a), (b)); \ |
| 306 | exit(1); \ |
| 307 | } |
| 308 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 309 | PyInterpreterState *interp = _PyInterpreterState_Get(); |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 310 | _PyCoreConfig *config = &interp->core_config; |
| 311 | |
| 312 | printf("install_signal_handlers = %i\n", config->install_signal_handlers); |
| 313 | |
| 314 | printf("use_environment = %i\n", config->use_environment); |
| 315 | ASSERT_EQUAL(config->use_environment, !Py_IgnoreEnvironmentFlag); |
| 316 | |
| 317 | printf("use_hash_seed = %i\n", config->use_hash_seed); |
| 318 | printf("hash_seed = %lu\n", config->hash_seed); |
| 319 | |
| 320 | printf("allocator = %s\n", config->allocator); |
| 321 | |
| 322 | printf("dev_mode = %i\n", config->dev_mode); |
| 323 | printf("faulthandler = %i\n", config->faulthandler); |
| 324 | printf("tracemalloc = %i\n", config->tracemalloc); |
| 325 | printf("import_time = %i\n", config->import_time); |
| 326 | printf("show_ref_count = %i\n", config->show_ref_count); |
| 327 | printf("show_alloc_count = %i\n", config->show_alloc_count); |
| 328 | printf("dump_refs = %i\n", config->dump_refs); |
| 329 | printf("malloc_stats = %i\n", config->malloc_stats); |
| 330 | |
| 331 | printf("coerce_c_locale = %i\n", config->coerce_c_locale); |
| 332 | printf("coerce_c_locale_warn = %i\n", config->coerce_c_locale_warn); |
| 333 | printf("utf8_mode = %i\n", config->utf8_mode); |
| 334 | |
| 335 | printf("pycache_prefix = %ls\n", config->pycache_prefix); |
| 336 | printf("program_name = %ls\n", config->program_name); |
| 337 | ASSERT_STR_EQUAL(config->program_name, Py_GetProgramName()); |
Victor Stinner | ea68d83 | 2018-08-01 03:07:18 +0200 | [diff] [blame] | 338 | |
| 339 | printf("argc = %i\n", config->argc); |
| 340 | printf("argv = ["); |
| 341 | for (int i=0; i < config->argc; i++) { |
| 342 | if (i) { |
| 343 | printf(", "); |
| 344 | } |
| 345 | printf("\"%ls\"", config->argv[i]); |
| 346 | } |
| 347 | printf("]\n"); |
| 348 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 349 | printf("program = %ls\n", config->program); |
| 350 | /* FIXME: test xoptions */ |
| 351 | /* FIXME: test warnoptions */ |
| 352 | /* FIXME: test module_search_path_env */ |
| 353 | /* FIXME: test home */ |
| 354 | /* FIXME: test module_search_paths */ |
| 355 | /* FIXME: test executable */ |
| 356 | /* FIXME: test prefix */ |
| 357 | /* FIXME: test base_prefix */ |
| 358 | /* FIXME: test exec_prefix */ |
| 359 | /* FIXME: test base_exec_prefix */ |
| 360 | /* FIXME: test dll_path */ |
| 361 | |
| 362 | printf("isolated = %i\n", config->isolated); |
| 363 | ASSERT_EQUAL(config->isolated, Py_IsolatedFlag); |
| 364 | printf("site_import = %i\n", config->site_import); |
| 365 | printf("bytes_warning = %i\n", config->bytes_warning); |
| 366 | printf("inspect = %i\n", config->inspect); |
| 367 | printf("interactive = %i\n", config->interactive); |
| 368 | printf("optimization_level = %i\n", config->optimization_level); |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 369 | printf("parser_debug = %i\n", config->parser_debug); |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 370 | printf("write_bytecode = %i\n", config->write_bytecode); |
| 371 | printf("verbose = %i\n", config->verbose); |
| 372 | ASSERT_EQUAL(config->verbose, Py_VerboseFlag); |
| 373 | printf("quiet = %i\n", config->quiet); |
| 374 | printf("user_site_directory = %i\n", config->user_site_directory); |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 375 | printf("buffered_stdio = %i\n", config->buffered_stdio); |
Victor Stinner | 5a953fd | 2018-08-03 22:49:07 +0200 | [diff] [blame^] | 376 | ASSERT_EQUAL(config->buffered_stdio, !Py_UnbufferedStdioFlag); |
| 377 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 378 | /* FIXME: test legacy_windows_fs_encoding */ |
| 379 | /* FIXME: test legacy_windows_stdio */ |
| 380 | |
| 381 | printf("_install_importlib = %i\n", config->_install_importlib); |
| 382 | printf("_check_hash_pycs_mode = %s\n", config->_check_hash_pycs_mode); |
Victor Stinner | b75d7e2 | 2018-08-01 02:13:04 +0200 | [diff] [blame] | 383 | printf("_frozen = %i\n", config->_frozen); |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 384 | |
| 385 | #undef ASSERT_EQUAL |
| 386 | #undef ASSERT_STR_EQUAL |
| 387 | } |
| 388 | |
| 389 | |
| 390 | static int test_init_default_config(void) |
| 391 | { |
| 392 | _testembed_Py_Initialize(); |
| 393 | dump_config(); |
| 394 | Py_Finalize(); |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | static int test_init_global_config(void) |
| 400 | { |
| 401 | /* FIXME: test Py_IgnoreEnvironmentFlag */ |
| 402 | |
| 403 | putenv("PYTHONUTF8=0"); |
| 404 | Py_UTF8Mode = 1; |
| 405 | |
| 406 | /* Test initialization from global configuration variables (Py_xxx) */ |
| 407 | Py_SetProgramName(L"./globalvar"); |
| 408 | |
| 409 | /* Py_IsolatedFlag is not tested */ |
| 410 | Py_NoSiteFlag = 1; |
| 411 | Py_BytesWarningFlag = 1; |
| 412 | |
| 413 | putenv("PYTHONINSPECT="); |
| 414 | Py_InspectFlag = 1; |
| 415 | |
| 416 | putenv("PYTHONOPTIMIZE=0"); |
| 417 | Py_InteractiveFlag = 1; |
| 418 | |
| 419 | putenv("PYTHONDEBUG=0"); |
| 420 | Py_OptimizeFlag = 2; |
| 421 | |
| 422 | /* Py_DebugFlag is not tested */ |
| 423 | |
| 424 | putenv("PYTHONDONTWRITEBYTECODE="); |
| 425 | Py_DontWriteBytecodeFlag = 1; |
| 426 | |
| 427 | putenv("PYTHONVERBOSE=0"); |
| 428 | Py_VerboseFlag = 1; |
| 429 | |
| 430 | Py_QuietFlag = 1; |
| 431 | Py_NoUserSiteDirectory = 1; |
| 432 | |
| 433 | putenv("PYTHONUNBUFFERED="); |
| 434 | Py_UnbufferedStdioFlag = 1; |
| 435 | |
Victor Stinner | b75d7e2 | 2018-08-01 02:13:04 +0200 | [diff] [blame] | 436 | Py_FrozenFlag = 1; |
| 437 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 438 | /* FIXME: test Py_LegacyWindowsFSEncodingFlag */ |
| 439 | /* FIXME: test Py_LegacyWindowsStdioFlag */ |
| 440 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 441 | Py_Initialize(); |
| 442 | dump_config(); |
| 443 | Py_Finalize(); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | |
| 448 | static int test_init_from_config(void) |
| 449 | { |
| 450 | /* Test _Py_InitializeFromConfig() */ |
| 451 | _PyCoreConfig config = _PyCoreConfig_INIT; |
| 452 | config.install_signal_handlers = 0; |
| 453 | |
| 454 | /* FIXME: test use_environment */ |
| 455 | |
| 456 | putenv("PYTHONHASHSEED=42"); |
| 457 | config.use_hash_seed = 1; |
| 458 | config.hash_seed = 123; |
| 459 | |
| 460 | putenv("PYTHONMALLOC=malloc"); |
| 461 | config.allocator = "malloc_debug"; |
| 462 | |
| 463 | /* dev_mode=1 is tested in test_init_dev_mode() */ |
| 464 | |
| 465 | putenv("PYTHONFAULTHANDLER="); |
| 466 | config.faulthandler = 1; |
| 467 | |
| 468 | putenv("PYTHONTRACEMALLOC=0"); |
| 469 | config.tracemalloc = 2; |
| 470 | |
| 471 | putenv("PYTHONPROFILEIMPORTTIME=0"); |
| 472 | config.import_time = 1; |
| 473 | |
| 474 | config.show_ref_count = 1; |
| 475 | config.show_alloc_count = 1; |
| 476 | /* FIXME: test dump_refs: bpo-34223 */ |
| 477 | |
| 478 | putenv("PYTHONMALLOCSTATS=0"); |
| 479 | config.malloc_stats = 1; |
| 480 | |
| 481 | /* FIXME: test coerce_c_locale and coerce_c_locale_warn */ |
| 482 | |
| 483 | putenv("PYTHONUTF8=0"); |
| 484 | Py_UTF8Mode = 0; |
| 485 | config.utf8_mode = 1; |
| 486 | |
| 487 | putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix"); |
| 488 | config.pycache_prefix = L"conf_pycache_prefix"; |
| 489 | |
| 490 | Py_SetProgramName(L"./globalvar"); |
| 491 | config.program_name = L"./conf_program_name"; |
| 492 | |
| 493 | /* FIXME: test argc/argv */ |
| 494 | config.program = L"conf_program"; |
| 495 | /* FIXME: test xoptions */ |
| 496 | /* FIXME: test warnoptions */ |
| 497 | /* FIXME: test module_search_path_env */ |
| 498 | /* FIXME: test home */ |
| 499 | /* FIXME: test path config: module_search_path .. dll_path */ |
| 500 | |
| 501 | putenv("PYTHONVERBOSE=0"); |
| 502 | Py_VerboseFlag = 0; |
| 503 | config.verbose = 1; |
| 504 | |
| 505 | Py_NoSiteFlag = 0; |
| 506 | config.site_import = 0; |
| 507 | |
| 508 | Py_BytesWarningFlag = 0; |
| 509 | config.bytes_warning = 1; |
| 510 | |
| 511 | putenv("PYTHONINSPECT="); |
| 512 | Py_InspectFlag = 0; |
| 513 | config.inspect = 1; |
| 514 | |
| 515 | Py_InteractiveFlag = 0; |
| 516 | config.interactive = 1; |
| 517 | |
| 518 | putenv("PYTHONOPTIMIZE=0"); |
| 519 | Py_OptimizeFlag = 1; |
| 520 | config.optimization_level = 2; |
| 521 | |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 522 | /* FIXME: test parser_debug */ |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 523 | |
| 524 | putenv("PYTHONDONTWRITEBYTECODE="); |
| 525 | Py_DontWriteBytecodeFlag = 0; |
| 526 | config.write_bytecode = 0; |
| 527 | |
| 528 | Py_QuietFlag = 0; |
| 529 | config.quiet = 1; |
| 530 | |
| 531 | putenv("PYTHONUNBUFFERED="); |
| 532 | Py_UnbufferedStdioFlag = 0; |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 533 | config.buffered_stdio = 0; |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 534 | |
| 535 | putenv("PYTHONNOUSERSITE="); |
| 536 | Py_NoUserSiteDirectory = 0; |
| 537 | config.user_site_directory = 0; |
| 538 | |
| 539 | config._check_hash_pycs_mode = "always"; |
| 540 | |
Victor Stinner | b75d7e2 | 2018-08-01 02:13:04 +0200 | [diff] [blame] | 541 | Py_FrozenFlag = 0; |
| 542 | config._frozen = 1; |
| 543 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 544 | _PyInitError err = _Py_InitializeFromConfig(&config); |
| 545 | /* Don't call _PyCoreConfig_Clear() since all strings are static */ |
| 546 | if (_Py_INIT_FAILED(err)) { |
| 547 | _Py_FatalInitError(err); |
| 548 | } |
| 549 | dump_config(); |
| 550 | Py_Finalize(); |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | |
| 555 | static void test_init_env_putenvs(void) |
| 556 | { |
| 557 | putenv("PYTHONHASHSEED=42"); |
| 558 | putenv("PYTHONMALLOC=malloc_debug"); |
| 559 | putenv("PYTHONTRACEMALLOC=2"); |
| 560 | putenv("PYTHONPROFILEIMPORTTIME=1"); |
| 561 | putenv("PYTHONMALLOCSTATS=1"); |
| 562 | putenv("PYTHONUTF8=1"); |
| 563 | putenv("PYTHONVERBOSE=1"); |
| 564 | putenv("PYTHONINSPECT=1"); |
| 565 | putenv("PYTHONOPTIMIZE=2"); |
| 566 | putenv("PYTHONDONTWRITEBYTECODE=1"); |
| 567 | putenv("PYTHONUNBUFFERED=1"); |
| 568 | putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix"); |
| 569 | putenv("PYTHONNOUSERSITE=1"); |
| 570 | putenv("PYTHONFAULTHANDLER=1"); |
| 571 | putenv("PYTHONDEVMODE=1"); |
| 572 | /* FIXME: test PYTHONWARNINGS */ |
| 573 | /* FIXME: test PYTHONEXECUTABLE */ |
| 574 | /* FIXME: test PYTHONHOME */ |
| 575 | /* FIXME: test PYTHONDEBUG */ |
| 576 | /* FIXME: test PYTHONDUMPREFS */ |
| 577 | /* FIXME: test PYTHONCOERCECLOCALE */ |
| 578 | /* FIXME: test PYTHONPATH */ |
| 579 | } |
| 580 | |
| 581 | |
| 582 | static int test_init_env(void) |
| 583 | { |
| 584 | /* Test initialization from environment variables */ |
| 585 | Py_IgnoreEnvironmentFlag = 0; |
| 586 | test_init_env_putenvs(); |
| 587 | _testembed_Py_Initialize(); |
| 588 | dump_config(); |
| 589 | Py_Finalize(); |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | static int test_init_isolated(void) |
| 595 | { |
| 596 | /* Test _PyCoreConfig.isolated=1 */ |
| 597 | _PyCoreConfig config = _PyCoreConfig_INIT; |
| 598 | |
| 599 | /* Set coerce_c_locale and utf8_mode to not depend on the locale */ |
| 600 | config.coerce_c_locale = 0; |
| 601 | config.utf8_mode = 0; |
| 602 | /* Use path starting with "./" avoids a search along the PATH */ |
| 603 | config.program_name = L"./_testembed"; |
| 604 | |
| 605 | Py_IsolatedFlag = 0; |
| 606 | config.isolated = 1; |
| 607 | |
| 608 | test_init_env_putenvs(); |
| 609 | _PyInitError err = _Py_InitializeFromConfig(&config); |
| 610 | if (_Py_INIT_FAILED(err)) { |
| 611 | _Py_FatalInitError(err); |
| 612 | } |
| 613 | dump_config(); |
| 614 | Py_Finalize(); |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | |
| 619 | static int test_init_dev_mode(void) |
| 620 | { |
| 621 | _PyCoreConfig config = _PyCoreConfig_INIT; |
| 622 | putenv("PYTHONFAULTHANDLER="); |
| 623 | putenv("PYTHONMALLOC="); |
| 624 | config.dev_mode = 1; |
| 625 | config.program_name = L"./_testembed"; |
| 626 | _PyInitError err = _Py_InitializeFromConfig(&config); |
| 627 | if (_Py_INIT_FAILED(err)) { |
| 628 | _Py_FatalInitError(err); |
| 629 | } |
| 630 | dump_config(); |
| 631 | Py_Finalize(); |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 636 | /* ********************************************************* |
| 637 | * List of test cases and the function that implements it. |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 638 | * |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 639 | * Names are compared case-sensitively with the first |
| 640 | * argument. If no match is found, or no first argument was |
| 641 | * provided, the names of all test cases are printed and |
| 642 | * the exit code will be -1. |
| 643 | * |
| 644 | * The int returned from test functions is used as the exit |
| 645 | * code, and test_capi treats all non-zero exit codes as a |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 646 | * failed test. |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 647 | *********************************************************/ |
| 648 | struct TestCase |
| 649 | { |
| 650 | const char *name; |
| 651 | int (*func)(void); |
| 652 | }; |
| 653 | |
| 654 | static struct TestCase TestCases[] = { |
| 655 | { "forced_io_encoding", test_forced_io_encoding }, |
| 656 | { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters }, |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 657 | { "pre_initialization_api", test_pre_initialization_api }, |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 658 | { "pre_initialization_sys_options", test_pre_initialization_sys_options }, |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 659 | { "bpo20891", test_bpo20891 }, |
Victor Stinner | 209abf7 | 2018-06-22 19:14:51 +0200 | [diff] [blame] | 660 | { "initialize_twice", test_initialize_twice }, |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 661 | { "initialize_pymain", test_initialize_pymain }, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 662 | { "init_default_config", test_init_default_config }, |
| 663 | { "init_global_config", test_init_global_config }, |
| 664 | { "init_from_config", test_init_from_config }, |
| 665 | { "init_env", test_init_env }, |
| 666 | { "init_dev_mode", test_init_dev_mode }, |
| 667 | { "init_isolated", test_init_isolated }, |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 668 | { NULL, NULL } |
| 669 | }; |
| 670 | |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 671 | int main(int argc, char *argv[]) |
| 672 | { |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 673 | if (argc > 1) { |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 674 | for (struct TestCase *tc = TestCases; tc && tc->name; tc++) { |
| 675 | if (strcmp(argv[1], tc->name) == 0) |
| 676 | return (*tc->func)(); |
| 677 | } |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 678 | } |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 679 | |
| 680 | /* No match found, or no test name provided, so display usage */ |
| 681 | printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n" |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 682 | "Normally executed via 'EmbeddingTests' in Lib/test/test_embed.py\n\n" |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 683 | "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]); |
| 684 | for (struct TestCase *tc = TestCases; tc && tc->name; tc++) { |
| 685 | printf(" %s\n", tc->name); |
| 686 | } |
| 687 | |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 688 | /* Non-zero exit code will cause test_embed.py tests to fail. |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 689 | This is intentional. */ |
| 690 | return -1; |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 691 | } |