chiark / gitweb /
build-sys: add a makefile target to run all tests through valgrind
[elogind.git] / src / python-systemd / _reader.c
index 6ac2f204911f676907c1adc4698fa8020661e9d3..bc5db19049577d079bba9db08bd3269d2d164597 100644 (file)
@@ -64,6 +64,10 @@ static PyStructSequence_Desc Monotonic_desc = {
 };
 #endif
 
+/**
+ * Convert a Python sequence object into a strv (char**), and
+ * None into a NULL pointer.
+ */
 static int strv_converter(PyObject* obj, void *_result) {
         char ***result = _result;
         Py_ssize_t i, len;
@@ -71,13 +75,22 @@ static int strv_converter(PyObject* obj, void *_result) {
         assert(result);
 
         if (!obj)
-            goto cleanup;
+            return 0;
+
+        if (obj == Py_None) {
+            *result = NULL;
+            return 1;
+        }
 
         if (!PySequence_Check(obj))
             return 0;
 
         len = PySequence_Length(obj);
         *result = new0(char*, len + 1);
+        if (!*result) {
+            set_error(-ENOMEM, NULL, NULL);
+            return 0;
+        }
 
         for (i = 0; i < len; i++) {
             PyObject *item;
@@ -145,7 +158,7 @@ static int Reader_init(Reader *self, PyObject *args, PyObject *keywds)
     char **files = NULL;
 
     static const char* const kwlist[] = {"flags", "path", "files", NULL};
-    if (!PyArg_ParseTupleAndKeywords(args, keywds, "|izO&", (char**) kwlist,
+    if (!PyArg_ParseTupleAndKeywords(args, keywds, "|izO&:__init__", (char**) kwlist,
                                      &flags, &path, strv_converter, &files))
         return -1;
 
@@ -908,9 +921,9 @@ static PyObject* Reader_get_catalog(Reader *self, PyObject *args)
 
         r = sd_journal_get_data(self->j, "MESSAGE_ID", &mid, &mid_len);
         if (r == 0) {
-            const int l = sizeof("MESSAGE_ID");
+            const size_t l = sizeof("MESSAGE_ID");
             assert(mid_len > l);
-            PyErr_Format(PyExc_KeyError, "%.*s", (int) mid_len - l,
+            PyErr_Format(PyExc_KeyError, "%.*s", (int) (mid_len - l),
                          (const char*) mid + l);
         } else if (r == -ENOENT)
             PyErr_SetString(PyExc_IndexError, "no MESSAGE_ID field");
@@ -1046,48 +1059,20 @@ static PyMethodDef Reader_methods[] = {
 
 static PyTypeObject ReaderType = {
     PyVarObject_HEAD_INIT(NULL, 0)
-    "_reader._Reader",                        /*tp_name*/
-    sizeof(Reader),                           /*tp_basicsize*/
-    0,                                        /*tp_itemsize*/
-    (destructor)Reader_dealloc,               /*tp_dealloc*/
-    0,                                        /*tp_print*/
-    0,                                        /*tp_getattr*/
-    0,                                        /*tp_setattr*/
-    0,                                        /*tp_compare*/
-    0,                                        /*tp_repr*/
-    0,                                        /*tp_as_number*/
-    0,                                        /*tp_as_sequence*/
-    0,                                        /*tp_as_mapping*/
-    0,                                        /*tp_hash */
-    0,                                        /*tp_call*/
-    0,                                        /*tp_str*/
-    0,                                        /*tp_getattro*/
-    0,                                        /*tp_setattro*/
-    0,                                        /*tp_as_buffer*/
-    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
-    Reader__doc__,                            /* tp_doc */
-    0,                                        /* tp_traverse */
-    0,                                        /* tp_clear */
-    0,                                        /* tp_richcompare */
-    0,                                        /* tp_weaklistoffset */
-    0,                                        /* tp_iter */
-    0,                                        /* tp_iternext */
-    Reader_methods,                           /* tp_methods */
-    0,                                        /* tp_members */
-    Reader_getsetters,                        /* tp_getset */
-    0,                                        /* tp_base */
-    0,                                        /* tp_dict */
-    0,                                        /* tp_descr_get */
-    0,                                        /* tp_descr_set */
-    0,                                        /* tp_dictoffset */
-    (initproc) Reader_init,                   /* tp_init */
-    0,                                        /* tp_alloc */
-    PyType_GenericNew,                        /* tp_new */
+    .tp_name = "_reader._Reader",
+    .tp_basicsize = sizeof(Reader),
+    .tp_dealloc = (destructor) Reader_dealloc,
+    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+    .tp_doc = Reader__doc__,
+    .tp_methods = Reader_methods,
+    .tp_getset = Reader_getsetters,
+    .tp_init = (initproc) Reader_init,
+    .tp_new = PyType_GenericNew,
 };
 
 static PyMethodDef methods[] = {
         { "_get_catalog", get_catalog, METH_VARARGS, get_catalog__doc__},
-        { NULL, NULL, 0, NULL }        /* Sentinel */
+        {} /* Sentinel */
 };
 
 #if PY_MAJOR_VERSION >= 3
@@ -1097,7 +1082,6 @@ static PyModuleDef module = {
     module__doc__,
     -1,
     methods,
-    NULL, NULL, NULL, NULL
 };
 #endif