Author vstinner
Recipients Mark.Shannon, brett.cannon, vstinner
Date 2020-03-20.16:22:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
It looks safe to remove this feature.

I failed to find any recent user of _PyThreadState_GetFrame.

Moreover, _PyRuntime.getframe and _PyThreadState_GetFrame have been moved to the internal C API in Python 3.7. This C API was not accessible by third-party projects in Python 3.7. The internal C API can only be used by third-party projects since Python 3.8 and it's tricky to use it... on purpose. I don't recall any complain about _PyThreadState_GetFrame becoming inaccessible.


I searched for "_PyThreadState_GetFrame" in C code on GitHub. I only found copies of Python/pystate.c files in the first 10 pages of search results, with one exception. I found one file:

It's an old (latest commit on Mar 31, 2015) debugger project written by Xavier de Gaye.

It seems like the following flavor is more recent (latest commit on Apr 20, 2019):

Note: the project contains 4 .c files, but only _pdbhandler-py27.c uses _PyThreadState_GetFrame: _pdbhandler-py3.c which is for Python 3 doesn't use _PyThreadState_GetFrame. But Python 2.7 reached its end of life, it's no longer supported.

Copy of the code:

    /* Disable the Python 2 restricted mode in the subinterpreter (see
     * PyEval_GetRestricted()) that prevents linecache to open the source
     * files and prevents attribute access. */
    saved_globals = mainstate->frame->f_globals;
    saved_locals = mainstate->frame->f_locals;
    saved_tstate_getframe = _PyThreadState_GetFrame;
    mainstate->frame->f_globals = globals;
    mainstate->frame->f_locals = locals;
    _PyThreadState_GetFrame = threadstate_getframe;
    pdbhandler_tstate = mainstate;

So _PyThreadState_GetFrame was used to "disable the Python 2 restricted mode", but this mode has been removed from Python 3.
Date User Action Args
2020-03-20 16:22:25vstinnersetrecipients: + vstinner, brett.cannon, Mark.Shannon
2020-03-20 16:22:25vstinnersetmessageid: <>
2020-03-20 16:22:25vstinnerlinkissue39946 messages
2020-03-20 16:22:24vstinnercreate