diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-10-04 05:27:00 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-10-04 05:27:00 (GMT) |
commit | 2f93e28a19e3f250e8c19f9f4334cfa76f5e3645 (patch) | |
tree | 22e884413dc23251e95f9737bd9e14daaf43bd0f /Objects | |
parent | f137f75ab82019b7b4db6d45bd69e2c0b155b2eb (diff) | |
download | cpython-2f93e28a19e3f250e8c19f9f4334cfa76f5e3645.zip cpython-2f93e28a19e3f250e8c19f9f4334cfa76f5e3645.tar.gz cpython-2f93e28a19e3f250e8c19f9f4334cfa76f5e3645.tar.bz2 |
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*.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/typeobject.c | 25 |
1 files changed, 24 insertions, 1 deletions
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 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) } } + /* 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 @@ type_clear(PyTypeObject *type) 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 @@ object_set_class(PyObject *self, PyObject *value, void *closure) PyErr_Format(PyExc_TypeError, "__class__ assignment: " "'%s' object layout differs from '%s'", - new->tp_name, + new->tp_name, old->tp_name); return -1; } |