99#include "pycore_pyerrors.h" // _PyErr_SetString()
1010#include "pycore_pyhash.h" // _Py_KeyedHash()
1111#include "pycore_pylifecycle.h"
12- #include "pycore_pymem.h" // _PyMem_SetDefaultAllocator ()
12+ #include "pycore_pymem.h" // _PyMem_DefaultRawFree ()
1313#include "pycore_pystate.h" // _PyInterpreterState_GET()
1414#include "pycore_sysmodule.h" // _PySys_Audit()
1515#include "marshal.h" // PyMarshal_ReadObjectFromString()
@@ -228,30 +228,23 @@ _PyImport_Init(void)
228228 if (_PyRuntime .imports .inittab != NULL ) {
229229 return _PyStatus_ERR ("global import state already initialized" );
230230 }
231- PyStatus status = _PyStatus_OK ();
232231
233232 size_t size ;
234233 for (size = 0 ; PyImport_Inittab [size ].name != NULL ; size ++ )
235234 ;
236235 size ++ ;
237236
238- /* Force default raw memory allocator to get a known allocator to be able
237+ /* Use default raw memory allocator to get a known allocator to be able
239238 to release the memory in _PyImport_Fini() */
240- PyMemAllocatorEx old_alloc ;
241- _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
242239
243240 /* Make the copy. */
244- struct _inittab * copied = PyMem_RawMalloc (size * sizeof (struct _inittab ));
241+ struct _inittab * copied = _PyMem_DefaultRawMalloc (size * sizeof (struct _inittab ));
245242 if (copied == NULL ) {
246- status = PyStatus_NoMemory ();
247- goto done ;
243+ return PyStatus_NoMemory ();
248244 }
249245 memcpy (copied , PyImport_Inittab , size * sizeof (struct _inittab ));
250246 _PyRuntime .imports .inittab = copied ;
251-
252- done :
253- PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
254- return status ;
247+ return _PyStatus_OK ();
255248}
256249
257250static inline void _extensions_cache_clear (void );
@@ -266,32 +259,22 @@ _PyImport_Fini(void)
266259 }
267260
268261 /* Use the same memory allocator as _PyImport_Init(). */
269- PyMemAllocatorEx old_alloc ;
270- _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
271-
272262 /* Free memory allocated by _PyImport_Init() */
273263 struct _inittab * inittab = _PyRuntime .imports .inittab ;
274264 _PyRuntime .imports .inittab = NULL ;
275- PyMem_RawFree (inittab );
276-
277- PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
265+ _PyMem_DefaultRawFree (inittab );
278266}
279267
280268void
281269_PyImport_Fini2 (void )
282270{
283271 /* Use the same memory allocator than PyImport_ExtendInittab(). */
284- PyMemAllocatorEx old_alloc ;
285- _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
286-
287272 // Reset PyImport_Inittab
288273 PyImport_Inittab = _PyImport_Inittab ;
289274
290275 /* Free memory allocated by PyImport_ExtendInittab() */
291- PyMem_RawFree (inittab_copy );
276+ _PyMem_DefaultRawFree (inittab_copy );
292277 inittab_copy = NULL ;
293-
294- PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
295278}
296279
297280/* Helper for sys */
@@ -2657,14 +2640,11 @@ PyImport_ExtendInittab(struct _inittab *newtab)
26572640
26582641 /* Force default raw memory allocator to get a known allocator to be able
26592642 to release the memory in _PyImport_Fini2() */
2660- PyMemAllocatorEx old_alloc ;
2661- _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
2662-
26632643 /* Allocate new memory for the combined table */
26642644 p = NULL ;
26652645 if (i + n <= SIZE_MAX / sizeof (struct _inittab ) - 1 ) {
26662646 size_t size = sizeof (struct _inittab ) * (i + n + 1 );
2667- p = PyMem_RawRealloc (inittab_copy , size );
2647+ p = _PyMem_DefaultRawRealloc (inittab_copy , size );
26682648 }
26692649 if (p == NULL ) {
26702650 res = -1 ;
@@ -2678,9 +2658,7 @@ PyImport_ExtendInittab(struct _inittab *newtab)
26782658 }
26792659 memcpy (p + i , newtab , (n + 1 ) * sizeof (struct _inittab ));
26802660 PyImport_Inittab = inittab_copy = p ;
2681-
26822661done :
2683- PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
26842662 return res ;
26852663}
26862664
0 commit comments