Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 50 additions & 39 deletions Objects/methodobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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')
Expand All @@ -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;

Expand All @@ -238,37 +244,38 @@ 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);
return 0;
}

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}
};

Expand All @@ -280,15 +287,18 @@ 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);
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,
m->m_self);
m->m_ml->ml_name,
Py_TYPE(m->m_self)->tp_name,
m->m_self);
}

static PyObject *
Expand Down Expand Up @@ -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;
}

Expand All @@ -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 */
Expand All @@ -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 */
Expand Down
Loading