[Python-checkins] CVS: python/dist/src/Objects moduleobject.c,2.34,2.35

Barry Warsaw bwarsaw@users.sourceforge.net
Thu, 16 Aug 2001 13:39:26 -0700


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

Modified Files:
	moduleobject.c 
Log Message:
module_repr(): Instead of fixing the maximum buf size to 400,
    calculate it on the fly.  This way even modules with long package
    names get an accurate repr instead of a truncated one.  The extra
    malloc/free cost shouldn't be a problem in a repr function.

    Closes SF bug #437984


Index: moduleobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/moduleobject.c,v
retrieving revision 2.34
retrieving revision 2.35
diff -C2 -d -r2.34 -r2.35
*** moduleobject.c	2001/08/02 04:15:00	2.34
--- moduleobject.c	2001/08/16 20:39:24	2.35
***************
*** 158,164 ****
  module_repr(PyModuleObject *m)
  {
! 	char buf[400];
  	char *name;
  	char *filename;
  	name = PyModule_GetName((PyObject *)m);
  	if (name == NULL) {
--- 158,169 ----
  module_repr(PyModuleObject *m)
  {
! 	static int template1len = sizeof("<module '' (built-in)>") + 1;
! 	static int template2len = sizeof("<module '' from ''>") + 1;
! 
! 	char *buf;
  	char *name;
  	char *filename;
+ 	PyObject *rtn;
+ 
  	name = PyModule_GetName((PyObject *)m);
  	if (name == NULL) {
***************
*** 169,178 ****
  	if (filename == NULL) {
  		PyErr_Clear();
! 		sprintf(buf, "<module '%.80s' (built-in)>", name);
! 	} else {
! 		sprintf(buf, "<module '%.80s' from '%.255s'>", name, filename);
  	}
! 
! 	return PyString_FromString(buf);
  }
  
--- 174,190 ----
  	if (filename == NULL) {
  		PyErr_Clear();
! 		buf = PyObject_MALLOC(
! 			sizeof(char) * (strlen(name) + template1len));
! 		sprintf(buf, "<module '%s' (built-in)>", name);
  	}
! 	else {
! 		buf = PyObject_MALLOC(
! 			sizeof(char) * (strlen(name) + strlen(filename) +
! 					template2len));
! 		sprintf(buf, "<module '%s' from '%s'>", name, filename);
! 	}
! 	rtn = PyString_FromString(buf);
! 	PyObject_FREE(buf);
! 	return rtn;
  }