Skip to content

Commit 872ce30

Browse files
committed
journal: fix compatibility with python2
This is a lazy workaround: 4c9a241 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. f868a56 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.
1 parent 6320847 commit 872ce30

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

systemd/journal.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ def _convert_monotonic(m):
5151
def _convert_source_monotonic(s):
5252
return _datetime.timedelta(microseconds=int(s))
5353

54-
_LOCAL_TIMEZONE = _datetime.datetime.now().astimezone().tzinfo
54+
try:
55+
_LOCAL_TIMEZONE = _datetime.datetime.now().astimezone().tzinfo
56+
except TypeError:
57+
_LOCAL_TIMEZONE = None
5558

5659
def _convert_realtime(t):
5760
return _datetime.datetime.fromtimestamp(t / 1000000, _LOCAL_TIMEZONE)
@@ -313,7 +316,13 @@ def seek_realtime(self, realtime):
313316
>>> j.seek_realtime(yesterday)
314317
"""
315318
if isinstance(realtime, _datetime.datetime):
316-
realtime = int(float(realtime.astimezone().strftime("%s.%f")) * 1000000)
319+
try:
320+
realtime = realtime.astimezone()
321+
except TypeError:
322+
# With python2: Required argument 'tz' (pos 1) not found
323+
pass
324+
325+
realtime = int(float(realtime.strftime("%s.%f")) * 1000000)
317326
elif not isinstance(realtime, int):
318327
realtime = int(realtime * 1000000)
319328
return super(Reader, self).seek_realtime(realtime)

systemd/test/test_journal.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import time
88
import uuid
9+
import sys
910
import traceback as _traceback
1011

1112
from systemd import journal, id128
@@ -294,13 +295,16 @@ def test_reader_convert_timestamps(tmpdir):
294295
j = journal.Reader(path=tmpdir.strpath)
295296

296297
val = j._convert_field('_SOURCE_REALTIME_TIMESTAMP', 1641651559324187)
297-
assert val.tzinfo is not None
298+
if sys.version_info >= (3,):
299+
assert val.tzinfo is not None
298300

299301
val = j._convert_field('__REALTIME_TIMESTAMP', 1641651559324187)
300-
assert val.tzinfo is not None
302+
if sys.version_info >= (3,):
303+
assert val.tzinfo is not None
301304

302305
val = j._convert_field('COREDUMP_TIMESTAMP', 1641651559324187)
303-
assert val.tzinfo is not None
306+
if sys.version_info >= (3,):
307+
assert val.tzinfo is not None
304308

305309
def test_seek_realtime(tmpdir):
306310
j = journal.Reader(path=tmpdir.strpath)

0 commit comments

Comments
 (0)