]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-98878: Use builtins from the bound frame when offering a suggestion (#98880)
authorBatuhan Taskaya <isidentical@gmail.com>
Mon, 31 Oct 2022 13:27:13 +0000 (16:27 +0300)
committerGitHub <noreply@github.com>
Mon, 31 Oct 2022 13:27:13 +0000 (13:27 +0000)
Lib/test/test_traceback.py
Lib/traceback.py
Misc/NEWS.d/next/Library/2022-10-30-22-42-48.gh-issue-98878.fgrykp.rst [new file with mode: 0644]

index 56b168735d15098c2b388f3a1ffa145369c45a3e..149d0234fe8a72b77a440943e0f503af1576b526 100644 (file)
@@ -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
index 0f0f2b317de264f73b1e428a9530ce9995452d94..cf5f355ff04c3b359cc9c0da529d536842f521bb 100644 (file)
@@ -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
diff --git a/Misc/NEWS.d/next/Library/2022-10-30-22-42-48.gh-issue-98878.fgrykp.rst b/Misc/NEWS.d/next/Library/2022-10-30-22-42-48.gh-issue-98878.fgrykp.rst
new file mode 100644 (file)
index 0000000..e83422a
--- /dev/null
@@ -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.