Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-144330: Initialize staticmethod/classmethod callables in tp_new#144440

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Aniketsy wants to merge3 commits intopython:main
base:main
Choose a base branch
Loading
fromAniketsy:fix-144330-clean
Open
Show file tree
Hide file tree
Changes fromall 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
18 changes: 18 additions & 0 deletionsLib/test/test_descr.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1832,6 +1832,24 @@ def test_staticmethods_in_c(self):
self.assertEqual(a, a1)
self.assertEqual(d, d1)

def test_staticmethod_new(self):
sm = staticmethod.__new__(staticmethod, None)
self.assertEqual(repr(sm), '<staticmethod(None)>')

def test_classmethod_new(self):
cm = classmethod.__new__(classmethod, None)
self.assertEqual(repr(cm), '<classmethod(None)>')

def test_staticmethod_func_readonly(self):
sm = staticmethod(lambda x: x)
with self.assertRaises(AttributeError):
sm.__func__ = None

def test_classmethod_func_readonly(self):
cm = classmethod(lambda x: x)
with self.assertRaises(AttributeError):
cm.__func__ = None

def test_classic(self):
# Testing classic classes...
class C:
Expand Down
34 changes: 30 additions & 4 deletionsObjects/funcobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1555,11 +1555,24 @@ static PyMethodDef cm_methodlist[] = {
{NULL} /* Sentinel */
};

static PyObject *
cm_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
classmethod *cm = (classmethod *)PyType_GenericAlloc(type, 0);
if (cm == NULL) {
return NULL;
}
cm->cm_callable = Py_None;
cm->cm_dict = NULL;
return (PyObject *)cm;
}

static PyObject*
cm_repr(PyObject *self)
{
classmethod *cm = _PyClassMethod_CAST(self);
return PyUnicode_FromFormat("<classmethod(%R)>", cm->cm_callable);
PyObject *callable = cm->cm_callable != NULL ? cm->cm_callable : Py_None;
return PyUnicode_FromFormat("<classmethod(%R)>", callable);
}

PyDoc_STRVAR(classmethod_doc,
Expand DownExpand Up@@ -1623,7 +1636,7 @@ PyTypeObject PyClassMethod_Type = {
offsetof(classmethod, cm_dict), /* tp_dictoffset */
cm_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */
cm_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
};

Expand DownExpand Up@@ -1796,7 +1809,20 @@ static PyObject*
sm_repr(PyObject *self)
{
staticmethod *sm = _PyStaticMethod_CAST(self);
return PyUnicode_FromFormat("<staticmethod(%R)>", sm->sm_callable);
PyObject *callable = sm->sm_callable != NULL ? sm->sm_callable : Py_None;
return PyUnicode_FromFormat("<staticmethod(%R)>", callable);
}

static PyObject *
sm_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
staticmethod *sm = (staticmethod *)PyType_GenericAlloc(type, 0);
if (sm == NULL) {
return NULL;
}
sm->sm_callable = Py_None;
sm->sm_dict = NULL;
return (PyObject *)sm;
}

PyDoc_STRVAR(staticmethod_doc,
Expand DownExpand Up@@ -1858,7 +1884,7 @@ PyTypeObject PyStaticMethod_Type = {
offsetof(staticmethod, sm_dict), /* tp_dictoffset */
sm_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */
sm_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
};

Expand Down
Loading

[8]ページ先頭

©2009-2026 Movatter.jp