[Python-checkins] CVS: python/dist/src/Python ceval.c,2.235,2.236

Jeremy Hylton jhylton@users.sourceforge.net
Wed, 11 Apr 2001 06:52:31 -0700


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

Modified Files:
	ceval.c 
Log Message:
Fix exception handling for non-PyFunction objects, SF bug 414743.
Fix based on patch #414750 by Michael Hudson.

New functions get_func_name() and get_func_desc() return reasonable
names and descriptions for all objects.  XXX Even objects that aren't
actually callable.


Index: ceval.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/ceval.c,v
retrieving revision 2.235
retrieving revision 2.236
diff -C2 -r2.235 -r2.236
*** ceval.c	2001/03/22 02:47:58	2.235
--- ceval.c	2001/04/11 13:52:29	2.236
***************
*** 41,44 ****
--- 41,46 ----
  			    PyObject *);
  
+ static char *get_func_name(PyObject *);
+ static char *get_func_desc(PyObject *);
  static PyObject *call_object(PyObject *, PyObject *, PyObject *);
  static PyObject *call_cfunction(PyObject *, PyObject *, PyObject *);
***************
*** 2742,2745 ****
--- 2744,2784 ----
  */
  
+ static char *
+ get_func_name(PyObject *func)
+ {
+ 	if (PyMethod_Check(func))
+ 		return get_func_name(PyMethod_GET_FUNCTION(func));
+ 	else if (PyFunction_Check(func))
+ 		return PyString_AsString(((PyFunctionObject*)func)->func_name);
+ 	else if (PyCFunction_Check(func))
+ 		return ((PyCFunctionObject*)func)->m_ml->ml_name;
+ 	else if (PyClass_Check(func))
+ 		return PyString_AsString(((PyClassObject*)func)->cl_name);
+ 	else if (PyInstance_Check(func)) {
+ 		return PyString_AsString(
+ 			((PyInstanceObject*)func)->in_class->cl_name);
+ 	} else {
+ 		return func->ob_type->tp_name;
+ 	}
+ }
+ 
+ static char *
+ get_func_desc(PyObject *func)
+ {
+ 	if (PyMethod_Check(func))
+ 		return "()";
+ 	else if (PyFunction_Check(func))
+ 		return "()";
+ 	else if (PyCFunction_Check(func))
+ 		return "()";
+ 	else if (PyClass_Check(func))
+ 		return " constructor";
+ 	else if (PyInstance_Check(func)) {
+ 		return " instance";
+ 	} else {
+ 		return " object";
+ 	}
+ }
+ 
  static PyObject *
  call_object(PyObject *func, PyObject *arg, PyObject *kw)
***************
*** 2993,3002 ****
  		PyObject *key = EXT_POP(*pp_stack);
  		if (PyDict_GetItem(kwdict, key) != NULL) {
-                         PyObject* fn = ((PyFunctionObject*) func)->func_name;
                          PyErr_Format(PyExc_TypeError,
                                       "%.200s%s got multiple values "
!                                      "for keyword argument '%.400s'",
!                                      fn ? PyString_AsString(fn) : "function",
!                                      fn ? "()" : "", PyString_AsString(key));
  			Py_DECREF(key);
  			Py_DECREF(value);
--- 3032,3041 ----
  		PyObject *key = EXT_POP(*pp_stack);
  		if (PyDict_GetItem(kwdict, key) != NULL) {
                          PyErr_Format(PyExc_TypeError,
                                       "%.200s%s got multiple values "
!                                      "for keyword argument '%.200s'",
! 				     get_func_name(func),
! 				     get_func_desc(func),
! 				     PyString_AsString(key));
  			Py_DECREF(key);
  			Py_DECREF(value);
***************
*** 3089,3097 ****
  		kwdict = EXT_POP(*pp_stack);
  		if (!(kwdict && PyDict_Check(kwdict))) {
-                         PyObject* fn = ((PyFunctionObject*) func)->func_name;
  			PyErr_Format(PyExc_TypeError,
!                             "%s%s argument after ** must be a dictionary",
!                             fn ? PyString_AsString(fn) : "function",
!                             fn ? "()" : "");
  			goto ext_call_fail;
  		}
--- 3128,3136 ----
  		kwdict = EXT_POP(*pp_stack);
  		if (!(kwdict && PyDict_Check(kwdict))) {
  			PyErr_Format(PyExc_TypeError,
! 				     "%s%s argument after ** "
! 				     "must be a dictionary",
! 				     get_func_name(func),
! 				     get_func_desc(func));
  			goto ext_call_fail;
  		}
***************
*** 3103,3114 ****
  			t = PySequence_Tuple(stararg);
  			if (t == NULL) {
! 			    if (PyErr_ExceptionMatches(PyExc_TypeError)) {
!                                 PyObject* fn =
!                                     ((PyFunctionObject*) func)->func_name;
! 				PyErr_Format(PyExc_TypeError,
!                                     "%s%s argument after * must be a sequence",
!                                     fn ? PyString_AsString(fn) : "function",
!                                     fn ? "()" : "");
! 			    }
  				goto ext_call_fail;
  			}
--- 3142,3152 ----
  			t = PySequence_Tuple(stararg);
  			if (t == NULL) {
! 				if (PyErr_ExceptionMatches(PyExc_TypeError)) {
! 					PyErr_Format(PyExc_TypeError,
! 						     "%s%s argument after * "
! 						     "must be a sequence",
! 						     get_func_name(func),
! 						     get_func_desc(func));
! 				}
  				goto ext_call_fail;
  			}