Skip to content

Commit

Permalink
Overhaul debugging environment variables
Browse files Browse the repository at this point in the history
`HY_DEBUG` and `HY_FILTER_INTERNAL_ERRORS` were redundant in intention, but `HY_DEBUG` worked more reliably because `hy_exc_handler` is called explicitly in a few places. The new environment variable `HY_SHOW_INTERNAL_ERRORS` uses a more specific name while avoiding the awkwardness of how `HY_FILTER_INTERNAL_ERRORS` had to be set to an empty string to have an effect.

The new environment variable is still untested, because it's only for internal use, and is documented merely for completeness.
  • Loading branch information
Kodiologist committed Nov 10, 2023
1 parent 43e5bcf commit 1c7e0dd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
2 changes: 2 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 1c7e0dd

Please sign in to comment.