Skip to content

Commit 6956666

Browse files
committed
annotate dunder getter/setter
1 parent 036dd17 commit 6956666

File tree

2 files changed

+109
-37
lines changed

2 files changed

+109
-37
lines changed

Objects/clinic/funcobject.c.h

Lines changed: 58 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/funcobject.c

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -680,41 +680,6 @@ func_set_qualname(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
680680
return 0;
681681
}
682682

683-
static PyObject *
684-
func_get_annotate(PyObject *self, void *Py_UNUSED(ignored))
685-
{
686-
PyFunctionObject *op = _PyFunction_CAST(self);
687-
if (op->func_annotate == NULL) {
688-
Py_RETURN_NONE;
689-
}
690-
return Py_NewRef(op->func_annotate);
691-
}
692-
693-
static int
694-
func_set_annotate(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
695-
{
696-
PyFunctionObject *op = _PyFunction_CAST(self);
697-
if (value == NULL) {
698-
PyErr_SetString(PyExc_TypeError,
699-
"__annotate__ cannot be deleted");
700-
return -1;
701-
}
702-
if (Py_IsNone(value)) {
703-
Py_XSETREF(op->func_annotate, value);
704-
return 0;
705-
}
706-
else if (PyCallable_Check(value)) {
707-
Py_XSETREF(op->func_annotate, Py_XNewRef(value));
708-
Py_CLEAR(op->func_annotations);
709-
return 0;
710-
}
711-
else {
712-
PyErr_SetString(PyExc_TypeError,
713-
"__annotate__ must be callable or None");
714-
return -1;
715-
}
716-
}
717-
718683
static PyObject *
719684
func_get_type_params(PyObject *self, void *Py_UNUSED(ignored))
720685
{
@@ -1101,12 +1066,62 @@ function___kwdefaults___set_impl(PyFunctionObject *self, PyObject *value)
11011066
return 0;
11021067
}
11031068

1069+
/*[clinic input]
1070+
@critical_section
1071+
@getter
1072+
function.__annotate__
1073+
1074+
Get __annotate__ attribute for a function object.
1075+
[clinic start generated code]*/
1076+
1077+
static PyObject *
1078+
function___annotate___get_impl(PyFunctionObject *self)
1079+
/*[clinic end generated code: output=5ec7219ff2bda9e6 input=2932381f16ffeb75]*/
1080+
{
1081+
if (self->func_annotate == NULL) {
1082+
Py_RETURN_NONE;
1083+
}
1084+
return Py_NewRef(self->func_annotate);
1085+
}
1086+
1087+
/*[clinic input]
1088+
@critical_section
1089+
@setter
1090+
function.__annotate__
1091+
[clinic start generated code]*/
1092+
1093+
static int
1094+
function___annotate___set_impl(PyFunctionObject *self, PyObject *value)
1095+
/*[clinic end generated code: output=05b7dfc07ada66cd input=eb6225e358d97448]*/
1096+
{
1097+
PyFunctionObject *op = _PyFunction_CAST(self);
1098+
if (value == NULL) {
1099+
PyErr_SetString(PyExc_TypeError,
1100+
"__annotate__ cannot be deleted");
1101+
return -1;
1102+
}
1103+
if (Py_IsNone(value)) {
1104+
Py_XSETREF(op->func_annotate, value);
1105+
return 0;
1106+
}
1107+
else if (PyCallable_Check(value)) {
1108+
Py_XSETREF(op->func_annotate, Py_XNewRef(value));
1109+
Py_CLEAR(op->func_annotations);
1110+
return 0;
1111+
}
1112+
else {
1113+
PyErr_SetString(PyExc_TypeError,
1114+
"__annotate__ must be callable or None");
1115+
return -1;
1116+
}
1117+
}
1118+
11041119
static PyGetSetDef func_getsetlist[] = {
11051120
FUNCTION___CODE___GETSETDEF
11061121
FUNCTION___DEFAULTS___GETSETDEF
11071122
FUNCTION___KWDEFAULTS___GETSETDEF
11081123
FUNCTION___ANNOTATIONS___GETSETDEF
1109-
{"__annotate__", func_get_annotate, func_set_annotate},
1124+
FUNCTION___ANNOTATE___GETSETDEF
11101125
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
11111126
{"__name__", func_get_name, func_set_name},
11121127
{"__qualname__", func_get_qualname, func_set_qualname},

0 commit comments

Comments
 (0)