mirror of
https://github.com/python/cpython.git
synced 2025-09-09 18:32:22 +00:00
bpo-38530: Include builtins in NameError suggestions (GH-25460)
This commit is contained in:
parent
7f1305ef9e
commit
3ab4bea5a3
2 changed files with 31 additions and 5 deletions
|
@ -1477,6 +1477,16 @@ class NameErrorTests(unittest.TestCase):
|
||||||
sys.__excepthook__(*sys.exc_info())
|
sys.__excepthook__(*sys.exc_info())
|
||||||
self.assertIn("global_for_suggestions?", err.getvalue())
|
self.assertIn("global_for_suggestions?", err.getvalue())
|
||||||
|
|
||||||
|
def test_name_error_suggestions_from_builtins(self):
|
||||||
|
def func():
|
||||||
|
print(AttributeErrop)
|
||||||
|
try:
|
||||||
|
func()
|
||||||
|
except NameError as exc:
|
||||||
|
with support.captured_stderr() as err:
|
||||||
|
sys.__excepthook__(*sys.exc_info())
|
||||||
|
self.assertIn("AttributeError?", err.getvalue())
|
||||||
|
|
||||||
def test_name_error_suggestions_do_not_trigger_for_long_names(self):
|
def test_name_error_suggestions_do_not_trigger_for_long_names(self):
|
||||||
def f():
|
def f():
|
||||||
somethingverywronghehehehehehe = None
|
somethingverywronghehehehehehe = None
|
||||||
|
@ -1490,7 +1500,7 @@ class NameErrorTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertNotIn("somethingverywronghehe", err.getvalue())
|
self.assertNotIn("somethingverywronghehe", err.getvalue())
|
||||||
|
|
||||||
def test_name_error_suggestions_do_not_trigger_for_big_dicts(self):
|
def test_name_error_suggestions_do_not_trigger_for_too_many_locals(self):
|
||||||
def f():
|
def f():
|
||||||
# Mutating locals() is unreliable, so we need to do it by hand
|
# Mutating locals() is unreliable, so we need to do it by hand
|
||||||
a1 = a2 = a3 = a4 = a5 = a6 = a7 = a8 = a9 = a10 = a11 = a12 = a13 = \
|
a1 = a2 = a3 = a4 = a5 = a6 = a7 = a8 = a9 = a10 = a11 = a12 = a13 = \
|
||||||
|
@ -1501,7 +1511,13 @@ class NameErrorTests(unittest.TestCase):
|
||||||
a62 = a63 = a64 = a65 = a66 = a67 = a68 = a69 = a70 = a71 = a72 = a73 = \
|
a62 = a63 = a64 = a65 = a66 = a67 = a68 = a69 = a70 = a71 = a72 = a73 = \
|
||||||
a74 = a75 = a76 = a77 = a78 = a79 = a80 = a81 = a82 = a83 = a84 = a85 = \
|
a74 = a75 = a76 = a77 = a78 = a79 = a80 = a81 = a82 = a83 = a84 = a85 = \
|
||||||
a86 = a87 = a88 = a89 = a90 = a91 = a92 = a93 = a94 = a95 = a96 = a97 = \
|
a86 = a87 = a88 = a89 = a90 = a91 = a92 = a93 = a94 = a95 = a96 = a97 = \
|
||||||
a98 = a99 = a100 = a101 = a102 = a103 = None
|
a98 = a99 = a100 = a101 = a102 = a103 = a104 = a105 = a106 = a107 = \
|
||||||
|
a108 = a109 = a110 = a111 = a112 = a113 = a114 = a115 = a116 = a117 = \
|
||||||
|
a118 = a119 = a120 = a121 = a122 = a123 = a124 = a125 = a126 = \
|
||||||
|
a127 = a128 = a129 = a130 = a131 = a132 = a133 = a134 = a135 = a136 = \
|
||||||
|
a137 = a138 = a139 = a140 = a141 = a142 = a143 = a144 = a145 = \
|
||||||
|
a146 = a147 = a148 = a149 = a150 = a151 = a152 = a153 = a154 = a155 = \
|
||||||
|
a156 = a157 = a158 = a159 = a160 = a161 = None
|
||||||
print(a0)
|
print(a0)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -1510,7 +1526,7 @@ class NameErrorTests(unittest.TestCase):
|
||||||
with support.captured_stderr() as err:
|
with support.captured_stderr() as err:
|
||||||
sys.__excepthook__(*sys.exc_info())
|
sys.__excepthook__(*sys.exc_info())
|
||||||
|
|
||||||
self.assertNotIn("a10", err.getvalue())
|
self.assertNotIn("a1", err.getvalue())
|
||||||
|
|
||||||
def test_name_error_with_custom_exceptions(self):
|
def test_name_error_with_custom_exceptions(self):
|
||||||
def f():
|
def f():
|
||||||
|
@ -1643,7 +1659,7 @@ class AttributeErrorTests(unittest.TestCase):
|
||||||
blech = None
|
blech = None
|
||||||
# A class with a very big __dict__ will not be consider
|
# A class with a very big __dict__ will not be consider
|
||||||
# for suggestions.
|
# for suggestions.
|
||||||
for index in range(101):
|
for index in range(160):
|
||||||
setattr(A, f"index_{index}", None)
|
setattr(A, f"index_{index}", None)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#include "pycore_pyerrors.h"
|
#include "pycore_pyerrors.h"
|
||||||
|
|
||||||
#define MAX_DISTANCE 3
|
#define MAX_DISTANCE 3
|
||||||
#define MAX_CANDIDATE_ITEMS 100
|
#define MAX_CANDIDATE_ITEMS 160
|
||||||
#define MAX_STRING_SIZE 25
|
#define MAX_STRING_SIZE 25
|
||||||
|
|
||||||
/* Calculate the Levenshtein distance between string1 and string2 */
|
/* Calculate the Levenshtein distance between string1 and string2 */
|
||||||
|
@ -171,6 +171,16 @@ offer_suggestions_for_name_error(PyNameErrorObject *exc) {
|
||||||
}
|
}
|
||||||
suggestions = calculate_suggestions(dir, name);
|
suggestions = calculate_suggestions(dir, name);
|
||||||
Py_DECREF(dir);
|
Py_DECREF(dir);
|
||||||
|
if (suggestions != NULL) {
|
||||||
|
return suggestions;
|
||||||
|
}
|
||||||
|
|
||||||
|
dir = PySequence_List(frame->f_builtins);
|
||||||
|
if (dir == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
suggestions = calculate_suggestions(dir, name);
|
||||||
|
Py_DECREF(dir);
|
||||||
|
|
||||||
return suggestions;
|
return suggestions;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue