bpo-36142: Add _PyMem_GetDebugAllocatorsName() (GH-12185) · python/cpython@a9df651

5 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -155,6 +155,8 @@ PyAPI_FUNC(int) _PyMem_SetDefaultAllocator(

155155

PyMemAllocatorDomain domain,

156156

PyMemAllocatorEx *old_alloc);

157157
158+

PyAPI_FUNC(const char*) _PyMem_GetDebugAllocatorsName(void);

159+
158160

#ifdef __cplusplus

159161

}

160162

#endif

Original file line numberDiff line numberDiff line change

@@ -336,6 +336,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):

336336

'legacy_windows_fs_encoding': 0,

337337

'legacy_windows_stdio': 0,

338338

})

339+

DEBUG_ALLOCATOR = 'pymalloc_debug' if support.with_pymalloc() else 'malloc_debug'

339340
340341

# main config

341342

COPY_MAIN_CONFIG = (

@@ -588,15 +589,15 @@ def test_init_env(self):

588589
589590

def test_init_env_dev_mode(self):

590591

config = dict(self.INIT_ENV_CONFIG,

591-

allocator='debug',

592+

allocator=self.DEBUG_ALLOCATOR,

592593

dev_mode=1)

593594

self.check_config("init_env_dev_mode", config)

594595
595596

def test_init_dev_mode(self):

596597

config = {

597598

'dev_mode': 1,

598599

'faulthandler': 1,

599-

'allocator': 'debug',

600+

'allocator': self.DEBUG_ALLOCATOR,

600601

}

601602

self.check_config("init_dev_mode", config)

602603
Original file line numberDiff line numberDiff line change

@@ -221,6 +221,20 @@ static PyMemAllocatorEx _PyObject = PYOBJ_ALLOC;

221221

#endif

222222
223223
224+

/* Get the effective name of "debug" memory allocators,

225+

as if _PyMem_GetAllocatorsName() is called after

226+

_PyMem_SetupAllocators("debug"). */

227+

const char*

228+

_PyMem_GetDebugAllocatorsName(void)

229+

{

230+

#ifdef WITH_PYMALLOC

231+

return "pymalloc_debug";

232+

#else

233+

return "malloc_debug";

234+

#endif

235+

}

236+
237+
224238

static int

225239

pymem_set_default_allocator(PyMemAllocatorDomain domain, int debug,

226240

PyMemAllocatorEx *old_alloc)

Original file line numberDiff line numberDiff line change

@@ -139,6 +139,9 @@ static int test_forced_io_encoding(void)

139139
140140

static int test_pre_initialization_api(void)

141141

{

142+

/* the test doesn't support custom memory allocators */

143+

putenv("PYTHONMALLOC=");

144+
142145

/* Leading "./" ensures getpath.c can still find the standard library */

143146

_Py_EMBED_PREINIT_CHECK("Checking Py_DecodeLocale\n");

144147

wchar_t *program = Py_DecodeLocale("./spam", NULL);

@@ -235,6 +238,9 @@ static void bpo20891_thread(void *lockp)

235238
236239

static int test_bpo20891(void)

237240

{

241+

/* the test doesn't support custom memory allocators */

242+

putenv("PYTHONMALLOC=");

243+
238244

/* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before

239245

calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must

240246

call PyEval_InitThreads() for us in this case. */

Original file line numberDiff line numberDiff line change

@@ -453,7 +453,8 @@ preconfig_read(_PyPreConfig *config, const _PyPreCmdline *cmdline)

453453
454454

/* allocator */

455455

if (config->dev_mode && config->allocator == NULL) {

456-

config->allocator = _PyMem_RawStrdup("debug");

456+

const char *allocator = _PyMem_GetDebugAllocatorsName();

457+

config->allocator = _PyMem_RawStrdup(allocator);

457458

if (config->allocator == NULL) {

458459

return _Py_INIT_NO_MEMORY();

459460

}