1. """pydbg is an implementation of the Rust2018 builtin `dbg` for Python."""
    2. import inspect
    3. import sys
    4. import typing
    5. __version__ = "0.3.0"
    6. _ExpType = typing.TypeVar('_ExpType')
    7. def dbg(exp: _ExpType) -> _ExpType:
    8. """Call dbg with any variable or expression.
    9. Calling debug will print out the content information (file, lineno) as wil as the
    10. passed expression and what the expression is equal to::
    11. from pydbg import dbg
    12. a = 2
    13. b = 5
    14. dbg(a+b)
    15. def square(x: int) -> int:
    16. return x * x
    17. dbg(square(a))
    18. """
    19. for frame in inspect.stack():
    20. line = frame.code_context[0]
    21. if "dbg" in line:
    22. start = line.find('(') + 1
    23. end = line.rfind(')')
    24. if end == -1:
    25. end = len(line)
    26. print(
    27. f"[{frame.filename}:{frame.lineno}] {line[start:end]} = {exp!r}",
    28. file=sys.stderr,
    29. )
    30. break
    31. return exp