diff options
| author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2012-06-28 11:28:51 -0400 |
|---|---|---|
| committer | Zbyszek Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2012-07-10 10:26:40 +0200 |
| commit | bf05f619b016888909d912f5d6ad6b79da2269c2 (patch) | |
| tree | 06c9ba0cf661f79bafdfa6d069eaf059f3ef4d38 | |
| parent | 7f9ebf50adf24e506ea9b5c9b8fa299e5235cb70 (diff) | |
| download | python-systemd-bf05f619b016888909d912f5d6ad6b79da2269c2.tar.gz | |
Rework error handling in preparation for py3k changes
When the #ifdefs neccesary to support Python 2 and 3 are added,
there will be many more paths through the code. This style of error
handling makes the code easier to read.
| -rw-r--r-- | journald.c | 24 |
1 files changed, 14 insertions, 10 deletions
@@ -6,11 +6,13 @@ journald_send(PyObject *self, PyObject *args) { struct iovec *iov = NULL; int argc = PyTuple_Size(args); int i, r; + PyObject *ret = NULL; // Allocate sufficient iovector space for the arguments. iov = malloc(argc * sizeof(struct iovec)); if (!iov) { - return PyErr_NoMemory(); + ret = PyErr_NoMemory(); + goto out; } // Iterate through the Python arguments and fill the iovector. @@ -21,9 +23,8 @@ journald_send(PyObject *self, PyObject *args) { if (PyString_AsStringAndSize(item, &stritem, &length)) { // PyString_AsS&S has already raised TypeError at this // point. We can just free iov and return NULL. - free(iov); - return NULL; - } + goto out; + } iov[i].iov_base = stritem; iov[i].iov_len = length; } @@ -35,21 +36,24 @@ journald_send(PyObject *self, PyObject *args) { // Send the iovector to journald. r = sd_journal_sendv(iov, argc); - // Free the iovector. The actual strings - // are already managed by Python. - free(iov); - if (r) { if (errno) PyErr_SetFromErrno(PyExc_IOError); else PyErr_SetString(PyExc_ValueError, "invalid message format"); - return NULL; + goto out; } // End with success. Py_INCREF(Py_None); - return Py_None; + ret = Py_None; + + out: + // Free the iovector. The actual strings + // are already managed by Python. + free(iov); + + return ret; } static PyMethodDef journaldMethods[] = { |
