8000 gh-106719: Fix __annotations__ getter and setter in the type and module types by serhiy-storchaka · Pull Request #106720 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-106719: Fix __annotations__ getter and setter in the type and module types #106720

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 1 commit into from
Jul 18, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
No longer suppress arbitrary errors in the ``__annotations__`` getter and
setter in the type and module types.
48 changes: 21 additions & 27 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,26 +937,20 @@ static PyObject *
module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
{
PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));

if ((dict == NULL) || !PyDict_Check(dict)) {
if (dict == NULL) {
return NULL;
}
if (!PyDict_Check(dict)) {
PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
Py_XDECREF(dict);
Py_DECREF(dict);
return NULL;
}

PyObject *annotations;
/* there's no _PyDict_GetItemId without WithError, so let's LBYL. */
if (PyDict_Contains(dict, &_Py_ID(__annotations__))) {
annotations = PyDict_GetItemWithError(dict, &_Py_ID(__annotations__));
/*
** _PyDict_GetItemIdWithError could still fail,
** for instance with a well-timed Ctrl-C or a MemoryError.
** so let's be totally safe.
*/
if (annotations) {
Py_INCREF(annotations);
}
} else {
PyObject *annotations = PyDict_GetItemWithError(dict, &_Py_ID(__annotations__));
if (annotations) {
Py_INCREF(annotations);
}
else if (!PyErr_Occurred()) {
annotations = PyDict_New();
if (annotations) {
int result = PyDict_SetItem(
Expand All @@ -975,28 +969,28 @@ module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignor
{
int ret = -1;
PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));

if ((dict == NULL) || !PyDict_Check(dict)) {
if (dict == NULL) {
return -1;
}
if (!PyDict_Check(dict)) {
PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
goto exit;
}

if (value != NULL) {
/* set */
ret = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
goto exit;
}

/* delete */
if (!PyDict_Contains(dict, &_Py_ID(__annotations__))) {
PyErr_Format(PyExc_AttributeError, "__annotations__");
goto exit;
else {
/* delete */
ret = PyDict_DelItem(dict, &_Py_ID(__annotations__));
if (ret < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
PyErr_SetString(PyExc_AttributeError, "__annotations__");
}
}

ret = PyDict_DelItem(dict, &_Py_ID(__annotations__));

exit:
Py_XDECREF(dict);
Py_DECREF(dict);
return ret;
}

Expand Down
32 changes: 12 additions & 20 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1451,24 +1451,17 @@ type_get_annotations(PyTypeObject *type, void *context)
}

PyObject *annotations;
/* there's no _PyDict_GetItemId without WithError, so let's LBYL. */
PyObject *dict = lookup_tp_dict(type);
if (PyDict_Contains(dict, &_Py_ID(__annotations__))) {
annotations = PyDict_GetItemWithError(dict, &_Py_ID(__annotations__));
/*
** PyDict_GetItemWithError could still fail,
** for instance with a well-timed Ctrl-C or a MemoryError.
** so let's be totally safe.
*/
if (annotations) {
if (Py_TYPE(annotations)->tp_descr_get) {
annotations = Py_TYPE(annotations)->tp_descr_get(
annotations, NULL, (PyObject *)type);
} else {
Py_INCREF(annotations);
}
annotations = PyDict_GetItemWithError(dict, &_Py_ID(__annotations__));
if (annotations) {
if (Py_TYPE(annotations)->tp_descr_get) {
annotations = Py_TYPE(annotations)->tp_descr_get(
annotations, NULL, (PyObject *)type);
} else {
Py_INCREF(annotations);
}
} else {
}
else if (!PyErr_Occurred()) {
annotations = PyDict_New();
if (annotations) {
int result = PyDict_SetItem(
Expand Down Expand Up @@ -1500,11 +1493,10 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
result = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
} else {
/* delete */
if (!PyDict_Contains(dict, &_Py_ID(__annotations__))) {
PyErr_Format(PyExc_AttributeError, "__annotations__");
return -1;
}
result = PyDict_DelItem(dict, &_Py_ID(__annotations__));
if (result < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
PyErr_SetString(PyExc_AttributeError, "__annotations__");
}
}

if (result == 0) {
Expand Down
0