bpo-32030: Split Py_Main() into subfunctions#4399
Conversation
|
Hum, test_capi fails on Windows. |
Sorry, something went wrong.
|
test_capi failed on the "gcc C" job of Travis CI: |
Sorry, something went wrong.
ncoghlan
left a comment
There was a problem hiding this comment.
Nice! This looks really good to me - just a couple of minor suggestions inline.
Sorry, something went wrong.
|
One general comment: while I didn't spot any particular instances of this going wrong, it's important to keep in mind that the various global flags (Py_IsolatedFlag, etc) are part of the embedding configuration API. This means that even as we add the more explicit configuration structs and use those to pass settings from Py_Main into the CPython runtime, we still need to account for other applications that are passing in the settings via the global variables instead (and either keep that model working, or else provide explicit porting guidance). |
Sorry, something went wrong.
|
The PR compiles correctly on macOS and the following tests passed: |
Sorry, something went wrong.
* Don't use "Python runtime" anymore to parse command line options or to get environment variables: pymain_init() is now a strict separation. * Use an error message rather than "crashing" directly with Py_FatalError(). Limit the number of calls to Py_FatalError(). It prepares the code to handle errors more nicely later. * Warnings options (-W, PYTHONWARNINGS) and "XOptions" (-X) are now only added to the sys module once Python core is properly initialized. * _PyMain is now the well identified owner of some important strings like: warnings options, XOptions, and the "program name". The program name string is now properly freed at exit. pymain_free() is now responsible to free the "command" string. * Rename most methods in Modules/main.c to use a "pymain_" prefix to avoid conflits and ease debug. * Replace _Py_CommandLineDetails_INIT with memset(0) * Reorder a lot of code to fix the initialization ordering. For example, initializing standard streams now comes before parsing PYTHONWARNINGS. * Py_Main() now handles errors when adding warnings options and XOptions. * Add _PyMem_GetDefaultRawAllocator() private function. * Cleanup _PyMem_Initialize(): remove useless global constants: move them into _PyMem_Initialize(). * Call _PyRuntime_Initialize() as soon as possible: _PyRuntime_Initialize() now returns an error message on failure. * Add _PyInitError structure and following macros: * _Py_INIT_OK() * _Py_INIT_ERR(msg) * _Py_INIT_USER_ERR(msg): "user" error, don't abort() in that case * _Py_INIT_FAILED(err)
Ok, I changed my code to set Py_xxx flags as soon as possible, and use Py_xxx flags rather than using pymain. |
Sorry, something went wrong.
and
Oh, I expect PyMem_RawMalloc() to be usable directly, but this changed recently: _PyRuntime_Initialize() must now be called before the first call to PyMem_RawMalloc(). I fixed that as well. |
Sorry, something went wrong.
Call _PyRuntime_Initialize() and copy core config before calling _PyMem_SetupAllocators().
Programs/python.c now uses internals/pystate.h which requires Py_BUILD_CORE.
|
Haven't looked closely, but this looks like overall goodness! If you want one extra case to keep in mind, see how I implemented my spython.c for my PEP 551 implementation. There's obviously no compatibility requirement, but I'd want to be able to keep doing the same things after this change. |
Sorry, something went wrong.
|
@zooba: "Haven't looked closely, but this looks like overall goodness!" Oh thanks. With @ncoghlan and you who know the topic, I'm now confident enough to merge my change (I already merged it ;-)). "If you want one extra case to keep in mind, see how I implemented my spython.c for my PEP 551 implementation. There's obviously no compatibility requirement, but I'd want to be able to keep doing the same things after this change." My change shouldn't break this code. @ncoghlan: would you mind to double-check? In the first version of this change, I overrided Py_xxx flags from the command lines, ignoring their previous value. @ncoghlan warned me about Py_xxx flags set when Python is embedded. So I fixed my code. |
Sorry, something went wrong.
* 'master' of https://github.com/python/cpython: (550 commits) bpo-31867: Remove duplicates in default mimetypes. (python#4388) tokenizer: Remove unused tabs options (python#4422) bpo-31691: Specify where to find build instructions for the Windows installer (python#4426) Fix typo in atexit documentation. (pythonGH-4419) bpo-31702: Allow to specify rounds for SHA-2 hashing in crypt.mksalt(). (python#4110) bpo-32043: New "developer mode": "-X dev" option (python#4413) bpo-30349: Raise FutureWarning for nested sets and set operations (python#1553) bpo-32037: Use the INT opcode for 32-bit integers in protocol 0 pickles. (python#4407) bpo-30143: 2to3 now generates a code that uses abstract collection classes (python#1262) bpo-32030: Enhance Py_Main() (python#4412) bpo-32030: Split Py_Main() into subfunctions (python#4399) bpo-32034: Make IncompleteReadError & LimitOverrunError pickleable python#4409 bpo-32025: Add time.thread_time() (python#4410) bpo-32018: Fix inspect.signature repr to follow PEP 8 (python#4408) bpo-30399: Get rid of trailing comma in the repr of BaseException. (python#1650) bpo-30950: Convert round() to Argument Clinic. (python#2740) bpo-32011: Revert "Issue python#15480: Remove the deprecated and unused TYPE_INT64 code from marshal." (python#4381) bpo-32023: Disallow genexprs without parenthesis in class definitions. (python#4400) bpo-31949: Fixed several issues in printing tracebacks (PyTraceBack_Print()). (python#4289) bpo-32032: Test both implementations of module-level pickle API. (python#4401) ...
to get environment variables: pymain_init() is now a strict
separation.
Py_FatalError(). Limit the number of calls to Py_FatalError(). It
prepares the code to handle errors more nicely later.
only added to the sys module once Python core is properly
initialized.
like: warnings options, XOptions, and the "program name". The
program name string is now properly freed at exit.
pymain_free() is now responsible to free the "command" string.
avoid conflits and ease debug.
example, initializing standard streams now comes before parsing
PYTHONWARNINGS.
Py_xxx global configuration variables. For example, replace
Py_UnbufferedStdioFlag with cmdline.use_unbuffered_io.
XOptions.
https://bugs.python.org/issue32030