Skip to content

Commit 28a3fbc

Browse files
committed
clinic dunders for func object
1 parent 6956666 commit 28a3fbc

File tree

2 files changed

+288
-75
lines changed

2 files changed

+288
-75
lines changed

Objects/clinic/funcobject.c.h

Lines changed: 172 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: 116 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -636,77 +636,6 @@ static PyMemberDef func_memberlist[] = {
636636
{NULL} /* Sentinel */
637637
};
638638

639-
static PyObject *
640-
func_get_name(PyObject *self, void *Py_UNUSED(ignored))
641-
{
642-
PyFunctionObject *op = _PyFunction_CAST(self);
643-
return Py_NewRef(op->func_name);
644-
}
645-
646-
static int
647-
func_set_name(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
648-
{
649-
PyFunctionObject *op = _PyFunction_CAST(self);
650-
/* Not legal to del f.func_name or to set it to anything
651-
* other than a string object. */
652-
if (value == NULL || !PyUnicode_Check(value)) {
653-
PyErr_SetString(PyExc_TypeError,
654-
"__name__ must be set to a string object");
655-
return -1;
656-
}
657-
Py_XSETREF(op->func_name, Py_NewRef(value));
658-
return 0;
659-
}
660-
661-
static PyObject *
662-
func_get_qualname(PyObject *self, void *Py_UNUSED(ignored))
663-
{
664-
PyFunctionObject *op = _PyFunction_CAST(self);
665-
return Py_NewRef(op->func_qualname);
666-
}
667-
668-
static int
669-
func_set_qualname(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
670-
{
671-
PyFunctionObject *op = _PyFunction_CAST(self);
672-
/* Not legal to del f.__qualname__ or to set it to anything
673-
* other than a string object. */
674-
if (value == NULL || !PyUnicode_Check(value)) {
675-
PyErr_SetString(PyExc_TypeError,
676-
"__qualname__ must be set to a string object");
677-
return -1;
678-
}
679-
Py_XSETREF(op->func_qualname, Py_NewRef(value));
680-
return 0;
681-
}
682-
683-
static PyObject *
684-
func_get_type_params(PyObject *self, void *Py_UNUSED(ignored))
685-
{
686-
PyFunctionObject *op = _PyFunction_CAST(self);
687-
if (op->func_typeparams == NULL) {
688-
return PyTuple_New(0);
689-
}
690-
691-
assert(PyTuple_Check(op->func_typeparams));
692-
return Py_NewRef(op->func_typeparams);
693-
}
694-
695-
static int
696-
func_set_type_params(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
697-
{
698-
/* Not legal to del f.__type_params__ or to set it to anything
699-
* other than a tuple object. */
700-
PyFunctionObject *op = _PyFunction_CAST(self);
701-
if (value == NULL || !PyTuple_Check(value)) {
702-
PyErr_SetString(PyExc_TypeError,
703-
"__type_params__ must be set to a tuple");
704-
return -1;
705-
}
706-
Py_XSETREF(op->func_typeparams, Py_NewRef(value));
707-
return 0;
708-
}
709-
710639
PyObject *
711640
_Py_set_function_type_params(PyThreadState *Py_UNUSED(ignored), PyObject *func,
712641
PyObject *type_params)
@@ -1116,16 +1045,129 @@ function___annotate___set_impl(PyFunctionObject *self, PyObject *value)
11161045
}
11171046
}
11181047

1048+
/*[clinic input]
1049+
@critical_section
1050+
@getter
1051+
function.__name__
1052+
1053+
Get the function name.
1054+
[clinic start generated code]*/
1055+
1056+
static PyObject *
1057+
function___name___get_impl(PyFunctionObject *self)
1058+
/*[clinic end generated code: output=436852c5b4f6d259 input=64ef3b4545423cdc]*/
1059+
{
1060+
return Py_NewRef(self->func_name);
1061+
}
1062+
1063+
/*[clinic input]
1064+
@critical_section
1065+
@setter
1066+
function.__name__
1067+
[clinic start generated code]*/
1068+
1069+
static int
1070+
function___name___set_impl(PyFunctionObject *self, PyObject *value)
1071+
/*[clinic end generated code: output=2c571635b003b9cc input=705634aafaa00198]*/
1072+
{
1073+
/* Not legal to del f.func_name or to set it to anything
1074+
* other than a string object. */
1075+
if (value == NULL || !PyUnicode_Check(value)) {
1076+
PyErr_SetString(PyExc_TypeError,
1077+
"__name__ must be set to a string object");
1078+
return -1;
1079+
}
1080+
Py_XSETREF(self->func_name, Py_NewRef(value));
1081+
return 0;
1082+
}
1083+
1084+
/*[clinic input]
1085+
@critical_section
1086+
@getter
1087+
function.__qualname__
1088+
1089+
Get the qualified name for a function.
1090+
[clinic start generated code]*/
1091+
1092+
static PyObject *
1093+
function___qualname___get_impl(PyFunctionObject *self)
1094+
/*[clinic end generated code: output=8fbd60e64464da5f input=d6dab58778741083]*/
1095+
{
1096+
return Py_NewRef(self->func_qualname);
1097+
}
1098+
1099+
/*[clinic input]
1100+
@critical_section
1101+
@setter
1102+
function.__qualname__
1103+
[clinic start generated code]*/
1104+
1105+
static int
1106+
function___qualname___set_impl(PyFunctionObject *self, PyObject *value)
1107+
/*[clinic end generated code: output=4cc5e270fd55f139 input=c803ac4dfdf04c87]*/
1108+
{
1109+
/* Not legal to del f.__qualname__ or to set it to anything
1110+
* other than a string object. */
1111+
if (value == NULL || !PyUnicode_Check(value)) {
1112+
PyErr_SetString(PyExc_TypeError,
1113+
"__qualname__ must be set to a string object");
1114+
return -1;
1115+
}
1116+
Py_XSETREF(self->func_qualname, Py_NewRef(value));
1117+
return 0;
1118+
}
1119+
1120+
/*[clinic input]
1121+
@critical_section
1122+
@getter
1123+
function.__type_params__
1124+
1125+
Get the declared type parameters for a function.
1126+
[clinic start generated code]*/
1127+
1128+
static PyObject *
1129+
function___type_params___get_impl(PyFunctionObject *self)
1130+
/*[clinic end generated code: output=eb844d7ffca517a8 input=0864721484293724]*/
1131+
{
1132+
if (self->func_typeparams == NULL) {
1133+
return PyTuple_New(0);
1134+
}
1135+
1136+
assert(PyTuple_Check(self->func_typeparams));
1137+
return Py_NewRef(self->func_typeparams);
1138+
}
1139+
1140+
/*[clinic input]
1141+
@critical_section
1142+
@setter
1143+
function.__type_params__
1144+
[clinic start generated code]*/
1145+
1146+
static int
1147+
function___type_params___set_impl(PyFunctionObject *self, PyObject *value)
1148+
/*[clinic end generated code: output=038b4cda220e56fb input=3862fbd4db2b70e8]*/
1149+
{
1150+
/* It's not legal to del f.__type_params__ or to set it to anything
1151+
* other than a tuple object. */
1152+
if (value == NULL || !PyTuple_Check(value)) {
1153+
PyErr_SetString(PyExc_TypeError,
1154+
"__type_params__ must be set to a tuple");
1155+
return -1;
1156+
}
1157+
Py_XSETREF(self->func_typeparams, Py_NewRef(value));
1158+
return 0;
1159+
}
1160+
11191161
static PyGetSetDef func_getsetlist[] = {
11201162
FUNCTION___CODE___GETSETDEF
11211163
FUNCTION___DEFAULTS___GETSETDEF
11221164
FUNCTION___KWDEFAULTS___GETSETDEF
11231165
FUNCTION___ANNOTATIONS___GETSETDEF
11241166
FUNCTION___ANNOTATE___GETSETDEF
11251167
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
1126-
{"__name__", func_get_name, func_set_name},
1127-
{"__qualname__", func_get_qualname, func_set_qualname},
1128-
{"__type_params__", func_get_type_params, func_set_type_params},
1168+
FUNCTION___NAME___GETSETDEF
1169+
FUNCTION___QUALNAME___GETSETDEF
1170+
FUNCTION___TYPE_PARAMS___GETSETDEF
11291171
{NULL} /* Sentinel */
11301172
};
11311173

0 commit comments

Comments
 (0)