[Python-checkins] CVS: python/dist/src/Objects typeobject.c,2.86,2.87

Tim Peters tim_one@users.sourceforge.net
Wed, 03 Oct 2001 22:27:02 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv1972/python/Objects

Modified Files:
	typeobject.c 
Log Message:
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*.


Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.86
retrieving revision 2.87
diff -C2 -d -r2.86 -r2.87
*** typeobject.c	2001/10/03 13:58:35	2.86
--- typeobject.c	2001/10/04 05:27:00	2.87
***************
*** 901,904 ****
--- 901,922 ----
  	}
  
+ 	/* 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 */
***************
*** 1163,1166 ****
--- 1181,1189 ----
  	CLEAR(et->slots);
  
+ 	if (type->tp_doc != NULL) {
+ 		PyObject_FREE(type->tp_doc);
+ 		type->tp_doc = NULL;
+ 	}
+ 
  #undef CLEAR
  
***************
*** 1351,1355 ****
  			     "__class__ assignment: "
  			     "'%s' object layout differs from '%s'",
! 			     new->tp_name, 
  			     old->tp_name);
  		return -1;
--- 1374,1378 ----
  			     "__class__ assignment: "
  			     "'%s' object layout differs from '%s'",
! 			     new->tp_name,
  			     old->tp_name);
  		return -1;