Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/internal/pycore_pymem.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ PyAPI_FUNC(int) _PyMem_SetDefaultAllocator(
PyMemAllocatorDomain domain,
PyMemAllocatorEx *old_alloc);

PyAPI_FUNC(const char*) _PyMem_GetDebugAllocatorsName(void);

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'legacy_windows_fs_encoding': 0,
'legacy_windows_stdio': 0,
})
DEBUG_ALLOCATOR = 'pymalloc_debug' if support.with_pymalloc() else 'malloc_debug'

# main config
COPY_MAIN_CONFIG = (
Expand Down Expand Up @@ -588,15 +589,15 @@ def test_init_env(self):

def test_init_env_dev_mode(self):
config = dict(self.INIT_ENV_CONFIG,
allocator='debug',
allocator=self.DEBUG_ALLOCATOR,
dev_mode=1)
self.check_config("init_env_dev_mode", config)

def test_init_dev_mode(self):
config = {
'dev_mode': 1,
'faulthandler': 1,
'allocator': 'debug',
'allocator': self.DEBUG_ALLOCATOR,
}
self.check_config("init_dev_mode", config)

Expand Down
14 changes: 14 additions & 0 deletions Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ static PyMemAllocatorEx _PyObject = PYOBJ_ALLOC;
#endif


/* Get the effective name of "debug" memory allocators,
as if _PyMem_GetAllocatorsName() is called after
_PyMem_SetupAllocators("debug"). */
const char*
_PyMem_GetDebugAllocatorsName(void)
{
#ifdef WITH_PYMALLOC
return "pymalloc_debug";
#else
return "malloc_debug";
#endif
}


static int
pymem_set_default_allocator(PyMemAllocatorDomain domain, int debug,
PyMemAllocatorEx *old_alloc)
Expand Down
6 changes: 6 additions & 0 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ static int test_forced_io_encoding(void)

static int test_pre_initialization_api(void)
{
/* the test doesn't support custom memory allocators */
putenv("PYTHONMALLOC=");

/* Leading "./" ensures getpath.c can still find the standard library */
_Py_EMBED_PREINIT_CHECK("Checking Py_DecodeLocale\n");
wchar_t *program = Py_DecodeLocale("./spam", NULL);
Expand Down Expand Up @@ -235,6 +238,9 @@ static void bpo20891_thread(void *lockp)

static int test_bpo20891(void)
{
/* the test doesn't support custom memory allocators */
putenv("PYTHONMALLOC=");

/* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
call PyEval_InitThreads() for us in this case. */
Expand Down
3 changes: 2 additions & 1 deletion Python/preconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ preconfig_read(_PyPreConfig *config, const _PyPreCmdline *cmdline)

/* allocator */
if (config->dev_mode && config->allocator == NULL) {
config->allocator = _PyMem_RawStrdup("debug");
const char *allocator = _PyMem_GetDebugAllocatorsName();
config->allocator = _PyMem_RawStrdup(allocator);
if (config->allocator == NULL) {
return _Py_INIT_NO_MEMORY();
}
Expand Down