Allow FrameType to be instantiated

A frame has a stable API to instantiate it: PyFrameObject*
PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
PyObject *globals, PyObject *locals). Despite this, for some reason, FrameType is not allowed to be instantiated in Python. This creates problems when trying to (for example) control an exception traceback, which is common for debuggers, among other uses. This is also inconsistent with most other internal types that exposed to Python and in the types module.

The only reason I can think of is because PyFrame_New requires a PyThreadState object, which is not exposed to Python. However, there are several ways around this:

  • Run PyThreadState_GetUnchecked, raising RuntimeError (or another error) if NULL is returned
  • Expose frames’ PyThreadState to the interpreter
  • Pass another frame as a parameter, and get its thread state

This should be relatively trivial to implement and I can’t think of any downsides, backwards compatibility concerns, or maintenance overhead. I can probably write a PR if one of the solutions to the thread state parameter is decided.

(I originally opened this as issue #149844 but it closed due to being a non-trivial change)

Or, if your goal is to make a variant of a frame, the frame itself could have a replace() method that changes other aspects but keeps the same thread state?

1 Like

That would definitely make more sense then passing a frame as a parameter. imo there should also be an instantiator in addition to that because if the goal is to manipulate tracebacks, thread state doesn’t matter