Skip to content

Commit 8360d1d

Browse files
authored
Merge pull request #117 from keszybz/python2-compat
Compat with python2
2 parents 6320847 + c5f8b88 commit 8360d1d

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

NEWS

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ CHANGES WITH 235:
1212

1313
* id128: update for systemd-243 compatibility and other fixes.
1414

15-
* C syntax modernization. A minimum of C99 is assumed.
15+
* C syntax modernization. A minimum of C99 is assumed.
1616

17-
* Fix seek_realtime to work with timezone aware date.
17+
* Fix seek_realtime to work with timezone aware date on Python 3.
1818

1919
* journal: add namespace support.
2020

21-
* Memory leak fixes.
21+
* Fixes for memory leaks and documentation.
2222

23-
* Documentation and other fixes.
23+
* Support for Python 2 will be removed after this release.
2424

2525
CHANGES WITH 234:
2626

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,11 @@ Notes
160160
-----
161161

162162
* Unlike the native C version of journald's `sd_journal_send()`,
163-
printf-style substitution is not supported. Perform any
164-
substitution using Python's f-strings first (or .format()
165-
capabilities or `%` operator).
163+
printf-style substitution is not supported. Perform any substitution
164+
using Python's f-strings first (or `.format()` or the `%` operator).
166165
* A `ValueError` is raised if `sd_journald_sendv()` results in an
167-
error. This might happen if there are no arguments or one of them
168-
is invalid.
166+
error. This might happen if there are no arguments or one of them is
167+
invalid.
169168

170169
A handler class for the Python logging framework is also provided:
171170

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_daemon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def test_daemon_notify_memleak():
353353

354354
try:
355355
notify('', True, 0, fds)
356-
except ConnectionRefusedError:
356+
except connection_error:
357357
pass
358358

359359
assert sys.getrefcount(fd) <= ref_cnt, 'leak'

systemd/test/test_journal.py

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

1112
from systemd import journal, id128
1213
from systemd.journal import _make_line
@@ -30,7 +31,7 @@ def send(self, MESSAGE, MESSAGE_ID=None,
3031
args.append('MESSAGE_ID=' + id)
3132

3233
if CODE_LINE is CODE_FILE is CODE_FUNC is None:
33-
CODE_FILE, CODE_LINE, CODE_FUNC = _traceback.extract_stack(limit=2)[0][:3]
34+
CODE_FILE, CODE_LINE, CODE_FUNC = traceback.extract_stack(limit=2)[0][:3]
3435
if CODE_FILE is not None:
3536
args.append('CODE_FILE=' + CODE_FILE)
3637
if CODE_LINE is not None:
@@ -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)