Unverified Kaydet (Commit) 9e87e777 authored tarafından Victor Stinner's avatar Victor Stinner Kaydeden (comit) GitHub

bpo-32096: Remove obj and mem from _PyRuntime (#4532)

bpo-32096, bpo-30860:  Partially revert the commit
2ebc5ce4:

* Move structures back from Include/internal/mem.h to
  Objects/obmalloc.c
* Remove _PyObject_Initialize() and _PyMem_Initialize()
* Remove Include/internal/pymalloc.h
* Add test_capi.test_pre_initialization_api():
   Make sure that it's possible to call Py_DecodeLocale(), and then call
   Py_SetProgramName() with the decoded string, before Py_Initialize().

PyMem_RawMalloc() and Py_DecodeLocale() can be called again before
_PyRuntimeState_Init().
Co-Authored-By: 's avatarEric Snow <ericsnowcurrently@gmail.com>
üst 4864a619
......@@ -7,54 +7,6 @@ extern "C" {
#include "objimpl.h"
#include "pymem.h"
#ifdef WITH_PYMALLOC
#include "internal/pymalloc.h"
#endif
/* Low-level memory runtime state */
struct _pymem_runtime_state {
struct _allocator_runtime_state {
PyMemAllocatorEx mem;
PyMemAllocatorEx obj;
PyMemAllocatorEx raw;
} allocators;
#ifdef WITH_PYMALLOC
/* Array of objects used to track chunks of memory (arenas). */
struct arena_object* arenas;
/* The head of the singly-linked, NULL-terminated list of available
arena_objects. */
struct arena_object* unused_arena_objects;
/* The head of the doubly-linked, NULL-terminated at each end,
list of arena_objects associated with arenas that have pools
available. */
struct arena_object* usable_arenas;
/* Number of slots currently allocated in the `arenas` vector. */
unsigned int maxarenas;
/* Number of arenas allocated that haven't been free()'d. */
size_t narenas_currently_allocated;
/* High water mark (max value ever seen) for
* narenas_currently_allocated. */
size_t narenas_highwater;
/* Total number of times malloc() called to allocate an arena. */
size_t ntimes_arena_allocated;
poolp usedpools[MAX_POOLS];
Py_ssize_t num_allocated_blocks;
#endif /* WITH_PYMALLOC */
size_t serialno; /* incremented on each debug {m,re}alloc */
};
PyAPI_FUNC(void) _PyMem_Initialize(struct _pymem_runtime_state *);
/* High-level memory runtime state */
struct _pyobj_runtime_state {
PyObjectArenaAllocator allocator_arenas;
};
PyAPI_FUNC(void) _PyObject_Initialize(struct _pyobj_runtime_state *);
/* GC runtime state */
......
This diff is collapsed.
......@@ -64,9 +64,7 @@ typedef struct pyruntimestate {
int nexitfuncs;
void (*pyexitfunc)(void);
struct _pyobj_runtime_state obj;
struct _gc_runtime_state gc;
struct _pymem_runtime_state mem;
struct _warnings_runtime_state warnings;
struct _ceval_runtime_state ceval;
struct _gilstate_runtime_state gilstate;
......
......@@ -593,6 +593,16 @@ class EmbeddingTests(unittest.TestCase):
self.maxDiff = None
self.assertEqual(out.strip(), expected_output)
def test_pre_initialization_api(self):
"""
Checks the few parts of the C-API that work before the runtine
is initialized (via Py_Initialize()).
"""
env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path))
out, err = self.run_embedded_interpreter("pre_initialization_api", env=env)
self.assertEqual(out, '')
self.assertEqual(err, '')
class SkipitemTest(unittest.TestCase):
......
......@@ -1015,7 +1015,6 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/ceval.h \
$(srcdir)/Include/internal/gil.h \
$(srcdir)/Include/internal/mem.h \
$(srcdir)/Include/internal/pymalloc.h \
$(srcdir)/Include/internal/pystate.h \
$(srcdir)/Include/internal/warnings.h \
$(DTRACE_HEADERS)
......
Revert memory allocator changes in the C API: move structures back from
_PyRuntime to Objects/obmalloc.c. The memory allocators are once again initialized
statically, and so PyMem_RawMalloc() and Py_DecodeLocale() can be
called before _PyRuntime_Initialize().
This diff is collapsed.
......@@ -116,7 +116,6 @@
<ClInclude Include="..\Include\internal\condvar.h" />
<ClInclude Include="..\Include\internal\gil.h" />
<ClInclude Include="..\Include\internal\mem.h" />
<ClInclude Include="..\Include\internal\pymalloc.h" />
<ClInclude Include="..\Include\internal\pystate.h" />
<ClInclude Include="..\Include\internal\warnings.h" />
<ClInclude Include="..\Include\intrcheck.h" />
......
......@@ -141,9 +141,6 @@
<ClInclude Include="..\Include\internal\mem.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pymalloc.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pystate.h">
<Filter>Include</Filter>
</ClInclude>
......@@ -1031,4 +1028,4 @@
<Filter>Resource Files</Filter>
</ResourceCompile>
</ItemGroup>
</Project>
\ No newline at end of file
</Project>
......@@ -60,8 +60,6 @@ main(int argc, char **argv)
filename = argv[1];
graminit_h = argv[2];
graminit_c = argv[3];
_PyObject_Initialize(&_PyRuntime.obj);
_PyMem_Initialize(&_PyRuntime.mem);
g = getgrammar(filename);
fp = fopen(graminit_c, "w");
if (fp == NULL) {
......
......@@ -125,6 +125,28 @@ static int test_forced_io_encoding(void)
return 0;
}
/*********************************************************
* Test parts of the C-API that work before initialization
*********************************************************/
static int test_pre_initialization_api(void)
{
wchar_t *program = Py_DecodeLocale("spam", NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode program name\n");
return 1;
}
Py_SetProgramName(program);
Py_Initialize();
Py_Finalize();
PyMem_RawFree(program);
return 0;
}
/* *********************************************************
* List of test cases and the function that implements it.
*
......@@ -146,6 +168,7 @@ struct TestCase
static struct TestCase TestCases[] = {
{ "forced_io_encoding", test_forced_io_encoding },
{ "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
{ "pre_initialization_api", test_pre_initialization_api },
{ NULL, NULL }
};
......
......@@ -40,8 +40,6 @@ _PyRuntimeState_Init(_PyRuntimeState *runtime)
{
memset(runtime, 0, sizeof(*runtime));
_PyObject_Initialize(&runtime->obj);
_PyMem_Initialize(&runtime->mem);
_PyGC_Initialize(&runtime->gc);
_PyEval_Initialize(&runtime->ceval);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment