Blame 0009-Use-codeobject-co_varnames-for-Py-versions-11.patch

3dcd857
From bf5cb748ffc3d228593a388b2286e35529f059d7 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 15:23:47 +0300
3dcd857
Subject: [PATCH 09/11] Use codeobject->co_varnames for Py versions < 11
3dcd857
3dcd857
---
3dcd857
 run_tests.py               |  4 +--
3dcd857
 setup.py                   |  9 +++--
3dcd857
 tests/test_gevent.py       | 71 ++++++++++++++++++++++++--------------
3dcd857
 yappi/_yappi.c             |  9 +++--
3dcd857
 5 files changed, 62 insertions(+), 33 deletions(-)
3dcd857
3dcd857
diff --git a/run_tests.py b/run_tests.py
3dcd857
index e632cbf..df36f74 100644
3dcd857
--- a/run_tests.py
3dcd857
+++ b/run_tests.py
3dcd857
@@ -19,9 +19,9 @@ if __name__ == '__main__':
3dcd857
         'test_functionality',
3dcd857
         'test_hooks',
3dcd857
         'test_tags',
3dcd857
+        'test_gevent',
3dcd857
     ]
3dcd857
-    if sys.version_info < (3, 10):
3dcd857
-        tests += ['test_gevent']
3dcd857
+    # TODO: make these auto-skip if cannot be imported
3dcd857
     if sys.version_info >= (3, 4):
3dcd857
         tests += ['test_asyncio']
3dcd857
     if sys.version_info >= (3, 7):
3dcd857
diff --git a/setup.py b/setup.py
3dcd857
index 5e4fe66..f241810 100644
3dcd857
--- a/setup.py
3dcd857
+++ b/setup.py
3dcd857
@@ -62,6 +62,11 @@ CLASSIFIERS = [
3dcd857
     'Topic :: Software Development :: Libraries :: Python Modules',
3dcd857
 ]
3dcd857
 
3dcd857
+test_deps = []
3dcd857
+if sys.version_info <= (3, 10):
3dcd857
+    # TODO: fix this when gevent supports 3.11
3dcd857
+    test_deps += ['gevent>=20.6.2']
3dcd857
+
3dcd857
 setup(
3dcd857
     name=NAME,
3dcd857
     version=VERSION,
3dcd857
@@ -91,11 +96,11 @@ setup(
3dcd857
     description="Yet Another Python Profiler",
3dcd857
     long_description=long_description,
3dcd857
     long_description_content_type='text/markdown',
3dcd857
-    keywords="python thread multithread profiler",
3dcd857
+    keywords="python thread multithread asyncio gevent profiler",
3dcd857
     classifiers=CLASSIFIERS,
3dcd857
     license="MIT",
3dcd857
     url=HOMEPAGE,
3dcd857
     extras_require={
3dcd857
-        'test': ['gevent>=20.6.2'],
3dcd857
+        'test': test_deps,
3dcd857
     }
3dcd857
 )
3dcd857
diff --git a/tests/test_gevent.py b/tests/test_gevent.py
3dcd857
index 8569712..0b105a2 100644
3dcd857
--- a/tests/test_gevent.py
3dcd857
+++ b/tests/test_gevent.py
3dcd857
@@ -1,15 +1,21 @@
3dcd857
 import unittest
3dcd857
 import _yappi
3dcd857
 import yappi
3dcd857
-import gevent
3dcd857
-from gevent.event import Event
3dcd857
 import threading
3dcd857
 from utils import (
3dcd857
-    YappiUnitTestCase, find_stat_by_name, burn_cpu, burn_io,
3dcd857
-    burn_io_gevent
3dcd857
+    YappiUnitTestCase, find_stat_by_name, burn_cpu, burn_io, burn_io_gevent
3dcd857
 )
3dcd857
 
3dcd857
+GEVENT_AVAIL = True
3dcd857
+try:
3dcd857
+    import gevent
3dcd857
+    from gevent.event import Event
3dcd857
+except ImportError:
3dcd857
+    GEVENT_AVAIL = False
3dcd857
+
3dcd857
+
3dcd857
 class GeventTestThread(threading.Thread):
3dcd857
+
3dcd857
     def __init__(self, name, *args, **kwargs):
3dcd857
         super(GeventTestThread, self).__init__(*args, **kwargs)
3dcd857
         self.name = name
3dcd857
@@ -19,6 +25,10 @@ class GeventTestThread(threading.Thread):
3dcd857
         gevent.get_hub().name = "Hub"
3dcd857
         super(GeventTestThread, self).run()
3dcd857
 
3dcd857
+
3dcd857
+@unittest.skipIf(
3dcd857
+    not GEVENT_AVAIL, "Gevent is not installed on the target system"
3dcd857
+)
3dcd857
 class GeventTest(YappiUnitTestCase):
3dcd857
 
3dcd857
     def setUp(self):
3dcd857
@@ -51,6 +61,7 @@ class GeventTest(YappiUnitTestCase):
3dcd857
         t.start()
3dcd857
         return t
3dcd857
 
3dcd857
+
3dcd857
 class TestAPI(GeventTest):
3dcd857
 
3dcd857
     def test_start_flags(self):
3dcd857
@@ -68,7 +79,9 @@ class TestAPI(GeventTest):
3dcd857
         yappi.stop()
3dcd857
         yappi.clear_stats()
3dcd857
 
3dcd857
-        yappi.start(builtins=True, profile_greenlets=True, profile_threads=False)
3dcd857
+        yappi.start(
3dcd857
+            builtins=True, profile_greenlets=True, profile_threads=False
3dcd857
+        )
3dcd857
         self.assertEqual(_yappi._get_start_flags()["profile_builtins"], 1)
3dcd857
         self.assertEqual(_yappi._get_start_flags()["profile_multicontext"], 1)
3dcd857
         self.assertEqual(len(yappi.get_greenlet_stats()), 1)
3dcd857
@@ -76,16 +89,21 @@ class TestAPI(GeventTest):
3dcd857
 
3dcd857
     def test_context_change_exception(self):
3dcd857
         yappi.start()
3dcd857
+
3dcd857
         def a():
3dcd857
             pass
3dcd857
 
3dcd857
         a()
3dcd857
         # Setting to same backend should succeed
3dcd857
         # Changing backend should fail
3dcd857
-        self.assertRaises(_yappi.error, yappi.set_context_backend, "native_thread")
3dcd857
+        self.assertRaises(
3dcd857
+            _yappi.error, yappi.set_context_backend, "native_thread"
3dcd857
+        )
3dcd857
         yappi.stop()
3dcd857
         # Still fail, stats need to be cleared
3dcd857
-        self.assertRaises(_yappi.error, yappi.set_context_backend, "native_thread")
3dcd857
+        self.assertRaises(
3dcd857
+            _yappi.error, yappi.set_context_backend, "native_thread"
3dcd857
+        )
3dcd857
         yappi.clear_stats()
3dcd857
         # Should succeed now
3dcd857
         yappi.set_context_backend("native_thread")
3dcd857
@@ -93,6 +111,7 @@ class TestAPI(GeventTest):
3dcd857
 
3dcd857
     def test_get_context_stat_exception(self):
3dcd857
         yappi.start()
3dcd857
+
3dcd857
         def a():
3dcd857
             pass
3dcd857
 
3dcd857
@@ -106,6 +125,7 @@ class TestAPI(GeventTest):
3dcd857
         yappi.set_context_backend("native_thread")
3dcd857
 
3dcd857
         class ThreadA(threading.Thread):
3dcd857
+
3dcd857
             def run(self):
3dcd857
                 burn_cpu(0.05)
3dcd857
 
3dcd857
@@ -127,7 +147,9 @@ class TestAPI(GeventTest):
3dcd857
 
3dcd857
         tstats = yappi.get_thread_stats()
3dcd857
 
3dcd857
-        self.assertEqual(len(tstats), 2, "Incorrect number of contexts captured")
3dcd857
+        self.assertEqual(
3dcd857
+            len(tstats), 2, "Incorrect number of contexts captured"
3dcd857
+        )
3dcd857
 
3dcd857
         # First stat should be of threadA since it is sorted by ttot
3dcd857
         statsA = tstats[0]
3dcd857
@@ -139,6 +161,7 @@ class TestAPI(GeventTest):
3dcd857
         self.assertEqual(statsMain.tid, main_thread.ident)
3dcd857
         self.assertEqual(statsMain.name, main_thread.__class__.__name__)
3dcd857
 
3dcd857
+
3dcd857
 class SingleThreadTests(GeventTest):
3dcd857
 
3dcd857
     def test_recursive_greenlet(self):
3dcd857
@@ -148,14 +171,14 @@ class SingleThreadTests(GeventTest):
3dcd857
                 return
3dcd857
             burn_io_gevent(0.1)
3dcd857
             burn_cpu(0.1)
3dcd857
-            g1 = self.spawn_greenlet("a_%d" % (n-1), a, n - 1)
3dcd857
+            g1 = self.spawn_greenlet("a_%d" % (n - 1), a, n - 1)
3dcd857
             g1.get()
3dcd857
-            g2 = self.spawn_greenlet("a_%d" % (n-2), a, n - 2)
3dcd857
+            g2 = self.spawn_greenlet("a_%d" % (n - 2), a, n - 2)
3dcd857
             g2.get()
3dcd857
 
3dcd857
         yappi.start()
3dcd857
         g = self.spawn_greenlet("a", a, 3)
3dcd857
-        g.get() # run until complete, report exception (if any)
3dcd857
+        g.get()  # run until complete, report exception (if any)
3dcd857
         yappi.stop()
3dcd857
 
3dcd857
         r1 = '''
3dcd857
@@ -239,6 +262,7 @@ class SingleThreadTests(GeventTest):
3dcd857
         self.assert_ctx_stats_almost_equal(r2, gstats)
3dcd857
 
3dcd857
     def test_recursive_function(self):
3dcd857
+
3dcd857
         def a(n):
3dcd857
             if (n <= 0):
3dcd857
                 return
3dcd857
@@ -280,6 +304,7 @@ class SingleThreadTests(GeventTest):
3dcd857
         self.assert_ctx_stats_almost_equal(r2, gstats)
3dcd857
 
3dcd857
     def test_exception_raised(self):
3dcd857
+
3dcd857
         def a(n):
3dcd857
             burn_cpu(0.1)
3dcd857
             burn_io_gevent(0.1)
3dcd857
@@ -287,7 +312,7 @@ class SingleThreadTests(GeventTest):
3dcd857
             if (n == 0):
3dcd857
                 raise Exception
3dcd857
 
3dcd857
-            a(n-1)
3dcd857
+            a(n - 1)
3dcd857
 
3dcd857
         yappi.set_clock_type("cpu")
3dcd857
         yappi.start()
3dcd857
@@ -325,7 +350,6 @@ class SingleThreadTests(GeventTest):
3dcd857
             burn_cpu(0.1)
3dcd857
             burn_io_gevent(0.1)
3dcd857
 
3dcd857
-
3dcd857
         ev1 = Event()
3dcd857
         ev2 = Event()
3dcd857
         gl = self.spawn_greenlet("a", a, ev1, ev2)
3dcd857
@@ -353,7 +377,6 @@ class SingleThreadTests(GeventTest):
3dcd857
         '''
3dcd857
         self.assert_ctx_stats_almost_equal(r2, gstats)
3dcd857
 
3dcd857
-
3dcd857
     def test_many_context_switches(self):
3dcd857
 
3dcd857
         def common():
3dcd857
@@ -455,15 +478,14 @@ class MultiThreadTests(GeventTest):
3dcd857
 
3dcd857
         def driver():
3dcd857
             to_run = [
3dcd857
-                (a, ()),
3dcd857
-                (b, ()),
3dcd857
-                (recursive_a, (5,)),
3dcd857
-                (recursive_a, (5,))
3dcd857
+                (a, ()), (b, ()), (recursive_a, (5, )), (recursive_a, (5, ))
3dcd857
             ]
3dcd857
 
3dcd857
             ts = []
3dcd857
             for idx, (func, args) in enumerate(to_run):
3dcd857
-                t = self.spawn_thread("%s-%d" %  (func.__name__, idx), func, *args)
3dcd857
+                t = self.spawn_thread(
3dcd857
+                    "%s-%d" % (func.__name__, idx), func, *args
3dcd857
+                )
3dcd857
                 ts.append(t)
3dcd857
 
3dcd857
             for t in ts:
3dcd857
@@ -520,14 +542,13 @@ class MultiThreadTests(GeventTest):
3dcd857
 
3dcd857
         def driver():
3dcd857
 
3dcd857
-            to_run = [
3dcd857
-                (recursive_a, (5,)),
3dcd857
-                (recursive_a, (5,))
3dcd857
-            ]
3dcd857
+            to_run = [(recursive_a, (5, )), (recursive_a, (5, ))]
3dcd857
 
3dcd857
             ts = []
3dcd857
             for idx, (func, args) in enumerate(to_run):
3dcd857
-                t = self.spawn_thread("%s_%d" % (func.__name__, idx), func, *args)
3dcd857
+                t = self.spawn_thread(
3dcd857
+                    "%s_%d" % (func.__name__, idx), func, *args
3dcd857
+                )
3dcd857
                 ts.append(t)
3dcd857
 
3dcd857
             recursive_a(6)
3dcd857
@@ -558,7 +579,6 @@ class MultiThreadTests(GeventTest):
3dcd857
         # Set context backend to confgiure default callbacks
3dcd857
         yappi.set_context_backend("greenlet")
3dcd857
 
3dcd857
-
3dcd857
         class GreenletA(gevent.Greenlet):
3dcd857
             pass
3dcd857
 
3dcd857
@@ -598,5 +618,6 @@ class MultiThreadTests(GeventTest):
3dcd857
         '''
3dcd857
         self.assert_ctx_stats_almost_equal(r2, gstats)
3dcd857
 
3dcd857
+
3dcd857
 if __name__ == '__main__':
3dcd857
     unittest.main()
3dcd857
diff --git a/yappi/_yappi.c b/yappi/_yappi.c
3dcd857
index 88236ee..41ff517 100644
3dcd857
--- a/yappi/_yappi.c
3dcd857
+++ b/yappi/_yappi.c
3dcd857
@@ -662,6 +662,7 @@ _code2pit(PyFrameObject *fobj, uintptr_t current_tag)
3dcd857
     PyCodeObject *cobj;
3dcd857
     _pit *pit;
3dcd857
     _htab *pits;
3dcd857
+    PyObject *co_varnames;
3dcd857
 
3dcd857
     pits = _get_pits_tbl(current_tag);
3dcd857
     if (!pits) {
3dcd857
@@ -690,7 +691,11 @@ _code2pit(PyFrameObject *fobj, uintptr_t current_tag)
3dcd857
     if (cobj->co_argcount) {
3dcd857
         // There has been a lot going on with `co_varnames`, but finally in 
3dcd857
         // 3.11.0rc1, it is added as a public API
3dcd857
-        PyObject *co_varnames = PyCode_GetVarnames(cobj);
3dcd857
+#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION == 11
3dcd857
+        co_varnames = PyCode_GetVarnames(cobj);
3dcd857
+#else
3dcd857
+        co_varnames = cobj->co_varnames;
3dcd857
+#endif
3dcd857
         const char *firstarg = PyStr_AS_CSTRING(PyTuple_GET_ITEM(co_varnames, 0));
3dcd857
 
3dcd857
         if (!strcmp(firstarg, "self")) {
3dcd857
@@ -1503,8 +1508,6 @@ _start(void)
3dcd857
         return 0;
3dcd857
     }
3dcd857
 
3dcd857
-    //flags.multicontext = 0;
3dcd857
-
3dcd857
     if (flags.multicontext) {
3dcd857
         _enum_threads(&_bootstrap_thread);
3dcd857
     } else {
3dcd857
-- 
3dcd857
2.34.1
3dcd857