Skip to content

Commit

Permalink
Merge pull request #117 from keszybz/python2-compat
Browse files Browse the repository at this point in the history
Compat with python2
  • Loading branch information
keszybz committed Aug 16, 2022
2 parents 6320847 + c5f8b88 commit 8360d1d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
8 changes: 4 additions & 4 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
13 changes: 11 additions & 2 deletions systemd/journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion systemd/test/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
14 changes: 9 additions & 5 deletions systemd/test/test_journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import os
import time
import uuid
import traceback as _traceback
import sys
import traceback

from systemd import journal, id128
from systemd.journal import _make_line
Expand All @@ -30,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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8360d1d

Please sign in to comment.