Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f9a7115

Browse files
committed
Issue #26588: remove debug traces from _tracemalloc.
1 parent 92c21d7 commit f9a7115

5 files changed

Lines changed: 5 additions & 91 deletions

File tree

Include/pymem.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ PyAPI_FUNC(int) _PyTraceMalloc_Untrack(
5959
PyAPI_FUNC(PyObject*) _PyTraceMalloc_GetTraceback(
6060
_PyTraceMalloc_domain_t domain,
6161
Py_uintptr_t ptr);
62-
63-
PyAPI_DATA(int) tracemalloc_debug;
6462
#endif /* !Py_LIMITED_API */
6563

6664

Lib/test/test_tracemalloc.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,13 @@ def traceback_filename(filename):
8888

8989
class TestTracemallocEnabled(unittest.TestCase):
9090
def setUp(self):
91-
if _testcapi:
92-
_testcapi.tracemalloc_set_debug(True)
93-
9491
if tracemalloc.is_tracing():
9592
self.skipTest("tracemalloc must be stopped before the test")
9693

9794
tracemalloc.start(1)
9895

9996
def tearDown(self):
10097
tracemalloc.stop()
101-
if _testcapi:
102-
_testcapi.tracemalloc_set_debug(False)
10398

10499
def test_get_tracemalloc_memory(self):
105100
data = [allocate_bytes(123) for count in range(1000)]
@@ -882,9 +877,6 @@ class TestCAPI(unittest.TestCase):
882877
maxDiff = 80 * 20
883878

884879
def setUp(self):
885-
if _testcapi:
886-
_testcapi.tracemalloc_set_debug(True)
887-
888880
if tracemalloc.is_tracing():
889881
self.skipTest("tracemalloc must be stopped before the test")
890882

@@ -898,8 +890,6 @@ def setUp(self):
898890

899891
def tearDown(self):
900892
tracemalloc.stop()
901-
if _testcapi:
902-
_testcapi.tracemalloc_set_debug(False)
903893

904894
def get_traceback(self):
905895
frames = _testcapi.tracemalloc_get_traceback(self.domain, self.ptr)

Lib/test/test_warnings/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,6 @@ class CWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
772772
class PyWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
773773
module = py_warnings
774774

775-
@unittest.skipIf(True, "FIXME: Issue #26588")
776775
def test_tracemalloc(self):
777776
self.addCleanup(support.unlink, support.TESTFN)
778777

Modules/_testcapimodule.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3747,19 +3747,6 @@ tracemalloc_get_traceback(PyObject *self, PyObject *args)
37473747
return _PyTraceMalloc_GetTraceback(domain, (Py_uintptr_t)ptr);
37483748
}
37493749

3750-
PyObject*
3751-
tracemalloc_set_debug(PyObject *self, PyObject *args)
3752-
{
3753-
int debug;
3754-
extern int tracemalloc_debug;
3755-
3756-
if (!PyArg_ParseTuple(args, "i", &debug))
3757-
return NULL;
3758-
3759-
tracemalloc_debug = debug;
3760-
Py_RETURN_NONE;
3761-
}
3762-
37633750

37643751
static PyMethodDef TestMethods[] = {
37653752
{"raise_exception", raise_exception, METH_VARARGS},
@@ -3949,7 +3936,6 @@ static PyMethodDef TestMethods[] = {
39493936
{"tracemalloc_track", tracemalloc_track, METH_VARARGS},
39503937
{"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS},
39513938
{"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS},
3952-
{"tracemalloc_set_debug", tracemalloc_set_debug, METH_VARARGS},
39533939
{NULL, NULL} /* sentinel */
39543940
};
39553941

Modules/_tracemalloc.c

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ static struct {
4545
int use_domain;
4646
} tracemalloc_config = {TRACEMALLOC_NOT_INITIALIZED, 0, 1, 1};
4747

48-
int tracemalloc_debug = 0;
49-
5048
#if defined(TRACE_RAW_MALLOC) && defined(WITH_THREAD)
5149
/* This lock is needed because tracemalloc_free() is called without
5250
the GIL held from PyMem_RawFree(). It cannot acquire the lock because it
@@ -898,35 +896,23 @@ tracemalloc_clear_traces(void)
898896
_Py_hashtable_clear(tracemalloc_filenames);
899897
}
900898

901-
#define DEBUG(MSG) \
902-
if (tracemalloc_debug) { fprintf(stderr, "[pid %li, tid %li, reentrant key %i] " MSG "\n", (long)getpid(), PyThread_get_thread_ident(), tracemalloc_reentrant_key); fflush(stderr); }
903-
904899

905900
static int
906901
tracemalloc_init(void)
907902
{
908-
DEBUG("tracemalloc_init()");
909-
910-
#ifdef WITH_THREAD
911-
assert(PyGILState_Check());
912-
#endif
913-
914903
if (tracemalloc_config.initialized == TRACEMALLOC_FINALIZED) {
915904
PyErr_SetString(PyExc_RuntimeError,
916905
"the tracemalloc module has been unloaded");
917906
return -1;
918907
}
919908

920-
if (tracemalloc_config.initialized == TRACEMALLOC_INITIALIZED) {
921-
DEBUG("tracemalloc_init(): exit (already initialized)");
909+
if (tracemalloc_config.initialized == TRACEMALLOC_INITIALIZED)
922910
return 0;
923-
}
924911

925912
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
926913

927914
#ifdef REENTRANT_THREADLOCAL
928915
tracemalloc_reentrant_key = PyThread_create_key();
929-
fprintf(stderr, "[pid %li, tid %li] PyThread_create_key() -> %i\n", (long)getpid(), PyThread_get_thread_ident(), tracemalloc_reentrant_key); fflush(stderr);
930916
if (tracemalloc_reentrant_key == -1) {
931917
#ifdef MS_WINDOWS
932918
PyErr_SetFromWindowsErr(0);
@@ -988,24 +974,18 @@ fprintf(stderr, "[pid %li, tid %li] PyThread_create_key() -> %i\n", (long)getpid
988974
/* Disable tracing allocations until hooks are installed. Set
989975
also the reentrant flag to detect bugs: fail with an assertion error
990976
if set_reentrant(1) is called while tracing is disabled. */
991-
DEBUG("tracemalloc_init(): set_reentrant(1)");
992977
set_reentrant(1);
993978

994979
tracemalloc_config.initialized = TRACEMALLOC_INITIALIZED;
995-
DEBUG("tracemalloc_init(): done");
996-
assert(get_reentrant());
997980
return 0;
998981
}
999982

1000983

1001984
static void
1002985
tracemalloc_deinit(void)
1003986
{
1004-
DEBUG("tracemalloc_deinit()");
1005-
if (tracemalloc_config.initialized != TRACEMALLOC_INITIALIZED) {
1006-
DEBUG("tracemalloc_deinit(): exit (not initialized)");
987+
if (tracemalloc_config.initialized != TRACEMALLOC_INITIALIZED)
1007988
return;
1008-
}
1009989
tracemalloc_config.initialized = TRACEMALLOC_FINALIZED;
1010990

1011991
tracemalloc_stop();
@@ -1022,15 +1002,12 @@ DEBUG("tracemalloc_deinit(): exit (not initialized)");
10221002
}
10231003
#endif
10241004

1025-
DEBUG("tracemalloc_deinit(): delete reentrant key");
10261005
#ifdef REENTRANT_THREADLOCAL
1027-
fprintf(stderr, "[pid %li, tid %li] PyThread_delete_key(%i)\n", (long)getpid(), PyThread_get_thread_ident(), tracemalloc_reentrant_key); fflush(stderr);
10281006
PyThread_delete_key(tracemalloc_reentrant_key);
10291007
tracemalloc_reentrant_key = -1;
10301008
#endif
10311009

10321010
Py_XDECREF(unknown_filename);
1033-
DEBUG("tracemalloc_deinit(): done");
10341011
}
10351012

10361013

@@ -1040,24 +1017,13 @@ tracemalloc_start(int max_nframe)
10401017
PyMemAllocatorEx alloc;
10411018
size_t size;
10421019

1043-
DEBUG("tracemalloc_start()");
1044-
1045-
#ifdef WITH_THREAD
1046-
assert(PyGILState_Check());
1047-
#endif
1048-
1049-
if (tracemalloc_init() < 0) {
1050-
DEBUG("tracemalloc_start(): ERROR! init failed!");
1020+
if (tracemalloc_init() < 0)
10511021
return -1;
1052-
}
10531022

10541023
if (tracemalloc_config.tracing) {
10551024
/* hook already installed: do nothing */
1056-
DEBUG("tracemalloc_start(): exit (already tracing)");
1057-
assert(!get_reentrant());
10581025
return 0;
10591026
}
1060-
assert(get_reentrant());
10611027

10621028
assert(1 <= max_nframe && max_nframe <= MAX_NFRAME);
10631029
tracemalloc_config.max_nframe = max_nframe;
@@ -1097,38 +1063,23 @@ assert(get_reentrant());
10971063

10981064
/* everything is ready: start tracing Python memory allocations */
10991065
tracemalloc_config.tracing = 1;
1100-
1101-
DEBUG("tracemalloc_start(): set_reentrant(0)");
11021066
set_reentrant(0);
11031067

1104-
DEBUG("tracemalloc_start(): done");
1105-
assert(!get_reentrant());
11061068
return 0;
11071069
}
11081070

11091071

11101072
static void
11111073
tracemalloc_stop(void)
11121074
{
1113-
DEBUG("tracemalloc_stop()");
1114-
1115-
#ifdef WITH_THREAD
1116-
assert(PyGILState_Check());
1117-
#endif
1118-
1119-
if (!tracemalloc_config.tracing) {
1120-
DEBUG("tracemalloc_stop(): exit (not tracing)");
1121-
assert(get_reentrant());
1075+
if (!tracemalloc_config.tracing)
11221076
return;
1123-
}
1124-
assert(!get_reentrant());
11251077

11261078
/* stop tracing Python memory allocations */
11271079
tracemalloc_config.tracing = 0;
11281080

11291081
/* set the reentrant flag to detect bugs: fail with an assertion error if
11301082
set_reentrant(1) is called while tracing is disabled. */
1131-
DEBUG("tracemalloc_stop(): set_reentrant(1)");
11321083
set_reentrant(1);
11331084

11341085
/* unregister the hook on memory allocators */
@@ -1143,8 +1094,6 @@ DEBUG("tracemalloc_stop(): set_reentrant(1)");
11431094
/* release memory */
11441095
raw_free(tracemalloc_traceback);
11451096
tracemalloc_traceback = NULL;
1146-
DEBUG("tracemalloc_stop(): done");
1147-
assert(get_reentrant());
11481097
}
11491098

11501099
PyDoc_STRVAR(tracemalloc_is_tracing_doc,
@@ -1512,11 +1461,8 @@ py_tracemalloc_start(PyObject *self, PyObject *args)
15121461
}
15131462
nframe_int = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int);
15141463

1515-
if (tracemalloc_start(nframe_int) < 0) {
1516-
DEBUG("start(): ERROR!");
1464+
if (tracemalloc_start(nframe_int) < 0)
15171465
return NULL;
1518-
}
1519-
DEBUG("start(): done");
15201466

15211467
Py_RETURN_NONE;
15221468
}
@@ -1642,17 +1588,12 @@ PyMODINIT_FUNC
16421588
PyInit__tracemalloc(void)
16431589
{
16441590
PyObject *m;
1645-
1646-
fprintf(stderr, "[pid %li, tid %li] PyInit__tracemalloc\n", (long)getpid(), PyThread_get_thread_ident()); fflush(stderr);
1647-
16481591
m = PyModule_Create(&module_def);
16491592
if (m == NULL)
16501593
return NULL;
16511594

1652-
#if 0
16531595
if (tracemalloc_init() < 0)
16541596
return NULL;
1655-
#endif
16561597

16571598
return m;
16581599
}

0 commit comments

Comments
 (0)