Skip to content

sys.last_exc not set upon exception #2674

@jsquaredosquared

Description

@jsquaredosquared

Using pdb, one can enter post-mortem debugging after an exception is raised by invoking pdb.pm. However, because pdb.pm relies on the sys.last_exc variable to be set, it doesn't work in Hy.

Steps to reproduce
Hy 1.1.0 (Business Hugs) using CPython(main) 3.13.9 on Linux
=> (import pdb)
=> (/ 1 0)
Traceback (most recent call last):
  File "stdin-7b3ace8766f1e1cfb3ae7c01a1a61cebed24f482", line 1, in <module>
    (/ 1 0)
     ^^^^^^
ZeroDivisionError: division by zero
=> (pdb.pm)
Traceback (most recent call last):
  File "stdin-c488e1048949edc04280fcc81645aec6dacfacf2", line 1, in <module>
    (pdb.pm)
     ^^^^^^^
  File "[...]/default/lib/python3.13/pdb.py", line 2410, in pm
    post_mortem(sys.last_exc)
                ^^^^^^^^^^^^
AttributeError: module 'sys' has no attribute 'last_exc'
=> (import sys)
=> (sys.last_exc)
Traceback (most recent call last):
  File "stdin-40af2c7b8fb01a3a56d9c709d1da6fe79ea3925b", line 1, in <module>
    (sys.last_exc)
      ^^^^^^^^^^^
AttributeError: module 'sys' has no attribute 'last_exc'
=> (sys.exc_info)
#(None None None)
=> sys.last_traceback
<traceback object at 0x7f13d3a07040>
=> sys.last_type
<class 'AttributeError'>
=> sys.last_value
AttributeError("module 'sys' has no attribute 'last_exc'")

It looks like the Hy REPL is still using these three variables, which are deprecated:

hy/hy/repl.py

Lines 168 to 172 in 7e3e0d5

except Exception as e:
# Capture and save the error before we handle further
sys.last_type, sys.last_value, sys.last_traceback = sys.exc_info()
self._update_exc_info()

I can still enter post-mortem debugging by using pdb.post_mortem(sys.last_traceback):

=> (pdb.post_mortem sys.last_traceback)
> [...]/stdin-98d7e1f6385932f80c64e8b078c1165d986c6c2c(1)<module>()
(Pdb) 

...but because sys.last_traceback is deprecated, perhaps Hy should switch to using sys.last_exc? A search of the codebase shows that these deprecated variables are used in other places as well.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions