mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
[3.11] gh-106719: Fix __annotations__ getter and setter in the type and module types (GH-106720) (GH-106850)
No longer suppress arbitrary errors. Simplify the code.
(cherry picked from commit e1c295e3da
)
This commit is contained in:
parent
a7acc5cb5c
commit
fb04874053
3 changed files with 35 additions and 48 deletions
|
@ -912,24 +912,16 @@ type_get_annotations(PyTypeObject *type, void *context)
|
|||
}
|
||||
|
||||
PyObject *annotations;
|
||||
/* there's no _PyDict_GetItemId without WithError, so let's LBYL. */
|
||||
if (PyDict_Contains(type->tp_dict, &_Py_ID(__annotations__))) {
|
||||
annotations = PyDict_GetItemWithError(
|
||||
type->tp_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(type->tp_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(
|
||||
|
@ -960,11 +952,10 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
|
|||
result = PyDict_SetItem(type->tp_dict, &_Py_ID(__annotations__), value);
|
||||
} else {
|
||||
/* delete */
|
||||
if (!PyDict_Contains(type->tp_dict, &_Py_ID(__annotations__))) {
|
||||
PyErr_Format(PyExc_AttributeError, "__annotations__");
|
||||
return -1;
|
||||
}
|
||||
result = PyDict_DelItem(type->tp_dict, &_Py_ID(__annotations__));
|
||||
if (result < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
|
||||
PyErr_SetString(PyExc_AttributeError, "__annotations__");
|
||||
}
|
||||
}
|
||||
|
||||
if (result == 0) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue