[Python-checkins] GH-96612: Skip incomplete frames in tracemalloc traces. (GH-96613)

markshannon webhook-mailer at python.org
Tue Sep 6 12:37:56 EDT 2022


https://github.com/python/cpython/commit/95e271b2266b8f2e7b60ede86ccf3ede4a7f83eb
commit: 95e271b2266b8f2e7b60ede86ccf3ede4a7f83eb
branch: main
author: Mark Shannon <mark at hotpy.org>
committer: markshannon <mark at hotpy.org>
date: 2022-09-06T17:37:47+01:00
summary:

GH-96612: Skip incomplete frames in tracemalloc traces. (GH-96613)

files:
A Misc/NEWS.d/next/Core and Builtins/2022-09-06-14-26-36.gh-issue-96612.P4ZbeY.rst
M Lib/test/test_tracemalloc.py
M Modules/_tracemalloc.c

diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py
index d2a5ede61e3f..94bcee302fe7 100644
--- a/Lib/test/test_tracemalloc.py
+++ b/Lib/test/test_tracemalloc.py
@@ -360,6 +360,20 @@ def test_fork(self):
         else:
             support.wait_process(pid, exitcode=0)
 
+    def test_no_incomplete_frames(self):
+        tracemalloc.stop()
+        tracemalloc.start(8)
+
+        def f(x):
+            def g():
+                return x
+            return g
+
+        obj = f(0).__closure__[0]
+        traceback = tracemalloc.get_object_traceback(obj)
+        self.assertIn("test_tracemalloc", traceback[-1].filename)
+        self.assertNotIn("test_tracemalloc", traceback[-2].filename)
+
 
 class TestSnapshot(unittest.TestCase):
     maxDiff = 4000
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-09-06-14-26-36.gh-issue-96612.P4ZbeY.rst b/Misc/NEWS.d/next/Core and Builtins/2022-09-06-14-26-36.gh-issue-96612.P4ZbeY.rst
new file mode 100644
index 000000000000..52e92703c9c4
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-09-06-14-26-36.gh-issue-96612.P4ZbeY.rst	
@@ -0,0 +1 @@
+Make sure that incomplete frames do not show up in tracemalloc traces.
diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c
index ae09869deda7..44a1f7b673c0 100644
--- a/Modules/_tracemalloc.c
+++ b/Modules/_tracemalloc.c
@@ -400,7 +400,13 @@ traceback_get_frames(traceback_t *traceback)
     }
 
     _PyInterpreterFrame *pyframe = tstate->cframe->current_frame;
-    for (; pyframe != NULL;) {
+    for (;;) {
+        while (pyframe && _PyFrame_IsIncomplete(pyframe)) {
+            pyframe = pyframe->previous;
+        }
+        if (pyframe == NULL) {
+            break;
+        }
         if (traceback->nframe < _Py_tracemalloc_config.max_nframe) {
             tracemalloc_get_frame(pyframe, &traceback->frames[traceback->nframe]);
             assert(traceback->frames[traceback->nframe].filename != NULL);
@@ -410,8 +416,7 @@ traceback_get_frames(traceback_t *traceback)
             traceback->total_nframe++;
         }
 
-        _PyInterpreterFrame *back = pyframe->previous;
-        pyframe = back;
+        pyframe = pyframe->previous;
     }
 }
 



More information about the Python-checkins mailing list