[Python-checkins] gh-98878: Use builtins from the bound frame when offering a suggestion (#98880)
pablogsal
webhook-mailer at python.org
Mon Oct 31 09:27:19 EDT 2022
https://github.com/python/cpython/commit/a41de32942b7ac14524587a08d219f3bfe346415
commit: a41de32942b7ac14524587a08d219f3bfe346415
branch: main
author: Batuhan Taskaya <isidentical at gmail.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2022-10-31T13:27:13Z
summary:
gh-98878: Use builtins from the bound frame when offering a suggestion (#98880)
files:
A Misc/NEWS.d/next/Library/2022-10-30-22-42-48.gh-issue-98878.fgrykp.rst
M Lib/test/test_traceback.py
M Lib/traceback.py
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 56b168735d15..149d0234fe8a 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -7,6 +7,7 @@
import types
import inspect
import importlib
+import builtins
import unittest
import re
import tempfile
@@ -3209,6 +3210,14 @@ def func():
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
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 0f0f2b317de2..cf5f355ff04c 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -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
index 000000000000..e83422a77a22
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-10-30-22-42-48.gh-issue-98878.fgrykp.rst
@@ -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.
More information about the Python-checkins
mailing list