gh-111178: Fix function signatures in methodobject.c (#124902)

This commit is contained in:
Victor Stinner 2024-10-02 17:24:10 +02:00 committed by GitHub
parent 1ea6672a6f
commit 29951c8471
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 50 additions and 39 deletions

View File

@ -156,8 +156,9 @@ PyCMethod_GetClass(PyObject *op)
/* Methods (the standard built-in methods, that is) */
static void
meth_dealloc(PyCFunctionObject *m)
meth_dealloc(PyObject *self)
{
PyCFunctionObject *m = _PyCFunctionObject_CAST(self);
// The Py_TRASHCAN mechanism requires that we be able to
// call PyObject_GC_UnTrack twice on an object.
PyObject_GC_UnTrack(m);
@ -175,8 +176,9 @@ meth_dealloc(PyCFunctionObject *m)
}
static PyObject *
meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
meth_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyCFunctionObject *m = _PyCFunctionObject_CAST(self);
if (m->m_self == NULL || PyModule_Check(m->m_self))
return PyUnicode_FromString(m->m_ml->ml_name);
@ -185,32 +187,35 @@ meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
}
static PyMethodDef meth_methods[] = {
{"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
{"__reduce__", meth_reduce, METH_NOARGS, NULL},
{NULL, NULL}
};
static PyObject *
meth_get__text_signature__(PyCFunctionObject *m, void *closure)
meth_get__text_signature__(PyObject *self, void *closure)
{
PyCFunctionObject *m = _PyCFunctionObject_CAST(self);
return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name,
m->m_ml->ml_doc,
m->m_ml->ml_flags);
}
static PyObject *
meth_get__doc__(PyCFunctionObject *m, void *closure)
meth_get__doc__(PyObject *self, void *closure)
{
PyCFunctionObject *m = _PyCFunctionObject_CAST(self);
return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
}
static PyObject *
meth_get__name__(PyCFunctionObject *m, void *closure)
meth_get__name__(PyObject *self, void *closure)
{
PyCFunctionObject *m = _PyCFunctionObject_CAST(self);
return PyUnicode_FromString(m->m_ml->ml_name);
}
static PyObject *
meth_get__qualname__(PyCFunctionObject *m, void *closure)
meth_get__qualname__(PyObject *self, void *closure)
{
/* If __self__ is a module or NULL, return m.__name__
(e.g. len.__qualname__ == 'len')
@ -220,14 +225,15 @@ meth_get__qualname__(PyCFunctionObject *m, void *closure)
Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
(e.g. [].append.__qualname__ == 'list.append') */
PyObject *type, *type_qualname, *res;
if (m->m_self == NULL || PyModule_Check(m->m_self))
PyCFunctionObject *m = _PyCFunctionObject_CAST(self);
if (m->m_self == NULL || PyModule_Check(m->m_self)) {
return PyUnicode_FromString(m->m_ml->ml_name);
}
type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
PyObject *type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
type_qualname = PyObject_GetAttr(type, &_Py_ID(__qualname__));
PyObject *type_qualname = PyObject_GetAttr(type, &_Py_ID(__qualname__));
if (type_qualname == NULL)
return NULL;
@ -238,14 +244,15 @@ meth_get__qualname__(PyCFunctionObject *m, void *closure)
return NULL;
}
res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
PyObject *res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
Py_DECREF(type_qualname);
return res;
}
static int
meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
meth_traverse(PyObject *self, visitproc visit, void *arg)
{
PyCFunctionObject *m = _PyCFunctionObject_CAST(self);
Py_VISIT(PyCFunction_GET_CLASS(m));
Py_VISIT(m->m_self);
Py_VISIT(m->m_module);
@ -253,22 +260,22 @@ meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
}
static PyObject *
meth_get__self__(PyCFunctionObject *m, void *closure)
meth_get__self__(PyObject *meth, void *closure)
{
PyObject *self;
self = PyCFunction_GET_SELF(m);
if (self == NULL)
PyCFunctionObject *m = _PyCFunctionObject_CAST(meth);
PyObject *self = PyCFunction_GET_SELF(m);
if (self == NULL) {
self = Py_None;
}
return Py_NewRef(self);
}
static PyGetSetDef meth_getsets [] = {
{"__doc__", (getter)meth_get__doc__, NULL, NULL},
{"__name__", (getter)meth_get__name__, NULL, NULL},
{"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
{"__self__", (getter)meth_get__self__, NULL, NULL},
{"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
static PyGetSetDef meth_getsets[] = {
{"__doc__", meth_get__doc__, NULL, NULL},
{"__name__", meth_get__name__, NULL, NULL},
{"__qualname__", meth_get__qualname__, NULL, NULL},
{"__self__", meth_get__self__, NULL, NULL},
{"__text_signature__", meth_get__text_signature__, NULL, NULL},
{0}
};
@ -280,11 +287,14 @@ static PyMemberDef meth_members[] = {
};
static PyObject *
meth_repr(PyCFunctionObject *m)
meth_repr(PyObject *self)
{
if (m->m_self == NULL || PyModule_Check(m->m_self))
PyCFunctionObject *m = _PyCFunctionObject_CAST(self);
if (m->m_self == NULL || PyModule_Check(m->m_self)) {
return PyUnicode_FromFormat("<built-in function %s>",
m->m_ml->ml_name);
}
return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
m->m_ml->ml_name,
Py_TYPE(m->m_self)->tp_name,
@ -317,14 +327,15 @@ meth_richcompare(PyObject *self, PyObject *other, int op)
}
static Py_hash_t
meth_hash(PyCFunctionObject *a)
meth_hash(PyObject *self)
{
Py_hash_t x, y;
x = PyObject_GenericHash(a->m_self);
y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
PyCFunctionObject *a = _PyCFunctionObject_CAST(self);
Py_hash_t x = PyObject_GenericHash(a->m_self);
Py_hash_t y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
x ^= y;
if (x == -1)
if (x == -1) {
x = -2;
}
return x;
}
@ -334,16 +345,16 @@ PyTypeObject PyCFunction_Type = {
"builtin_function_or_method",
sizeof(PyCFunctionObject),
0,
(destructor)meth_dealloc, /* tp_dealloc */
meth_dealloc, /* tp_dealloc */
offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
(reprfunc)meth_repr, /* tp_repr */
meth_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)meth_hash, /* tp_hash */
meth_hash, /* tp_hash */
cfunction_call, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
@ -352,7 +363,7 @@ PyTypeObject PyCFunction_Type = {
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
0, /* tp_doc */
(traverseproc)meth_traverse, /* tp_traverse */
meth_traverse, /* tp_traverse */
0, /* tp_clear */
meth_richcompare, /* tp_richcompare */
offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */