SF bug [#467331] ClassType.__doc__ always None.
For a dynamically constructed type object, fill in the tp_doc slot with
a copy of the argument dict's "__doc__" value, provided the latter exists
and is a string.
NOTE: I don't know what to do if it's a Unicode string, so in that case
tp_doc is left NULL (which shows up as Py_None if you do Class.__doc__).
Note that tp_doc holds a char*, not a general PyObject*.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index c50b446..e8f436c 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -900,6 +900,24 @@
}
}
+ /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
+ and is a string (tp_doc is a char* -- can't copy a general object
+ into it).
+ XXX What if it's a Unicode string? Don't know -- this ignores it.
+ */
+ {
+ PyObject *doc = PyDict_GetItemString(dict, "__doc__");
+ if (doc != NULL && PyString_Check(doc)) {
+ const size_t n = (size_t)PyString_GET_SIZE(doc);
+ type->tp_doc = PyObject_MALLOC(n+1);
+ if (type->tp_doc == NULL) {
+ Py_DECREF(type);
+ return NULL;
+ }
+ memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
+ }
+ }
+
/* Special-case __new__: if it's a plain function,
make it a static function */
tmp = PyDict_GetItemString(dict, "__new__");
@@ -1162,6 +1180,11 @@
CLEAR(type->tp_base);
CLEAR(et->slots);
+ if (type->tp_doc != NULL) {
+ PyObject_FREE(type->tp_doc);
+ type->tp_doc = NULL;
+ }
+
#undef CLEAR
return 0;
@@ -1350,7 +1373,7 @@
PyErr_Format(PyExc_TypeError,
"__class__ assignment: "
"'%s' object layout differs from '%s'",
- new->tp_name,
+ new->tp_name,
old->tp_name);
return -1;
}