Skip to content

some hy/macro pdb commands - #2721

Closed
atisharma wants to merge 1 commit into
hylang:masterfrom
atisharma:pdb-macros
Closed

some hy/macro pdb commands#2721
atisharma wants to merge 1 commit into
hylang:masterfrom
atisharma:pdb-macros

Conversation

@atisharma

@atisharma atisharma commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

I tried to keep this miminal. This PR ameliorates, but probably wouldn't close, #741, but I thought it was worth mooting for discussion. No hard feelings if you feel it's misconceived.

Adding breakpoints in macros already works (surprisingly), so this PR is not needed for that. However it adds (via a new HyPdb subclass of Pdb) the following pdb commands that incrementally help qol in the debugger.

  • macros/m: list module macros
  • hy_repr/hy: Hy representation of Python objects
  • macroexpand/me: full macro expansion
  • macroexpand_1/me1: single-step expansion

It installs via sys.breakpointhook when hy is imported.

For example:

(import hy)

(defmacro squared [x]
  (print f"  [macro] Expanding (squared {x})")
  `(* ~x ~x))

(defmacro sum-of-squares [a b]
  (print f"  [macro] Expanding (sum-of-squares {a} {b})")
  `(+ (squared ~a) (squared ~b)))

(defn compute []
  (setv code (hy.read "(-> data :users (get 0) :name)"))
  (print "Computing sum of squares...")
  (breakpoint)
  (setv result (sum-of-squares 3 4))
  (print f"Result: {result}"))

(compute)
» hy debugging_demo.hy
  [macro] Expanding (sum-of-squares hy.models.Integer(3) hy.models.Integer(4))
  [macro] Expanding (squared hy.models.Integer(3))
  [macro] Expanding (squared hy.models.Integer(4))
Computing sum of squares...
> /tmp/debugging_demo.hy(14)compute()
-> (breakpoint)
(Pdb) ll
 11     (defn compute []
 12       (setv code (hy.read "(-> data :users (get 0) :name)"))
 13       (print "Computing sum of squares...")
 14  ->   (breakpoint)
 15       (setv result (sum-of-squares 3 4))
 16       (print f"Result: {result}"))
(Pdb) p code
hy.models.Expression([
  hy.models.Symbol('->'),
  hy.models.Symbol('data'),
  hy.models.Keyword('users'),
  hy.models.Expression([
    hy.models.Symbol('get'),
    hy.models.Integer(0)]),
  hy.models.Keyword('name')])
(Pdb) hy code
'(-> data :users (get 0) :name)
(Pdb) m
Macros (2):
  squared
  sum-of-squares
(Pdb) p sum_of_squares
*** NameError: name 'sum_of_squares' is not defined
(Pdb) me1 (sum-of-squares a b)
  [macro] Expanding (sum-of-squares a b)
'(+ (squared a) (squared b))
(Pdb) me (sum-of-squares 1 2)
  [macro] Expanding (sum-of-squares hy.models.Integer(1) hy.models.Integer(2))
  [macro] Expanding (squared hy.models.Integer(1))
  [macro] Expanding (squared hy.models.Integer(2))
'(+ (squared 1) (squared 2))
(Pdb) p sum_of_squares
*** NameError: name 'sum_of_squares' is not defined
(Pdb) n
> /tmp/debugging_demo.hy(15)compute()
-> (setv result (sum-of-squares 3 4))
(Pdb) n
> /tmp/debugging_demo.hy(16)compute()
-> (print f"Result: {result}"))
(Pdb) p result
25
(Pdb) c
Result: 25

@atisharma

Copy link
Copy Markdown
Contributor Author

One concern with the mechanism: import hy always overrides the breakpointhook. This would clobber your own custom breakpointhook setup if you have one in python, for instance. Maybe do it only if hy is the entrypoint?

Adds HyPdb class with commands:
- macroexpand/me: full macro expansion
- macroexpand_1/me1: single-step expansion
- macros/m: list module macros
- hy_repr/hy: Hy representation of Python objects

Installs via sys.breakpointhook when hy is imported.
@Kodiologist

Copy link
Copy Markdown
Member

I think the linked issue isn't really about the debugger; it's about position data. I added a comment and retitled it to focus it a bit. This PR doesn't address the positioning problem.

Taking the PR on its own merits, it's adding new PDB features that will call for documentation etc. But I have been loath to provide any official support for PDB in Hy because it feels like a rabbit hole that's never going to entirely work (and will likely be highly inconsistent between Python versions and implementations). So if you want to pursue this, I think it's better done as a separate library.

@atisharma

Copy link
Copy Markdown
Contributor Author

Thanks for taking a look. Stepping through macros with line numbers attached is beyond my appetite.
I'll spin it off, no problem.

@atisharma atisharma closed this Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants