Skip to content

Commit

Permalink
skip test on cpy < 3.10 and pypy < 3.8, fix pypy difference in logic …
Browse files Browse the repository at this point in the history
…about builtins
  • Loading branch information
cfbolz committed Nov 14, 2022
1 parent 5f9fce7 commit bba5d63
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/exceptiongroup/_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,15 @@ def _compute_suggestion_error(exc_value, tb):
while tb.tb_next is not None:
tb = tb.tb_next
frame = tb.tb_frame
d = list(frame.f_locals) + list(frame.f_globals) + dir(__builtins__)

d = list(frame.f_locals) + list(frame.f_globals)
# NB: __builtins__ can be either a dict or a module here
builtins = frame.f_globals["__builtins__"]
if isinstance(builtins, dict):
d.extend(builtins)
else:
# module
d.extend(dir(builtins))
if len(d) > _MAX_CANDIDATE_ITEMS:
return None
wrong_name_len = len(wrong_name)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ def test_print_exc(
)


@pytest.mark.skipif(
not hasattr(NameError, "name"),
reason="only works if NameError exposes the missing name",
)
def test_nameerror_suggestions(
patched: bool, monkeypatch: MonkeyPatch, capsys: CaptureFixture
) -> None:
Expand All @@ -474,4 +478,4 @@ def test_nameerror_suggestions(
except NameError:
print_exc()
output = capsys.readouterr().err
assert "Did you mean 'filter'?" in output
assert "Did you mean: 'filter'?" in output

0 comments on commit bba5d63

Please sign in to comment.