Skip to content

Commit d86178f

Browse files
author
zouyu.zzx
committed
fix: supports keyword only method spy
1 parent 615090f commit d86178f

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

flight_profiler/common/bytecode_transformer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ def _execute_bytecode_transform_intern(
9393
copy_fn = types.FunctionType(
9494
fn.__code__, global_space, fn.__name__, fn.__defaults__, fn.__closure__
9595
)
96+
copy_fn.__kwdefaults__ = fn.__kwdefaults__
97+
copy_fn.__annotations__ = fn.__annotations__
98+
copy_fn.__dict__.update(fn.__dict__) # 别忘了同步装饰器可能加上的其他属性
99+
copy_fn.__qualname__ = fn.__qualname__
96100

97101
if sys.version_info >= (3, 11):
98102
# emit COPY_FREE_VARIABLE

flight_profiler/test/plugins/watch/watch_plugin_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@ def test_watch_module_method(self):
3535
raise
3636
finally:
3737
integration.stop()
38+
39+
def test_watch_keyword_only_module_method(self):
40+
current_directory = os.path.dirname(os.path.abspath(__file__))
41+
file = os.path.join(current_directory, "watch_server_script.py")
42+
integration = ProfileIntegration()
43+
integration.start(file, 15)
44+
try:
45+
integration.execute_profile_cmd(
46+
"watch __main__ kwonly_func --expr {args,return_obj}"
47+
)
48+
process = integration.client_process
49+
find = False
50+
start = time.time()
51+
while time.time() - start < 15:
52+
output = process.stdout.readline()
53+
print(output)
54+
if output:
55+
line = str(output)
56+
if line.find("kwonly_func_called") >= 0:
57+
find = True
58+
break
59+
else:
60+
break
61+
62+
self.assertTrue(find)
63+
except:
64+
raise
65+
finally:
66+
integration.stop()
3867

3968
def test_watch_module_class_nested_method(self):
4069
current_directory = os.path.dirname(os.path.abspath(__file__))
@@ -151,3 +180,5 @@ def test_watch_module_exp_method(self):
151180
raise
152181
finally:
153182
integration.stop()
183+
184+

flight_profiler/test/plugins/watch/watch_server_script.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def nested_func_inner():
1818
return test_func("nested_method")
1919
return nested_func_inner()
2020

21+
def kwonly_func(x, y, *, c=1):
22+
print("kwonly_func called")
23+
24+
2125
def test_func(name):
2226
print("hello func")
2327
return name + " " + "watch_plugin"
@@ -35,5 +39,6 @@ def exp_func():
3539
A().hello()
3640
A().nested_method()
3741
A.cls_method()
42+
kwonly_func("kwonly_func_called", 2)
3843
time.sleep(1)
3944
idx += 1

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "flight_profiler"
3-
version = "1.0.3"
3+
version = "1.0.4"
44
description = "A diagnostic toolkit for on-the-fly analysis of remote python processes."
55
homepage = "https://github.com/alibaba/PyFlightProfiler"
66
readme = "README.md"

0 commit comments

Comments
 (0)