1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
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.
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.
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/>.
25 void cleanup_Py_DECREFp(PyObject **p) {
32 PyObject* absolute_timeout(uint64_t t) {
33 if (t == (uint64_t) -1)
34 return PyLong_FromLong(-1);
40 clock_gettime(CLOCK_MONOTONIC, &ts);
41 n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
42 msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
44 return PyLong_FromLong(msec);
48 int set_error(int r, const char* path, const char* invalid_message) {
51 if (r == -EINVAL && invalid_message)
52 PyErr_SetString(PyExc_ValueError, invalid_message);
53 else if (r == -ENOMEM)
54 PyErr_SetString(PyExc_MemoryError, "Not enough memory");
57 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
62 #if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
63 int Unicode_FSConverter(PyObject* obj, void *_result) {
64 PyObject **result = _result;
69 /* cleanup: we don't return Py_CLEANUP_SUPPORTED, so
70 * we can assume that it was PyUnicode_FSConverter. */
71 return PyUnicode_FSConverter(obj, result);
78 return PyUnicode_FSConverter(obj, result);