Skip to content

Commit 3e557f0

Browse files
committed
allow SDObject and Unit to be pickled
1 parent e9d1f29 commit 3e557f0

5 files changed

Lines changed: 46 additions & 1 deletion

File tree

pystemd/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ def __init__(self, destination, path, bus=None, _autoload=False):
2727
if _autoload:
2828
self.load()
2929

30+
def __getstate__(self):
31+
return {
32+
"destination": self.destination,
33+
"path": self.path,
34+
"bus": self._bus,
35+
"_autoload": self._loaded,
36+
}
37+
38+
def __setstate__(self, state):
39+
self.__init__(**state)
40+
3041
def __enter__(self):
3142
self.load()
3243
return self
@@ -112,6 +123,7 @@ def load(self, force=False):
112123
)
113124
elif interface_name == "org.freedesktop.DBus.Properties":
114125
self.Properties = self._interfaces[interface_name]
126+
self._loaded = True
115127

116128

117129
class SDInterface(object):

pystemd/machine1/machine.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ def __init__(self, external_id, bus=None, _autoload=False):
2222
bus=bus,
2323
_autoload=_autoload,
2424
)
25+
26+
def __getstate__(self):
27+
return {
28+
"external_id": self.external_id,
29+
"bus": self._bus,
30+
"_autoload": self._loaded,
31+
}

pystemd/systemd1/unit.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ def __init__(self, external_id, bus=None, _autoload=False):
2121
bus=bus,
2222
_autoload=_autoload,
2323
)
24+
25+
def __getstate__(self):
26+
return {
27+
"external_id": self.external_id,
28+
"bus": self._bus,
29+
"_autoload": self._loaded,
30+
}

tests/test_pickle.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pickle
2+
3+
from pystemd.systemd1.unit import Unit
4+
5+
6+
def test_unloaded_unit():
7+
unit = Unit("foo.service")
8+
sour_unit = pickle.loads(pickle.dumps(unit))
9+
assert unit.external_id == sour_unit.external_id
10+
assert not sour_unit._loaded
11+
12+
13+
def test_loaded_unit():
14+
unit = Unit("foo.service")
15+
unit.load()
16+
sour_unit = pickle.loads(pickle.dumps(unit))
17+
assert unit.external_id == sour_unit.external_id
18+
assert sour_unit._loaded
19+
assert sour_unit._interfaces

tests/test_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pathlib import Path
2-
import toml
32

3+
import toml
44
from cstq import Query
55

66

0 commit comments

Comments
 (0)