22#include "pycore_fileutils.h" // _Py_write_noraise()
33#include "pycore_gc.h" // PyGC_Head
44#include "pycore_hashtable.h" // _Py_hashtable_t
5+ #include "pycore_initconfig.h" // _PyStatus_NO_MEMORY()
56#include "pycore_object.h" // _PyType_PreHeaderSize()
67#include "pycore_pymem.h" // _Py_tracemalloc_config
78#include "pycore_runtime.h" // _Py_ID()
@@ -772,37 +773,24 @@ tracemalloc_clear_traces_unlocked(void)
772773}
773774
774775
775- int
776+ PyStatus
776777_PyTraceMalloc_Init (void )
777778{
778- if (tracemalloc_config .initialized == TRACEMALLOC_FINALIZED ) {
779- PyErr_SetString (PyExc_RuntimeError ,
780- "the tracemalloc module has been unloaded" );
781- return -1 ;
782- }
783-
784- if (tracemalloc_config .initialized == TRACEMALLOC_INITIALIZED )
785- return 0 ;
779+ assert (tracemalloc_config .initialized == TRACEMALLOC_NOT_INITIALIZED );
786780
787781 PyMem_GetAllocator (PYMEM_DOMAIN_RAW , & allocators .raw );
788782
789783#ifdef REENTRANT_THREADLOCAL
790784 if (PyThread_tss_create (& tracemalloc_reentrant_key ) != 0 ) {
791- #ifdef MS_WINDOWS
792- PyErr_SetFromWindowsErr (0 );
793- #else
794- PyErr_SetFromErrno (PyExc_OSError );
795- #endif
796- return -1 ;
785+ return _PyStatus_NO_MEMORY ();
797786 }
798787#endif
799788
800789#if defined(TRACE_RAW_MALLOC )
801790 if (tables_lock == NULL ) {
802791 tables_lock = PyThread_allocate_lock ();
803792 if (tables_lock == NULL ) {
804- PyErr_SetString (PyExc_RuntimeError , "cannot allocate lock" );
805- return -1 ;
793+ return _PyStatus_NO_MEMORY ();
806794 }
807795 }
808796#endif
@@ -819,9 +807,9 @@ _PyTraceMalloc_Init(void)
819807 tracemalloc_domains = tracemalloc_create_domains_table ();
820808
821809 if (tracemalloc_filenames == NULL || tracemalloc_tracebacks == NULL
822- || tracemalloc_traces == NULL || tracemalloc_domains == NULL ) {
823- PyErr_NoMemory ();
824- return -1 ;
810+ || tracemalloc_traces == NULL || tracemalloc_domains == NULL )
811+ {
812+ return _PyStatus_NO_MEMORY () ;
825813 }
826814
827815 tracemalloc_empty_traceback .nframe = 1 ;
@@ -832,7 +820,7 @@ _PyTraceMalloc_Init(void)
832820 tracemalloc_empty_traceback .hash = traceback_hash (& tracemalloc_empty_traceback );
833821
834822 tracemalloc_config .initialized = TRACEMALLOC_INITIALIZED ;
835- return 0 ;
823+ return _PyStatus_OK () ;
836824}
837825
838826
@@ -874,10 +862,6 @@ _PyTraceMalloc_Start(int max_nframe)
874862 return -1 ;
875863 }
876864
877- if (_PyTraceMalloc_Init () < 0 ) {
878- return -1 ;
879- }
880-
881865 if (_PyTraceMalloc_IsTracing ()) {
882866 /* hooks already installed: do nothing */
883867 return 0 ;
0 commit comments