Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5138,6 +5138,26 @@ def foo(self):
with self.assertRaisesRegex(NotImplementedError, "BAR"):
B().foo

def test_staticmethod_new(self):
class MyStaticMethod(staticmethod):
def __init__(self, func):
pass
def func(): pass
sm = MyStaticMethod(func)
self.assertEqual(repr(sm), '<staticmethod(None)>')
self.assertIsNone(sm.__func__)
self.assertIsNone(sm.__wrapped__)

def test_classmethod_new(self):
class MyClassMethod(classmethod):
def __init__(self, func):
pass
def func(): pass
cm = MyClassMethod(func)
self.assertEqual(repr(cm), '<classmethod(None)>')
self.assertIsNone(cm.__func__)
self.assertIsNone(cm.__wrapped__)


class DictProxyTests(unittest.TestCase):
def setUp(self):
Expand Down
28 changes: 26 additions & 2 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,18 @@ cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
return PyMethod_New(cm->cm_callable, type);
}

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 int
cm_init(PyObject *self, PyObject *args, PyObject *kwds)
{
Expand Down Expand Up @@ -1616,7 +1628,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 Down Expand Up @@ -1701,6 +1713,18 @@ sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
return Py_NewRef(sm->sm_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;
}

static int
sm_init(PyObject *self, PyObject *args, PyObject *kwds)
{
Expand Down Expand Up @@ -1851,7 +1875,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