[Python-checkins] cpython: make __doc__ mutable on heaptypes (closes #12773)

benjamin.peterson python-checkins at python.org
Wed Aug 17 19:04:30 CEST 2011


http://hg.python.org/cpython/rev/ed2511c23dae
changeset:   71895:ed2511c23dae
user:        Benjamin Peterson <benjamin at python.org>
date:        Wed Aug 17 12:03:47 2011 -0500
summary:
  make __doc__ mutable on heaptypes (closes #12773)

files:
  Lib/test/test_descr.py |  13 +++++++++++++
  Misc/NEWS              |   2 ++
  Objects/typeobject.c   |  11 ++++++++++-
  3 files changed, 25 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -4261,6 +4261,19 @@
         m = str(cm.exception)
         self.assertEqual("'foo' in __slots__ conflicts with class variable", m)
 
+    def test_set_doc(self):
+        class X:
+            "elephant"
+        X.__doc__ = "banana"
+        self.assertEqual(X.__doc__, "banana")
+        with self.assertRaises(TypeError) as cm:
+            type(list).__dict__["__doc__"].__set__(list, "blah")
+        self.assertIn("can't set list.__doc__", str(cm.exception))
+        with self.assertRaises(TypeError) as cm:
+            type(X).__dict__["__doc__"].__delete__(X)
+        self.assertIn("can't delete X.__doc__", str(cm.exception))
+        self.assertEqual(X.__doc__, "banana")
+
 class DictProxyTests(unittest.TestCase):
     def setUp(self):
         class C(object):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@
 Core and Builtins
 -----------------
 
+- Issue #12773: Make __doc__ mutable on user-defined classes.
+
 - Issue #12766: Raise an ValueError when creating a class with a class variable
   that conflicts with a name in __slots__.
 
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -588,6 +588,15 @@
     return result;
 }
 
+static int
+type_set_doc(PyTypeObject *type, PyObject *value, void *context)
+{
+    if (!check_set_special_type_attr(type, value, "__doc__"))
+        return -1;
+    PyType_Modified(type);
+    return PyDict_SetItemString(type->tp_dict, "__doc__", value);
+}
+
 static PyObject *
 type___instancecheck__(PyObject *type, PyObject *inst)
 {
@@ -623,7 +632,7 @@
     {"__abstractmethods__", (getter)type_abstractmethods,
      (setter)type_set_abstractmethods, NULL},
     {"__dict__",  (getter)type_dict,  NULL, NULL},
-    {"__doc__", (getter)type_get_doc, NULL, NULL},
+    {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL},
     {0}
 };
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list