bpo-32030: Split Py_Main() into subfunctions (#4399)

* 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)
This commit is contained in:
Victor Stinner 2017-11-15 15:48:08 -08:00 committed by GitHub
parent 43605e6bfa
commit f7e5b56c37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 1346 additions and 657 deletions

View file

@ -182,31 +182,17 @@ static struct {
#define _PyMem_Raw _PyRuntime.mem.allocators.raw
static const PyMemAllocatorEx _pymem_raw = {
#ifdef Py_DEBUG
&_PyMem_Debug.raw, PYRAWDBG_FUNCS
#else
NULL, PYRAW_FUNCS
#endif
};
#define _PyMem _PyRuntime.mem.allocators.mem
static const PyMemAllocatorEx _pymem = {
#ifdef Py_DEBUG
&_PyMem_Debug.mem, PYDBG_FUNCS
#else
NULL, PYMEM_FUNCS
#endif
};
#define _PyObject _PyRuntime.mem.allocators.obj
static const PyMemAllocatorEx _pyobject = {
#ifdef Py_DEBUG
&_PyMem_Debug.obj, PYDBG_FUNCS
#else
NULL, PYOBJ_FUNCS
#endif
};
void
_PyMem_GetDefaultRawAllocator(PyMemAllocatorEx *alloc_p)
{
PyMemAllocatorEx alloc = {NULL, PYRAW_FUNCS};
*alloc_p = alloc;
}
int
_PyMem_SetupAllocators(const char *opt)
@ -267,34 +253,52 @@ _PyMem_SetupAllocators(const char *opt)
return 0;
}
#undef PYRAW_FUNCS
#undef PYMEM_FUNCS
#undef PYOBJ_FUNCS
#undef PYRAWDBG_FUNCS
#undef PYDBG_FUNCS
static const PyObjectArenaAllocator _PyObject_Arena = {NULL,
#ifdef MS_WINDOWS
_PyObject_ArenaVirtualAlloc, _PyObject_ArenaVirtualFree
#elif defined(ARENAS_USE_MMAP)
_PyObject_ArenaMmap, _PyObject_ArenaMunmap
#else
_PyObject_ArenaMalloc, _PyObject_ArenaFree
#endif
};
void
_PyObject_Initialize(struct _pyobj_runtime_state *state)
{
PyObjectArenaAllocator _PyObject_Arena = {NULL,
#ifdef MS_WINDOWS
_PyObject_ArenaVirtualAlloc, _PyObject_ArenaVirtualFree
#elif defined(ARENAS_USE_MMAP)
_PyObject_ArenaMmap, _PyObject_ArenaMunmap
#else
_PyObject_ArenaMalloc, _PyObject_ArenaFree
#endif
};
state->allocator_arenas = _PyObject_Arena;
}
void
_PyMem_Initialize(struct _pymem_runtime_state *state)
{
state->allocators.raw = _pymem_raw;
state->allocators.mem = _pymem;
state->allocators.obj = _pyobject;
PyMemAllocatorEx pymem_raw = {
#ifdef Py_DEBUG
&_PyMem_Debug.raw, PYRAWDBG_FUNCS
#else
NULL, PYRAW_FUNCS
#endif
};
PyMemAllocatorEx pymem = {
#ifdef Py_DEBUG
&_PyMem_Debug.mem, PYDBG_FUNCS
#else
NULL, PYMEM_FUNCS
#endif
};
PyMemAllocatorEx pyobject = {
#ifdef Py_DEBUG
&_PyMem_Debug.obj, PYDBG_FUNCS
#else
NULL, PYOBJ_FUNCS
#endif
};
state->allocators.raw = pymem_raw;
state->allocators.mem = pymem;
state->allocators.obj = pyobject;
#ifdef WITH_PYMALLOC
Py_BUILD_ASSERT(NB_SMALL_SIZE_CLASSES == 64);
@ -311,6 +315,7 @@ _PyMem_Initialize(struct _pymem_runtime_state *state)
#endif /* WITH_PYMALLOC */
}
#ifdef WITH_PYMALLOC
static int
_PyMem_DebugEnabled(void)