[Patches] in-overloading: done right, with Guido's comments folded in

Greg Stein gstein@lyra.org
Sat, 19 Feb 2000 04:25:49 -0800 (PST)


On Sat, 19 Feb 2000, Moshe Zadka wrote:
> (Greg, a favour: my connection to Python.Org seems flaky. If you don't see
> it on the list within reasonable time, please forward this to the patch
> list. Thanks a lot)

I'm seeing it, too. The python.org mail handler (dinsdale.python.org)
appears to be down. This mail thread is waiting in my outbound queue for
the machine to reappear :-)  (I presume the same on your end)

>...
> ! 	if(PyType_HasFeature(w->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
> ! 		sq = w->ob_type->tp_as_sequence;
> ! 	        if(sq != NULL && sq->sq_contains != NULL)
> ! 			return (*sq->sq_contains)(w, v);
> ! 	}
> ! 	
> ! 	/* If there is no better way to check whether an item is is contained,
> ! 	   do it the hard way */
>   	sq = w->ob_type->tp_as_sequence;
>   	if (sq == NULL || sq->sq_item == NULL) {
>   		PyErr_SetString(PyExc_TypeError,

It would be nice to fall through that "return" statement, but I see that
we aren't really able to do that. Bummer :-(

Well, a bit of code replication can't hurt too bad, I guess...

>...
> + 	func = instance_getattr(inst, __contains__);
> + 	if(func == NULL) {
> + 		/* fall back to previous behaviour */
> + 		int i, cmp_res;
> + 
> + 		if(!PyErr_ExceptionMatches(PyExc_AttributeError))
> + 			return -1;
> + 		PyErr_Clear();

Ah! Very good catch on "eating up" the AttributeError!

> + 		for(i=0;;i++) {
> + 			PyObject *obj = instance_item(inst, i);
> + 			if(obj == NULL) {
> + 				if(!PyErr_ExceptionMatches(PyExc_IndexError))
> + 					return -1;
> + 				PyErr_Clear();
> + 				return 0;
> + 			}
> + 			if(PyObject_Cmp(obj, member, &cmp_res) == -1) {

The PyObject_Compare() used in abstract.c is a teeny bit faster.

> + 				Py_DECREF(obj);
> + 				return -1;
> + 			}
> + 			if(cmp_res == 0) {
> + 				Py_DECREF(obj);
> + 				return 1;
> + 			}
> + 		}

obj is never DECREF'd if the "if" statements never match. The style used
in abstract.c, would work well here (I'd recommend following that one).
Note that abstract.c uses Py_XDECREF even though we know x != NULL...  
your code can simply Py_DECREF. And your direct use of instance_item() is
good!

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/