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: 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 @@ class Reader(_Reader): >>> 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 logging 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) -- cgit v1.2.1