summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2022-08-16 12:20:44 +0200
committerGitHub <noreply@github.com>2022-08-16 12:20:44 +0200
commit8360d1d01cf0e0064238c539d2fb4e47a29c65f2 (patch)
tree2a76c84ec4c8a7fa17e94330105fd791f5d8d051
parent6320847472657fc2b4f98dd5bfeb4c826ed68a06 (diff)
parentc5f8b887bef5c11908913168ad566aa1bc07166e (diff)
downloadpython-systemd-8360d1d01cf0e0064238c539d2fb4e47a29c65f2.tar.gz
Merge pull request #117 from keszybz/python2-compat
Compat with python2
-rw-r--r--NEWS8
-rw-r--r--README.md9
-rw-r--r--systemd/journal.py13
-rw-r--r--systemd/test/test_daemon.py2
-rw-r--r--systemd/test/test_journal.py14
5 files changed, 29 insertions, 17 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:
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_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'
diff --git a/systemd/test/test_journal.py b/systemd/test/test_journal.py
index e6761ca..c192136 100644
--- a/systemd/test/test_journal.py
+++ b/systemd/test/test_journal.py
@@ -6,7 +6,8 @@ import logging
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
@@ -30,7 +31,7 @@ class MockSender:
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:
@@ -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)