@@ -27,14 +27,18 @@ cpdef _extract_class_name(frame):
27
27
"""
28
28
# Python 3.11 moved PyFrameObject members to internal C API and cannot be directly accessed
29
29
IF PY_MAJOR_VERSION > 3 or (PY_MAJOR_VERSION == 3 and PY_MINOR_VERSION >= 11 ):
30
- code = PyFrame_GetCode(< PyFrameObject* > frame)
31
- co_varnames = PyCode_GetVarnames(code)
32
- if PyTuple_Size(co_varnames) == 0 :
30
+ code = PyFrame_GetCode(< PyFrameObject* > frame) # Returns strong reference
31
+ co_varnames = PyCode_GetVarnames(code) # Returns new reference
32
+ if co_varnames == NULL or PyTuple_Size(co_varnames) == 0 :
33
+ Py_XDECREF(< PyObject* > code)
33
34
return " "
34
35
argname = PyTuple_GetItem(co_varnames, 0 )
36
+ Py_XDECREF(< PyObject* > code)
37
+ Py_XDECREF(co_varnames)
35
38
try :
36
- f_locals = PyFrame_GetLocals(< PyFrameObject* > frame)
39
+ f_locals = PyFrame_GetLocals(< PyFrameObject* > frame) # Returns strong reference
37
40
value = PyDict_GetItem(f_locals, argname)
41
+ Py_XDECREF(f_locals)
38
42
except KeyError :
39
43
return " "
40
44
ELSE :
@@ -71,10 +75,10 @@ cpdef traceback_to_frames(traceback, max_nframes):
71
75
frame = tb.tb_frame
72
76
# Python 3.11 moved PyFrameObject members to internal C API and cannot be directly accessed
73
77
IF PY_MAJOR_VERSION > 3 or (PY_MAJOR_VERSION == 3 and PY_MINOR_VERSION >= 11 ):
74
- code = PyFrame_GetCode(< PyFrameObject* > frame)
78
+ code = < object > PyFrame_GetCode(< PyFrameObject* > frame)
75
79
lineno = PyFrame_GetLineNumber(< PyFrameObject* > frame)
76
80
lineno = 0 if lineno is None else lineno
77
- frames.insert(0 , (( < object > code) .co_filename, lineno, ( < object > code) .co_name, _extract_class_name(frame)))
81
+ frames.insert(0 , (code.co_filename, lineno, code.co_name, _extract_class_name(frame)))
78
82
Py_XDECREF(< PyObject* > code)
79
83
ELSE :
80
84
code = frame.f_code
@@ -99,11 +103,10 @@ cpdef pyframe_to_frames(frame, max_nframes):
99
103
while pyframe != NULL :
100
104
nframes += 1
101
105
if len (frames) < max_nframes:
102
- code = PyFrame_GetCode(pyframe)
106
+ code = < object > PyFrame_GetCode(pyframe)
103
107
lineno = PyFrame_GetLineNumber(pyframe)
104
108
lineno = 0 if lineno is None else lineno
105
- frames.append(((< object > code).co_filename, lineno, (< object > code).co_name, _extract_class_name(< object > pyframe)))
106
- Py_XDECREF(< PyObject* > code)
109
+ frames.append((code.co_filename, lineno, code.co_name, _extract_class_name(< object > pyframe)))
107
110
pyframe = PyFrame_GetBack(pyframe)
108
111
# FIXME: Where to Py_XDECREF(pyframe)?
109
112
ELSE :
0 commit comments