33#include "pycore_interp.h" // PyInterpreterState.warnings
44#include "pycore_pyerrors.h"
55#include "pycore_pystate.h" // _PyThreadState_GET()
6- #include "frameobject.h"
6+ #include "frameobject.h" // PyFrame_GetBack()
77#include "clinic/_warnings.c.h"
88
99#define MODULE_NAME "_warnings"
@@ -815,7 +815,9 @@ static PyFrameObject *
815815next_external_frame (PyFrameObject * frame )
816816{
817817 do {
818- frame = frame -> f_back ;
818+ PyFrameObject * back = PyFrame_GetBack (frame );
819+ Py_DECREF (frame );
820+ frame = back ;
819821 } while (frame != NULL && is_internal_frame (frame ));
820822
821823 return frame ;
@@ -831,12 +833,15 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
831833 PyObject * globals ;
832834
833835 /* Setup globals, filename and lineno. */
834- PyFrameObject * f = _PyThreadState_GET ()-> frame ;
836+ PyThreadState * tstate = _PyThreadState_GET ();
837+ PyFrameObject * f = PyThreadState_GetFrame (tstate );
835838 // Stack level comparisons to Python code is off by one as there is no
836839 // warnings-related stack level to avoid.
837840 if (stack_level <= 0 || is_internal_frame (f )) {
838841 while (-- stack_level > 0 && f != NULL ) {
839- f = f -> f_back ;
842+ PyFrameObject * back = PyFrame_GetBack (f );
843+ Py_DECREF (f );
844+ f = back ;
840845 }
841846 }
842847 else {
@@ -857,6 +862,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
857862 Py_DECREF (code );
858863 Py_INCREF (* filename );
859864 * lineno = PyFrame_GetLineNumber (f );
865+ Py_DECREF (f );
860866 }
861867
862868 * module = NULL ;
@@ -868,7 +874,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
868874 if (* registry == NULL ) {
869875 int rc ;
870876
871- if (PyErr_Occurred ( )) {
877+ if (_PyErr_Occurred ( tstate )) {
872878 goto handle_error ;
873879 }
874880 * registry = PyDict_New ();
@@ -887,7 +893,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
887893 if (* module == Py_None || (* module != NULL && PyUnicode_Check (* module ))) {
888894 Py_INCREF (* module );
889895 }
890- else if (PyErr_Occurred ( )) {
896+ else if (_PyErr_Occurred ( tstate )) {
891897 goto handle_error ;
892898 }
893899 else {
0 commit comments