Skip to content

Commit

Permalink
Merge pull request #2529 from Kodiologist/hy-debug
Browse files Browse the repository at this point in the history
Overhaul debugging environment variables
  • Loading branch information
Kodiologist committed Nov 12, 2023
2 parents 43e5bcf + 1c7e0dd commit 8eb4d6b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
2 changes: 2 additions & 0 deletions NEWS.rst
Expand Up @@ -11,6 +11,8 @@ Removals
or Python's built-in `keyword` module instead.
* `doc` has been removed. Use `(help (get-macro foo))` or `(help
(get-macro :reader foo))` instead.
* The environment variables `HY_DEBUG` and `HY_FILTER_INTERNAL_ERRORS`
have been replaced with `HY_SHOW_INTERNAL_ERRORS`.

Breaking Changes
------------------------------
Expand Down
9 changes: 2 additions & 7 deletions docs/env_var.rst
Expand Up @@ -11,14 +11,9 @@ set to anything else.
(Default: nothing) Path to a file containing Hy source code to execute when
starting the REPL.

.. envvar:: HY_DEBUG
.. envvar:: HY_SHOW_INTERNAL_ERRORS

(Default: false) Does something mysterious that's probably similar to
``HY_FILTER_INTERNAL_ERRORS``.

.. envvar:: HY_FILTER_INTERNAL_ERRORS

(Default: true) Whether to hide some parts of tracebacks that point to
(Default: false) Whether to show some parts of tracebacks that point to
internal Hy code and won't be helpful to the typical Hy user.

.. envvar:: HY_HISTORY
Expand Down
24 changes: 10 additions & 14 deletions hy/errors.py
Expand Up @@ -8,7 +8,7 @@
from hy import _initialize_env_var
from hy._compat import PYPY

_hy_filter_internal_errors = _initialize_env_var("HY_FILTER_INTERNAL_ERRORS", True)
_hy_show_internal_errors = _initialize_env_var("HY_SHOW_INTERNAL_ERRORS", False)


class HyError(Exception):
Expand Down Expand Up @@ -270,11 +270,11 @@ def hy_exc_filter(exc_type, exc_value, exc_traceback):

def hy_exc_handler(exc_type, exc_value, exc_traceback):
"""A `sys.excepthook` handler that uses `hy_exc_filter` to
remove internal Hy frames from a traceback print-out.
remove internal Hy frames from a traceback print-out, so long
as `_hy_show_internal_errors` is false.
"""
if os.environ.get("HY_DEBUG", False):
if _hy_show_internal_errors:
return sys.__excepthook__(exc_type, exc_value, exc_traceback)

try:
output = hy_exc_filter(exc_type, exc_value, exc_traceback)
sys.stderr.write(output)
Expand All @@ -289,14 +289,10 @@ def filtered_hy_exceptions():
from tracebacks.
Filtering can be controlled by the variable
`hy.errors._hy_filter_internal_errors` and environment variable
`HY_FILTER_INTERNAL_ERRORS`.
`hy.errors._hy_show_internal_errors` and environment variable
`HY_SHOW_INTERNAL_ERRORS`.
"""
global _hy_filter_internal_errors
if _hy_filter_internal_errors:
current_hook = sys.excepthook
sys.excepthook = hy_exc_handler
yield
sys.excepthook = current_hook
else:
yield
current_hook = sys.excepthook
sys.excepthook = hy_exc_handler
yield
sys.excepthook = current_hook
3 changes: 0 additions & 3 deletions tests/test_bin.py
Expand Up @@ -410,9 +410,6 @@ def test_macro_require():
def test_tracebacks():
"""Make sure the printed tracebacks are correct."""

# We want the filtered tracebacks.
os.environ["HY_DEBUG"] = ""

def req_err(x):
assert x == "hy.errors.HyRequireError: No module named 'not_a_real_module'"

Expand Down

0 comments on commit 8eb4d6b

Please sign in to comment.