Skip to content

Commit

Permalink
Update readme and add inspect locals example
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Jun 7, 2024
1 parent e6a954a commit 01c082a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

## About

A simple test case runner in Python that uses TOML configurations and decorator syntax.
A simple test case runner in Python that uses data in the format of a TOML file or dictionary and uses decorator syntax.

## Installation

`pip install pysvt`

## Upgrade version

`pip install --upgrade pysvt`

## Usage

Check the [examples directory](https://github.com/dhzdhd/pysvt/tree/master/examples) for more elaborate examples.

- Function

Python
Expand Down
53 changes: 53 additions & 0 deletions examples/inspect_locals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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


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

0 comments on commit 01c082a

Please sign in to comment.