summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-09-17 11:23:31 (GMT)
committerGitHub <noreply@github.com>2023-09-17 11:23:31 (GMT)
commitadd16f1a5e4013f97d33cc677dc008e8199f5b11 (patch)
tree2c187adbae2766f942e35780950a526dfe84ff48 /Python
parente57ecf6bbc59f999d27b125ea51b042c24a07bd9 (diff)
downloadcpython-add16f1a5e4013f97d33cc677dc008e8199f5b11.zip
cpython-add16f1a5e4013f97d33cc677dc008e8199f5b11.tar.gz
cpython-add16f1a5e4013f97d33cc677dc008e8199f5b11.tar.bz2
gh-108511: Add C API functions which do not silently ignore errors (GH-109025)
Add the following functions: * PyObject_HasAttrWithError() * PyObject_HasAttrStringWithError() * PyMapping_HasKeyWithError() * PyMapping_HasKeyStringWithError()
Diffstat (limited to 'Python')
-rw-r--r--Python/errors.c16
-rw-r--r--Python/import.c7
-rw-r--r--Python/suggestions.c6
3 files changed, 11 insertions, 18 deletions
diff --git a/Python/errors.c b/Python/errors.c
index f670b78..e6fa15f 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -1776,13 +1776,11 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
}
}
if ((PyObject *)Py_TYPE(exc) != PyExc_SyntaxError) {
- if (PyObject_GetOptionalAttr(exc, &_Py_ID(msg), &tmp) < 0) {
+ int rc = PyObject_HasAttrWithError(exc, &_Py_ID(msg));
+ if (rc < 0) {
_PyErr_Clear(tstate);
}
- else if (tmp) {
- Py_DECREF(tmp);
- }
- else {
+ else if (!rc) {
tmp = PyObject_Str(exc);
if (tmp) {
if (PyObject_SetAttr(exc, &_Py_ID(msg), tmp)) {
@@ -1795,13 +1793,11 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
}
}
- if (PyObject_GetOptionalAttr(exc, &_Py_ID(print_file_and_line), &tmp) < 0) {
+ rc = PyObject_HasAttrWithError(exc, &_Py_ID(print_file_and_line));
+ if (rc < 0) {
_PyErr_Clear(tstate);
}
- else if (tmp) {
- Py_DECREF(tmp);
- }
- else {
+ else if (!rc) {
if (PyObject_SetAttr(exc, &_Py_ID(print_file_and_line), Py_None)) {
_PyErr_Clear(tstate);
}
diff --git a/Python/import.c b/Python/import.c
index 126eb5e..9b0be02 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -2887,12 +2887,11 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
}
}
else {
- PyObject *path;
- if (PyObject_GetOptionalAttr(mod, &_Py_ID(__path__), &path) < 0) {
+ int has_path = PyObject_HasAttrWithError(mod, &_Py_ID(__path__));
+ if (has_path < 0) {
goto error;
}
- if (path) {
- Py_DECREF(path);
+ if (has_path) {
final_mod = PyObject_CallMethodObjArgs(
IMPORTLIB(interp), &_Py_ID(_handle_fromlist),
mod, fromlist, IMPORT_FUNC(interp), NULL);
diff --git a/Python/suggestions.c b/Python/suggestions.c
index 9247da4..1ad359b 100644
--- a/Python/suggestions.c
+++ b/Python/suggestions.c
@@ -245,14 +245,12 @@ get_suggestions_for_name_error(PyObject* name, PyFrameObject* frame)
goto error;
}
- PyObject *value;
- res = PyObject_GetOptionalAttr(self, name, &value);
+ res = PyObject_HasAttrWithError(self, name);
Py_DECREF(locals);
if (res < 0) {
goto error;
}
- if (value) {
- Py_DECREF(value);
+ if (res) {
Py_DECREF(dir);
return PyUnicode_FromFormat("self.%U", name);
}