-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unnecessary print and add inspect locals example
- Loading branch information
Showing
3 changed files
with
76 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# This has not been implemented in the library yet, for testing purposes only | ||
# | ||
# It will either be implemented as a decorator composable with @test | ||
# For example - | ||
# @test(...) | ||
# @inspect_locals | ||
# def foo(...): | ||
# ... | ||
# | ||
# or as a part of @test | ||
# For example - | ||
# @test(..., inspect_locals=True) | ||
# def foo(...): | ||
# ... | ||
|
||
|
||
import sys | ||
import types | ||
|
||
|
||
# https://stackoverflow.com/a/52358426 | ||
def call_function_get_frame(func, *args, **kwargs): | ||
frame: types.FrameType | None = None | ||
trace = sys.gettrace() | ||
|
||
def snatch_locals(_frame, name, arg): | ||
nonlocal frame | ||
if frame is None and name == "call": | ||
frame = _frame | ||
sys.settrace(trace) | ||
return trace | ||
|
||
sys.settrace(snatch_locals) | ||
try: | ||
result = func(*args, **kwargs) | ||
finally: | ||
sys.settrace(trace) | ||
return frame, result | ||
|
||
|
||
def inspect_locals(func): | ||
frame, result = call_function_get_frame(func) | ||
print(f"Local variables: {frame.f_locals}") | ||
print(f"Result: {result}") | ||
|
||
return func | ||
|
||
|
||
@inspect_locals | ||
def hello(): | ||
a = 5 | ||
|
||
for _ in range(5): | ||
a += 1 | ||
print("Hello, World!") | ||
|
||
return 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters