summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoerg Behrmann <behrmann@physik.fu-berlin.de>2022-04-14 15:08:02 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-02-11 14:39:52 +0100
commit802c8dcaa3096719be0a1c121e747b2681ad31dc (patch)
tree4c859504bb703aac066ae635c24f7fa17f1a7d2c
parent7e3313a15b83400f00d3891f16e0f85f19cb3862 (diff)
downloadpython-systemd-802c8dcaa3096719be0a1c121e747b2681ad31dc.tar.gz
treewide: remove python 2 support and assume we have python 3.3+
-rw-r--r--systemd/_daemon.c68
-rw-r--r--systemd/_journal.c18
-rw-r--r--systemd/_reader.c42
-rw-r--r--systemd/id128.c22
-rw-r--r--systemd/journal.py19
-rw-r--r--systemd/login.c24
-rw-r--r--systemd/pyutil.c2
-rw-r--r--systemd/pyutil.h12
-rw-r--r--systemd/test/test_daemon.py14
9 files changed, 10 insertions, 211 deletions
diff --git a/systemd/_daemon.c b/systemd/_daemon.c
index e83da48..8369c0e 100644
--- a/systemd/_daemon.c
+++ b/systemd/_daemon.c
@@ -89,20 +89,9 @@ static PyObject* notify(PyObject *self, PyObject *args, PyObject *keywds) {
"fds",
NULL,
};
-#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 3
if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|piO:notify",
(char**) kwlist, &msg, &unset, &_pid, &fds))
return NULL;
-#else
- PyObject *obj = NULL;
- if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|OiO:notify",
- (char**) kwlist, &msg, &obj, &_pid, &fds))
- return NULL;
- if (obj)
- unset = PyObject_IsTrue(obj);
- if (unset < 0)
- return NULL;
-#endif
pid = _pid;
if (pid < 0 || pid != _pid) {
PyErr_SetString(PyExc_OverflowError, "Bad pid_t");
@@ -176,20 +165,9 @@ static PyObject* listen_fds(PyObject *self, PyObject *args, PyObject *keywds) {
int unset = true;
static const char* const kwlist[] = {"unset_environment", NULL};
-#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 3
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|p:_listen_fds",
(char**) kwlist, &unset))
return NULL;
-#else
- PyObject *obj = NULL;
- if (!PyArg_ParseTupleAndKeywords(args, keywds, "|O:_listen_fds",
- (char**) kwlist, &obj))
- return NULL;
- if (obj)
- unset = PyObject_IsTrue(obj);
- if (unset < 0)
- return NULL;
-#endif
r = sd_listen_fds(unset);
if (set_error(r, NULL, NULL) < 0)
@@ -223,20 +201,9 @@ static PyObject* listen_fds_with_names(PyObject *self, PyObject *args, PyObject
PyObject *tpl, *item;
static const char* const kwlist[] = {"unset_environment", NULL};
-#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 3
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|p:_listen_fds_with_names",
(char**) kwlist, &unset))
return NULL;
-#else
- PyObject *obj = NULL;
- if (!PyArg_ParseTupleAndKeywords(args, keywds, "|O:_listen_fds_with_names",
- (char**) kwlist, &obj))
- return NULL;
- if (obj != NULL)
- unset = PyObject_IsTrue(obj);
- if (unset < 0)
- return NULL;
-#endif
#if HAVE_SD_LISTEN_FDS_WITH_NAMES
r = sd_listen_fds_with_names(unset, &names);
@@ -284,17 +251,12 @@ static PyObject* is_fifo(PyObject *self, PyObject *args) {
int fd;
const char *path = NULL;
-#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
_cleanup_Py_DECREF_ PyObject *_path = NULL;
if (!PyArg_ParseTuple(args, "i|O&:_is_fifo",
&fd, Unicode_FSConverter, &_path))
return NULL;
if (_path)
path = PyBytes_AsString(_path);
-#else
- if (!PyArg_ParseTuple(args, "i|z:_is_fifo", &fd, &path))
- return NULL;
-#endif
r = sd_is_fifo(fd, path);
if (set_error(r, path, NULL) < 0)
@@ -315,17 +277,12 @@ static PyObject* is_mq(PyObject *self, PyObject *args) {
int fd;
const char *path = NULL;
-#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
_cleanup_Py_DECREF_ PyObject *_path = NULL;
if (!PyArg_ParseTuple(args, "i|O&:_is_mq",
&fd, Unicode_FSConverter, &_path))
return NULL;
if (_path)
path = PyBytes_AsString(_path);
-#else
- if (!PyArg_ParseTuple(args, "i|z:_is_mq", &fd, &path))
- return NULL;
-#endif
r = sd_is_mq(fd, path);
if (set_error(r, path, NULL) < 0)
@@ -451,7 +408,6 @@ static PyObject* is_socket_unix(PyObject *self, PyObject *args) {
char* path = NULL;
Py_ssize_t length = 0;
-#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
_cleanup_Py_DECREF_ PyObject *_path = NULL;
if (!PyArg_ParseTuple(args, "i|iiO&:_is_socket_unix",
&fd, &type, &listening, Unicode_FSConverter, &_path))
@@ -461,11 +417,6 @@ static PyObject* is_socket_unix(PyObject *self, PyObject *args) {
if (PyBytes_AsStringAndSize(_path, &path, &length))
return NULL;
}
-#else
- if (!PyArg_ParseTuple(args, "i|iiz#:_is_socket_unix",
- &fd, &type, &listening, &path, &length))
- return NULL;
-#endif
r = sd_is_socket_unix(fd, type, listening, path, length);
if (set_error(r, path, NULL) < 0)
@@ -490,23 +441,6 @@ static PyMethodDef methods[] = {
{} /* Sentinel */
};
-#if PY_MAJOR_VERSION < 3
-
-DISABLE_WARNING_MISSING_PROTOTYPES;
-PyMODINIT_FUNC init_daemon(void) {
- PyObject *m;
-
- m = Py_InitModule3("_daemon", methods, module__doc__);
- if (!m)
- return;
-
- PyModule_AddIntConstant(m, "LISTEN_FDS_START", SD_LISTEN_FDS_START);
- PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
-}
-REENABLE_WARNING;
-
-#else
-
static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT,
.m_name = "_daemon", /* name of module */
@@ -532,5 +466,3 @@ PyMODINIT_FUNC PyInit__daemon(void) {
return m;
}
REENABLE_WARNING;
-
-#endif
diff --git a/systemd/_journal.c b/systemd/_journal.c
index 4f53dba..b698996 100644
--- a/systemd/_journal.c
+++ b/systemd/_journal.c
@@ -113,22 +113,6 @@ static PyMethodDef methods[] = {
{} /* Sentinel */
};
-#if PY_MAJOR_VERSION < 3
-
-DISABLE_WARNING_MISSING_PROTOTYPES;
-PyMODINIT_FUNC init_journal(void) {
- PyObject *m;
-
- m = Py_InitModule("_journal", methods);
- if (!m)
- return;
-
- PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
-}
-REENABLE_WARNING;
-
-#else
-
static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT,
.m_name = "_journal", /* name of module */
@@ -152,5 +136,3 @@ PyMODINIT_FUNC PyInit__journal(void) {
return m;
}
REENABLE_WARNING;
-
-#endif
diff --git a/systemd/_reader.c b/systemd/_reader.c
index 4e8f47f..333eb9a 100644
--- a/systemd/_reader.c
+++ b/systemd/_reader.c
@@ -71,7 +71,6 @@ PyDoc_STRVAR(module__doc__,
"Class to reads the systemd journal similar to journalctl.");
-#if PY_MAJOR_VERSION >= 3
static PyTypeObject MonotonicType;
PyDoc_STRVAR(MonotonicType__doc__,
@@ -89,14 +88,12 @@ static PyStructSequence_Desc Monotonic_desc = {
MonotonicType_fields,
2,
};
-#endif
/**
* Convert a str or bytes object into a C-string path.
* Returns NULL on error.
*/
static char* str_converter(PyObject *str, PyObject **bytes) {
-#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
int r;
r = PyUnicode_FSConverter(str, bytes);
@@ -104,9 +101,6 @@ static char* str_converter(PyObject *str, PyObject **bytes) {
return NULL;
return PyBytes_AsString(*bytes);
-#else
- return PyString_AsString(str);
-#endif
}
/**
@@ -706,11 +700,7 @@ static PyObject* Reader_get_monotonic(Reader *self, PyObject *args) {
assert_cc(sizeof(unsigned long long) == sizeof(timestamp));
monotonic = PyLong_FromUnsignedLongLong(timestamp);
bootid = PyBytes_FromStringAndSize((const char*) &id.bytes, sizeof(id.bytes));
-#if PY_MAJOR_VERSION >= 3
tuple = PyStructSequence_New(&MonotonicType);
-#else
- tuple = PyTuple_New(2);
-#endif
if (!monotonic || !bootid || !tuple) {
Py_XDECREF(monotonic);
Py_XDECREF(bootid);
@@ -718,13 +708,8 @@ static PyObject* Reader_get_monotonic(Reader *self, PyObject *args) {
return NULL;
}
-#if PY_MAJOR_VERSION >= 3
PyStructSequence_SET_ITEM(tuple, 0, monotonic);
PyStructSequence_SET_ITEM(tuple, 1, bootid);
-#else
- PyTuple_SET_ITEM(tuple, 0, monotonic);
- PyTuple_SET_ITEM(tuple, 1, bootid);
-#endif
return tuple;
}
@@ -1353,7 +1338,6 @@ static PyMethodDef methods[] = {
{} /* Sentinel */
};
-#if PY_MAJOR_VERSION >= 3
static PyModuleDef module = {
PyModuleDef_HEAD_INIT,
.m_name = "_reader",
@@ -1361,33 +1345,20 @@ static PyModuleDef module = {
.m_size = -1,
.m_methods = methods,
};
-#endif
-
-#if PY_MAJOR_VERSION >= 3
static bool initialized = false;
-#endif
DISABLE_WARNING_MISSING_PROTOTYPES;
PyMODINIT_FUNC
-#if PY_MAJOR_VERSION >= 3
PyInit__reader(void)
-#else
-init_reader(void)
-#endif
{
PyObject* m;
PyDateTime_IMPORT;
if (PyType_Ready(&ReaderType) < 0)
-#if PY_MAJOR_VERSION >= 3
return NULL;
-#else
- return;
-#endif
-#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&module);
if (!m)
return NULL;
@@ -1396,20 +1367,11 @@ init_reader(void)
PyStructSequence_InitType(&MonotonicType, &Monotonic_desc);
initialized = true;
}
-#else
- m = Py_InitModule3("_reader", methods, module__doc__);
- if (!m)
- return;
-#endif
Py_INCREF(&ReaderType);
-#if PY_MAJOR_VERSION >= 3
Py_INCREF(&MonotonicType);
-#endif
if (PyModule_AddObject(m, "_Reader", (PyObject *) &ReaderType) ||
-#if PY_MAJOR_VERSION >= 3
PyModule_AddObject(m, "Monotonic", (PyObject*) &MonotonicType) ||
-#endif
PyModule_AddIntConstant(m, "NOP", SD_JOURNAL_NOP) ||
PyModule_AddIntConstant(m, "APPEND", SD_JOURNAL_APPEND) ||
PyModule_AddIntConstant(m, "INVALIDATE", SD_JOURNAL_INVALIDATE) ||
@@ -1420,15 +1382,11 @@ init_reader(void)
PyModule_AddIntConstant(m, "CURRENT_USER", SD_JOURNAL_CURRENT_USER) ||
PyModule_AddIntConstant(m, "OS_ROOT", SD_JOURNAL_OS_ROOT) ||
PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
-#if PY_MAJOR_VERSION >= 3
Py_DECREF(m);
return NULL;
-#endif
}
-#if PY_MAJOR_VERSION >= 3
return m;
-#endif
}
REENABLE_WARNING;
diff --git a/systemd/id128.c b/systemd/id128.c
index 891c658..a887d43 100644
--- a/systemd/id128.c
+++ b/systemd/id128.c
@@ -161,26 +161,6 @@ static int add_id(PyObject *module, const char* name, sd_id128_t id) {
return PyModule_AddObject(module, name, obj);
}
-#if PY_MAJOR_VERSION < 3
-
-DISABLE_WARNING_MISSING_PROTOTYPES;
-PyMODINIT_FUNC initid128(void) {
- PyObject *m;
-
- m = Py_InitModule3("id128", methods, module__doc__);
- if (!m)
- return;
-
- /* a series of lines like 'add_id() ;' follow */
-#define JOINER ;
-#include "id128-constants.h"
-#undef JOINER
- PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
-}
-REENABLE_WARNING;
-
-#else
-
static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT,
.m_name = "id128", /* name of module */
@@ -209,5 +189,3 @@ PyMODINIT_FUNC PyInit_id128(void) {
return m;
}
REENABLE_WARNING;
-
-#endif
diff --git a/systemd/journal.py b/systemd/journal.py
index 9d33fa8..0de5a7f 100644
--- a/systemd/journal.py
+++ b/systemd/journal.py
@@ -34,14 +34,9 @@ from ._reader import (_Reader, NOP, APPEND, INVALIDATE,
LOCAL_ONLY, RUNTIME_ONLY,
SYSTEM, SYSTEM_ONLY, CURRENT_USER,
OS_ROOT,
- _get_catalog)
+ _get_catalog, Monotonic)
from . import id128 as _id128
-if _sys.version_info >= (3,):
- from ._reader import Monotonic
-else:
- Monotonic = tuple
-
def _convert_monotonic(m):
return Monotonic((_datetime.timedelta(microseconds=m[0]),
@@ -66,11 +61,10 @@ def _convert_timestamp(s):
def _convert_trivial(x):
return x
-if _sys.version_info >= (3,):
- def _convert_uuid(s):
- return _uuid.UUID(s.decode())
-else:
- _convert_uuid = _uuid.UUID
+
+def _convert_uuid(s):
+ return _uuid.UUID(s.decode())
+
DEFAULT_CONVERTERS = {
'MESSAGE_ID': _convert_uuid,
@@ -222,9 +216,6 @@ class Reader(_Reader):
else:
raise StopIteration()
- if _sys.version_info < (3,):
- next = __next__
-
def add_match(self, *args, **kwargs):
"""Add one or more matches to the filter journal log entries.
diff --git a/systemd/login.c b/systemd/login.c
index b029229..5a5bfb9 100644
--- a/systemd/login.c
+++ b/systemd/login.c
@@ -313,28 +313,6 @@ static PyTypeObject MonitorType = {
.tp_new = PyType_GenericNew,
};
-#if PY_MAJOR_VERSION < 3
-
-DISABLE_WARNING_MISSING_PROTOTYPES;
-PyMODINIT_FUNC initlogin(void) {
- PyObject *m;
-
- if (PyType_Ready(&MonitorType) < 0)
- return;
-
- m = Py_InitModule3("login", methods, module__doc__);
- if (!m)
- return;
-
- PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
-
- Py_INCREF(&MonitorType);
- PyModule_AddObject(m, "Monitor", (PyObject *) &MonitorType);
-}
-REENABLE_WARNING;
-
-#else
-
static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT,
"login", /* name of module */
@@ -369,5 +347,3 @@ PyMODINIT_FUNC PyInit_login(void) {
return m;
}
REENABLE_WARNING;
-
-#endif
diff --git a/systemd/pyutil.c b/systemd/pyutil.c
index cd91ec3..3526a25 100644
--- a/systemd/pyutil.c
+++ b/systemd/pyutil.c
@@ -58,7 +58,6 @@ int set_error(int r, const char* path, const char* invalid_message) {
return -1;
}
-#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
int Unicode_FSConverter(PyObject* obj, void *_result) {
PyObject **result = _result;
@@ -76,4 +75,3 @@ int Unicode_FSConverter(PyObject* obj, void *_result) {
return PyUnicode_FSConverter(obj, result);
}
-#endif
diff --git a/systemd/pyutil.h b/systemd/pyutil.h
index f840e87..e41794d 100644
--- a/systemd/pyutil.h
+++ b/systemd/pyutil.h
@@ -29,25 +29,13 @@ void cleanup_Py_DECREFp(PyObject **p);
PyObject* absolute_timeout(uint64_t t);
int set_error(int r, const char* path, const char* invalid_message);
-#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
int Unicode_FSConverter(PyObject* obj, void *_result);
-#endif
#define _cleanup_Py_DECREF_ _cleanup_(cleanup_Py_DECREFp)
-#if PY_MAJOR_VERSION >=3
# define unicode_FromStringAndSize PyUnicode_FromStringAndSize
# define unicode_FromString PyUnicode_FromString
# define long_FromLong PyLong_FromLong
# define long_FromSize_t PyLong_FromSize_t
# define long_Check PyLong_Check
# define long_AsLong PyLong_AsLong
-#else
-/* Python 3 type naming convention is used */
-# define unicode_FromStringAndSize PyString_FromStringAndSize
-# define unicode_FromString PyString_FromString
-# define long_FromLong PyInt_FromLong
-# define long_FromSize_t PyInt_FromSize_t
-# define long_Check PyInt_Check
-# define long_AsLong PyInt_AsLong
-#endif
diff --git a/systemd/test/test_daemon.py b/systemd/test/test_daemon.py
index d2eb10f..f6ae07e 100644
--- a/systemd/test/test_daemon.py
+++ b/systemd/test/test_daemon.py
@@ -306,24 +306,20 @@ def test_notify_no_socket():
assert notify('FDSTORE=1', pid=os.getpid()) is False
assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) is False
-if sys.version_info >= (3,):
- connection_error = ConnectionRefusedError
-else:
- connection_error = OSError
def test_notify_bad_socket():
os.environ['NOTIFY_SOCKET'] = '/dev/null'
- with pytest.raises(connection_error):
+ with pytest.raises(ConnectionRefusedError):
notify('READY=1')
- with pytest.raises(connection_error):
+ with pytest.raises(ConnectionRefusedError):
with skip_enosys():
notify('FDSTORE=1', fds=[])
- with pytest.raises(connection_error):
+ with pytest.raises(ConnectionRefusedError):
notify('FDSTORE=1', fds=[1, 2])
- with pytest.raises(connection_error):
+ with pytest.raises(ConnectionRefusedError):
notify('FDSTORE=1', pid=os.getpid())
- with pytest.raises(connection_error):
+ with pytest.raises(ConnectionRefusedError):
notify('FDSTORE=1', pid=os.getpid(), fds=(1,))
def test_notify_with_socket(tmpdir):