Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 1 | #ifndef Py_BUILD_CORE_MODULE |
| 2 | # define Py_BUILD_CORE_MODULE |
| 3 | #endif |
| 4 | |
Miss Islington (bot) | 4062841 | 2019-06-06 05:42:53 -0700 | [diff] [blame] | 5 | /* Always enable assertion (even in release mode) */ |
| 6 | #undef NDEBUG |
| 7 | |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 8 | #include <Python.h> |
Miss Islington (bot) | 4062841 | 2019-06-06 05:42:53 -0700 | [diff] [blame] | 9 | #include "pycore_initconfig.h" /* _PyConfig_InitCompatConfig() */ |
| 10 | #include "pycore_pystate.h" /* _PyRuntime */ |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 11 | #include <Python.h> |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 12 | #include "pythread.h" |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 13 | #include <inttypes.h> |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 14 | #include <stdio.h> |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 15 | #include <wchar.h> |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 16 | |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 17 | /********************************************************* |
| 18 | * Embedded interpreter tests that need a custom exe |
| 19 | * |
| 20 | * Executed via 'EmbeddingTests' in Lib/test/test_capi.py |
| 21 | *********************************************************/ |
| 22 | |
| 23 | static void _testembed_Py_Initialize(void) |
| 24 | { |
| 25 | /* HACK: the "./" at front avoids a search along the PATH in |
| 26 | Modules/getpath.c */ |
| 27 | Py_SetProgramName(L"./_testembed"); |
| 28 | Py_Initialize(); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | /***************************************************** |
Martin Panter | 8f26565 | 2016-04-19 04:03:41 +0000 | [diff] [blame] | 33 | * Test repeated initialisation and subinterpreters |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 34 | *****************************************************/ |
| 35 | |
| 36 | static void print_subinterp(void) |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 37 | { |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 38 | /* Output information about the interpreter in the format |
| 39 | expected in Lib/test/test_capi.py (test_subinterps). */ |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 40 | PyThreadState *ts = PyThreadState_Get(); |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 41 | PyInterpreterState *interp = ts->interp; |
| 42 | int64_t id = PyInterpreterState_GetID(interp); |
Eric Snow | d1c3c13 | 2017-05-24 17:19:47 -0700 | [diff] [blame] | 43 | printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ", |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 44 | id, (uintptr_t)interp, (uintptr_t)ts); |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 45 | fflush(stdout); |
| 46 | PyRun_SimpleString( |
| 47 | "import sys;" |
| 48 | "print('id(modules) =', id(sys.modules));" |
| 49 | "sys.stdout.flush()" |
| 50 | ); |
| 51 | } |
| 52 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 53 | static int test_repeated_init_and_subinterpreters(void) |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 54 | { |
| 55 | PyThreadState *mainstate, *substate; |
| 56 | PyGILState_STATE gilstate; |
| 57 | int i, j; |
| 58 | |
Ned Deily | 939231b | 2016-08-16 00:17:42 -0400 | [diff] [blame] | 59 | for (i=0; i<15; i++) { |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 60 | printf("--- Pass %d ---\n", i); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 61 | _testembed_Py_Initialize(); |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 62 | mainstate = PyThreadState_Get(); |
| 63 | |
| 64 | PyEval_InitThreads(); |
| 65 | PyEval_ReleaseThread(mainstate); |
| 66 | |
| 67 | gilstate = PyGILState_Ensure(); |
| 68 | print_subinterp(); |
| 69 | PyThreadState_Swap(NULL); |
| 70 | |
| 71 | for (j=0; j<3; j++) { |
| 72 | substate = Py_NewInterpreter(); |
| 73 | print_subinterp(); |
| 74 | Py_EndInterpreter(substate); |
| 75 | } |
| 76 | |
| 77 | PyThreadState_Swap(mainstate); |
| 78 | print_subinterp(); |
| 79 | PyGILState_Release(gilstate); |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 80 | |
| 81 | PyEval_RestoreThread(mainstate); |
| 82 | Py_Finalize(); |
| 83 | } |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 84 | return 0; |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /***************************************************** |
| 88 | * Test forcing a particular IO encoding |
| 89 | *****************************************************/ |
| 90 | |
| 91 | static void check_stdio_details(const char *encoding, const char * errors) |
| 92 | { |
| 93 | /* Output info for the test case to check */ |
| 94 | if (encoding) { |
| 95 | printf("Expected encoding: %s\n", encoding); |
| 96 | } else { |
| 97 | printf("Expected encoding: default\n"); |
| 98 | } |
| 99 | if (errors) { |
| 100 | printf("Expected errors: %s\n", errors); |
| 101 | } else { |
| 102 | printf("Expected errors: default\n"); |
| 103 | } |
| 104 | fflush(stdout); |
| 105 | /* Force the given IO encoding */ |
| 106 | Py_SetStandardStreamEncoding(encoding, errors); |
| 107 | _testembed_Py_Initialize(); |
| 108 | PyRun_SimpleString( |
| 109 | "import sys;" |
| 110 | "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));" |
| 111 | "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));" |
| 112 | "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));" |
| 113 | "sys.stdout.flush()" |
| 114 | ); |
| 115 | Py_Finalize(); |
| 116 | } |
| 117 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 118 | static int test_forced_io_encoding(void) |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 119 | { |
| 120 | /* Check various combinations */ |
| 121 | printf("--- Use defaults ---\n"); |
| 122 | check_stdio_details(NULL, NULL); |
| 123 | printf("--- Set errors only ---\n"); |
Victor Stinner | b2bef62 | 2014-03-18 02:38:12 +0100 | [diff] [blame] | 124 | check_stdio_details(NULL, "ignore"); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 125 | printf("--- Set encoding only ---\n"); |
Victor Stinner | 9e4994d | 2018-08-28 23:26:33 +0200 | [diff] [blame] | 126 | check_stdio_details("iso8859-1", NULL); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 127 | printf("--- Set encoding and errors ---\n"); |
Victor Stinner | 9e4994d | 2018-08-28 23:26:33 +0200 | [diff] [blame] | 128 | check_stdio_details("iso8859-1", "replace"); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 129 | |
| 130 | /* Check calling after initialization fails */ |
| 131 | Py_Initialize(); |
| 132 | |
| 133 | if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) { |
| 134 | printf("Unexpected success calling Py_SetStandardStreamEncoding"); |
| 135 | } |
| 136 | Py_Finalize(); |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 137 | return 0; |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 138 | } |
| 139 | |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 140 | /********************************************************* |
| 141 | * Test parts of the C-API that work before initialization |
| 142 | *********************************************************/ |
| 143 | |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 144 | /* The pre-initialization tests tend to break by segfaulting, so explicitly |
| 145 | * flushed progress messages make the broken API easier to find when they fail. |
| 146 | */ |
| 147 | #define _Py_EMBED_PREINIT_CHECK(msg) \ |
| 148 | do {printf(msg); fflush(stdout);} while (0); |
| 149 | |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 150 | static int test_pre_initialization_api(void) |
| 151 | { |
Victor Stinner | a9df651 | 2019-03-05 23:31:54 +0100 | [diff] [blame] | 152 | /* the test doesn't support custom memory allocators */ |
| 153 | putenv("PYTHONMALLOC="); |
| 154 | |
Nick Coghlan | 4274609 | 2017-11-26 14:19:13 +1000 | [diff] [blame] | 155 | /* Leading "./" ensures getpath.c can still find the standard library */ |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 156 | _Py_EMBED_PREINIT_CHECK("Checking Py_DecodeLocale\n"); |
Nick Coghlan | 4274609 | 2017-11-26 14:19:13 +1000 | [diff] [blame] | 157 | wchar_t *program = Py_DecodeLocale("./spam", NULL); |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 158 | if (program == NULL) { |
| 159 | fprintf(stderr, "Fatal error: cannot decode program name\n"); |
| 160 | return 1; |
| 161 | } |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 162 | _Py_EMBED_PREINIT_CHECK("Checking Py_SetProgramName\n"); |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 163 | Py_SetProgramName(program); |
| 164 | |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 165 | _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n"); |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 166 | Py_Initialize(); |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 167 | _Py_EMBED_PREINIT_CHECK("Check sys module contents\n"); |
| 168 | PyRun_SimpleString("import sys; " |
| 169 | "print('sys.executable:', sys.executable)"); |
| 170 | _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n"); |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 171 | Py_Finalize(); |
| 172 | |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 173 | _Py_EMBED_PREINIT_CHECK("Freeing memory allocated by Py_DecodeLocale\n"); |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 174 | PyMem_RawFree(program); |
| 175 | return 0; |
| 176 | } |
| 177 | |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 178 | |
| 179 | /* bpo-33042: Ensure embedding apps can predefine sys module options */ |
| 180 | static int test_pre_initialization_sys_options(void) |
| 181 | { |
Nick Coghlan | 69f5c73 | 2018-03-30 15:36:42 +1000 | [diff] [blame] | 182 | /* We allocate a couple of the options dynamically, and then delete |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 183 | * them before calling Py_Initialize. This ensures the interpreter isn't |
| 184 | * relying on the caller to keep the passed in strings alive. |
| 185 | */ |
Nick Coghlan | 69f5c73 | 2018-03-30 15:36:42 +1000 | [diff] [blame] | 186 | const wchar_t *static_warnoption = L"once"; |
| 187 | const wchar_t *static_xoption = L"also_not_an_option=2"; |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 188 | size_t warnoption_len = wcslen(static_warnoption); |
| 189 | size_t xoption_len = wcslen(static_xoption); |
Nick Coghlan | 69f5c73 | 2018-03-30 15:36:42 +1000 | [diff] [blame] | 190 | wchar_t *dynamic_once_warnoption = \ |
| 191 | (wchar_t *) calloc(warnoption_len+1, sizeof(wchar_t)); |
| 192 | wchar_t *dynamic_xoption = \ |
| 193 | (wchar_t *) calloc(xoption_len+1, sizeof(wchar_t)); |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 194 | wcsncpy(dynamic_once_warnoption, static_warnoption, warnoption_len+1); |
| 195 | wcsncpy(dynamic_xoption, static_xoption, xoption_len+1); |
| 196 | |
| 197 | _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption\n"); |
| 198 | PySys_AddWarnOption(L"default"); |
| 199 | _Py_EMBED_PREINIT_CHECK("Checking PySys_ResetWarnOptions\n"); |
| 200 | PySys_ResetWarnOptions(); |
| 201 | _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption linked list\n"); |
| 202 | PySys_AddWarnOption(dynamic_once_warnoption); |
| 203 | PySys_AddWarnOption(L"module"); |
| 204 | PySys_AddWarnOption(L"default"); |
| 205 | _Py_EMBED_PREINIT_CHECK("Checking PySys_AddXOption\n"); |
| 206 | PySys_AddXOption(L"not_an_option=1"); |
| 207 | PySys_AddXOption(dynamic_xoption); |
| 208 | |
| 209 | /* Delete the dynamic options early */ |
| 210 | free(dynamic_once_warnoption); |
| 211 | dynamic_once_warnoption = NULL; |
| 212 | free(dynamic_xoption); |
| 213 | dynamic_xoption = NULL; |
| 214 | |
| 215 | _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n"); |
| 216 | _testembed_Py_Initialize(); |
| 217 | _Py_EMBED_PREINIT_CHECK("Check sys module contents\n"); |
| 218 | PyRun_SimpleString("import sys; " |
| 219 | "print('sys.warnoptions:', sys.warnoptions); " |
| 220 | "print('sys._xoptions:', sys._xoptions); " |
| 221 | "warnings = sys.modules['warnings']; " |
| 222 | "latest_filters = [f[0] for f in warnings.filters[:3]]; " |
| 223 | "print('warnings.filters[:3]:', latest_filters)"); |
| 224 | _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n"); |
| 225 | Py_Finalize(); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /* bpo-20891: Avoid race condition when initialising the GIL */ |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 232 | static void bpo20891_thread(void *lockp) |
| 233 | { |
| 234 | PyThread_type_lock lock = *((PyThread_type_lock*)lockp); |
| 235 | |
| 236 | PyGILState_STATE state = PyGILState_Ensure(); |
| 237 | if (!PyGILState_Check()) { |
| 238 | fprintf(stderr, "PyGILState_Check failed!"); |
| 239 | abort(); |
| 240 | } |
| 241 | |
| 242 | PyGILState_Release(state); |
| 243 | |
| 244 | PyThread_release_lock(lock); |
| 245 | |
| 246 | PyThread_exit_thread(); |
| 247 | } |
| 248 | |
| 249 | static int test_bpo20891(void) |
| 250 | { |
Victor Stinner | a9df651 | 2019-03-05 23:31:54 +0100 | [diff] [blame] | 251 | /* the test doesn't support custom memory allocators */ |
| 252 | putenv("PYTHONMALLOC="); |
| 253 | |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 254 | /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before |
| 255 | calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must |
| 256 | call PyEval_InitThreads() for us in this case. */ |
| 257 | PyThread_type_lock lock = PyThread_allocate_lock(); |
| 258 | if (!lock) { |
| 259 | fprintf(stderr, "PyThread_allocate_lock failed!"); |
| 260 | return 1; |
| 261 | } |
| 262 | |
| 263 | _testembed_Py_Initialize(); |
| 264 | |
| 265 | unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock); |
| 266 | if (thrd == PYTHREAD_INVALID_THREAD_ID) { |
| 267 | fprintf(stderr, "PyThread_start_new_thread failed!"); |
| 268 | return 1; |
| 269 | } |
| 270 | PyThread_acquire_lock(lock, WAIT_LOCK); |
| 271 | |
| 272 | Py_BEGIN_ALLOW_THREADS |
| 273 | /* wait until the thread exit */ |
| 274 | PyThread_acquire_lock(lock, WAIT_LOCK); |
| 275 | Py_END_ALLOW_THREADS |
| 276 | |
| 277 | PyThread_free_lock(lock); |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
Victor Stinner | 209abf7 | 2018-06-22 19:14:51 +0200 | [diff] [blame] | 282 | static int test_initialize_twice(void) |
| 283 | { |
| 284 | _testembed_Py_Initialize(); |
| 285 | |
| 286 | /* bpo-33932: Calling Py_Initialize() twice should do nothing |
| 287 | * (and not crash!). */ |
| 288 | Py_Initialize(); |
| 289 | |
| 290 | Py_Finalize(); |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 295 | static int test_initialize_pymain(void) |
| 296 | { |
| 297 | wchar_t *argv[] = {L"PYTHON", L"-c", |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 298 | (L"import sys; " |
| 299 | L"print(f'Py_Main() after Py_Initialize: " |
| 300 | L"sys.argv={sys.argv}')"), |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 301 | L"arg2"}; |
| 302 | _testembed_Py_Initialize(); |
| 303 | |
| 304 | /* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */ |
| 305 | Py_Main(Py_ARRAY_LENGTH(argv), argv); |
| 306 | |
| 307 | Py_Finalize(); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 312 | |
Victor Stinner | 00b137c | 2018-11-13 19:59:26 +0100 | [diff] [blame] | 313 | static void |
| 314 | dump_config(void) |
| 315 | { |
Victor Stinner | 23bace2 | 2019-04-18 11:37:26 +0200 | [diff] [blame] | 316 | (void) PyRun_SimpleStringFlags( |
| 317 | "import _testinternalcapi, json; " |
| 318 | "print(json.dumps(_testinternalcapi.get_configs()))", |
| 319 | 0); |
Victor Stinner | 00b137c | 2018-11-13 19:59:26 +0100 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 323 | static int test_init_initialize_config(void) |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 324 | { |
| 325 | _testembed_Py_Initialize(); |
| 326 | dump_config(); |
| 327 | Py_Finalize(); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 332 | static void config_set_string(PyConfig *config, wchar_t **config_str, const wchar_t *str) |
| 333 | { |
| 334 | PyStatus status = PyConfig_SetString(config, config_str, str); |
| 335 | if (PyStatus_Exception(status)) { |
| 336 | PyConfig_Clear(config); |
| 337 | Py_ExitStatusException(status); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | |
| 342 | static void config_set_argv(PyConfig *config, Py_ssize_t argc, wchar_t * const *argv) |
| 343 | { |
| 344 | PyStatus status = PyConfig_SetArgv(config, argc, argv); |
| 345 | if (PyStatus_Exception(status)) { |
| 346 | PyConfig_Clear(config); |
| 347 | Py_ExitStatusException(status); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | |
| 352 | static void |
| 353 | config_set_wide_string_list(PyConfig *config, PyWideStringList *list, |
| 354 | Py_ssize_t length, wchar_t **items) |
| 355 | { |
| 356 | PyStatus status = PyConfig_SetWideStringList(config, list, length, items); |
| 357 | if (PyStatus_Exception(status)) { |
| 358 | PyConfig_Clear(config); |
| 359 | Py_ExitStatusException(status); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | |
| 364 | static void config_set_program_name(PyConfig *config) |
| 365 | { |
| 366 | /* Use path starting with "./" avoids a search along the PATH */ |
| 367 | const wchar_t *program_name = L"./_testembed"; |
| 368 | config_set_string(config, &config->program_name, program_name); |
| 369 | } |
| 370 | |
| 371 | |
| 372 | static void init_from_config_clear(PyConfig *config) |
| 373 | { |
| 374 | PyStatus status = Py_InitializeFromConfig(config); |
| 375 | PyConfig_Clear(config); |
| 376 | if (PyStatus_Exception(status)) { |
| 377 | Py_ExitStatusException(status); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 382 | static int check_init_compat_config(int preinit) |
| 383 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 384 | PyStatus status; |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 385 | |
| 386 | if (preinit) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 387 | PyPreConfig preconfig; |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 388 | _PyPreConfig_InitCompatConfig(&preconfig); |
| 389 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 390 | status = Py_PreInitialize(&preconfig); |
| 391 | if (PyStatus_Exception(status)) { |
| 392 | Py_ExitStatusException(status); |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 396 | PyConfig config; |
| 397 | _PyConfig_InitCompatConfig(&config); |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 398 | config_set_program_name(&config); |
| 399 | init_from_config_clear(&config); |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 400 | |
| 401 | dump_config(); |
| 402 | Py_Finalize(); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | static int test_preinit_compat_config(void) |
| 408 | { |
| 409 | return check_init_compat_config(1); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | static int test_init_compat_config(void) |
| 414 | { |
| 415 | return check_init_compat_config(0); |
| 416 | } |
| 417 | |
| 418 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 419 | static int test_init_global_config(void) |
| 420 | { |
| 421 | /* FIXME: test Py_IgnoreEnvironmentFlag */ |
| 422 | |
| 423 | putenv("PYTHONUTF8=0"); |
| 424 | Py_UTF8Mode = 1; |
| 425 | |
| 426 | /* Test initialization from global configuration variables (Py_xxx) */ |
| 427 | Py_SetProgramName(L"./globalvar"); |
| 428 | |
| 429 | /* Py_IsolatedFlag is not tested */ |
| 430 | Py_NoSiteFlag = 1; |
| 431 | Py_BytesWarningFlag = 1; |
| 432 | |
| 433 | putenv("PYTHONINSPECT="); |
| 434 | Py_InspectFlag = 1; |
| 435 | |
| 436 | putenv("PYTHONOPTIMIZE=0"); |
| 437 | Py_InteractiveFlag = 1; |
| 438 | |
| 439 | putenv("PYTHONDEBUG=0"); |
| 440 | Py_OptimizeFlag = 2; |
| 441 | |
| 442 | /* Py_DebugFlag is not tested */ |
| 443 | |
| 444 | putenv("PYTHONDONTWRITEBYTECODE="); |
| 445 | Py_DontWriteBytecodeFlag = 1; |
| 446 | |
| 447 | putenv("PYTHONVERBOSE=0"); |
| 448 | Py_VerboseFlag = 1; |
| 449 | |
| 450 | Py_QuietFlag = 1; |
| 451 | Py_NoUserSiteDirectory = 1; |
| 452 | |
| 453 | putenv("PYTHONUNBUFFERED="); |
| 454 | Py_UnbufferedStdioFlag = 1; |
| 455 | |
Victor Stinner | 54b43bb | 2019-05-16 18:30:15 +0200 | [diff] [blame] | 456 | Py_FrozenFlag = 1; |
| 457 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 458 | /* FIXME: test Py_LegacyWindowsFSEncodingFlag */ |
| 459 | /* FIXME: test Py_LegacyWindowsStdioFlag */ |
| 460 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 461 | Py_Initialize(); |
| 462 | dump_config(); |
| 463 | Py_Finalize(); |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | static int test_init_from_config(void) |
| 469 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 470 | PyStatus status; |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 471 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 472 | PyPreConfig preconfig; |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 473 | _PyPreConfig_InitCompatConfig(&preconfig); |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 474 | |
| 475 | putenv("PYTHONMALLOC=malloc_debug"); |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 476 | preconfig.allocator = PYMEM_ALLOCATOR_MALLOC; |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 477 | |
| 478 | putenv("PYTHONUTF8=0"); |
| 479 | Py_UTF8Mode = 0; |
| 480 | preconfig.utf8_mode = 1; |
| 481 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 482 | status = Py_PreInitialize(&preconfig); |
| 483 | if (PyStatus_Exception(status)) { |
| 484 | Py_ExitStatusException(status); |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 487 | PyConfig config; |
| 488 | _PyConfig_InitCompatConfig(&config); |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 489 | config.install_signal_handlers = 0; |
| 490 | |
| 491 | /* FIXME: test use_environment */ |
| 492 | |
| 493 | putenv("PYTHONHASHSEED=42"); |
| 494 | config.use_hash_seed = 1; |
| 495 | config.hash_seed = 123; |
| 496 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 497 | /* dev_mode=1 is tested in test_init_dev_mode() */ |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 498 | |
| 499 | putenv("PYTHONFAULTHANDLER="); |
| 500 | config.faulthandler = 1; |
| 501 | |
| 502 | putenv("PYTHONTRACEMALLOC=0"); |
| 503 | config.tracemalloc = 2; |
| 504 | |
| 505 | putenv("PYTHONPROFILEIMPORTTIME=0"); |
| 506 | config.import_time = 1; |
| 507 | |
| 508 | config.show_ref_count = 1; |
| 509 | config.show_alloc_count = 1; |
| 510 | /* FIXME: test dump_refs: bpo-34223 */ |
| 511 | |
| 512 | putenv("PYTHONMALLOCSTATS=0"); |
| 513 | config.malloc_stats = 1; |
| 514 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 515 | putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix"); |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 516 | config_set_string(&config, &config.pycache_prefix, L"conf_pycache_prefix"); |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 517 | |
| 518 | Py_SetProgramName(L"./globalvar"); |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 519 | config_set_string(&config, &config.program_name, L"./conf_program_name"); |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 520 | |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 521 | wchar_t* argv[] = { |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 522 | L"python3", |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 523 | L"-W", |
| 524 | L"cmdline_warnoption", |
| 525 | L"-X", |
| 526 | L"cmdline_xoption", |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 527 | L"-c", |
| 528 | L"pass", |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 529 | L"arg2", |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 530 | }; |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 531 | config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 532 | config.parse_argv = 1; |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 533 | |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 534 | wchar_t* xoptions[3] = { |
| 535 | L"config_xoption1=3", |
| 536 | L"config_xoption2=", |
| 537 | L"config_xoption3", |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 538 | }; |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 539 | config_set_wide_string_list(&config, &config.xoptions, |
| 540 | Py_ARRAY_LENGTH(xoptions), xoptions); |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 541 | |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 542 | wchar_t* warnoptions[1] = { |
| 543 | L"config_warnoption", |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 544 | }; |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 545 | config_set_wide_string_list(&config, &config.warnoptions, |
| 546 | Py_ARRAY_LENGTH(warnoptions), warnoptions); |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 547 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 548 | /* FIXME: test pythonpath_env */ |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 549 | /* FIXME: test home */ |
| 550 | /* FIXME: test path config: module_search_path .. dll_path */ |
| 551 | |
| 552 | putenv("PYTHONVERBOSE=0"); |
| 553 | Py_VerboseFlag = 0; |
| 554 | config.verbose = 1; |
| 555 | |
| 556 | Py_NoSiteFlag = 0; |
| 557 | config.site_import = 0; |
| 558 | |
| 559 | Py_BytesWarningFlag = 0; |
| 560 | config.bytes_warning = 1; |
| 561 | |
| 562 | putenv("PYTHONINSPECT="); |
| 563 | Py_InspectFlag = 0; |
| 564 | config.inspect = 1; |
| 565 | |
| 566 | Py_InteractiveFlag = 0; |
| 567 | config.interactive = 1; |
| 568 | |
| 569 | putenv("PYTHONOPTIMIZE=0"); |
| 570 | Py_OptimizeFlag = 1; |
| 571 | config.optimization_level = 2; |
| 572 | |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 573 | /* FIXME: test parser_debug */ |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 574 | |
| 575 | putenv("PYTHONDONTWRITEBYTECODE="); |
| 576 | Py_DontWriteBytecodeFlag = 0; |
| 577 | config.write_bytecode = 0; |
| 578 | |
| 579 | Py_QuietFlag = 0; |
| 580 | config.quiet = 1; |
| 581 | |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 582 | config.configure_c_stdio = 1; |
Victor Stinner | 54b43bb | 2019-05-16 18:30:15 +0200 | [diff] [blame] | 583 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 584 | putenv("PYTHONUNBUFFERED="); |
| 585 | Py_UnbufferedStdioFlag = 0; |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 586 | config.buffered_stdio = 0; |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 587 | |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 588 | putenv("PYTHONIOENCODING=cp424"); |
| 589 | Py_SetStandardStreamEncoding("ascii", "ignore"); |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 590 | #ifdef MS_WINDOWS |
| 591 | /* Py_SetStandardStreamEncoding() sets Py_LegacyWindowsStdioFlag to 1. |
| 592 | Force it to 0 through the config. */ |
| 593 | config.legacy_windows_stdio = 0; |
| 594 | #endif |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 595 | config_set_string(&config, &config.stdio_encoding, L"iso8859-1"); |
| 596 | config_set_string(&config, &config.stdio_errors, L"replace"); |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 597 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 598 | putenv("PYTHONNOUSERSITE="); |
| 599 | Py_NoUserSiteDirectory = 0; |
| 600 | config.user_site_directory = 0; |
| 601 | |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 602 | config_set_string(&config, &config.check_hash_pycs_mode, L"always"); |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 603 | |
Victor Stinner | 54b43bb | 2019-05-16 18:30:15 +0200 | [diff] [blame] | 604 | Py_FrozenFlag = 0; |
| 605 | config.pathconfig_warnings = 0; |
| 606 | |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 607 | init_from_config_clear(&config); |
| 608 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 609 | dump_config(); |
| 610 | Py_Finalize(); |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 615 | static int check_init_parse_argv(int parse_argv) |
Victor Stinner | ae239f6 | 2019-05-16 17:02:56 +0200 | [diff] [blame] | 616 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 617 | PyStatus status; |
Victor Stinner | ae239f6 | 2019-05-16 17:02:56 +0200 | [diff] [blame] | 618 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 619 | PyConfig config; |
| 620 | status = PyConfig_InitPythonConfig(&config); |
| 621 | if (PyStatus_Exception(status)) { |
| 622 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 623 | } |
Victor Stinner | ae239f6 | 2019-05-16 17:02:56 +0200 | [diff] [blame] | 624 | |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 625 | config.parse_argv = parse_argv; |
| 626 | |
| 627 | wchar_t* argv[] = { |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 628 | L"./argv0", |
| 629 | L"-E", |
Victor Stinner | ae239f6 | 2019-05-16 17:02:56 +0200 | [diff] [blame] | 630 | L"-c", |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 631 | L"pass", |
Victor Stinner | ae239f6 | 2019-05-16 17:02:56 +0200 | [diff] [blame] | 632 | L"arg1", |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 633 | L"-v", |
| 634 | L"arg3", |
Victor Stinner | ae239f6 | 2019-05-16 17:02:56 +0200 | [diff] [blame] | 635 | }; |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 636 | config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv); |
| 637 | init_from_config_clear(&config); |
Victor Stinner | ae239f6 | 2019-05-16 17:02:56 +0200 | [diff] [blame] | 638 | |
Victor Stinner | ae239f6 | 2019-05-16 17:02:56 +0200 | [diff] [blame] | 639 | dump_config(); |
| 640 | Py_Finalize(); |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 645 | static int test_init_parse_argv(void) |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 646 | { |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 647 | return check_init_parse_argv(1); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 651 | static int test_init_dont_parse_argv(void) |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 652 | { |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 653 | return check_init_parse_argv(0); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 657 | static void set_most_env_vars(void) |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 658 | { |
| 659 | putenv("PYTHONHASHSEED=42"); |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 660 | putenv("PYTHONMALLOC=malloc"); |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 661 | putenv("PYTHONTRACEMALLOC=2"); |
| 662 | putenv("PYTHONPROFILEIMPORTTIME=1"); |
| 663 | putenv("PYTHONMALLOCSTATS=1"); |
| 664 | putenv("PYTHONUTF8=1"); |
| 665 | putenv("PYTHONVERBOSE=1"); |
| 666 | putenv("PYTHONINSPECT=1"); |
| 667 | putenv("PYTHONOPTIMIZE=2"); |
| 668 | putenv("PYTHONDONTWRITEBYTECODE=1"); |
| 669 | putenv("PYTHONUNBUFFERED=1"); |
| 670 | putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix"); |
| 671 | putenv("PYTHONNOUSERSITE=1"); |
| 672 | putenv("PYTHONFAULTHANDLER=1"); |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 673 | putenv("PYTHONIOENCODING=iso8859-1:replace"); |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | |
| 677 | static void set_all_env_vars(void) |
| 678 | { |
| 679 | set_most_env_vars(); |
| 680 | |
| 681 | putenv("PYTHONWARNINGS=EnvVar"); |
| 682 | putenv("PYTHONPATH=/my/path"); |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 686 | static int test_init_compat_env(void) |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 687 | { |
| 688 | /* Test initialization from environment variables */ |
| 689 | Py_IgnoreEnvironmentFlag = 0; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 690 | set_all_env_vars(); |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 691 | _testembed_Py_Initialize(); |
| 692 | dump_config(); |
| 693 | Py_Finalize(); |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 698 | static int test_init_python_env(void) |
| 699 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 700 | PyStatus status; |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 701 | |
| 702 | set_all_env_vars(); |
| 703 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 704 | PyConfig config; |
| 705 | status = PyConfig_InitPythonConfig(&config); |
| 706 | if (PyStatus_Exception(status)) { |
| 707 | Py_ExitStatusException(status); |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 708 | } |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 709 | |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 710 | config_set_program_name(&config); |
| 711 | init_from_config_clear(&config); |
| 712 | |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 713 | dump_config(); |
| 714 | Py_Finalize(); |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 719 | static void set_all_env_vars_dev_mode(void) |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 720 | { |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 721 | putenv("PYTHONMALLOC="); |
| 722 | putenv("PYTHONFAULTHANDLER="); |
| 723 | putenv("PYTHONDEVMODE=1"); |
| 724 | } |
| 725 | |
| 726 | |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 727 | static int test_init_env_dev_mode(void) |
| 728 | { |
| 729 | /* Test initialization from environment variables */ |
| 730 | Py_IgnoreEnvironmentFlag = 0; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 731 | set_all_env_vars_dev_mode(); |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 732 | _testembed_Py_Initialize(); |
| 733 | dump_config(); |
| 734 | Py_Finalize(); |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 739 | static int test_init_env_dev_mode_alloc(void) |
| 740 | { |
| 741 | /* Test initialization from environment variables */ |
| 742 | Py_IgnoreEnvironmentFlag = 0; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 743 | set_all_env_vars_dev_mode(); |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 744 | putenv("PYTHONMALLOC=malloc"); |
| 745 | _testembed_Py_Initialize(); |
| 746 | dump_config(); |
| 747 | Py_Finalize(); |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 752 | static int test_init_isolated_flag(void) |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 753 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 754 | PyStatus status; |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 755 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 756 | /* Test PyConfig.isolated=1 */ |
| 757 | PyConfig config; |
| 758 | status = PyConfig_InitPythonConfig(&config); |
| 759 | if (PyStatus_Exception(status)) { |
| 760 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 761 | } |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 762 | |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 763 | Py_IsolatedFlag = 0; |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 764 | config.isolated = 1; |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 765 | |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 766 | config_set_program_name(&config); |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 767 | set_all_env_vars(); |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 768 | init_from_config_clear(&config); |
| 769 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 770 | dump_config(); |
| 771 | Py_Finalize(); |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 776 | /* PyPreConfig.isolated=1, PyConfig.isolated=0 */ |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 777 | static int test_preinit_isolated1(void) |
| 778 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 779 | PyStatus status; |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 780 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 781 | PyPreConfig preconfig; |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 782 | _PyPreConfig_InitCompatConfig(&preconfig); |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 783 | preconfig.isolated = 1; |
| 784 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 785 | status = Py_PreInitialize(&preconfig); |
| 786 | if (PyStatus_Exception(status)) { |
| 787 | Py_ExitStatusException(status); |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 788 | } |
| 789 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 790 | PyConfig config; |
| 791 | _PyConfig_InitCompatConfig(&config); |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 792 | config_set_program_name(&config); |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 793 | set_all_env_vars(); |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 794 | init_from_config_clear(&config); |
| 795 | |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 796 | dump_config(); |
| 797 | Py_Finalize(); |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 802 | /* PyPreConfig.isolated=0, PyConfig.isolated=1 */ |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 803 | static int test_preinit_isolated2(void) |
| 804 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 805 | PyStatus status; |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 806 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 807 | PyPreConfig preconfig; |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 808 | _PyPreConfig_InitCompatConfig(&preconfig); |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 809 | preconfig.isolated = 0; |
| 810 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 811 | status = Py_PreInitialize(&preconfig); |
| 812 | if (PyStatus_Exception(status)) { |
| 813 | Py_ExitStatusException(status); |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 814 | } |
| 815 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 816 | /* Test PyConfig.isolated=1 */ |
| 817 | PyConfig config; |
| 818 | _PyConfig_InitCompatConfig(&config); |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 819 | |
| 820 | Py_IsolatedFlag = 0; |
| 821 | config.isolated = 1; |
| 822 | |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 823 | config_set_program_name(&config); |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 824 | set_all_env_vars(); |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 825 | init_from_config_clear(&config); |
| 826 | |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 827 | dump_config(); |
| 828 | Py_Finalize(); |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 833 | static int test_preinit_dont_parse_argv(void) |
| 834 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 835 | PyStatus status; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 836 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 837 | PyPreConfig preconfig; |
| 838 | PyPreConfig_InitIsolatedConfig(&preconfig); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 839 | |
| 840 | preconfig.isolated = 0; |
| 841 | |
| 842 | /* -X dev must be ignored by isolated preconfiguration */ |
| 843 | wchar_t *argv[] = {L"python3", |
| 844 | L"-E", |
| 845 | L"-I", |
| 846 | L"-X", L"dev", |
| 847 | L"-X", L"utf8", |
| 848 | L"script.py"}; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 849 | status = Py_PreInitializeFromArgs(&preconfig, Py_ARRAY_LENGTH(argv), argv); |
| 850 | if (PyStatus_Exception(status)) { |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 851 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 852 | } |
| 853 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 854 | PyConfig config; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 855 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 856 | status = PyConfig_InitIsolatedConfig(&config); |
| 857 | if (PyStatus_Exception(status)) { |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 858 | PyConfig_Clear(&config); |
| 859 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | config.isolated = 0; |
| 863 | |
| 864 | /* Pre-initialize implicitly using argv: make sure that -X dev |
| 865 | is used to configure the allocation in preinitialization */ |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 866 | config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv); |
| 867 | config_set_program_name(&config); |
| 868 | init_from_config_clear(&config); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 869 | |
| 870 | dump_config(); |
| 871 | Py_Finalize(); |
| 872 | return 0; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | |
| 876 | static int test_preinit_parse_argv(void) |
| 877 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 878 | PyStatus status; |
| 879 | PyConfig config; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 880 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 881 | status = PyConfig_InitPythonConfig(&config); |
| 882 | if (PyStatus_Exception(status)) { |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 883 | PyConfig_Clear(&config); |
| 884 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* Pre-initialize implicitly using argv: make sure that -X dev |
| 888 | is used to configure the allocation in preinitialization */ |
| 889 | wchar_t *argv[] = {L"python3", L"-X", L"dev", L"script.py"}; |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 890 | config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv); |
| 891 | config_set_program_name(&config); |
| 892 | init_from_config_clear(&config); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 893 | |
| 894 | dump_config(); |
| 895 | Py_Finalize(); |
| 896 | return 0; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 902 | static void set_all_global_config_variables(void) |
| 903 | { |
| 904 | Py_IsolatedFlag = 0; |
| 905 | Py_IgnoreEnvironmentFlag = 0; |
| 906 | Py_BytesWarningFlag = 2; |
| 907 | Py_InspectFlag = 1; |
| 908 | Py_InteractiveFlag = 1; |
| 909 | Py_OptimizeFlag = 1; |
| 910 | Py_DebugFlag = 1; |
| 911 | Py_VerboseFlag = 1; |
| 912 | Py_QuietFlag = 1; |
| 913 | Py_FrozenFlag = 0; |
| 914 | Py_UnbufferedStdioFlag = 1; |
| 915 | Py_NoSiteFlag = 1; |
| 916 | Py_DontWriteBytecodeFlag = 1; |
| 917 | Py_NoUserSiteDirectory = 1; |
| 918 | #ifdef MS_WINDOWS |
| 919 | Py_LegacyWindowsStdioFlag = 1; |
| 920 | #endif |
| 921 | } |
| 922 | |
| 923 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 924 | static int check_preinit_isolated_config(int preinit) |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 925 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 926 | PyStatus status; |
| 927 | PyPreConfig *rt_preconfig; |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 928 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 929 | /* environment variables must be ignored */ |
| 930 | set_all_env_vars(); |
| 931 | |
| 932 | /* global configuration variables must be ignored */ |
| 933 | set_all_global_config_variables(); |
| 934 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 935 | if (preinit) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 936 | PyPreConfig preconfig; |
| 937 | PyPreConfig_InitIsolatedConfig(&preconfig); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 938 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 939 | status = Py_PreInitialize(&preconfig); |
| 940 | if (PyStatus_Exception(status)) { |
| 941 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | rt_preconfig = &_PyRuntime.preconfig; |
| 945 | assert(rt_preconfig->isolated == 1); |
| 946 | assert(rt_preconfig->use_environment == 0); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 947 | } |
| 948 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 949 | PyConfig config; |
| 950 | status = PyConfig_InitIsolatedConfig(&config); |
| 951 | if (PyStatus_Exception(status)) { |
| 952 | PyConfig_Clear(&config); |
| 953 | Py_ExitStatusException(status); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 954 | } |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 955 | config_set_program_name(&config); |
| 956 | init_from_config_clear(&config); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 957 | |
| 958 | rt_preconfig = &_PyRuntime.preconfig; |
| 959 | assert(rt_preconfig->isolated == 1); |
| 960 | assert(rt_preconfig->use_environment == 0); |
| 961 | |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 962 | dump_config(); |
| 963 | Py_Finalize(); |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 968 | static int test_preinit_isolated_config(void) |
| 969 | { |
| 970 | return check_preinit_isolated_config(1); |
| 971 | } |
| 972 | |
| 973 | |
| 974 | static int test_init_isolated_config(void) |
| 975 | { |
| 976 | return check_preinit_isolated_config(0); |
| 977 | } |
| 978 | |
| 979 | |
| 980 | static int check_init_python_config(int preinit) |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 981 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 982 | PyStatus status; |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 983 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 984 | /* global configuration variables must be ignored */ |
| 985 | set_all_global_config_variables(); |
| 986 | Py_IsolatedFlag = 1; |
| 987 | Py_IgnoreEnvironmentFlag = 1; |
| 988 | Py_FrozenFlag = 1; |
| 989 | Py_UnbufferedStdioFlag = 1; |
| 990 | Py_NoSiteFlag = 1; |
| 991 | Py_DontWriteBytecodeFlag = 1; |
| 992 | Py_NoUserSiteDirectory = 1; |
| 993 | #ifdef MS_WINDOWS |
| 994 | Py_LegacyWindowsStdioFlag = 1; |
| 995 | #endif |
| 996 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 997 | if (preinit) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 998 | PyPreConfig preconfig; |
| 999 | PyPreConfig_InitPythonConfig(&preconfig); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 1000 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1001 | status = Py_PreInitialize(&preconfig); |
| 1002 | if (PyStatus_Exception(status)) { |
| 1003 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1004 | } |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1007 | PyConfig config; |
| 1008 | status = PyConfig_InitPythonConfig(&config); |
| 1009 | if (PyStatus_Exception(status)) { |
| 1010 | Py_ExitStatusException(status); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 1011 | } |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 1012 | config_set_program_name(&config); |
| 1013 | init_from_config_clear(&config); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 1014 | |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 1015 | dump_config(); |
| 1016 | Py_Finalize(); |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1021 | static int test_preinit_python_config(void) |
| 1022 | { |
| 1023 | return check_init_python_config(1); |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | static int test_init_python_config(void) |
| 1028 | { |
| 1029 | return check_init_python_config(0); |
| 1030 | } |
| 1031 | |
| 1032 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 1033 | static int test_init_dont_configure_locale(void) |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 1034 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1035 | PyStatus status; |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 1036 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1037 | PyPreConfig preconfig; |
| 1038 | PyPreConfig_InitPythonConfig(&preconfig); |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 1039 | preconfig.configure_locale = 0; |
| 1040 | preconfig.coerce_c_locale = 1; |
| 1041 | preconfig.coerce_c_locale_warn = 1; |
| 1042 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1043 | status = Py_PreInitialize(&preconfig); |
| 1044 | if (PyStatus_Exception(status)) { |
| 1045 | Py_ExitStatusException(status); |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1048 | PyConfig config; |
| 1049 | status = PyConfig_InitPythonConfig(&config); |
| 1050 | if (PyStatus_Exception(status)) { |
| 1051 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1052 | } |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 1053 | config_set_program_name(&config); |
| 1054 | init_from_config_clear(&config); |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 1055 | |
| 1056 | dump_config(); |
| 1057 | Py_Finalize(); |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 1062 | static int test_init_dev_mode(void) |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 1063 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1064 | PyStatus status; |
| 1065 | PyConfig config; |
| 1066 | status = PyConfig_InitPythonConfig(&config); |
| 1067 | if (PyStatus_Exception(status)) { |
| 1068 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1069 | } |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 1070 | putenv("PYTHONFAULTHANDLER="); |
| 1071 | putenv("PYTHONMALLOC="); |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 1072 | config.dev_mode = 1; |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 1073 | config_set_program_name(&config); |
| 1074 | init_from_config_clear(&config); |
| 1075 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 1076 | dump_config(); |
| 1077 | Py_Finalize(); |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1081 | static PyObject *_open_code_hook(PyObject *path, void *data) |
| 1082 | { |
| 1083 | if (PyUnicode_CompareWithASCIIString(path, "$$test-filename") == 0) { |
| 1084 | return PyLong_FromVoidPtr(data); |
| 1085 | } |
| 1086 | PyObject *io = PyImport_ImportModule("_io"); |
| 1087 | if (!io) { |
| 1088 | return NULL; |
| 1089 | } |
| 1090 | return PyObject_CallMethod(io, "open", "Os", path, "rb"); |
| 1091 | } |
| 1092 | |
| 1093 | static int test_open_code_hook(void) |
| 1094 | { |
| 1095 | int result = 0; |
| 1096 | |
| 1097 | /* Provide a hook */ |
| 1098 | result = PyFile_SetOpenCodeHook(_open_code_hook, &result); |
| 1099 | if (result) { |
| 1100 | printf("Failed to set hook\n"); |
| 1101 | return 1; |
| 1102 | } |
| 1103 | /* A second hook should fail */ |
| 1104 | result = PyFile_SetOpenCodeHook(_open_code_hook, &result); |
| 1105 | if (!result) { |
| 1106 | printf("Should have failed to set second hook\n"); |
| 1107 | return 2; |
| 1108 | } |
| 1109 | |
| 1110 | Py_IgnoreEnvironmentFlag = 0; |
| 1111 | _testembed_Py_Initialize(); |
| 1112 | result = 0; |
| 1113 | |
| 1114 | PyObject *r = PyFile_OpenCode("$$test-filename"); |
| 1115 | if (!r) { |
| 1116 | PyErr_Print(); |
| 1117 | result = 3; |
| 1118 | } else { |
| 1119 | void *cmp = PyLong_AsVoidPtr(r); |
| 1120 | Py_DECREF(r); |
| 1121 | if (cmp != &result) { |
| 1122 | printf("Did not get expected result from hook\n"); |
| 1123 | result = 4; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | if (!result) { |
| 1128 | PyObject *io = PyImport_ImportModule("_io"); |
| 1129 | PyObject *r = io |
| 1130 | ? PyObject_CallMethod(io, "open_code", "s", "$$test-filename") |
| 1131 | : NULL; |
| 1132 | if (!r) { |
| 1133 | PyErr_Print(); |
| 1134 | result = 5; |
| 1135 | } else { |
| 1136 | void *cmp = PyLong_AsVoidPtr(r); |
| 1137 | Py_DECREF(r); |
| 1138 | if (cmp != &result) { |
| 1139 | printf("Did not get expected result from hook\n"); |
| 1140 | result = 6; |
| 1141 | } |
| 1142 | } |
| 1143 | Py_XDECREF(io); |
| 1144 | } |
| 1145 | |
| 1146 | Py_Finalize(); |
| 1147 | return result; |
| 1148 | } |
| 1149 | |
| 1150 | static int _audit_hook(const char *event, PyObject *args, void *userdata) |
| 1151 | { |
| 1152 | if (strcmp(event, "_testembed.raise") == 0) { |
| 1153 | PyErr_SetString(PyExc_RuntimeError, "Intentional error"); |
| 1154 | return -1; |
| 1155 | } else if (strcmp(event, "_testembed.set") == 0) { |
| 1156 | if (!PyArg_ParseTuple(args, "n", userdata)) { |
| 1157 | return -1; |
| 1158 | } |
| 1159 | return 0; |
| 1160 | } |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | static int _test_audit(Py_ssize_t setValue) |
| 1165 | { |
| 1166 | Py_ssize_t sawSet = 0; |
| 1167 | |
| 1168 | Py_IgnoreEnvironmentFlag = 0; |
| 1169 | PySys_AddAuditHook(_audit_hook, &sawSet); |
| 1170 | _testembed_Py_Initialize(); |
| 1171 | |
| 1172 | if (PySys_Audit("_testembed.raise", NULL) == 0) { |
| 1173 | printf("No error raised"); |
| 1174 | return 1; |
| 1175 | } |
| 1176 | if (PySys_Audit("_testembed.nop", NULL) != 0) { |
| 1177 | printf("Nop event failed"); |
| 1178 | /* Exception from above may still remain */ |
| 1179 | PyErr_Clear(); |
| 1180 | return 2; |
| 1181 | } |
| 1182 | if (!PyErr_Occurred()) { |
| 1183 | printf("Exception not preserved"); |
| 1184 | return 3; |
| 1185 | } |
| 1186 | PyErr_Clear(); |
| 1187 | |
| 1188 | if (PySys_Audit("_testembed.set", "n", setValue) != 0) { |
| 1189 | PyErr_Print(); |
| 1190 | printf("Set event failed"); |
| 1191 | return 4; |
| 1192 | } |
| 1193 | |
| 1194 | if (sawSet != 42) { |
| 1195 | printf("Failed to see *userData change\n"); |
| 1196 | return 5; |
| 1197 | } |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
| 1201 | static int test_audit(void) |
| 1202 | { |
| 1203 | int result = _test_audit(42); |
| 1204 | Py_Finalize(); |
| 1205 | return result; |
| 1206 | } |
| 1207 | |
| 1208 | static volatile int _audit_subinterpreter_interpreter_count = 0; |
| 1209 | |
| 1210 | static int _audit_subinterpreter_hook(const char *event, PyObject *args, void *userdata) |
| 1211 | { |
| 1212 | printf("%s\n", event); |
| 1213 | if (strcmp(event, "cpython.PyInterpreterState_New") == 0) { |
| 1214 | _audit_subinterpreter_interpreter_count += 1; |
| 1215 | } |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
| 1219 | static int test_audit_subinterpreter(void) |
| 1220 | { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1221 | Py_IgnoreEnvironmentFlag = 0; |
| 1222 | PySys_AddAuditHook(_audit_subinterpreter_hook, NULL); |
| 1223 | _testembed_Py_Initialize(); |
| 1224 | |
Pablo Galindo | cccc11b | 2019-05-24 00:53:21 +0100 | [diff] [blame] | 1225 | Py_NewInterpreter(); |
| 1226 | Py_NewInterpreter(); |
| 1227 | Py_NewInterpreter(); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1228 | |
| 1229 | Py_Finalize(); |
| 1230 | |
| 1231 | switch (_audit_subinterpreter_interpreter_count) { |
| 1232 | case 3: return 0; |
| 1233 | case 0: return -1; |
| 1234 | default: return _audit_subinterpreter_interpreter_count; |
| 1235 | } |
| 1236 | } |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 1237 | |
Miss Islington (bot) | 746992c | 2019-07-01 16:22:29 -0700 | [diff] [blame] | 1238 | typedef struct { |
| 1239 | const char* expected; |
| 1240 | int exit; |
| 1241 | } AuditRunCommandTest; |
| 1242 | |
| 1243 | static int _audit_hook_run(const char *eventName, PyObject *args, void *userData) |
| 1244 | { |
| 1245 | AuditRunCommandTest *test = (AuditRunCommandTest*)userData; |
| 1246 | if (strcmp(eventName, test->expected)) { |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | if (test->exit) { |
| 1251 | PyObject *msg = PyUnicode_FromFormat("detected %s(%R)", eventName, args); |
| 1252 | if (msg) { |
| 1253 | printf("%s\n", PyUnicode_AsUTF8(msg)); |
| 1254 | Py_DECREF(msg); |
| 1255 | } |
| 1256 | exit(test->exit); |
| 1257 | } |
| 1258 | |
| 1259 | PyErr_Format(PyExc_RuntimeError, "detected %s(%R)", eventName, args); |
| 1260 | return -1; |
| 1261 | } |
| 1262 | |
| 1263 | static int test_audit_run_command(void) |
| 1264 | { |
| 1265 | AuditRunCommandTest test = {"cpython.run_command"}; |
| 1266 | wchar_t *argv[] = {L"./_testembed", L"-c", L"pass"}; |
| 1267 | |
| 1268 | Py_IgnoreEnvironmentFlag = 0; |
| 1269 | PySys_AddAuditHook(_audit_hook_run, (void*)&test); |
| 1270 | |
| 1271 | return Py_Main(Py_ARRAY_LENGTH(argv), argv); |
| 1272 | } |
| 1273 | |
| 1274 | static int test_audit_run_file(void) |
| 1275 | { |
| 1276 | AuditRunCommandTest test = {"cpython.run_file"}; |
| 1277 | wchar_t *argv[] = {L"./_testembed", L"filename.py"}; |
| 1278 | |
| 1279 | Py_IgnoreEnvironmentFlag = 0; |
| 1280 | PySys_AddAuditHook(_audit_hook_run, (void*)&test); |
| 1281 | |
| 1282 | return Py_Main(Py_ARRAY_LENGTH(argv), argv); |
| 1283 | } |
| 1284 | |
| 1285 | static int run_audit_run_test(int argc, wchar_t **argv, void *test) |
| 1286 | { |
| 1287 | PyStatus status; |
| 1288 | PyConfig config; |
| 1289 | status = PyConfig_InitPythonConfig(&config); |
| 1290 | if (PyStatus_Exception(status)) { |
| 1291 | Py_ExitStatusException(status); |
| 1292 | } |
| 1293 | config.argv.length = argc; |
| 1294 | config.argv.items = argv; |
| 1295 | config.parse_argv = 1; |
| 1296 | config.program_name = argv[0]; |
| 1297 | config.interactive = 1; |
| 1298 | config.isolated = 0; |
| 1299 | config.use_environment = 1; |
| 1300 | config.quiet = 1; |
| 1301 | |
| 1302 | PySys_AddAuditHook(_audit_hook_run, test); |
| 1303 | |
| 1304 | status = Py_InitializeFromConfig(&config); |
| 1305 | if (PyStatus_Exception(status)) { |
| 1306 | Py_ExitStatusException(status); |
| 1307 | } |
| 1308 | |
| 1309 | return Py_RunMain(); |
| 1310 | } |
| 1311 | |
| 1312 | static int test_audit_run_interactivehook(void) |
| 1313 | { |
| 1314 | AuditRunCommandTest test = {"cpython.run_interactivehook", 10}; |
| 1315 | wchar_t *argv[] = {L"./_testembed"}; |
| 1316 | return run_audit_run_test(Py_ARRAY_LENGTH(argv), argv, &test); |
| 1317 | } |
| 1318 | |
| 1319 | static int test_audit_run_startup(void) |
| 1320 | { |
| 1321 | AuditRunCommandTest test = {"cpython.run_startup", 10}; |
| 1322 | wchar_t *argv[] = {L"./_testembed"}; |
| 1323 | return run_audit_run_test(Py_ARRAY_LENGTH(argv), argv, &test); |
| 1324 | } |
| 1325 | |
| 1326 | static int test_audit_run_stdin(void) |
| 1327 | { |
| 1328 | AuditRunCommandTest test = {"cpython.run_stdin"}; |
| 1329 | wchar_t *argv[] = {L"./_testembed"}; |
| 1330 | return run_audit_run_test(Py_ARRAY_LENGTH(argv), argv, &test); |
| 1331 | } |
| 1332 | |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 1333 | static int test_init_read_set(void) |
| 1334 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1335 | PyStatus status; |
| 1336 | PyConfig config; |
| 1337 | status = PyConfig_InitPythonConfig(&config); |
| 1338 | if (PyStatus_Exception(status)) { |
| 1339 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1340 | } |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 1341 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1342 | status = PyConfig_SetBytesString(&config, &config.program_name, |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1343 | "./init_read_set"); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1344 | if (PyStatus_Exception(status)) { |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 1345 | goto fail; |
| 1346 | } |
| 1347 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1348 | status = PyConfig_Read(&config); |
| 1349 | if (PyStatus_Exception(status)) { |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 1350 | goto fail; |
| 1351 | } |
| 1352 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1353 | status = PyWideStringList_Append(&config.module_search_paths, |
| 1354 | L"init_read_set_path"); |
| 1355 | if (PyStatus_Exception(status)) { |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 1356 | goto fail; |
| 1357 | } |
| 1358 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1359 | /* override executable computed by PyConfig_Read() */ |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 1360 | config_set_string(&config, &config.executable, L"my_executable"); |
| 1361 | init_from_config_clear(&config); |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 1362 | |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 1363 | dump_config(); |
| 1364 | Py_Finalize(); |
| 1365 | return 0; |
| 1366 | |
| 1367 | fail: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1368 | Py_ExitStatusException(status); |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1372 | static void configure_init_main(PyConfig *config) |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1373 | { |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 1374 | wchar_t* argv[] = { |
| 1375 | L"python3", L"-c", |
| 1376 | (L"import _testinternalcapi, json; " |
| 1377 | L"print(json.dumps(_testinternalcapi.get_configs()))"), |
| 1378 | L"arg2"}; |
| 1379 | |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 1380 | config->parse_argv = 1; |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 1381 | |
| 1382 | config_set_argv(config, Py_ARRAY_LENGTH(argv), argv); |
| 1383 | config_set_string(config, &config->program_name, L"./python3"); |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | |
| 1387 | static int test_init_run_main(void) |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 1388 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1389 | PyStatus status; |
| 1390 | PyConfig config; |
| 1391 | status = PyConfig_InitPythonConfig(&config); |
| 1392 | if (PyStatus_Exception(status)) { |
| 1393 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1394 | } |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1395 | configure_init_main(&config); |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 1396 | init_from_config_clear(&config); |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 1397 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1398 | return Py_RunMain(); |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1402 | static int test_init_main(void) |
| 1403 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1404 | PyStatus status; |
| 1405 | PyConfig config; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1406 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1407 | status = PyConfig_InitPythonConfig(&config); |
| 1408 | if (PyStatus_Exception(status)) { |
| 1409 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1410 | } |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1411 | configure_init_main(&config); |
| 1412 | config._init_main = 0; |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 1413 | init_from_config_clear(&config); |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1414 | |
| 1415 | /* sys.stdout don't exist yet: it is created by _Py_InitializeMain() */ |
| 1416 | int res = PyRun_SimpleString( |
| 1417 | "import sys; " |
| 1418 | "print('Run Python code before _Py_InitializeMain', " |
| 1419 | "file=sys.stderr)"); |
| 1420 | if (res < 0) { |
| 1421 | exit(1); |
| 1422 | } |
| 1423 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1424 | status = _Py_InitializeMain(); |
| 1425 | if (PyStatus_Exception(status)) { |
| 1426 | Py_ExitStatusException(status); |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1427 | } |
| 1428 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1429 | return Py_RunMain(); |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | |
| 1433 | static int test_run_main(void) |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 1434 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1435 | PyStatus status; |
| 1436 | PyConfig config; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1437 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1438 | status = PyConfig_InitPythonConfig(&config); |
| 1439 | if (PyStatus_Exception(status)) { |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 1440 | PyConfig_Clear(&config); |
| 1441 | Py_ExitStatusException(status); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1442 | } |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 1443 | |
| 1444 | wchar_t *argv[] = {L"python3", L"-c", |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1445 | (L"import sys; " |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1446 | L"print(f'Py_RunMain(): sys.argv={sys.argv}')"), |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 1447 | L"arg2"}; |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 1448 | config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv); |
| 1449 | config_set_string(&config, &config.program_name, L"./python3"); |
| 1450 | init_from_config_clear(&config); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1451 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1452 | return Py_RunMain(); |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 1456 | /* ********************************************************* |
| 1457 | * List of test cases and the function that implements it. |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 1458 | * |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 1459 | * Names are compared case-sensitively with the first |
| 1460 | * argument. If no match is found, or no first argument was |
| 1461 | * provided, the names of all test cases are printed and |
| 1462 | * the exit code will be -1. |
| 1463 | * |
| 1464 | * The int returned from test functions is used as the exit |
| 1465 | * code, and test_capi treats all non-zero exit codes as a |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 1466 | * failed test. |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 1467 | *********************************************************/ |
| 1468 | struct TestCase |
| 1469 | { |
| 1470 | const char *name; |
| 1471 | int (*func)(void); |
| 1472 | }; |
| 1473 | |
| 1474 | static struct TestCase TestCases[] = { |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 1475 | {"test_forced_io_encoding", test_forced_io_encoding}, |
| 1476 | {"test_repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters}, |
| 1477 | {"test_pre_initialization_api", test_pre_initialization_api}, |
| 1478 | {"test_pre_initialization_sys_options", test_pre_initialization_sys_options}, |
| 1479 | {"test_bpo20891", test_bpo20891}, |
| 1480 | {"test_initialize_twice", test_initialize_twice}, |
| 1481 | {"test_initialize_pymain", test_initialize_pymain}, |
| 1482 | {"test_init_initialize_config", test_init_initialize_config}, |
| 1483 | {"test_preinit_compat_config", test_preinit_compat_config}, |
| 1484 | {"test_init_compat_config", test_init_compat_config}, |
| 1485 | {"test_init_global_config", test_init_global_config}, |
| 1486 | {"test_init_from_config", test_init_from_config}, |
| 1487 | {"test_init_parse_argv", test_init_parse_argv}, |
| 1488 | {"test_init_dont_parse_argv", test_init_dont_parse_argv}, |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 1489 | {"test_init_compat_env", test_init_compat_env}, |
| 1490 | {"test_init_python_env", test_init_python_env}, |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 1491 | {"test_init_env_dev_mode", test_init_env_dev_mode}, |
| 1492 | {"test_init_env_dev_mode_alloc", test_init_env_dev_mode_alloc}, |
| 1493 | {"test_init_dont_configure_locale", test_init_dont_configure_locale}, |
| 1494 | {"test_init_dev_mode", test_init_dev_mode}, |
| 1495 | {"test_init_isolated_flag", test_init_isolated_flag}, |
| 1496 | {"test_preinit_isolated_config", test_preinit_isolated_config}, |
| 1497 | {"test_init_isolated_config", test_init_isolated_config}, |
| 1498 | {"test_preinit_python_config", test_preinit_python_config}, |
| 1499 | {"test_init_python_config", test_init_python_config}, |
| 1500 | {"test_preinit_isolated1", test_preinit_isolated1}, |
| 1501 | {"test_preinit_isolated2", test_preinit_isolated2}, |
| 1502 | {"test_preinit_parse_argv", test_preinit_parse_argv}, |
| 1503 | {"test_preinit_dont_parse_argv", test_preinit_dont_parse_argv}, |
| 1504 | {"test_init_read_set", test_init_read_set}, |
| 1505 | {"test_init_run_main", test_init_run_main}, |
| 1506 | {"test_init_main", test_init_main}, |
| 1507 | {"test_run_main", test_run_main}, |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1508 | {"test_open_code_hook", test_open_code_hook}, |
| 1509 | {"test_audit", test_audit}, |
| 1510 | {"test_audit_subinterpreter", test_audit_subinterpreter}, |
Miss Islington (bot) | 746992c | 2019-07-01 16:22:29 -0700 | [diff] [blame] | 1511 | {"test_audit_run_command", test_audit_run_command}, |
| 1512 | {"test_audit_run_file", test_audit_run_file}, |
| 1513 | {"test_audit_run_interactivehook", test_audit_run_interactivehook}, |
| 1514 | {"test_audit_run_startup", test_audit_run_startup}, |
| 1515 | {"test_audit_run_stdin", test_audit_run_stdin}, |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 1516 | {NULL, NULL} |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 1517 | }; |
| 1518 | |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 1519 | int main(int argc, char *argv[]) |
| 1520 | { |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 1521 | if (argc > 1) { |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 1522 | for (struct TestCase *tc = TestCases; tc && tc->name; tc++) { |
| 1523 | if (strcmp(argv[1], tc->name) == 0) |
| 1524 | return (*tc->func)(); |
| 1525 | } |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 1526 | } |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 1527 | |
| 1528 | /* No match found, or no test name provided, so display usage */ |
| 1529 | printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n" |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 1530 | "Normally executed via 'EmbeddingTests' in Lib/test/test_embed.py\n\n" |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 1531 | "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]); |
| 1532 | for (struct TestCase *tc = TestCases; tc && tc->name; tc++) { |
| 1533 | printf(" %s\n", tc->name); |
| 1534 | } |
| 1535 | |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 1536 | /* Non-zero exit code will cause test_embed.py tests to fail. |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 1537 | This is intentional. */ |
| 1538 | return -1; |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 1539 | } |