Skip to content

Commit 867f20b

Browse files
committed
Call _PyTraceMalloc_Init() earlier in Python init
1 parent a415ecb commit 867f20b

File tree

4 files changed

+22
-37
lines changed

4 files changed

+22
-37
lines changed

Include/internal/pycore_tracemalloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ extern PyObject* _PyTraceMalloc_GetTraces(void);
144144
extern PyObject* _PyTraceMalloc_GetObjectTraceback(PyObject *obj);
145145

146146
/* Initialize tracemalloc */
147-
extern int _PyTraceMalloc_Init(void);
147+
extern PyStatus _PyTraceMalloc_Init(void);
148148

149149
/* Start tracemalloc */
150150
extern int _PyTraceMalloc_Start(int max_nframe);

Modules/_tracemalloc.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,14 @@ static struct PyModuleDef module_def = {
215215
PyMODINIT_FUNC
216216
PyInit__tracemalloc(void)
217217
{
218-
PyObject *m;
219-
m = PyModule_Create(&module_def);
220-
if (m == NULL)
218+
PyObject *mod = PyModule_Create(&module_def);
219+
if (mod == NULL) {
221220
return NULL;
221+
}
222+
222223
#ifdef Py_GIL_DISABLED
223-
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
224+
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
224225
#endif
225226

226-
if (_PyTraceMalloc_Init() < 0) {
227-
Py_DECREF(m);
228-
return NULL;
229-
}
230-
231-
return m;
227+
return mod;
232228
}

Python/pylifecycle.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,12 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
707707
// the settings are loaded (so that feature_flags are set) but before
708708
// any calls are made to obmalloc functions.
709709
if (_PyMem_init_obmalloc(interp) < 0) {
710-
return _PyStatus_NO_MEMORY();
710+
return _PyStatus_NO_MEMORY();
711+
}
712+
713+
status = _PyTraceMalloc_Init();
714+
if (_PyStatus_EXCEPTION(status)) {
715+
return status;
711716
}
712717

713718
PyThreadState *tstate = _PyThreadState_New(interp,

Python/tracemalloc.c

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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

Comments
 (0)