Skip to content

gh-110721: Remove unused code from suggestions.c after moving PyErr_Display to use the traceback module #113712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,6 @@ struct _Py_global_strings {
STRUCT_FOR_ID(seek)
STRUCT_FOR_ID(seekable)
STRUCT_FOR_ID(selectors)
STRUCT_FOR_ID(self)
STRUCT_FOR_ID(send)
STRUCT_FOR_ID(sep)
STRUCT_FOR_ID(sequence)
Expand Down
1 change: 0 additions & 1 deletion Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,13 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
if hasattr(self, wrong_name):
return f"self.{wrong_name}"

try:
import _suggestions
except ImportError:
pass
else:
return _suggestions._generate_suggestions(d, wrong_name)

# Compute closest match

if len(d) > _MAX_CANDIDATE_ITEMS:
Expand Down
1 change: 1 addition & 0 deletions Modules/Setup.bootstrap.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ faulthandler faulthandler.c
posix posixmodule.c
_signal signalmodule.c
_tracemalloc _tracemalloc.c
_suggestions _suggestions.c

# modules used by importlib, deepfreeze, freeze, runpy, and sysconfig
_codecs _codecsmodule.c
Expand Down
63 changes: 63 additions & 0 deletions Modules/_suggestions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "Python.h"
#include "pycore_pyerrors.h"
#include "clinic/_suggestions.c.h"

/*[clinic input]
module _suggestions
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e58d81fafad5637b]*/

/*[clinic input]
_suggestions._generate_suggestions
candidates: object
item: unicode
/
Returns the candidate in candidates that's closest to item
[clinic start generated code]*/

static PyObject *
_suggestions__generate_suggestions_impl(PyObject *module,
PyObject *candidates, PyObject *item)
/*[clinic end generated code: output=79be7b653ae5e7ca input=ba2a8dddc654e33a]*/
{
// Check if dir is a list
if (!PyList_Check(candidates)) {
PyErr_SetString(PyExc_TypeError, "candidates must be a list");
return NULL;
}

// Check if all elements in the list are Unicode
Py_ssize_t size = PyList_Size(candidates);
for (Py_ssize_t i = 0; i < size; ++i) {
PyObject *elem = PyList_GetItem(candidates, i);
if (!PyUnicode_Check(elem)) {
PyErr_SetString(PyExc_TypeError, "all elements in 'candidates' must be strings");
return NULL;
}
}

PyObject* result = _Py_CalculateSuggestions(candidates, item);
if (!result && !PyErr_Occurred()) {
Py_RETURN_NONE;
}
return result;
}


static PyMethodDef module_methods[] = {
_SUGGESTIONS__GENERATE_SUGGESTIONS_METHODDEF
{NULL, NULL, 0, NULL} // Sentinel
};

static struct PyModuleDef suggestions_module = {
PyModuleDef_HEAD_INIT,
"_suggestions",
NULL,
-1,
module_methods
};

PyMODINIT_FUNC PyInit__suggestions(void) {
return PyModule_Create(&suggestions_module);
}

41 changes: 41 additions & 0 deletions Modules/clinic/_suggestions.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions PCbuild/pythoncore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@
<ClInclude Include="..\Modules\_sre\sre_lib.h" />
<ClCompile Include="..\Modules\_stat.c" />
<ClCompile Include="..\Modules\_struct.c" />
<ClCompile Include="..\Modules\_suggestions.c" />
<ClCompile Include="..\Modules\_weakref.c" />
<ClCompile Include="..\Modules\arraymodule.c" />
<ClCompile Include="..\Modules\atexitmodule.c" />
Expand Down
3 changes: 3 additions & 0 deletions PCbuild/pythoncore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,9 @@
<ClCompile Include="..\Modules\_struct.c">
<Filter>Modules</Filter>
</ClCompile>
<ClCompile Include="..\Modules\_suggestions.c">
<Filter>Modules</Filter>
</ClCompile>
<ClCompile Include="..\Modules\_weakref.c">
<Filter>Modules</Filter>
</ClCompile>
Expand Down
1 change: 1 addition & 0 deletions Python/stdlib_module_names.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading