]> granicus.if.org Git - python/commitdiff
Issue 5982: Classmethod and staticmethod expose wrapped function with __func__.
authorRaymond Hettinger <python@rcn.com>
Fri, 29 May 2009 04:58:52 +0000 (04:58 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 29 May 2009 04:58:52 +0000 (04:58 +0000)
Lib/test/test_funcattrs.py
Misc/NEWS
Objects/funcobject.c

index f374c8d628790227bcd56fc834db97672c2019b7..2961cd2755d1ce7bb97d3b9cd483af6128ba234b 100644 (file)
@@ -273,10 +273,23 @@ class FunctionDocstringTest(FuncAttrsTest):
         self.assertEqual(self.b.__doc__, None)
         self.assertEqual(self.b.func_doc, None)
 
+class StaticMethodAttrsTest(unittest.TestCase):
+    def test_func_attribute(self):
+        def f():
+            pass
+
+        c = classmethod(f)
+        self.assert_(c.__func__ is f)
+
+        s = staticmethod(f)
+        self.assert_(s.__func__ is f)
+
+
 def test_main():
     test_support.run_unittest(FunctionPropertiesTest, ImplicitReferencesTest,
                               ArbitraryFunctionAttrTest, FunctionDictsTest,
-                              FunctionDocstringTest)
+                              FunctionDocstringTest,
+                              StaticMethodAttrsTest)
 
 if __name__ == "__main__":
     test_main()
index 13be5e70a38fb7cfbcfe42dc2113bcf73a54cdd9..94405c5b3129676a26ff4ff369ad06a3be09ec91 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
 Core and Builtins
 -----------------
 
+- Issue #5982: staticmethod and classmethod now expose the wrapped
+  function with __func__.
+
 - Added support for multiple context managers in the same with-statement.
   Deprecated contextlib.nested() which is no longer needed.
 
index a2e87b733ccf69c3b23e8c1e068acfd45b61cf02..14484e5b3c30c09a260f65cad0ffe38d4d4ba232 100644 (file)
@@ -669,6 +669,11 @@ cm_init(PyObject *self, PyObject *args, PyObject *kwds)
        return 0;
 }
 
+static PyMemberDef cm_memberlist[] = {
+       {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
+       {NULL}  /* Sentinel */
+};
+
 PyDoc_STRVAR(classmethod_doc,
 "classmethod(function) -> method\n\
 \n\
@@ -719,7 +724,7 @@ PyTypeObject PyClassMethod_Type = {
        0,                                      /* tp_iter */
        0,                                      /* tp_iternext */
        0,                                      /* tp_methods */
-       0,                                      /* tp_members */
+       cm_memberlist,          /* tp_members */
        0,                                      /* tp_getset */
        0,                                      /* tp_base */
        0,                                      /* tp_dict */
@@ -819,6 +824,11 @@ sm_init(PyObject *self, PyObject *args, PyObject *kwds)
        return 0;
 }
 
+static PyMemberDef sm_memberlist[] = {
+       {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
+       {NULL}  /* Sentinel */
+};
+
 PyDoc_STRVAR(staticmethod_doc,
 "staticmethod(function) -> method\n\
 \n\
@@ -866,7 +876,7 @@ PyTypeObject PyStaticMethod_Type = {
        0,                                      /* tp_iter */
        0,                                      /* tp_iternext */
        0,                                      /* tp_methods */
-       0,                                      /* tp_members */
+       sm_memberlist,          /* tp_members */
        0,                                      /* tp_getset */
        0,                                      /* tp_base */
        0,                                      /* tp_dict */