Blame 0005-Use-_PyEval_SetProfile-instead-of-manually-updating-.patch

3dcd857
From 4d9c0667a24ead349333c47752747e51baa02d42 Mon Sep 17 00:00:00 2001
3dcd857
From: =?UTF-8?q?S=C3=BCmer=20Cip?= <sumer.cip@platform.sh>
3dcd857
Date: Thu, 18 Aug 2022 12:15:59 +0300
3dcd857
Subject: [PATCH 05/11] Use _PyEval_SetProfile instead of manually updating
3dcd857
 internal structures to enable per profiler per threadstate
3dcd857
3dcd857
---
3dcd857
 yappi/_yappi.c | 29 +++++++++++++++++------------
3dcd857
 1 file changed, 17 insertions(+), 12 deletions(-)
3dcd857
3dcd857
diff --git a/yappi/_yappi.c b/yappi/_yappi.c
3dcd857
index d2f0af4..5a3f78a 100644
3dcd857
--- a/yappi/_yappi.c
3dcd857
+++ b/yappi/_yappi.c
3dcd857
@@ -25,6 +25,10 @@
3dcd857
 #include "mem.h"
3dcd857
 #include "tls.h"
3dcd857
 
3dcd857
+#if PY_VERSION_HEX > 0x030b0000
3dcd857
+#include "internal/pycore_code.h"
3dcd857
+#endif
3dcd857
+
3dcd857
 #define SUPPRESS_WARNING(a) (void)a
3dcd857
 
3dcd857
 #ifdef IS_PY3K
3dcd857
@@ -683,9 +687,8 @@ _code2pit(PyFrameObject *fobj, uintptr_t current_tag)
3dcd857
     Py_INCREF(cobj);
3dcd857
 
3dcd857
     if (cobj->co_argcount) {
3dcd857
-        // todo: this is said to be slower. Maybe there is a better alternative 
3dcd857
-        // like _PyCode_GetVarnames(..). See https://discuss.python.org/t/python-3-11-frame-structure-and-various-changes/17895
3dcd857
-        PyObject *co_varnames = PyObject_GetAttrString((PyObject *)cobj, "co_varnames");
3dcd857
+        // todo: 3.12 this might be a public API
3dcd857
+        PyObject *co_varnames = _PyCode_GetVarnames(cobj);
3dcd857
         const char *firstarg = PyStr_AS_CSTRING(PyTuple_GET_ITEM(co_varnames, 0));
3dcd857
 
3dcd857
         if (!strcmp(firstarg, "self")) {
3dcd857
@@ -1295,28 +1298,30 @@ _resume_greenlet_ctx(_ctx *ctx)
3dcd857
 static void 
3dcd857
 _eval_setprofile(PyThreadState *ts)
3dcd857
 {
3dcd857
-#if PY_VERSION_HEX < 0x030a00b1
3dcd857
+#if PY_VERSION_HEX > 0x030b0000
3dcd857
+    _PyEval_SetProfile(ts, _yapp_callback, NULL);
3dcd857
+    //    ts->cframe->use_tracing = 255;
3dcd857
+#elif PY_VERSION_HEX < 0x030a00b1
3dcd857
     ts->use_tracing = 1;
3dcd857
+    ts->c_profilefunc = _yapp_callback;
3dcd857
 #else
3dcd857
     ts->cframe->use_tracing = 1;
3dcd857
-#endif
3dcd857
     ts->c_profilefunc = _yapp_callback;
3dcd857
-
3dcd857
-    //_update_tracing_state()
3dcd857
-
3dcd857
-//  todo: do this only for 3.11
3dcd857
-    ts->cframe->use_tracing = 255;
3dcd857
+#endif
3dcd857
 }
3dcd857
 
3dcd857
 static void
3dcd857
 _eval_unsetprofile(PyThreadState *ts)
3dcd857
 {
3dcd857
-#if PY_VERSION_HEX < 0x030a00b1
3dcd857
+#if PY_VERSION_HEX > 0x030b0000
3dcd857
+    _PyEval_SetProfile(ts, NULL, NULL);
3dcd857
+#elif PY_VERSION_HEX < 0x030a00b1
3dcd857
     ts->use_tracing = 0;
3dcd857
+    ts->c_profilefunc = NULL;
3dcd857
 #else
3dcd857
     ts->cframe->use_tracing = 0;
3dcd857
-#endif
3dcd857
     ts->c_profilefunc = NULL;
3dcd857
+#endif
3dcd857
 }
3dcd857
 
3dcd857
 static _ctx *
3dcd857
-- 
3dcd857
2.34.1
3dcd857