Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pystemd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ def __init__(self, destination, path, bus=None, _autoload=False):
if _autoload:
self.load()

def __getstate__(self):
return {
"destination": self.destination,
"path": self.path,
"bus": self._bus,
"_autoload": self._loaded,
}

def __setstate__(self, state):
self.__init__(**state)

def __enter__(self):
self.load()
return self
Expand Down Expand Up @@ -112,6 +123,7 @@ def load(self, force=False):
)
elif interface_name == "org.freedesktop.DBus.Properties":
self.Properties = self._interfaces[interface_name]
self._loaded = True


class SDInterface(object):
Expand Down
7 changes: 7 additions & 0 deletions pystemd/machine1/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ def __init__(self, external_id, bus=None, _autoload=False):
bus=bus,
_autoload=_autoload,
)

def __getstate__(self):
return {
"external_id": self.external_id,
"bus": self._bus,
"_autoload": self._loaded,
}
7 changes: 7 additions & 0 deletions pystemd/systemd1/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ def __init__(self, external_id, bus=None, _autoload=False):
bus=bus,
_autoload=_autoload,
)

def __getstate__(self):
return {
"external_id": self.external_id,
"bus": self._bus,
"_autoload": self._loaded,
}
19 changes: 19 additions & 0 deletions tests/test_pickle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pickle

from pystemd.systemd1.unit import Unit


def test_unloaded_unit():
unit = Unit("foo.service")
sour_unit = pickle.loads(pickle.dumps(unit))
assert unit.external_id == sour_unit.external_id
assert not sour_unit._loaded


def test_loaded_unit():
unit = Unit("foo.service")
unit.load()
sour_unit = pickle.loads(pickle.dumps(unit))
assert unit.external_id == sour_unit.external_id
assert sour_unit._loaded
assert sour_unit._interfaces
2 changes: 1 addition & 1 deletion tests/test_version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pathlib import Path
import toml

import toml
from cstq import Query


Expand Down