-
Notifications
You must be signed in to change notification settings - Fork 8
/
pydbg.py
47 lines (31 loc) · 994 Bytes
/
pydbg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""pydbg is an implementation of the Rust builtin `dbg!` for Python."""
import inspect
import sys
import typing
__version__ = "0.3.0"
_ExpType = typing.TypeVar('_ExpType')
def dbg(exp: _ExpType) -> _ExpType:
"""Call dbg with any variable or expression.
Calling dbg will print to stderr the current filename and lineno,
as well as the passed expression and what the expression evaluates to:
from pydbg import dbg
a = 2
b = 5
dbg(a+b)
def square(x: int) -> int:
return x * x
dbg(square(a))
"""
for frame in inspect.stack():
line = frame.code_context[0]
if "dbg" in line:
start = line.find('(') + 1
end = line.rfind(')')
if end == -1:
end = len(line)
print(
f"[{frame.filename}:{frame.lineno}] {line[start:end]} = {exp!r}",
file=sys.stderr,
)
break
return exp