Skip to content
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

gh-106033: Get rid of new occurrences of PyDict_GetItem and PyObject_HasAttr #106034

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
13 changes: 7 additions & 6 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,15 @@ py_digest_by_digestmod(PyObject *module, PyObject *digestmod, enum Py_hash_type
} else {
_hashlibstate *state = get_hashlib_state(module);
// borrowed ref
name_obj = PyDict_GetItem(state->constructs, digestmod);
name_obj = PyDict_GetItemWithError(state->constructs, digestmod);
}
if (name_obj == NULL) {
_hashlibstate *state = get_hashlib_state(module);
PyErr_Clear();
PyErr_Format(
state->unsupported_digestmod_error,
"Unsupported digestmod %R", digestmod);
if (!PyErr_Occurred()) {
_hashlibstate *state = get_hashlib_state(module);
PyErr_Format(
state->unsupported_digestmod_error,
"Unsupported digestmod %R", digestmod);
}
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion Modules/_testcapi/code.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ get_code_extra_index(PyInterpreterState* interp) {
PyObject *interp_dict = PyInterpreterState_GetDict(interp); // borrowed
assert(interp_dict); // real users would handle missing dict... somehow

PyObject *index_obj = PyDict_GetItemString(interp_dict, key); // borrowed
PyObject *index_obj = _PyDict_GetItemStringWithError(interp_dict, key); // borrowed
Py_ssize_t index = 0;
if (!index_obj) {
if (PyErr_Occurred()) {
Expand Down
31 changes: 15 additions & 16 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,21 @@ BaseException_add_note(PyObject *self, PyObject *note)
return NULL;
}

if (!PyObject_HasAttr(self, &_Py_ID(__notes__))) {
PyObject *new_notes = PyList_New(0);
if (new_notes == NULL) {
PyObject *notes;
if (_PyObject_LookupAttr(self, &_Py_ID(__notes__), &notes) < 0) {
return NULL;
}
if (notes == NULL) {
notes = PyList_New(0);
if (notes == NULL) {
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}
if (PyObject_SetAttr(self, &_Py_ID(__notes__), new_notes) < 0) {
Py_DECREF(new_notes);
if (PyObject_SetAttr(self, &_Py_ID(__notes__), notes) < 0) {
Py_DECREF(notes);
return NULL;
}
Py_DECREF(new_notes);
}
PyObject *notes = PyObject_GetAttr(self, &_Py_ID(__notes__));
if (notes == NULL) {
return NULL;
}
if (!PyList_Check(notes)) {
else if (!PyList_Check(notes)) {
Py_DECREF(notes);
PyErr_SetString(PyExc_TypeError, "Cannot add note: __notes__ is not a list");
return NULL;
Expand Down Expand Up @@ -941,11 +940,11 @@ exceptiongroup_subset(
PyException_SetContext(eg, PyException_GetContext(orig));
PyException_SetCause(eg, PyException_GetCause(orig));

if (PyObject_HasAttr(orig, &_Py_ID(__notes__))) {
PyObject *notes = PyObject_GetAttr(orig, &_Py_ID(__notes__));
if (notes == NULL) {
goto error;
}
PyObject *notes;
if (_PyObject_LookupAttr(orig, &_Py_ID(__notes__), &notes) < 0) {
goto error;
}
if (notes) {
if (PySequence_Check(notes)) {
/* Make a copy so the parts have independent notes lists. */
PyObject *notes_copy = PySequence_List(notes);
Expand Down
5 changes: 4 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1508,11 +1508,14 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
static PyObject *
type_get_type_params(PyTypeObject *type, void *context)
{
PyObject *params = PyDict_GetItem(lookup_tp_dict(type), &_Py_ID(__type_params__));
PyObject *params = PyDict_GetItemWithError(lookup_tp_dict(type), &_Py_ID(__type_params__));

if (params) {
return Py_NewRef(params);
}
if (PyErr_Occurred()) {
return NULL;
}

return PyTuple_New(0);
}
Expand Down
12 changes: 5 additions & 7 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -1134,15 +1134,13 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
return 0;
}

if (!PyObject_HasAttr(value, &_Py_ID(__notes__))) {
return 0;
}
PyObject *notes = PyObject_GetAttr(value, &_Py_ID(__notes__));
if (notes == NULL) {
return -1;
PyObject *notes;
int res = _PyObject_LookupAttr(value, &_Py_ID(__notes__), &notes);
if (res <= 0) {
return res;
}
if (!PySequence_Check(notes) || PyUnicode_Check(notes) || PyBytes_Check(notes)) {
int res = 0;
res = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason not to have this local to the block?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will hide the same named variable in the outer block. It looks errorprone, even if it does not do wrong in this case. The compiler can complain, and I would not blame it on this.

if (write_indented_margin(ctx, f) < 0) {
res = -1;
}
Expand Down