[3.13] gh-130999: Avoid exiting the new REPL when there are non-string candidates for suggestions (gh-131001) (gh-135020)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
Miss Islington (bot) 2025-06-02 16:58:29 +02:00 committed by GitHub
parent 11f7270e8f
commit 7ca17edd13
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 51 additions and 4 deletions

View file

@ -4176,6 +4176,15 @@ def __dir__(self):
self.assertNotIn("blech", actual)
self.assertNotIn("oh no!", actual)
def test_attribute_error_with_non_string_candidates(self):
class T:
bluch = 1
instance = T()
instance.__dict__[0] = 1
actual = self.get_suggestion(instance, 'blich')
self.assertIn("bluch", actual)
def test_attribute_error_with_bad_name(self):
def raise_attribute_error_with_bad_name():
raise AttributeError(name=12, obj=23)
@ -4211,8 +4220,8 @@ def make_module(self, code):
return mod_name
def get_import_from_suggestion(self, mod_dict, name):
modname = self.make_module(mod_dict)
def get_import_from_suggestion(self, code, name):
modname = self.make_module(code)
def callable():
try:
@ -4289,6 +4298,13 @@ def test_import_from_suggestions_underscored(self):
self.assertIn("'_bluch'", self.get_import_from_suggestion(code, '_luch'))
self.assertNotIn("'_bluch'", self.get_import_from_suggestion(code, 'bluch'))
def test_import_from_suggestions_non_string(self):
modWithNonStringAttr = textwrap.dedent("""\
globals()[0] = 1
bluch = 1
""")
self.assertIn("'bluch'", self.get_import_from_suggestion(modWithNonStringAttr, 'blech'))
def test_import_from_suggestions_do_not_trigger_for_long_attributes(self):
code = "blech = None"
@ -4385,6 +4401,15 @@ def func():
actual = self.get_suggestion(func)
self.assertIn("'ZeroDivisionError'?", actual)
def test_name_error_suggestions_with_non_string_candidates(self):
def func():
abc = 1
custom_globals = globals().copy()
custom_globals[0] = 1
print(eval("abv", custom_globals, locals()))
actual = self.get_suggestion(func)
self.assertIn("abc", actual)
def test_name_error_suggestions_do_not_trigger_for_long_names(self):
def func():
somethingverywronghehehehehehe = None