[Python-checkins] CVS: python/dist/src/Objects classobject.c,2.141,2.142

Guido van Rossum gvanrossum@users.sourceforge.net
Fri, 24 Aug 2001 11:48:29 -0700


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

Modified Files:
	classobject.c 
Log Message:
Improve the error message issued when an unbound method is called with
an inappropriate first argument.  Now that there are more ways for
this to fail, make sure to report the name of the class of the
expected instance and of the actual instance.


Index: classobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/classobject.c,v
retrieving revision 2.141
retrieving revision 2.142
diff -C2 -d -r2.141 -r2.142
*** classobject.c	2001/08/24 18:34:26	2.141
--- classobject.c	2001/08/24 18:48:27	2.142
***************
*** 2128,2131 ****
--- 2128,2173 ----
  }
  
+ static char *
+ getclassname(PyObject *class)
+ {
+ 	PyObject *name;
+ 
+ 	if (class == NULL)
+ 		name = NULL;
+ 	else
+ 		name = PyObject_GetAttrString(class, "__name__");
+ 	if (name == NULL) {
+ 		PyErr_Clear();
+ 		return "?";
+ 	}
+ 	if (!PyString_Check(name)) {
+ 		Py_DECREF(name);
+ 		return "?";
+ 	}
+ 	PyString_InternInPlace(&name);
+ 	Py_DECREF(name);
+ 	return PyString_AS_STRING(name);
+ }
+ 
+ static char *
+ getinstclassname(PyObject *inst)
+ {
+ 	PyObject *class;
+ 	char *name;
+ 
+ 	if (inst == NULL)
+ 		return "nothing";
+ 
+ 	class = PyObject_GetAttrString(inst, "__class__");
+ 	if (class == NULL) {
+ 		PyErr_Clear();
+ 		class = (PyObject *)(inst->ob_type);
+ 		Py_INCREF(class);
+ 	}
+ 	name = getclassname(class);
+ 	Py_XDECREF(class);
+ 	return name;
+ }
+ 
  static PyObject *
  instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
***************
*** 2151,2158 ****
  		if (!ok) {
  			PyErr_Format(PyExc_TypeError,
! 				     "unbound method %s%s must be "
! 				     "called with instance as first argument",
  				     PyEval_GetFuncName(func),
! 				     PyEval_GetFuncDesc(func));
  			return NULL;
  		}
--- 2193,2204 ----
  		if (!ok) {
  			PyErr_Format(PyExc_TypeError,
! 				     "unbound method %s%s must be called with "
! 				     "%s instance as first argument "
! 				     "(got %s%s instead)",
  				     PyEval_GetFuncName(func),
! 				     PyEval_GetFuncDesc(func),
! 				     getclassname(class),
! 				     getinstclassname(self),
! 				     self == NULL ? "" : " instance");
  			return NULL;
  		}