Skip to content
Draft
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: 1 addition & 0 deletions Include/cpython/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ typedef struct {
typedef struct {
PyException_HEAD
PyObject *name;
PyObject *op;
} PyNameErrorObject;

typedef struct {
Expand Down
4 changes: 2 additions & 2 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ PyAPI_FUNC(int) _PyEval_CheckExceptStarTypeValid(PyThreadState *tstate, PyObject
PyAPI_FUNC(int) _PyEval_CheckExceptTypeValid(PyThreadState *tstate, PyObject* right);
PyAPI_FUNC(int) _PyEval_ExceptionGroupMatch(_PyInterpreterFrame *, PyObject* exc_value, PyObject *match_type, PyObject **match, PyObject **rest);
PyAPI_FUNC(void) _PyEval_FormatAwaitableError(PyThreadState *tstate, PyTypeObject *type, int oparg);
PyAPI_FUNC(void) _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc, const char *format_str, PyObject *obj);
PyAPI_FUNC(void) _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
PyAPI_FUNC(void) _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc, const char *format_str, PyObject *obj, PyObject* op);
PyAPI_FUNC(void) _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg, int op_type);
PyAPI_FUNC(void) _PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwargs);
PyAPI_FUNC(PyObject *) _PyEval_ImportFrom(PyThreadState *, PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) _PyEval_ImportName(PyThreadState *, _PyInterpreterFrame *, PyObject *, PyObject *, PyObject *);
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4648,6 +4648,19 @@ def func():
actual = self.get_suggestion(func)
self.assertIn("'ZeroDivisionError'?", actual)

def test_name_error_ignore_suggestions_from_builtins_in_deleting(self):
try:
exec("del next")
except NameError:
msg = traceback.format_exc()
self.assertNotIn("anext", msg)

def func():
del next

actual = self.get_suggestion(func)
self.assertNotIn("anext", actual)

def test_name_error_suggestions_with_non_string_candidates(self):
def func():
abc = 1
Expand Down
14 changes: 9 additions & 5 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1674,11 +1674,15 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
while tb.tb_next is not None:
tb = tb.tb_next
frame = tb.tb_frame
d = (
list(frame.f_locals)
+ list(frame.f_globals)
+ list(frame.f_builtins)
)
if getattr(exc_value, "op", "getting") == "deleting":
d = (list(frame.f_locals)
+ list(frame.f_globals))
else:
d = (
list(frame.f_locals)
+ list(frame.f_globals)
+ list(frame.f_builtins)
)
d = [x for x in d if isinstance(x, str)]

# Check first if we are in a method and the instance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add "NameError.op" to avoid suggesting builtins name in deleting
12 changes: 9 additions & 3 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2506,8 +2506,9 @@ PyNameErrorObject_CAST(PyObject *self)
static int
NameError_init(PyObject *op, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"name", NULL};
static char *kwlist[] = {"name", "op", NULL};
PyObject *name = NULL;
PyObject* op_type = NULL;

if (BaseException_init(op, args, NULL) == -1) {
return -1;
Expand All @@ -2517,15 +2518,16 @@ NameError_init(PyObject *op, PyObject *args, PyObject *kwds)
if (!empty_tuple) {
return -1;
}
if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$O:NameError", kwlist,
&name)) {
if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OO:NameError", kwlist,
&name ,&op_type)) {
Py_DECREF(empty_tuple);
return -1;
}
Py_DECREF(empty_tuple);

PyNameErrorObject *self = PyNameErrorObject_CAST(op);
Py_XSETREF(self->name, Py_XNewRef(name));
Py_XSETREF(self->op, Py_XNewRef(op_type));

return 0;
}
Expand All @@ -2535,6 +2537,7 @@ NameError_clear(PyObject *op)
{
PyNameErrorObject *self = PyNameErrorObject_CAST(op);
Py_CLEAR(self->name);
Py_CLEAR(self->op);
return BaseException_clear(op);
}

Expand All @@ -2551,11 +2554,14 @@ NameError_traverse(PyObject *op, visitproc visit, void *arg)
{
PyNameErrorObject *self = PyNameErrorObject_CAST(op);
Py_VISIT(self->name);
Py_VISIT(self->op);
return BaseException_traverse(op, visit, arg);
}

static PyMemberDef NameError_members[] = {
{"name", _Py_T_OBJECT, offsetof(PyNameErrorObject, name), 0, PyDoc_STR("name")},
{"op", _Py_T_OBJECT, offsetof(PyNameErrorObject, op), 0,
PyDoc_STR("operation that caused the NameError ('getting' or 'deleting')")},
{NULL} /* Sentinel */
};

Expand Down
6 changes: 3 additions & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1917,7 +1917,7 @@ dummy_func(
// Fortunately we don't need its superpower.
PyObject *oldobj = PyCell_SwapTakeRef((PyCellObject *)cell, NULL);
if (oldobj == NULL) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg, 1);
ERROR_NO_POP();
}
Py_DECREF(oldobj);
Expand All @@ -1939,7 +1939,7 @@ dummy_func(
PyCellObject *cell = (PyCellObject *)PyStackRef_AsPyObjectBorrow(GETLOCAL(oparg));
value_o = PyCell_GetRef(cell);
if (value_o == NULL) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg, 0);
ERROR_NO_POP();
}
}
Expand All @@ -1951,7 +1951,7 @@ dummy_func(
PyCellObject *cell = (PyCellObject *)PyStackRef_AsPyObjectBorrow(GETLOCAL(oparg));
value = _PyCell_GetStackRef(cell);
if (PyStackRef_IsNull(value)) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg, 0);
ERROR_IF(true);
}
}
Expand Down
26 changes: 19 additions & 7 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3320,7 +3320,7 @@ _PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwarg

void
_PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc,
const char *format_str, PyObject *obj)
const char *format_str, PyObject *obj, PyObject *op)
{
const char *obj_str;

Expand All @@ -3342,25 +3342,35 @@ _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc,
// NameError anyway.
(void)PyObject_SetAttr(exc, &_Py_ID(name), obj);
}
if (((PyNameErrorObject*)exc)->op == NULL) {
(void)PyObject_SetAttrString(exc, "op", op);
}
}
PyErr_SetRaisedException(exc);
}
}

void
_PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
_PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg, int op_type)
{
PyObject *name;
PyObject* op;
if (op_type == 0) {
op = PyUnicode_FromString("getting");
}
else {
op = PyUnicode_FromString("deleting");
}
/* Don't stomp existing exception */
if (_PyErr_Occurred(tstate))
return;
name = PyTuple_GET_ITEM(co->co_localsplusnames, oparg);
if (oparg < PyUnstable_Code_GetFirstFree(co)) {
_PyEval_FormatExcCheckArg(tstate, PyExc_UnboundLocalError,
UNBOUNDLOCAL_ERROR_MSG, name);
UNBOUNDLOCAL_ERROR_MSG, name, op);
} else {
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
UNBOUNDFREE_ERROR_MSG, name);
UNBOUNDFREE_ERROR_MSG, name, op);
}
}

Expand Down Expand Up @@ -3453,6 +3463,7 @@ _PyEval_GetANext(PyObject *aiter)
void
_PyEval_LoadGlobalStackRef(PyObject *globals, PyObject *builtins, PyObject *name, _PyStackRef *writeto)
{
PyObject* op = PyUnicode_FromString("getting");
if (PyDict_CheckExact(globals) && PyDict_CheckExact(builtins)) {
_PyDict_LoadGlobalStackRef((PyDictObject *)globals,
(PyDictObject *)builtins,
Expand All @@ -3461,7 +3472,7 @@ _PyEval_LoadGlobalStackRef(PyObject *globals, PyObject *builtins, PyObject *name
/* _PyDict_LoadGlobal() returns NULL without raising
* an exception if the key doesn't exist */
_PyEval_FormatExcCheckArg(PyThreadState_GET(), PyExc_NameError,
NAME_ERROR_MSG, name);
NAME_ERROR_MSG, name, op);
}
}
else {
Expand All @@ -3481,7 +3492,7 @@ _PyEval_LoadGlobalStackRef(PyObject *globals, PyObject *builtins, PyObject *name
if (res == NULL) {
_PyEval_FormatExcCheckArg(
PyThreadState_GET(), PyExc_NameError,
NAME_ERROR_MSG, name);
NAME_ERROR_MSG, name, op);
*writeto = PyStackRef_NULL;
return;
}
Expand Down Expand Up @@ -3519,6 +3530,7 @@ _PyEval_LoadName(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObject *na
{

PyObject *value;
PyObject* op = PyUnicode_FromString("getting");
if (frame->f_locals == NULL) {
_PyErr_SetString(tstate, PyExc_SystemError,
"no locals found");
Expand All @@ -3542,7 +3554,7 @@ _PyEval_LoadName(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObject *na
if (value == NULL) {
_PyEval_FormatExcCheckArg(
tstate, PyExc_NameError,
NAME_ERROR_MSG, name);
NAME_ERROR_MSG, name, op);
}
return value;
}
Expand Down
21 changes: 14 additions & 7 deletions Python/executor_cases.c.h

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

Loading
Loading