From 872ce304a07bb4c1b4fc06b7bfbd9220652c3793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 16 Aug 2022 10:34:10 +0200 Subject: [PATCH 1/4] journal: fix compatibility with python2 This is a lazy workaround: 4c9a241949067fc8d55f3ea12170ad364bd8b18d is amended to do nothing on python2, so we have the same issue that was present before. This allows the code to execute, and hopefully almost nobody is using python2 code anyway. f868a56b935b6152d611b22f7a5538f14dafb194 is amended in the same way. For python2 code we have the same lack of timezone-awareness as before. This allows the tests to pass under python 2.7. --- systemd/journal.py | 13 +++++++++++-- systemd/test/test_journal.py | 10 +++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/systemd/journal.py b/systemd/journal.py index b1f2fef..9d33fa8 100644 --- a/systemd/journal.py +++ b/systemd/journal.py @@ -51,7 +51,10 @@ def _convert_monotonic(m): def _convert_source_monotonic(s): return _datetime.timedelta(microseconds=int(s)) -_LOCAL_TIMEZONE = _datetime.datetime.now().astimezone().tzinfo +try: + _LOCAL_TIMEZONE = _datetime.datetime.now().astimezone().tzinfo +except TypeError: + _LOCAL_TIMEZONE = None def _convert_realtime(t): return _datetime.datetime.fromtimestamp(t / 1000000, _LOCAL_TIMEZONE) @@ -313,7 +316,13 @@ def seek_realtime(self, realtime): >>> j.seek_realtime(yesterday) """ if isinstance(realtime, _datetime.datetime): - realtime = int(float(realtime.astimezone().strftime("%s.%f")) * 1000000) + try: + realtime = realtime.astimezone() + except TypeError: + # With python2: Required argument 'tz' (pos 1) not found + pass + + realtime = int(float(realtime.strftime("%s.%f")) * 1000000) elif not isinstance(realtime, int): realtime = int(realtime * 1000000) return super(Reader, self).seek_realtime(realtime) diff --git a/systemd/test/test_journal.py b/systemd/test/test_journal.py index e6761ca..98c7e87 100644 --- a/systemd/test/test_journal.py +++ b/systemd/test/test_journal.py @@ -6,6 +6,7 @@ import os import time import uuid +import sys import traceback as _traceback from systemd import journal, id128 @@ -294,13 +295,16 @@ def test_reader_convert_timestamps(tmpdir): j = journal.Reader(path=tmpdir.strpath) val = j._convert_field('_SOURCE_REALTIME_TIMESTAMP', 1641651559324187) - assert val.tzinfo is not None + if sys.version_info >= (3,): + assert val.tzinfo is not None val = j._convert_field('__REALTIME_TIMESTAMP', 1641651559324187) - assert val.tzinfo is not None + if sys.version_info >= (3,): + assert val.tzinfo is not None val = j._convert_field('COREDUMP_TIMESTAMP', 1641651559324187) - assert val.tzinfo is not None + if sys.version_info >= (3,): + assert val.tzinfo is not None def test_seek_realtime(tmpdir): j = journal.Reader(path=tmpdir.strpath) From ba9d9f89104a26a410e1eb9e371d366ef2702c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 16 Aug 2022 10:34:25 +0200 Subject: [PATCH 2/4] tests: python2-compat in another place --- systemd/test/test_daemon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/systemd/test/test_daemon.py b/systemd/test/test_daemon.py index ac782f9..d2eb10f 100644 --- a/systemd/test/test_daemon.py +++ b/systemd/test/test_daemon.py @@ -353,7 +353,7 @@ def test_daemon_notify_memleak(): try: notify('', True, 0, fds) - except ConnectionRefusedError: + except connection_error: pass assert sys.getrefcount(fd) <= ref_cnt, 'leak' From cc03ed6d2013f40c5e20efa2af8d822a47c592b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 16 Aug 2022 10:35:10 +0200 Subject: [PATCH 3/4] tests: simplify imports 'import foo as _foo' is useful in exported modules to avoid 'foo' being present in the public API. No need to play that game in test code. --- systemd/test/test_journal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/systemd/test/test_journal.py b/systemd/test/test_journal.py index 98c7e87..c192136 100644 --- a/systemd/test/test_journal.py +++ b/systemd/test/test_journal.py @@ -7,7 +7,7 @@ import time import uuid import sys -import traceback as _traceback +import traceback from systemd import journal, id128 from systemd.journal import _make_line @@ -31,7 +31,7 @@ def send(self, MESSAGE, MESSAGE_ID=None, args.append('MESSAGE_ID=' + id) if CODE_LINE is CODE_FILE is CODE_FUNC is None: - CODE_FILE, CODE_LINE, CODE_FUNC = _traceback.extract_stack(limit=2)[0][:3] + CODE_FILE, CODE_LINE, CODE_FUNC = traceback.extract_stack(limit=2)[0][:3] if CODE_FILE is not None: args.append('CODE_FILE=' + CODE_FILE) if CODE_LINE is not None: From c5f8b887bef5c11908913168ad566aa1bc07166e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 16 Aug 2022 10:45:30 +0200 Subject: [PATCH 4/4] NEWS: say that we'll drop support for python2 soon I think it's nicer to make one last release with the remaining code for python2 in place. --- NEWS | 8 ++++---- README.md | 9 ++++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/NEWS b/NEWS index 17e3b82..74108c5 100644 --- a/NEWS +++ b/NEWS @@ -12,15 +12,15 @@ CHANGES WITH 235: * id128: update for systemd-243 compatibility and other fixes. - * C syntax modernization. A minimum of C99 is assumed. + * C syntax modernization. A minimum of C99 is assumed. - * Fix seek_realtime to work with timezone aware date. + * Fix seek_realtime to work with timezone aware date on Python 3. * journal: add namespace support. - * Memory leak fixes. + * Fixes for memory leaks and documentation. - * Documentation and other fixes. + * Support for Python 2 will be removed after this release. CHANGES WITH 234: diff --git a/README.md b/README.md index 8c5c5bf..0107f50 100644 --- a/README.md +++ b/README.md @@ -160,12 +160,11 @@ Notes ----- * Unlike the native C version of journald's `sd_journal_send()`, - printf-style substitution is not supported. Perform any - substitution using Python's f-strings first (or .format() - capabilities or `%` operator). + printf-style substitution is not supported. Perform any substitution + using Python's f-strings first (or `.format()` or the `%` operator). * A `ValueError` is raised if `sd_journald_sendv()` results in an - error. This might happen if there are no arguments or one of them - is invalid. + error. This might happen if there are no arguments or one of them is + invalid. A handler class for the Python logging framework is also provided: