From e939a82ae8a1617a821547060e7a581492adda01 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=BCmer=20Cip?= <sumer.cip@platform.sh>
Date: Tue, 2 Aug 2022 11:27:29 +0300
Subject: [PATCH 01/11] Change frame object + update asyncio tests
---
tests/test_asyncio.py | 53 ++++++++++++++++++-------------------------
yappi/_yappi.c | 21 +++++++++++++----
yappi/timing.c | 3 +--
3 files changed, 39 insertions(+), 38 deletions(-)
diff --git a/tests/test_asyncio.py b/tests/test_asyncio.py
index b547674..8e9e631 100644
--- a/tests/test_asyncio.py
+++ b/tests/test_asyncio.py
@@ -5,20 +5,18 @@ import threading
from utils import YappiUnitTestCase, find_stat_by_name, burn_cpu, burn_io
-@asyncio.coroutine
-def async_sleep(sec):
- yield from asyncio.sleep(sec)
+async def async_sleep(sec):
+ await asyncio.sleep(sec)
class SingleThreadTests(YappiUnitTestCase):
def test_issue58(self):
- @asyncio.coroutine
- def mt(d):
+ async def mt(d):
t = asyncio.Task(async_sleep(3 + d))
- yield from async_sleep(3)
- yield from t
+ await async_sleep(3)
+ await t
yappi.set_clock_type('wall')
@@ -41,14 +39,13 @@ class SingleThreadTests(YappiUnitTestCase):
def test_recursive_coroutine(self):
- @asyncio.coroutine
- def a(n):
+ async def a(n):
if n <= 0:
return
- yield from async_sleep(0.1)
+ await async_sleep(0.1)
burn_cpu(0.1)
- yield from a(n - 1)
- yield from a(n - 2)
+ await a(n - 1)
+ await a(n - 2)
yappi.set_clock_type("cpu")
yappi.start()
@@ -65,13 +62,12 @@ class SingleThreadTests(YappiUnitTestCase):
def test_basic_old_style(self):
- @asyncio.coroutine
- def a():
- yield from async_sleep(0.1)
+ async def a():
+ await async_sleep(0.1)
burn_io(0.1)
- yield from async_sleep(0.1)
+ await async_sleep(0.1)
burn_io(0.1)
- yield from async_sleep(0.1)
+ await async_sleep(0.1)
burn_cpu(0.3)
yappi.set_clock_type("wall")
@@ -111,22 +107,19 @@ class MultiThreadTests(YappiUnitTestCase):
def test_basic(self):
- @asyncio.coroutine
- def a():
- yield from async_sleep(0.3)
+ async def a():
+ await async_sleep(0.3)
burn_cpu(0.4)
- @asyncio.coroutine
- def b():
- yield from a()
+ async def b():
+ await a()
- @asyncio.coroutine
- def recursive_a(n):
+ async def recursive_a(n):
if not n:
return
burn_io(0.3)
- yield from async_sleep(0.3)
- yield from recursive_a(n - 1)
+ await async_sleep(0.3)
+ await recursive_a(n - 1)
tlocal = threading.local()
@@ -158,12 +151,10 @@ class MultiThreadTests(YappiUnitTestCase):
ts.append(t)
_ctag += 1
- @asyncio.coroutine
- def stop_loop():
+ async def stop_loop():
asyncio.get_event_loop().stop()
- @asyncio.coroutine
- def driver():
+ async def driver():
futs = []
fut = asyncio.run_coroutine_threadsafe(a(), ts[0]._loop)
futs.append(fut)
diff --git a/yappi/_yappi.c b/yappi/_yappi.c
index fa3884e..0b10935 100644
--- a/yappi/_yappi.c
+++ b/yappi/_yappi.c
@@ -200,6 +200,15 @@ static int _pitenumdel(_hitem *item, void *arg);
// funcs
+static PyCodeObject *
+FRAME2CODE(PyFrameObject *frame) {
+#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 10
+ return PyFrame_GetCode(frame);
+#else
+ return frame->f_code;
+#endif
+}
+
static void _DebugPrintObjects(unsigned int arg_count, ...)
{
unsigned int i;
@@ -216,7 +225,9 @@ static void _DebugPrintObjects(unsigned int arg_count, ...)
int
IS_SUSPENDED(PyFrameObject *frame)
{
-#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 10
+#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
+ return 1;
+#elif PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION == 10
return (frame->f_state == FRAME_SUSPENDED);
#else
return (frame->f_stacktop != NULL);
@@ -229,11 +240,11 @@ int IS_ASYNC(PyFrameObject *frame)
#if defined(IS_PY3K)
#if PY_MINOR_VERSION >= 4
- result = frame->f_code->co_flags & CO_COROUTINE ||
- frame->f_code->co_flags & CO_ITERABLE_COROUTINE;
+ result = FRAME2CODE(frame)->co_flags & CO_COROUTINE ||
+ FRAME2CODE(frame)->co_flags & CO_ITERABLE_COROUTINE;
#endif
#if PY_MINOR_VERSION >= 6
- result = result || frame->f_code->co_flags & CO_ASYNC_GENERATOR;
+ result = result || FRAME2CODE(frame)->co_flags & CO_ASYNC_GENERATOR;
#endif
#endif
@@ -650,7 +661,7 @@ _code2pit(PyFrameObject *fobj, uintptr_t current_tag)
return NULL;
}
- cobj = fobj->f_code;
+ cobj = FRAME2CODE(fobj);
it = hfind(pits, (uintptr_t)cobj);
if (it) {
return ((_pit *)it->val);
diff --git a/yappi/timing.c b/yappi/timing.c
index fa2ad24..b5bf083 100644
--- a/yappi/timing.c
+++ b/yappi/timing.c
@@ -81,13 +81,12 @@ tickcount(void)
rc = 0;
if (g_clock_type == CPU_CLOCK) {
- kern_return_t kr;
thread_basic_info_t tinfo_b;
thread_info_data_t tinfo_d;
mach_msg_type_number_t tinfo_cnt;
tinfo_cnt = THREAD_INFO_MAX;
- kr = thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)tinfo_d, &tinfo_cnt);
+ thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)tinfo_d, &tinfo_cnt);
tinfo_b = (thread_basic_info_t)tinfo_d;
if (!(tinfo_b->flags & TH_FLAGS_IDLE))
--
2.34.1