mirror of
https://github.com/python/cpython.git
synced 2024-11-24 00:38:00 +01:00
gh-98878: Use builtins from the bound frame when offering a suggestion (#98880)
This commit is contained in:
parent
39448adc9d
commit
a41de32942
@ -7,6 +7,7 @@ import sys
|
||||
import types
|
||||
import inspect
|
||||
import importlib
|
||||
import builtins
|
||||
import unittest
|
||||
import re
|
||||
import tempfile
|
||||
@ -3209,6 +3210,14 @@ class SuggestionFormattingTestBase:
|
||||
actual = self.get_suggestion(func)
|
||||
self.assertIn("'ZeroDivisionError'?", actual)
|
||||
|
||||
def test_name_error_suggestions_from_builtins_when_builtins_is_module(self):
|
||||
def func():
|
||||
custom_globals = globals().copy()
|
||||
custom_globals["__builtins__"] = builtins
|
||||
print(eval("ZeroDivisionErrrrr", custom_globals))
|
||||
actual = self.get_suggestion(func)
|
||||
self.assertIn("'ZeroDivisionError'?", actual)
|
||||
|
||||
def test_name_error_suggestions_do_not_trigger_for_long_names(self):
|
||||
def func():
|
||||
somethingverywronghehehehehehe = None
|
||||
|
@ -1035,7 +1035,7 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
|
||||
d = (
|
||||
list(frame.f_locals)
|
||||
+ list(frame.f_globals)
|
||||
+ list(frame.f_globals['__builtins__'])
|
||||
+ list(frame.f_builtins)
|
||||
)
|
||||
if len(d) > _MAX_CANDIDATE_ITEMS:
|
||||
return None
|
||||
|
@ -0,0 +1,2 @@
|
||||
Use the frame bound builtins when offering a name suggestion in
|
||||
:mod:`traceback` to prevent crashing when ``__builtins__`` is not a dict.
|
Loading…
Reference in New Issue
Block a user