-
-
Notifications
You must be signed in to change notification settings - Fork 33k
gh-138890: Add clearer message when deleting builtins with del
#139078
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1 @@ | ||||||
Deleting builtin names with ``del`` now gives a clearer error message. | ||||||
|
Deleting builtin names with ``del`` now gives a clearer error message. | |
Deleting built-in names with :keyword:`del` now gives a clearer error message. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1577,9 +1577,10 @@ dummy_func( | |
err = PyObject_DelItem(ns, name); | ||
// Can't use ERROR_IF here. | ||
if (err != 0) { | ||
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError, | ||
NAME_ERROR_MSG, | ||
name); | ||
const char *err_msg = PyMapping_HasKey(BUILTINS(), name) | ||
? CANNOT_DELETE_BUILTIN_ERROR_MSG | ||
: NAME_ERROR_MSG; | ||
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError, err_msg, name); | ||
ERROR_NO_POP(); | ||
} | ||
} | ||
|
@@ -1720,8 +1721,11 @@ dummy_func( | |
ERROR_NO_POP(); | ||
} | ||
if (err == 0) { | ||
const char *err_msg = PyMapping_HasKey(BUILTINS(), name) | ||
|
||
? CANNOT_DELETE_BUILTIN_ERROR_MSG | ||
: NAME_ERROR_MSG; | ||
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError, | ||
NAME_ERROR_MSG, name); | ||
err_msg, name); | ||
ERROR_NO_POP(); | ||
} | ||
} | ||
|
@@ -1888,10 +1892,13 @@ dummy_func( | |
inst(DELETE_FAST, (--)) { | ||
_PyStackRef v = GETLOCAL(oparg); | ||
if (PyStackRef_IsNull(v)) { | ||
_PyEval_FormatExcCheckArg(tstate, PyExc_UnboundLocalError, | ||
UNBOUNDLOCAL_ERROR_MSG, | ||
PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, oparg) | ||
); | ||
PyObject *name; | ||
name = PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, | ||
|
||
oparg); | ||
const char *err_msg = PyMapping_HasKey(BUILTINS(), name) | ||
|
||
? CANNOT_DELETE_BUILTIN_ERROR_MSG | ||
: UNBOUNDLOCAL_ERROR_MSG; | ||
_PyEval_FormatExcCheckArg(tstate, PyExc_UnboundLocalError, err_msg, name); | ||
ERROR_IF(true); | ||
} | ||
_PyStackRef tmp = GETLOCAL(oparg); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The formatting makes this a bit difficult to read. This isn't coming near PEP 8's 79 character limit, so let's just keep all the arguments on the same line. This applies to all the
assertRaisesRegex
calls below.