richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
9fc0f6
From b22a215807207c1a6332a22505840478ac633afd Mon Sep 17 00:00:00 2001
9fc0f6
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
9fc0f6
Date: Sun, 29 Dec 2013 23:39:28 -0500
9fc0f6
Subject: [PATCH] systemd-python: fix setting of exception codes
9fc0f6
9fc0f6
The return value of 0 would be treated as failure by mistake,
9fc0f6
resulting in " SystemError: error return without exception set".
9fc0f6
The way that set_error() is used is changed to be the same
9fc0f6
everywhere.
9fc0f6
---
9fc0f6
 src/python-systemd/_daemon.c | 14 +++++-----
9fc0f6
 src/python-systemd/_reader.c | 63 ++++++++++++++++++++------------------------
9fc0f6
 2 files changed, 35 insertions(+), 42 deletions(-)
9fc0f6
9fc0f6
diff --git a/src/python-systemd/_daemon.c b/src/python-systemd/_daemon.c
9fc0f6
index f0ab16f..c6db69f 100644
9fc0f6
--- a/src/python-systemd/_daemon.c
9fc0f6
+++ b/src/python-systemd/_daemon.c
9fc0f6
@@ -88,7 +88,7 @@ static PyObject* notify(PyObject *self, PyObject *args, PyObject *keywds) {
9fc0f6
 #endif
9fc0f6
 
9fc0f6
         r = sd_notify(unset, msg);
9fc0f6
-        if (set_error(r, NULL, NULL))
9fc0f6
+        if (set_error(r, NULL, NULL) < 0)
9fc0f6
                 return NULL;
9fc0f6
 
9fc0f6
         return PyBool_FromLong(r);
9fc0f6
@@ -123,7 +123,7 @@ static PyObject* listen_fds(PyObject *self, PyObject *args, PyObject *keywds) {
9fc0f6
 #endif
9fc0f6
 
9fc0f6
         r = sd_listen_fds(unset);
9fc0f6
-        if (set_error(r, NULL, NULL))
9fc0f6
+        if (set_error(r, NULL, NULL) < 0)
9fc0f6
                 return NULL;
9fc0f6
 
9fc0f6
         return long_FromLong(r);
9fc0f6
@@ -151,7 +151,7 @@ static PyObject* is_fifo(PyObject *self, PyObject *args) {
9fc0f6
 #endif
9fc0f6
 
9fc0f6
         r = sd_is_fifo(fd, path);
9fc0f6
-        if (set_error(r, path, NULL))
9fc0f6
+        if (set_error(r, path, NULL) < 0)
9fc0f6
                 return NULL;
9fc0f6
 
9fc0f6
         return PyBool_FromLong(r);
9fc0f6
@@ -179,7 +179,7 @@ static PyObject* is_mq(PyObject *self, PyObject *args) {
9fc0f6
 #endif
9fc0f6
 
9fc0f6
         r = sd_is_mq(fd, path);
9fc0f6
-        if (set_error(r, path, NULL))
9fc0f6
+        if (set_error(r, path, NULL) < 0)
9fc0f6
                 return NULL;
9fc0f6
 
9fc0f6
         return PyBool_FromLong(r);
9fc0f6
@@ -203,7 +203,7 @@ static PyObject* is_socket(PyObject *self, PyObject *args) {
9fc0f6
                 return NULL;
9fc0f6
 
9fc0f6
         r = sd_is_socket(fd, family, type, listening);
9fc0f6
-        if (set_error(r, NULL, NULL))
9fc0f6
+        if (set_error(r, NULL, NULL) < 0)
9fc0f6
                 return NULL;
9fc0f6
 
9fc0f6
         return PyBool_FromLong(r);
9fc0f6
@@ -230,7 +230,7 @@ static PyObject* is_socket_inet(PyObject *self, PyObject *args) {
9fc0f6
         }
9fc0f6
 
9fc0f6
         r = sd_is_socket_inet(fd, family, type, listening, (uint16_t) port);
9fc0f6
-        if (set_error(r, NULL, NULL))
9fc0f6
+        if (set_error(r, NULL, NULL) < 0)
9fc0f6
                 return NULL;
9fc0f6
 
9fc0f6
         return PyBool_FromLong(r);
9fc0f6
@@ -265,7 +265,7 @@ static PyObject* is_socket_unix(PyObject *self, PyObject *args) {
9fc0f6
 #endif
9fc0f6
 
9fc0f6
         r = sd_is_socket_unix(fd, type, listening, path, length);
9fc0f6
-        if (set_error(r, path, NULL))
9fc0f6
+        if (set_error(r, path, NULL) < 0)
9fc0f6
                 return NULL;
9fc0f6
 
9fc0f6
         return PyBool_FromLong(r);
9fc0f6
diff --git a/src/python-systemd/_reader.c b/src/python-systemd/_reader.c
9fc0f6
index bc5db19..0c88926 100644
9fc0f6
--- a/src/python-systemd/_reader.c
9fc0f6
+++ b/src/python-systemd/_reader.c
9fc0f6
@@ -206,8 +206,7 @@ PyDoc_STRVAR(Reader_reliable_fd__doc__,
9fc0f6
 static PyObject* Reader_reliable_fd(Reader *self, PyObject *args)
9fc0f6
 {
9fc0f6
     int r = sd_journal_reliable_fd(self->j);
9fc0f6
-    set_error(r, NULL, NULL);
9fc0f6
-    if (r < 0)
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
     return PyBool_FromLong(r);
9fc0f6
 }
9fc0f6
@@ -221,8 +220,7 @@ PyDoc_STRVAR(Reader_get_events__doc__,
9fc0f6
 static PyObject* Reader_get_events(Reader *self, PyObject *args)
9fc0f6
 {
9fc0f6
     int r = sd_journal_get_events(self->j);
9fc0f6
-    set_error(r, NULL, NULL);
9fc0f6
-    if (r < 0)
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
     return long_FromLong(r);
9fc0f6
 }
9fc0f6
@@ -242,8 +240,7 @@ static PyObject* Reader_get_timeout(Reader *self, PyObject *args)
9fc0f6
     uint64_t t;
9fc0f6
 
9fc0f6
     r = sd_journal_get_timeout(self->j, &t);
9fc0f6
-    set_error(r, NULL, NULL);
9fc0f6
-    if (r < 0)
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     if (t == (uint64_t) -1)
9fc0f6
@@ -265,8 +262,7 @@ static PyObject* Reader_get_timeout_ms(Reader *self, PyObject *args)
9fc0f6
     uint64_t t;
9fc0f6
 
9fc0f6
     r = sd_journal_get_timeout(self->j, &t);
9fc0f6
-    set_error(r, NULL, NULL);
9fc0f6
-    if (r < 0)
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     return absolute_timeout(t);
9fc0f6
@@ -304,7 +300,7 @@ static PyObject* Reader_get_usage(Reader *self, PyObject *args)
9fc0f6
     uint64_t bytes;
9fc0f6
 
9fc0f6
     r = sd_journal_get_usage(self->j, &bytes);
9fc0f6
-    if (set_error(r, NULL, NULL))
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     assert_cc(sizeof(unsigned long long) == sizeof(bytes));
9fc0f6
@@ -366,8 +362,7 @@ static PyObject* Reader_next(Reader *self, PyObject *args)
9fc0f6
         assert_not_reached("should not be here");
9fc0f6
     Py_END_ALLOW_THREADS
9fc0f6
 
9fc0f6
-    set_error(r, NULL, NULL);
9fc0f6
-    if (r < 0)
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
     return PyBool_FromLong(r);
9fc0f6
 }
9fc0f6
@@ -445,7 +440,8 @@ static PyObject* Reader_get(Reader *self, PyObject *args)
9fc0f6
     if (r == -ENOENT) {
9fc0f6
         PyErr_SetString(PyExc_KeyError, field);
9fc0f6
         return NULL;
9fc0f6
-    } else if (set_error(r, NULL, "field name is not valid"))
9fc0f6
+    }
9fc0f6
+    if (set_error(r, NULL, "field name is not valid") < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     r = extract(msg, msg_len, NULL, &value);
9fc0f6
@@ -530,7 +526,7 @@ static PyObject* Reader_get_realtime(Reader *self, PyObject *args)
9fc0f6
     assert(!args);
9fc0f6
 
9fc0f6
     r = sd_journal_get_realtime_usec(self->j, &timestamp);
9fc0f6
-    if (set_error(r, NULL, NULL))
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     assert_cc(sizeof(unsigned long long) == sizeof(timestamp));
9fc0f6
@@ -555,7 +551,7 @@ static PyObject* Reader_get_monotonic(Reader *self, PyObject *args)
9fc0f6
     assert(!args);
9fc0f6
 
9fc0f6
     r = sd_journal_get_monotonic_usec(self->j, &timestamp, &id;;
9fc0f6
-    if (set_error(r, NULL, NULL))
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     assert_cc(sizeof(unsigned long long) == sizeof(timestamp));
9fc0f6
@@ -598,8 +594,7 @@ static PyObject* Reader_add_match(Reader *self, PyObject *args, PyObject *keywds
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     r = sd_journal_add_match(self->j, match, match_len);
9fc0f6
-    set_error(r, NULL, "Invalid match");
9fc0f6
-    if (r < 0)
9fc0f6
+    if (set_error(r, NULL, "Invalid match") < 0)
9fc0f6
             return NULL;
9fc0f6
 
9fc0f6
     Py_RETURN_NONE;
9fc0f6
@@ -616,8 +611,7 @@ static PyObject* Reader_add_disjunction(Reader *self, PyObject *args)
9fc0f6
 {
9fc0f6
     int r;
9fc0f6
     r = sd_journal_add_disjunction(self->j);
9fc0f6
-    set_error(r, NULL, NULL);
9fc0f6
-    if (r < 0)
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
     Py_RETURN_NONE;
9fc0f6
 }
9fc0f6
@@ -633,8 +627,7 @@ static PyObject* Reader_add_conjunction(Reader *self, PyObject *args)
9fc0f6
 {
9fc0f6
     int r;
9fc0f6
     r = sd_journal_add_conjunction(self->j);
9fc0f6
-    set_error(r, NULL, NULL);
9fc0f6
-    if (r < 0)
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
     Py_RETURN_NONE;
9fc0f6
 }
9fc0f6
@@ -661,7 +654,7 @@ static PyObject* Reader_seek_head(Reader *self, PyObject *args)
9fc0f6
     Py_BEGIN_ALLOW_THREADS
9fc0f6
     r = sd_journal_seek_head(self->j);
9fc0f6
     Py_END_ALLOW_THREADS
9fc0f6
-    if (set_error(r, NULL, NULL))
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
     Py_RETURN_NONE;
9fc0f6
 }
9fc0f6
@@ -678,7 +671,7 @@ static PyObject* Reader_seek_tail(Reader *self, PyObject *args)
9fc0f6
     Py_BEGIN_ALLOW_THREADS
9fc0f6
     r = sd_journal_seek_tail(self->j);
9fc0f6
     Py_END_ALLOW_THREADS
9fc0f6
-    if (set_error(r, NULL, NULL))
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
     Py_RETURN_NONE;
9fc0f6
 }
9fc0f6
@@ -699,7 +692,7 @@ static PyObject* Reader_seek_realtime(Reader *self, PyObject *args)
9fc0f6
     Py_BEGIN_ALLOW_THREADS
9fc0f6
     r = sd_journal_seek_realtime_usec(self->j, timestamp);
9fc0f6
     Py_END_ALLOW_THREADS
9fc0f6
-    if (set_error(r, NULL, NULL))
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
     Py_RETURN_NONE;
9fc0f6
 }
9fc0f6
@@ -723,20 +716,20 @@ static PyObject* Reader_seek_monotonic(Reader *self, PyObject *args)
9fc0f6
 
9fc0f6
     if (bootid) {
9fc0f6
         r = sd_id128_from_string(bootid, &id;;
9fc0f6
-        if (set_error(r, NULL, "Invalid bootid"))
9fc0f6
+        if (set_error(r, NULL, "Invalid bootid") < 0)
9fc0f6
             return NULL;
9fc0f6
     } else {
9fc0f6
         Py_BEGIN_ALLOW_THREADS
9fc0f6
         r = sd_id128_get_boot(&id;;
9fc0f6
         Py_END_ALLOW_THREADS
9fc0f6
-        if (set_error(r, NULL, NULL))
9fc0f6
+        if (set_error(r, NULL, NULL) < 0)
9fc0f6
             return NULL;
9fc0f6
     }
9fc0f6
 
9fc0f6
     Py_BEGIN_ALLOW_THREADS
9fc0f6
     r = sd_journal_seek_monotonic_usec(self->j, id, timestamp);
9fc0f6
     Py_END_ALLOW_THREADS
9fc0f6
-    if (set_error(r, NULL, NULL))
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     Py_RETURN_NONE;
9fc0f6
@@ -809,7 +802,7 @@ static PyObject* Reader_seek_cursor(Reader *self, PyObject *args)
9fc0f6
     Py_BEGIN_ALLOW_THREADS
9fc0f6
     r = sd_journal_seek_cursor(self->j, cursor);
9fc0f6
     Py_END_ALLOW_THREADS
9fc0f6
-    if (set_error(r, NULL, "Invalid cursor"))
9fc0f6
+    if (set_error(r, NULL, "Invalid cursor") < 0)
9fc0f6
         return NULL;
9fc0f6
     Py_RETURN_NONE;
9fc0f6
 }
9fc0f6
@@ -828,7 +821,7 @@ static PyObject* Reader_get_cursor(Reader *self, PyObject *args)
9fc0f6
     assert(!args);
9fc0f6
 
9fc0f6
     r = sd_journal_get_cursor(self->j, &cursor);
9fc0f6
-    if (set_error(r, NULL, NULL))
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     return unicode_FromString(cursor);
9fc0f6
@@ -851,8 +844,7 @@ static PyObject* Reader_test_cursor(Reader *self, PyObject *args)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     r = sd_journal_test_cursor(self->j, cursor);
9fc0f6
-    set_error(r, NULL, NULL);
9fc0f6
-    if (r < 0)
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     return PyBool_FromLong(r);
9fc0f6
@@ -876,7 +868,7 @@ static PyObject* Reader_query_unique(Reader *self, PyObject *args)
9fc0f6
     Py_BEGIN_ALLOW_THREADS
9fc0f6
     r = sd_journal_query_unique(self->j, query);
9fc0f6
     Py_END_ALLOW_THREADS
9fc0f6
-    if (set_error(r, NULL, "Invalid field name"))
9fc0f6
+    if (set_error(r, NULL, "Invalid field name") < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     value_set = PySet_New(0);
9fc0f6
@@ -930,7 +922,8 @@ static PyObject* Reader_get_catalog(Reader *self, PyObject *args)
9fc0f6
         else
9fc0f6
             set_error(r, NULL, NULL);
9fc0f6
         return NULL;
9fc0f6
-    } else if (set_error(r, NULL, NULL))
9fc0f6
+    }
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     return unicode_FromString(msg);
9fc0f6
@@ -955,13 +948,13 @@ static PyObject* get_catalog(PyObject *self, PyObject *args)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     r = sd_id128_from_string(id_, &id;;
9fc0f6
-    if (set_error(r, NULL, "Invalid id128"))
9fc0f6
+    if (set_error(r, NULL, "Invalid id128") < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     Py_BEGIN_ALLOW_THREADS
9fc0f6
     r = sd_journal_get_catalog_for_message_id(id, &msg;;
9fc0f6
     Py_END_ALLOW_THREADS
9fc0f6
-    if (set_error(r, NULL, NULL))
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     return unicode_FromString(msg);
9fc0f6
@@ -979,7 +972,7 @@ static PyObject* Reader_get_data_threshold(Reader *self, void *closure)
9fc0f6
     int r;
9fc0f6
 
9fc0f6
     r = sd_journal_get_data_threshold(self->j, &cvalue);
9fc0f6
-    if (set_error(r, NULL, NULL))
9fc0f6
+    if (set_error(r, NULL, NULL) < 0)
9fc0f6
         return NULL;
9fc0f6
 
9fc0f6
     return long_FromSize_t(cvalue);