bpo-32030: Fix usage of memory allocators (#4953) · python/cpython@31e9908 (original) (raw)

`@@ -649,11 +649,12 @@ pymain_free_raw(_PyMain *pymain)

`

649

649

` Py_Initialize()-Py_Finalize() can be called multiple times. */

`

650

650

`_PyPathConfig_Clear(&_Py_path_config);

`

651

651

``

``

652

`+

pymain_clear_config(pymain);

`

``

653

+

652

654

`/* Force the allocator used by pymain_read_conf() */

`

653

655

`PyMemAllocatorEx old_alloc;

`

654

656

`_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

655

657

``

656

``

`-

_PyCoreConfig_Clear(&pymain->config);

`

657

658

`pymain_clear_pymain(pymain);

`

658

659

``

659

660

`clear_wstrlist(orig_argc, orig_argv);

`

`@@ -1963,11 +1964,6 @@ pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)

`

1963

1964

`{

`

1964

1965

`int res = -1;

`

1965

1966

``

1966

``

`-

/* Force default allocator, since pymain_free() must use the same allocator

`

1967

``

`-

than this function. */

`

1968

``

`-

PyMemAllocatorEx old_alloc;

`

1969

``

`-

_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

1970

``

-

1971

1967

`char *oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));

`

1972

1968

`if (oldloc == NULL) {

`

1973

1969

`pymain->err = _Py_INIT_NO_MEMORY();

`

`@@ -2055,7 +2051,6 @@ pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)

`

2055

2051

`PyMem_RawFree(oldloc);

`

2056

2052

` }

`

2057

2053

``

2058

``

`-

PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

2059

2054

`return res;

`

2060

2055

`}

`

2061

2056

``

`@@ -2578,6 +2573,15 @@ pymain_cmdline_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)

`

2578

2573

`static int

`

2579

2574

`pymain_cmdline(_PyMain *pymain)

`

2580

2575

`{

`

``

2576

`+

/* Force default allocator, since pymain_free() and pymain_clear_config()

`

``

2577

`+

must use the same allocator than this function. */

`

``

2578

`+

PyMemAllocatorEx old_alloc;

`

``

2579

`+

_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

``

2580

`+

#ifdef Py_DEBUG

`

``

2581

`+

PyMemAllocatorEx default_alloc;

`

``

2582

`+

PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);

`

``

2583

`+

#endif

`

``

2584

+

2581

2585

`_Py_CommandLineDetails cmdline;

`

2582

2586

`memset(&cmdline, 0, sizeof(cmdline));

`

2583

2587

``

`@@ -2588,6 +2592,14 @@ pymain_cmdline(_PyMain *pymain)

`

2588

2592

`pymain_set_global_config(pymain, &cmdline);

`

2589

2593

``

2590

2594

`pymain_clear_cmdline(pymain, &cmdline);

`

``

2595

+

``

2596

`+

#ifdef Py_DEBUG

`

``

2597

`+

/* Make sure that PYMEM_DOMAIN_RAW has not been modified */

`

``

2598

`+

PyMemAllocatorEx cur_alloc;

`

``

2599

`+

PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);

`

``

2600

`+

assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);

`

``

2601

`+

#endif

`

``

2602

`+

PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

2591

2603

`return res;

`

2592

2604

`}

`

2593

2605

``