GOTO (was Re: Appeal for python developers)

Steven Bethard steven.bethard at gmail.com
Sat Mar 5 16:46:03 EST 2005


Dennis Lee Bieber wrote:
> On 5 Mar 2005 08:00:23 -0800, beliavsky at aol.com declaimed the following
> in comp.lang.python:
> 
>>"explicit GOTO"'. Goto's are less dangerous when they are in the
>>forward direction, to code appearing later.
> 
> 	UGH... That is the one direction I always avoid (in FORTRAN 77).

Typical example of forward GOTOs in Python source:

static PyObject *
min_max(PyObject *args, PyObject *kwds, int op)
{
	...
	while (( item = PyIter_Next(it) )) {
		/* get the value from the key function */
		if (keyfunc != NULL) {
			val = PyObject_CallFunctionObjArgs(
				keyfunc, item, NULL);
			if (val == NULL)
				goto Fail_it_item;
		}
		...
		else {
			int cmp = PyObject_RichCompareBool(
				val, maxval, op);
			if (cmp < 0)
				goto Fail_it_item_and_val;
			else if (cmp > 0) {
				...
			}
		}
	}
	if (PyErr_Occurred())
		goto Fail_it;
	...
	return maxitem;

Fail_it_item_and_val:
	Py_DECREF(val);
Fail_it_item:
	Py_DECREF(item);
Fail_it:
	Py_XDECREF(maxval);
	Py_XDECREF(maxitem);
	Py_DECREF(it);
	return NULL;
}

Note that the GOTOs are basically there to take care of the appropriate 
decref-ing if exceptions occur.

STeVe



More information about the Python-list mailing list