[Info-vax] BASIC (and Horizon)

Dan Cross cross at spitfire.i.gajendra.net
Thu Feb 1 08:18:31 EST 2024


In article <upf0r9$1t7cn$1 at dont-email.me>,
Lawrence D'Oliveiro  <ldo at nz.invalid> wrote:
>On Wed, 31 Jan 2024 19:29:14 -0500, Arne Vajhøj wrote:
>
>> That code would look a lot cleaner if the do while(false) loop got
>> removed and the relevant breaks got replaced by goto's.
>
>Show us how you would do it. I can take you through my code step by step, 
>block by block, if that will help.

As I posted in my other message (untested):

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdio.h>

static PyObject *
discipline_makedict(PyObject *self, PyObject *args)
{
	PyObject *items;
	const char *msg = NULL;

	const int parsed_ok = PyArg_ParseTuple(args, "Os", &items, &msg);
	if (!parsed_ok)
		return NULL;

	printf("makedict says: \xe2\x80\x9c%s\xe2\x80\x9d\n", msg);
	if (!PyTuple_Check(items)) {
		PyErr_SetString(PyExc_TypeError, "expecting a tuple");
		return NULL;
	}
	const ssize_t nitems = PyTuple_Size(items);
	if (PyErr_Occurred())
		return NULL;

	PyObject *result = PyDict_New();
	if (result == NULL)
		return NULL;

	for (ssize_t i = 0; i < nitems; ++i) {
		PyObject *const item = PyTuple_GetItem(items, i);
		if (item == NULL)
			break;
		if (!PyTuple_Check(item) || PyTuple_Size(item) != 2) {
			PyErr_SetString(PyExc_TypeError, "expecting a 2-tuple");
			break;
		}
		PyObject *const first = PyTuple_GetItem(item, 0);
		if (first == NULL)
			break;
		PyObject *const second = PyTuple_GetItem(item, 1);
		if (second == NULL)
			break;
		if (first == (PyObject *)&ExceptMe_type || second == (PyObject *)&ExceptMe_type) {
			PyErr_SetString(PyExc_ValueError, "ExceptMe object found");
			break;
		}
		if (PyDict_SetItem(result, first, second) < 0)
			break;
	}

	if (PyErr_Occurred()) {
		Py_XDECREF(result);
	}

	return result;
}

No gotos, no simulated goto-via-early-break in a do/while(false)
loop.

As I said previously, this is more idiomatic, simpler, easier to
reason about, easier to read, and uses fewer variables.  Your
initial code was, to be blunt, unreadable and truly awful shit.

	- Dan C.




More information about the Info-vax mailing list