]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-95818: Skip incomplete frames in `PyThreadState_GetFrame` (GH-95886)
authorMark Shannon <mark@hotpy.org>
Thu, 11 Aug 2022 13:06:32 +0000 (14:06 +0100)
committerGitHub <noreply@github.com>
Thu, 11 Aug 2022 13:06:32 +0000 (14:06 +0100)
Lib/test/test_frame.py
Misc/NEWS.d/next/Core and Builtins/2022-08-11-11-01-56.gh-issue-95818.iClLdl.rst [new file with mode: 0644]
Python/pystate.c

index a715e725a7e45b17d4cbcb43ed56e421e27678c1..9fab17684eec02b25c66afa4bf54b92a1f880e36 100644 (file)
@@ -235,6 +235,28 @@ class ReprTest(unittest.TestCase):
                          r"^<frame at 0x[0-9a-fA-F]+, file %s, line %d, code inner>$"
                          % (file_repr, offset + 5))
 
+class TestIncompleteFrameAreInvisible(unittest.TestCase):
+
+    def test_issue95818(self):
+        #See GH-95818 for details
+        import gc
+        self.addCleanup(gc.set_threshold, *gc.get_threshold())
+
+        gc.set_threshold(1,1,1)
+        class GCHello:
+            def __del__(self):
+                print("Destroyed from gc")
+
+        def gen():
+            yield
+
+        fd = open(__file__)
+        l = [fd, GCHello()]
+        l.append(l)
+        del fd
+        del l
+        gen()
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-08-11-11-01-56.gh-issue-95818.iClLdl.rst b/Misc/NEWS.d/next/Core and Builtins/2022-08-11-11-01-56.gh-issue-95818.iClLdl.rst
new file mode 100644 (file)
index 0000000..1e243f5
--- /dev/null
@@ -0,0 +1 @@
+Skip over incomplete frames in :c:func:`PyThreadState_GetFrame`.
index 11cc122185f781f31fa0e8ed9e39858a256727f1..bcdb825a8629b0caf9f24f6a4c750d59bf765e60 100644 (file)
@@ -1255,10 +1255,14 @@ PyFrameObject*
 PyThreadState_GetFrame(PyThreadState *tstate)
 {
     assert(tstate != NULL);
-    if (tstate->cframe->current_frame == NULL) {
+    _PyInterpreterFrame *f = tstate->cframe->current_frame;
+    while (f && _PyFrame_IsIncomplete(f)) {
+        f = f->previous;
+    }
+    if (f == NULL) {
         return NULL;
     }
-    PyFrameObject *frame = _PyFrame_GetFrameObject(tstate->cframe->current_frame);
+    PyFrameObject *frame = _PyFrame_GetFrameObject(f);
     if (frame == NULL) {
         PyErr_Clear();
     }