| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5efe2ee commit 57dd110
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -341,6 +341,12 @@ def test_finalize_structseq(self): | |||
| 341 | 341 | out, err = self.run_embedded_interpreter("test_repeated_init_exec", code) | |
| 342 | 342 | self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS) | |
| 343 | 343 | ||
| 344 | + def test_simple_initialization_api(self): | ||
| 345 | + # _testembed now uses Py_InitializeFromConfig by default | ||
| 346 | + # This case specifically checks Py_Initialize(Ex) still works | ||
| 347 | + out, err = self.run_embedded_interpreter("test_repeated_simple_init") | ||
| 348 | + self.assertEqual(out, 'Finalized\n' * INIT_LOOPS) | ||
| 349 | + | ||
| 344 | 350 | def test_quickened_static_code_gets_unquickened_at_Py_FINALIZE(self): | |
| 345 | 351 | # https://github.com/python/cpython/issues/92031 | |
| 346 | 352 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,4 @@ | |||
| 1 | + ``Py_InitializeEx`` now correctly calls ``PyConfig_Clear`` after initializing | ||
| 2 | + the interpreter (the omission didn't cause a memory leak only because none | ||
| 3 | + of the dynamically allocated config fields are populated by the wrapper | ||
| 4 | + function) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + Added explicit coverage of ``Py_Initialize`` (and hence ``Py_InitializeEx``) | ||
| 2 | + back to the embedding tests (all other embedding tests migrated to | ||
| 3 | + ``Py_InitializeFromConfig`` in Python 3.11) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,7 +22,7 @@ char **main_argv; | |||
| 22 | 22 | /********************************************************* | |
| 23 | 23 | * Embedded interpreter tests that need a custom exe | |
| 24 | 24 | * | |
| 25 | - * Executed via 'EmbeddingTests' in Lib/test/test_capi.py | ||
| 25 | + * Executed via Lib/test/test_embed.py | ||
| 26 | 26 | *********************************************************/ | |
| 27 | 27 | ||
| 28 | 28 | // Use to display the usage | |
@@ -73,14 +73,20 @@ static void init_from_config_clear(PyConfig *config) | |||
| 73 | 73 | } | |
| 74 | 74 | ||
| 75 | 75 | ||
| 76 | - static void _testembed_Py_Initialize(void) | ||
| 76 | + static void _testembed_Py_InitializeFromConfig(void) | ||
| 77 | 77 | { | |
| 78 | 78 | PyConfig config; | |
| 79 | 79 | _PyConfig_InitCompatConfig(&config); | |
| 80 | 80 | config_set_program_name(&config); | |
| 81 | 81 | init_from_config_clear(&config); | |
| 82 | 82 | } | |
| 83 | 83 | ||
| 84 | + static void _testembed_Py_Initialize(void) | ||
| 85 | + { | ||
| 86 | + Py_SetProgramName(PROGRAM_NAME); | ||
| 87 | + Py_Initialize(); | ||
| 88 | + } | ||
| 89 | + | ||
| 84 | 90 | ||
| 85 | 91 | /***************************************************** | |
| 86 | 92 | * Test repeated initialisation and subinterpreters | |
@@ -110,7 +116,7 @@ static int test_repeated_init_and_subinterpreters(void) | |||
| 110 | 116 | ||
| 111 | 117 | for (int i=1; i <= INIT_LOOPS; i++) { | |
| 112 | 118 | printf("--- Pass %d ---\n", i); | |
| 113 | - _testembed_Py_Initialize(); | ||
| 119 | + _testembed_Py_InitializeFromConfig(); | ||
| 114 | 120 | mainstate = PyThreadState_Get(); | |
| 115 | 121 | ||
| 116 | 122 | PyEval_ReleaseThread(mainstate); | |
@@ -168,7 +174,7 @@ static int test_repeated_init_exec(void) | |||
| 168 | 174 | fprintf(stderr, "--- Loop #%d ---\n", i); | |
| 169 | 175 | fflush(stderr); | |
| 170 | 176 | ||
| 171 | - _testembed_Py_Initialize(); | ||
| 177 | + _testembed_Py_InitializeFromConfig(); | ||
| 172 | 178 | int err = PyRun_SimpleString(code); | |
| 173 | 179 | Py_Finalize(); | |
| 174 | 180 | if (err) { | |
@@ -178,6 +184,23 @@ static int test_repeated_init_exec(void) | |||
| 178 | 184 | return 0; | |
| 179 | 185 | } | |
| 180 | 186 | ||
| 187 | + /**************************************************************************** | ||
| 188 | + * Test the Py_Initialize(Ex) convenience/compatibility wrappers | ||
| 189 | + ***************************************************************************/ | ||
| 190 | + // This is here to help ensure there are no wrapper resource leaks (gh-96853) | ||
| 191 | + static int test_repeated_simple_init(void) | ||
| 192 | + { | ||
| 193 | + for (int i=1; i <= INIT_LOOPS; i++) { | ||
| 194 | + fprintf(stderr, "--- Loop #%d ---\n", i); | ||
| 195 | + fflush(stderr); | ||
| 196 | + | ||
| 197 | + _testembed_Py_Initialize(); | ||
| 198 | + Py_Finalize(); | ||
| 199 | + printf("Finalized\n"); // Give test_embed some output to check | ||
| 200 | + } | ||
| 201 | + return 0; | ||
| 202 | + } | ||
| 203 | + | ||
| 181 | 204 | ||
| 182 | 205 | /***************************************************** | |
| 183 | 206 | * Test forcing a particular IO encoding | |
@@ -199,7 +222,7 @@ static void check_stdio_details(const char *encoding, const char * errors) | |||
| 199 | 222 | fflush(stdout); | |
| 200 | 223 | /* Force the given IO encoding */ | |
| 201 | 224 | Py_SetStandardStreamEncoding(encoding, errors); | |
| 202 | - _testembed_Py_Initialize(); | ||
| 225 | + _testembed_Py_InitializeFromConfig(); | ||
| 203 | 226 | PyRun_SimpleString( | |
| 204 | 227 | "import sys;" | |
| 205 | 228 | "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));" | |
@@ -308,7 +331,7 @@ static int test_pre_initialization_sys_options(void) | |||
| 308 | 331 | dynamic_xoption = NULL; | |
| 309 | 332 | ||
| 310 | 333 | _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n"); | |
| 311 | - _testembed_Py_Initialize(); | ||
| 334 | + _testembed_Py_InitializeFromConfig(); | ||
| 312 | 335 | _Py_EMBED_PREINIT_CHECK("Check sys module contents\n"); | |
| 313 | 336 | PyRun_SimpleString("import sys; " | |
| 314 | 337 | "print('sys.warnoptions:', sys.warnoptions); " | |
@@ -352,7 +375,7 @@ static int test_bpo20891(void) | |||
| 352 | 375 | return 1; | |
| 353 | 376 | } | |
| 354 | 377 | ||
| 355 | - _testembed_Py_Initialize(); | ||
| 378 | + _testembed_Py_InitializeFromConfig(); | ||
| 356 | 379 | ||
| 357 | 380 | unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock); | |
| 358 | 381 | if (thrd == PYTHREAD_INVALID_THREAD_ID) { | |
@@ -375,7 +398,7 @@ static int test_bpo20891(void) | |||
| 375 | 398 | ||
| 376 | 399 | static int test_initialize_twice(void) | |
| 377 | 400 | { | |
| 378 | - _testembed_Py_Initialize(); | ||
| 401 | + _testembed_Py_InitializeFromConfig(); | ||
| 379 | 402 | ||
| 380 | 403 | /* bpo-33932: Calling Py_Initialize() twice should do nothing | |
| 381 | 404 | * (and not crash!). */ | |
@@ -393,7 +416,7 @@ static int test_initialize_pymain(void) | |||
| 393 | 416 | L"print(f'Py_Main() after Py_Initialize: " | |
| 394 | 417 | L"sys.argv={sys.argv}')"), | |
| 395 | 418 | L"arg2"}; | |
| 396 | - _testembed_Py_Initialize(); | ||
| 419 | + _testembed_Py_InitializeFromConfig(); | ||
| 397 | 420 | ||
| 398 | 421 | /* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */ | |
| 399 | 422 | Py_Main(Py_ARRAY_LENGTH(argv), argv); | |
@@ -416,7 +439,7 @@ dump_config(void) | |||
| 416 | 439 | ||
| 417 | 440 | static int test_init_initialize_config(void) | |
| 418 | 441 | { | |
| 419 | - _testembed_Py_Initialize(); | ||
| 442 | + _testembed_Py_InitializeFromConfig(); | ||
| 420 | 443 | dump_config(); | |
| 421 | 444 | Py_Finalize(); | |
| 422 | 445 | return 0; | |
@@ -765,7 +788,7 @@ static int test_init_compat_env(void) | |||
| 765 | 788 | /* Test initialization from environment variables */ | |
| 766 | 789 | Py_IgnoreEnvironmentFlag = 0; | |
| 767 | 790 | set_all_env_vars(); | |
| 768 | - _testembed_Py_Initialize(); | ||
| 791 | + _testembed_Py_InitializeFromConfig(); | ||
| 769 | 792 | dump_config(); | |
| 770 | 793 | Py_Finalize(); | |
| 771 | 794 | return 0; | |
@@ -801,7 +824,7 @@ static int test_init_env_dev_mode(void) | |||
| 801 | 824 | /* Test initialization from environment variables */ | |
| 802 | 825 | Py_IgnoreEnvironmentFlag = 0; | |
| 803 | 826 | set_all_env_vars_dev_mode(); | |
| 804 | - _testembed_Py_Initialize(); | ||
| 827 | + _testembed_Py_InitializeFromConfig(); | ||
| 805 | 828 | dump_config(); | |
| 806 | 829 | Py_Finalize(); | |
| 807 | 830 | return 0; | |
@@ -814,7 +837,7 @@ static int test_init_env_dev_mode_alloc(void) | |||
| 814 | 837 | Py_IgnoreEnvironmentFlag = 0; | |
| 815 | 838 | set_all_env_vars_dev_mode(); | |
| 816 | 839 | putenv("PYTHONMALLOC=malloc"); | |
| 817 | - _testembed_Py_Initialize(); | ||
| 840 | + _testembed_Py_InitializeFromConfig(); | ||
| 818 | 841 | dump_config(); | |
| 819 | 842 | Py_Finalize(); | |
| 820 | 843 | return 0; | |
@@ -1154,7 +1177,7 @@ static int test_open_code_hook(void) | |||
| 1154 | 1177 | } | |
| 1155 | 1178 | ||
| 1156 | 1179 | Py_IgnoreEnvironmentFlag = 0; | |
| 1157 | - _testembed_Py_Initialize(); | ||
| 1180 | + _testembed_Py_InitializeFromConfig(); | ||
| 1158 | 1181 | result = 0; | |
| 1159 | 1182 | ||
| 1160 | 1183 | PyObject *r = PyFile_OpenCode("$$test-filename"); | |
@@ -1218,7 +1241,7 @@ static int _test_audit(Py_ssize_t setValue) | |||
| 1218 | 1241 | ||
| 1219 | 1242 | Py_IgnoreEnvironmentFlag = 0; | |
| 1220 | 1243 | PySys_AddAuditHook(_audit_hook, &sawSet); | |
| 1221 | - _testembed_Py_Initialize(); | ||
| 1244 | + _testembed_Py_InitializeFromConfig(); | ||
| 1222 | 1245 | ||
| 1223 | 1246 | if (PySys_Audit("_testembed.raise", NULL) == 0) { | |
| 1224 | 1247 | printf("No error raised"); | |
@@ -1274,7 +1297,7 @@ static int test_audit_subinterpreter(void) | |||
| 1274 | 1297 | { | |
| 1275 | 1298 | Py_IgnoreEnvironmentFlag = 0; | |
| 1276 | 1299 | PySys_AddAuditHook(_audit_subinterpreter_hook, NULL); | |
| 1277 | - _testembed_Py_Initialize(); | ||
| 1300 | + _testembed_Py_InitializeFromConfig(); | ||
| 1278 | 1301 | ||
| 1279 | 1302 | Py_NewInterpreter(); | |
| 1280 | 1303 | Py_NewInterpreter(); | |
@@ -1869,13 +1892,13 @@ static int test_unicode_id_init(void) | |||
| 1869 | 1892 | _Py_IDENTIFIER(test_unicode_id_init); | |
| 1870 | 1893 | ||
| 1871 | 1894 | // Initialize Python once without using the identifier | |
| 1872 | - _testembed_Py_Initialize(); | ||
| 1895 | + _testembed_Py_InitializeFromConfig(); | ||
| 1873 | 1896 | Py_Finalize(); | |
| 1874 | 1897 | ||
| 1875 | 1898 | // Now initialize Python multiple times and use the identifier. | |
| 1876 | 1899 | // The first _PyUnicode_FromId() call initializes the identifier index. | |
| 1877 | 1900 | for (int i=0; i<3; i++) { | |
| 1878 | - _testembed_Py_Initialize(); | ||
| 1901 | + _testembed_Py_InitializeFromConfig(); | ||
| 1879 | 1902 | ||
| 1880 | 1903 | PyObject *str1, *str2; | |
| 1881 | 1904 | ||
@@ -2007,7 +2030,7 @@ unwrap_allocator(PyMemAllocatorEx *allocator) | |||
| 2007 | 2030 | static int | |
| 2008 | 2031 | test_get_incomplete_frame(void) | |
| 2009 | 2032 | { | |
| 2010 | - _testembed_Py_Initialize(); | ||
| 2033 | + _testembed_Py_InitializeFromConfig(); | ||
| 2011 | 2034 | PyMemAllocatorEx allocator; | |
| 2012 | 2035 | wrap_allocator(&allocator); | |
| 2013 | 2036 | // Force an allocation with an incomplete (generator) frame: | |
@@ -2039,6 +2062,7 @@ struct TestCase | |||
| 2039 | 2062 | static struct TestCase TestCases[] = { | |
| 2040 | 2063 | // Python initialization | |
| 2041 | 2064 | {"test_repeated_init_exec", test_repeated_init_exec}, | |
| 2065 | + {"test_repeated_simple_init", test_repeated_simple_init}, | ||
| 2042 | 2066 | {"test_forced_io_encoding", test_forced_io_encoding}, | |
| 2043 | 2067 | {"test_repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters}, | |
| 2044 | 2068 | {"test_repeated_init_and_inittab", test_repeated_init_and_inittab}, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1289,6 +1289,7 @@ Py_InitializeEx(int install_sigs) | |||
| 1289 | 1289 | config.install_signal_handlers = install_sigs; | |
| 1290 | 1290 | ||
| 1291 | 1291 | status = Py_InitializeFromConfig(&config); | |
| 1292 | + PyConfig_Clear(&config); | ||
| 1292 | 1293 | if (_PyStatus_EXCEPTION(status)) { | |
| 1293 | 1294 | Py_ExitStatusException(status); | |
| 1294 | 1295 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments