chiark / gitweb /
Remove some unused variables
[elogind.git] / src / python-systemd / id128.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Zbigniew JÄ™drzejewski-Szmek <zbyszek@in.waw.pl>
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdbool.h>
23
24 #include <Python.h>
25
26 #include <systemd/sd-messages.h>
27
28 #include "pyutil.h"
29
30 PyDoc_STRVAR(module__doc__,
31              "Python interface to the libsystemd-id128 library.\n\n"
32              "Provides SD_MESSAGE_* constants and functions to query and generate\n"
33              "128-bit unique identifiers."
34 );
35
36 PyDoc_STRVAR(randomize__doc__,
37              "randomize() -> UUID\n\n"
38              "Return a new random 128-bit unique identifier.\n"
39              "Wraps sd_id128_randomize(3)."
40 );
41
42 PyDoc_STRVAR(get_machine__doc__,
43              "get_machine() -> UUID\n\n"
44              "Return a 128-bit unique identifier for this machine.\n"
45              "Wraps sd_id128_get_machine(3)."
46 );
47
48 PyDoc_STRVAR(get_boot__doc__,
49              "get_boot() -> UUID\n\n"
50              "Return a 128-bit unique identifier for this boot.\n"
51              "Wraps sd_id128_get_boot(3)."
52 );
53
54 static PyObject* make_uuid(sd_id128_t id) {
55         PyObject _cleanup_Py_DECREF_
56                 *uuid = NULL, *UUID = NULL, *bytes = NULL,
57                 *args = NULL, *kwargs = NULL;
58
59         uuid = PyImport_ImportModule("uuid");
60         if (!uuid)
61                 return NULL;
62
63         UUID = PyObject_GetAttrString(uuid, "UUID");
64         bytes = PyBytes_FromStringAndSize((const char*) &id.bytes, sizeof(id.bytes));
65         args = Py_BuildValue("()");
66         kwargs = PyDict_New();
67         if (!UUID || !bytes || !args || !kwargs)
68                 return NULL;
69
70         if (PyDict_SetItemString(kwargs, "bytes", bytes) < 0)
71                 return NULL;
72
73         return PyObject_Call(UUID, args, kwargs);
74 }
75
76 #define helper(name)                                                    \
77         static PyObject *name(PyObject *self, PyObject *args) {         \
78                 sd_id128_t id;                                          \
79                 int r;                                                  \
80                                                                         \
81                 assert(args == NULL);                                   \
82                                                                         \
83                 r = sd_id128_##name(&id);                               \
84                 if (r < 0) {                                            \
85                         errno = -r;                                     \
86                         return PyErr_SetFromErrno(PyExc_IOError);       \
87                 }                                                       \
88                                                                         \
89                 return make_uuid(id);                                   \
90         }
91
92 helper(randomize)
93 helper(get_machine)
94 helper(get_boot)
95
96 static PyMethodDef methods[] = {
97         { "randomize", randomize, METH_NOARGS, randomize__doc__},
98         { "get_machine", get_machine, METH_NOARGS, get_machine__doc__},
99         { "get_boot", get_boot, METH_NOARGS, get_boot__doc__},
100         { NULL, NULL, 0, NULL }        /* Sentinel */
101 };
102
103 static int add_id(PyObject *module, const char* name, sd_id128_t id) {
104         PyObject *obj;
105
106         obj = make_uuid(id);
107         if (!obj)
108                 return -1;
109
110         return PyModule_AddObject(module, name, obj);
111 }
112
113 #pragma GCC diagnostic push
114 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
115
116 #if PY_MAJOR_VERSION < 3
117
118 PyMODINIT_FUNC initid128(void) {
119         PyObject *m;
120
121         m = Py_InitModule3("id128", methods, module__doc__);
122         if (m == NULL)
123                 return;
124
125         /* a series of lines like 'add_id() ;' follow */
126 #define JOINER ;
127 #include "id128-constants.h"
128 #undef JOINER
129 }
130
131 #else
132
133 static struct PyModuleDef module = {
134         PyModuleDef_HEAD_INIT,
135         "id128", /* name of module */
136         module__doc__, /* module documentation, may be NULL */
137         -1, /* size of per-interpreter state of the module */
138         methods
139 };
140
141 PyMODINIT_FUNC PyInit_id128(void) {
142         PyObject *m;
143
144         m = PyModule_Create(&module);
145         if (m == NULL)
146                 return NULL;
147
148         if ( /* a series of lines like 'add_id() ||' follow */
149 #define JOINER ||
150 #include "id128-constants.h"
151 #undef JOINER
152                 false) {
153                 Py_DECREF(m);
154                 return NULL;
155         }
156
157         return m;
158 }
159
160 #endif
161
162 #pragma GCC diagnostic pop