Skip to content

Commit

Permalink
Allow dbg[] to work in the REPL, too.
Browse files Browse the repository at this point in the history
Resolves #12.
  • Loading branch information
Technologicat committed Mar 1, 2020
1 parent 1112f1f commit f4da595
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ If you're still stuck on 3.4 and find something in the latest `unpythonic` 0.14.
**Non-breaking changes**:

- `setescape`/`escape` have been renamed `catch`/`throw`, to match the standard terminology in the Lisp family. **The old nonstandard names are now deprecated, and will be removed in 0.15.0.**
- The `dbg[]` macro now works in the REPL, too.
- Move macro documentation to `doc/macros.md`. (Was `macro_extras/README.md`.)

**Fixed**:
Expand Down
6 changes: 3 additions & 3 deletions doc/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Our extensions to the Python language are built on [MacroPy](https://github.com/

Because in Python macro expansion occurs *at import time*, Python programs whose main module uses macros, such as [our unit tests that contain usage examples](../unpythonic/syntax/test/), cannot be run directly. Instead, run them via the included [generic MacroPy3 bootstrapper](../macropy3). For convenience, ``setup.py`` installs this bootstrapper.

**The bootstrapper is moving!** *Starting with `unpythonic` v0.14.1, it is already distributed in [imacropy](https://github.com/Technologicat/imacropy) [[PyPI](https://pypi.org/project/imacropy/)], which is its new, permanent home. It will be removed from `unpythonic` starting in v0.15.0. The reason is the bootstrapper is a general add-on for MacroPy, not specific to `unpythonic`.*
**The bootstrapper is moving!** *Starting with `unpythonic` v0.14.1, it is already distributed in [imacropy](https://github.com/Technologicat/imacropy) [[PyPI](https://pypi.org/project/imacropy/)], which is its new, permanent home. It will be removed from `unpythonic` starting in v0.15.0. The reason is the bootstrapper is a general add-on for MacroPy, not specific to `unpythonic`. The `imacropy` package also contains an IPython extension that adds macro support to IPython, as well as an embeddable macro-enabled REPL.*

*This document doubles as the API reference, but despite maintenance on a best-effort basis, may occasionally be out of date at places. In case of conflicts in documentation, believe the unit tests first; specifically the code, not necessarily the comments. Everything else (comments, docstrings and this guide) should agree with the unit tests. So if something fails to work as advertised, check what the tests say - and optionally file an issue on GitHub so that the documentation can be fixed.*

Expand Down Expand Up @@ -1533,6 +1533,8 @@ This is similar to the JavaScript [`with` construct](https://developer.mozilla.o

### ``dbg``: debug-print expressions with source code

*Changed in 0.14.2.* The `dbg[]` macro now works in the REPL, too. Use your favorite macro-enabled REPL; both [`imacropy`](https://github.com/Technologicat/imacropy) and `macropy.console` work.

[DRY](https://en.wikipedia.org/wiki/Don't_repeat_yourself) out your [qnd](https://en.wiktionary.org/wiki/quick-and-dirty) debug printing code. Both block and expression variants are provided:

```python
Expand Down Expand Up @@ -1579,8 +1581,6 @@ For details on implementing custom debug print functions, see the docstrings of

**CAUTION**: The source code is back-converted from the AST representation; hence its surface syntax may look slightly different to the original (e.g. extra parentheses). See ``macropy.core.unparse``.

**CAUTION**: ``dbg`` only works in ``.py`` files, not in [the IPython+MacroPy console](https://github.com/azazel75/macropy/pull/20), because the expanded code refers to ``__file__``, which is not defined in the REPL. This limitation may or may not be lifted in a future version.

Inspired by the [dbg macro in Rust](https://doc.rust-lang.org/std/macro.dbg.html).

## Other
Expand Down
13 changes: 11 additions & 2 deletions unpythonic/syntax/dbg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
"""

from ast import Call, Name, Tuple, keyword
import inspect

from macropy.core.quotes import macros, q, u, ast_literal
from macropy.core.hquotes import macros, hq
from macropy.core.walkers import Walker
from macropy.core import unparse

def _callsite_filename():
stack = inspect.stack()
frame = stack[1].frame
filename = frame.f_code.co_filename
del frame, stack
return filename

def dbgprint_block(ks, vs, *, filename=None, lineno=None, sep=", ", **kwargs):
"""Default debug printer for the ``dbg`` macro, block variant.
Expand Down Expand Up @@ -84,7 +92,7 @@ def transform(tree, **kw):
values = Tuple(elts=tree.args, lineno=tree.lineno, col_offset=tree.col_offset)
tree.args = [names, values]
# can't use inspect.stack in the printer itself because we want the line number *before macro expansion*.
tree.keywords += [keyword(arg="filename", value=q[__file__]),
tree.keywords += [keyword(arg="filename", value=hq[_callsite_filename()]),
keyword(arg="lineno", value=(q[u[tree.lineno]] if hasattr(tree, "lineno") else q[None]))]
tree.func = q[ast_literal[p]]
return tree
Expand Down Expand Up @@ -130,4 +138,5 @@ def dbgprint_expr(k, v, *, filename, lineno):

def dbg_expr(tree):
ln = q[u[tree.lineno]] if hasattr(tree, "lineno") else q[None]
return q[dbgprint_expr(u[unparse(tree)], ast_literal[tree], filename=__file__, lineno=ast_literal[ln])]
filename = hq[_callsite_filename()]
return q[dbgprint_expr(u[unparse(tree)], ast_literal[tree], filename=ast_literal[filename], lineno=ast_literal[ln])]

0 comments on commit f4da595

Please sign in to comment.