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

Guido van Rossum gvanrossum@users.sourceforge.net
Tue, 28 Aug 2001 11:22:16 -0700


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

Modified Files:
	typeobject.c 
Log Message:
Finish the previous checkin: also avoid getattr when calling the method
directly.


Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.47
retrieving revision 2.48
diff -C2 -d -r2.47 -r2.48
*** typeobject.c	2001/08/28 17:47:51	2.47
--- typeobject.c	2001/08/28 18:22:14	2.48
***************
*** 320,323 ****
--- 320,372 ----
  }
  
+ /* A variation of PyObject_CallMethod that uses lookup_method()
+    instead of PyObject_GetAttrString().  This uses the same convention
+    as lookup_method to cache the interned name string object. */
+ 
+ PyObject *
+ call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
+ {
+ 	va_list va;
+ 	PyObject *args, *func = 0, *retval;
+ 	PyObject *dummy_str = NULL;
+ 	va_start(va, format);
+ 
+ 	func = lookup_method(o, name, &dummy_str);
+ 	Py_XDECREF(dummy_str);
+ 	if (func == NULL) {
+ 		va_end(va);
+ 		PyErr_SetString(PyExc_AttributeError, name);
+ 		return 0;
+ 	}
+ 
+ 	if (format && *format)
+ 		args = Py_VaBuildValue(format, va);
+ 	else
+ 		args = PyTuple_New(0);
+ 
+ 	va_end(va);
+ 
+ 	if (!args)
+ 		return NULL;
+ 
+ 	if (!PyTuple_Check(args)) {
+ 		PyObject *a;
+ 
+ 		a = PyTuple_New(1);
+ 		if (a == NULL)
+ 			return NULL;
+ 		if (PyTuple_SetItem(a, 0, args) < 0)
+ 			return NULL;
+ 		args = a;
+ 	}
+ 
+ 	retval = PyObject_CallObject(func, args);
+ 
+ 	Py_DECREF(args);
+ 	Py_DECREF(func);
+ 
+ 	return retval;
+ }
+ 
  /* Method resolution order algorithm from "Putting Metaclasses to Work"
     by Forman and Danforth (Addison-Wesley 1999). */
***************
*** 2342,2346 ****
  FUNCNAME(PyObject *self) \
  { \
! 	return PyObject_CallMethod(self, OPSTR, ""); \
  }
  
--- 2391,2396 ----
  FUNCNAME(PyObject *self) \
  { \
! 	static PyObject cache_str; \
! 	return call_method(self, OPSTR, &cache_str, ""); \
  }
  
***************
*** 2349,2353 ****
  FUNCNAME(PyObject *self, ARG1TYPE arg1) \
  { \
! 	return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1); \
  }
  
--- 2399,2404 ----
  FUNCNAME(PyObject *self, ARG1TYPE arg1) \
  { \
! 	static PyObject *cache_str; \
! 	return call_method(self, OPSTR, &cache_str, ARGCODES, arg1); \
  }
  
***************
*** 2357,2365 ****
  FUNCNAME(PyObject *self, PyObject *other) \
  { \
  	if (self->ob_type->tp_as_number != NULL && \
  	    self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
  		PyObject *r; \
! 		r = PyObject_CallMethod( \
! 			self, OPSTR, "O", other); \
  		if (r != Py_NotImplemented || \
  		    other->ob_type == self->ob_type) \
--- 2408,2417 ----
  FUNCNAME(PyObject *self, PyObject *other) \
  { \
+ 	static PyObject *cache_str, *rcache_str; \
  	if (self->ob_type->tp_as_number != NULL && \
  	    self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
  		PyObject *r; \
! 		r = call_method( \
! 			self, OPSTR, &cache_str, "O", other); \
  		if (r != Py_NotImplemented || \
  		    other->ob_type == self->ob_type) \
***************
*** 2369,2374 ****
  	if (other->ob_type->tp_as_number != NULL && \
  	    other->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
! 		return PyObject_CallMethod( \
! 			other, ROPSTR, "O", self); \
  	} \
  	Py_INCREF(Py_NotImplemented); \
--- 2421,2426 ----
  	if (other->ob_type->tp_as_number != NULL && \
  	    other->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
! 		return call_method( \
! 			other, ROPSTR, &rcache_str, "O", self); \
  	} \
  	Py_INCREF(Py_NotImplemented); \
***************
*** 2383,2387 ****
  FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
  { \
! 	return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1, arg2); \
  }
  
--- 2435,2440 ----
  FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
  { \
! 	static PyObject *cache_str; \
! 	return call_method(self, OPSTR, &cache_str, ARGCODES, arg1, arg2); \
  }
  
***************
*** 2389,2393 ****
  slot_sq_length(PyObject *self)
  {
! 	PyObject *res = PyObject_CallMethod(self, "__len__", "");
  
  	if (res == NULL)
--- 2442,2447 ----
  slot_sq_length(PyObject *self)
  {
! 	static PyObject *len_str;
! 	PyObject *res = call_method(self, "__len__", &len_str, "");
  
  	if (res == NULL)
***************
*** 2405,2414 ****
  {
  	PyObject *res;
  
  	if (value == NULL)
! 		res = PyObject_CallMethod(self, "__delitem__", "i", index);
  	else
! 		res = PyObject_CallMethod(self, "__setitem__",
! 					  "iO", index, value);
  	if (res == NULL)
  		return -1;
--- 2459,2470 ----
  {
  	PyObject *res;
+ 	static PyObject *delitem_str, *setitem_str;
  
  	if (value == NULL)
! 		res = call_method(self, "__delitem__", &delitem_str,
! 				  "i", index);
  	else
! 		res = call_method(self, "__setitem__", &setitem_str,
! 				  "iO", index, value);
  	if (res == NULL)
  		return -1;
***************
*** 2421,2430 ****
  {
  	PyObject *res;
  
  	if (value == NULL)
! 		res = PyObject_CallMethod(self, "__delslice__", "ii", i, j);
  	else
! 		res = PyObject_CallMethod(self, "__setslice__",
! 					  "iiO", i, j, value);
  	if (res == NULL)
  		return -1;
--- 2477,2488 ----
  {
  	PyObject *res;
+ 	static PyObject *delslice_str, *setslice_str;
  
  	if (value == NULL)
! 		res = call_method(self, "__delslice__", &delslice_str,
! 				  "ii", i, j);
  	else
! 		res = call_method(self, "__setslice__", &setslice_str,
! 				  "iiO", i, j, value);
  	if (res == NULL)
  		return -1;
***************
*** 2471,2480 ****
  {
  	PyObject *res;
  
  	if (value == NULL)
! 		res = PyObject_CallMethod(self, "__delitem__", "O", key);
  	else
! 		res = PyObject_CallMethod(self, "__setitem__",
! 					  "OO", key, value);
  	if (res == NULL)
  		return -1;
--- 2529,2540 ----
  {
  	PyObject *res;
+ 	static PyObject *delitem_str, *setitem_str;
  
  	if (value == NULL)
! 		res = call_method(self, "__delitem__", &delitem_str,
! 				  "O", key);
  	else
! 		res = call_method(self, "__setitem__", &setitem_str,
! 				 "OO", key, value);
  	if (res == NULL)
  		return -1;
***************
*** 2498,2505 ****
  slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
  {
  	if (modulus == Py_None)
  		return slot_nb_power_binary(self, other);
  	/* Three-arg power doesn't use __rpow__ */
! 	return PyObject_CallMethod(self, "__pow__", "OO", other, modulus);
  }
  
--- 2558,2568 ----
  slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
  {
+ 	static PyObject *pow_str;
+ 
  	if (modulus == Py_None)
  		return slot_nb_power_binary(self, other);
  	/* Three-arg power doesn't use __rpow__ */
! 	return call_method(self, "__pow__", &pow_str,
! 			   "OO", other, modulus);
  }
  
***************
*** 2728,2737 ****
  {
  	PyObject *res;
  
  	if (value == NULL)
! 		res = PyObject_CallMethod(self, "__delattr__", "O", name);
  	else
! 		res = PyObject_CallMethod(self, "__setattr__",
! 					  "OO", name, value);
  	if (res == NULL)
  		return -1;
--- 2791,2802 ----
  {
  	PyObject *res;
+ 	static PyObject *delattr_str, *setattr_str;
  
  	if (value == NULL)
! 		res = call_method(self, "__delattr__", &delattr_str,
! 				  "O", name);
  	else
! 		res = call_method(self, "__setattr__", &setattr_str,
! 				  "OO", name, value);
  	if (res == NULL)
  		return -1;
***************
*** 2823,2827 ****
  slot_tp_iternext(PyObject *self)
  {
! 	return PyObject_CallMethod(self, "next", "");
  }
  
--- 2888,2893 ----
  slot_tp_iternext(PyObject *self)
  {
! 	static PyObject *next_str;
! 	return call_method(self, "next", &next_str, "");
  }
  
***************
*** 2857,2866 ****
  {
  	PyObject *res;
  
  	if (value == NULL)
! 		res = PyObject_CallMethod(self, "__del__", "O", target);
  	else
! 		res = PyObject_CallMethod(self, "__set__",
! 					  "OO", target, value);
  	if (res == NULL)
  		return -1;
--- 2923,2934 ----
  {
  	PyObject *res;
+ 	static PyObject *del_str, *set_str;
  
  	if (value == NULL)
! 		res = call_method(self, "__del__", &del_str,
! 				  "O", target);
  	else
! 		res = call_method(self, "__set__", &set_str,
! 				  "OO", target, value);
  	if (res == NULL)
  		return -1;