Skip to content

Commit

Permalink
Do not access PyFrameObject fields directly on Python 3.10+
Browse files Browse the repository at this point in the history
Fixes a Python 3.11 incompatibility. Still accesses PyCodeObject fields
directly.
  • Loading branch information
musicinmybrain committed Jun 28, 2022
1 parent e097b79 commit 899e645
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions pxr/base/tf/pyTracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@

#include <tbb/spin_mutex.h>

// This is from python, needed for PyFrameObject.
// These are from python, needed for PyFrameObject.
#include <frameobject.h>
#include <patchlevel.h>

#include <list>
#include <mutex>
Expand Down Expand Up @@ -110,11 +111,24 @@ static int _TracePythonFn(PyObject *, PyFrameObject *frame,
{
// Build up a trace info struct.
TfPyTraceInfo info;
#if PY_VERSION_HEX >= 0x030a00f0
// PyFrame_GetCode introduced in 3.9, added to Stable ABI in 3.10;
// PyFrameObject is opaque in 3.11
PyCodeObject * code = PyFrame_GetCode(frame);
#else
// Use direct access to PyFrameObject fields.
PyCodeObject * code = frame->f_code;
// Create a strong reference as PyFrame_GetCode() would, so we do not have
// to conditionalize decrementing the reference count.
Py_INCREF((PyObject *)code);
#endif
info.arg = arg;
info.funcName = TfPyString_AsString(frame->f_code->co_name);
info.fileName = TfPyString_AsString(frame->f_code->co_filename);
info.funcLine = frame->f_code->co_firstlineno;
info.funcName = TfPyString_AsString(code->co_name);
info.fileName = TfPyString_AsString(code->co_filename);
info.funcLine = code->co_firstlineno;
info.what = what;
// PyFrame_GetCode returned a strong reference
Py_DECREF((PyObject *)code);

_InvokeTraceFns(info);

Expand Down

0 comments on commit 899e645

Please sign in to comment.