summaryrefslogtreecommitdiffstats
path: root/Objects/typeobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/typeobject.c')
-rw-r--r--Objects/typeobject.c32
1 files changed, 12 insertions, 20 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 6662379..40e187d 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -1449,24 +1449,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(
@@ -1498,11 +1491,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) {