usd/1928.patch
2022-06-30 09:00:53 -04:00

112 lines
3.9 KiB
Diff

From 899e6456986d42629a1ca5f2a01d07bca261207b Mon Sep 17 00:00:00 2001
From: "Benjamin A. Beasley" <code@musicinmybrain.net>
Date: Mon, 27 Jun 2022 11:50:04 -0400
Subject: [PATCH 1/2] Do not access PyFrameObject fields directly on Python
3.10+
Fixes a Python 3.11 incompatibility. Still accesses PyCodeObject fields
directly.
---
pxr/base/tf/pyTracing.cpp | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)
diff --git a/pxr/base/tf/pyTracing.cpp b/pxr/base/tf/pyTracing.cpp
index 62262d7d75..300c30a435 100644
--- a/pxr/base/tf/pyTracing.cpp
+++ b/pxr/base/tf/pyTracing.cpp
@@ -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>
@@ -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);
From 71ec813641e4e763db5f674d78efe533997eac1a Mon Sep 17 00:00:00 2001
From: "Benjamin A. Beasley" <code@musicinmybrain.net>
Date: Wed, 29 Jun 2022 14:15:26 -0400
Subject: [PATCH 2/2] Simplified patch with compatibility PyFrame_GetCode()
Based on suggestions in https://docs.python.org/3.11/whatsnew/3.11.html#id6
---
pxr/base/tf/pyTracing.cpp | 24 ++++++++++++------------
1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/pxr/base/tf/pyTracing.cpp b/pxr/base/tf/pyTracing.cpp
index 300c30a435..4b201744fa 100644
--- a/pxr/base/tf/pyTracing.cpp
+++ b/pxr/base/tf/pyTracing.cpp
@@ -106,29 +106,29 @@ static void _SetTraceFnEnabled(bool enable) {
}
+#if PY_VERSION_HEX < 0x030900B1
+// Define PyFrame_GetCode() on Python 3.8 and older:
+// https://docs.python.org/3.11/whatsnew/3.11.html#id6
+static inline PyCodeObject* PyFrame_GetCode(PyFrameObject *frame)
+{
+ Py_INCREF(frame->f_code);
+ return frame->f_code;
+}
+#endif
+
+
static int _TracePythonFn(PyObject *, PyFrameObject *frame,
int what, PyObject *arg)
{
// 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(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);
+ Py_DECREF(code);
_InvokeTraceFns(info);