Skip to content

Commit

Permalink
Modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed May 21, 2024
1 parent b0809da commit 67cf08f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
5 changes: 0 additions & 5 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,11 +523,6 @@ func_get_annotation_dict(PyFunctionObject *op)
if (ann_dict == NULL) {
return NULL;
}
if (op->func_annotations != NULL) {
Py_DECREF(ann_dict);
assert(PyDict_Check(op->func_annotations));
return op->func_annotations;
}
if (!PyDict_Check(ann_dict)) {
PyErr_Format(PyExc_TypeError, "__annotate__ returned non-dict of type '%.100s'",
Py_TYPE(ann_dict)->tp_name);
Expand Down
32 changes: 25 additions & 7 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1683,8 +1683,9 @@ type_get_annotate(PyTypeObject *type, void *Py_UNUSED(ignored))
}

PyObject *annotate;
PyObject *dict = lookup_tp_dict(type);
PyObject *dict = PyType_GetDict(type);
if (PyDict_GetItemRef(dict, &_Py_ID(__annotate__), &annotate) < 0) {
Py_DECREF(dict);
return NULL;
}
if (annotate) {
Expand All @@ -1696,10 +1697,12 @@ type_get_annotate(PyTypeObject *type, void *Py_UNUSED(ignored))
else {
annotate = Py_None;
int result = PyDict_SetItem(dict, &_Py_ID(__annotate__), annotate);
if (result == 0) {
PyType_Modified(type);
if (result < 0) {
Py_DECREF(dict);
return NULL;
}
}
Py_DECREF(dict);
return annotate;
}

Expand All @@ -1722,17 +1725,22 @@ type_set_annotate(PyTypeObject *type, PyObject *value, void *Py_UNUSED(ignored))
return -1;
}

PyObject *dict = lookup_tp_dict(type);
PyObject *dict = PyType_GetDict(type);
assert(PyDict_Check(dict));
int result = PyDict_SetItem(dict, &_Py_ID(__annotate__), value);
if (result < 0) {
Py_DECREF(dict);
return -1;
}
if (!Py_IsNone(value)) {
if (PyDict_Pop(dict, &_Py_ID(__annotations__), NULL) == -1) {
Py_DECREF(dict);
PyType_Modified(type);
return -1;
}
}
Py_DECREF(dict);
PyType_Modified(type);
return 0;
}

Expand All @@ -1745,8 +1753,9 @@ type_get_annotations(PyTypeObject *type, void *context)
}

PyObject *annotations;
PyObject *dict = lookup_tp_dict(type);
PyObject *dict = PyType_GetDict(type);
if (PyDict_GetItemRef(dict, &_Py_ID(__annotations__), &annotations) < 0) {
Py_DECREF(dict);
return NULL;
}
if (annotations) {
Expand All @@ -1758,12 +1767,14 @@ type_get_annotations(PyTypeObject *type, void *context)
else {
PyObject *annotate = type_get_annotate(type, NULL);
if (annotate == NULL) {
Py_DECREF(dict);
return NULL;
}
if (PyCallable_Check(annotate)) {
PyObject *one = _PyLong_GetOne();
annotations = _PyObject_CallOneArg(annotate, one);
if (annotations == NULL) {
Py_DECREF(dict);
Py_DECREF(annotate);
return NULL;
}
Expand All @@ -1772,6 +1783,7 @@ type_get_annotations(PyTypeObject *type, void *context)
Py_TYPE(annotations)->tp_name);
Py_DECREF(annotations);
Py_DECREF(annotate);
Py_DECREF(dict);
return NULL;
}
}
Expand All @@ -1789,6 +1801,7 @@ type_get_annotations(PyTypeObject *type, void *context)
}
}
}
Py_DECREF(dict);
return annotations;
}

Expand All @@ -1803,7 +1816,7 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
}

int result;
PyObject *dict = lookup_tp_dict(type);
PyObject *dict = PyType_GetDict(type);
if (value != NULL) {
/* set */
result = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
Expand All @@ -1812,18 +1825,23 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
result = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL);
if (result == 0) {
PyErr_SetString(PyExc_AttributeError, "__annotations__");
Py_DECREF(dict);
return -1;
}
}
PyType_Modified(type);
if (result < 0) {
Py_DECREF(dict);
return -1;
}
else if (result == 0) {
if (PyDict_Pop(dict, &_Py_ID(__annotate__), NULL) < 0) {
PyType_Modified(type);
Py_DECREF(dict);
return -1;
}
}
PyType_Modified(type);
Py_DECREF(dict);
return 0;
}

Expand Down

0 comments on commit 67cf08f

Please sign in to comment.