Skip to content

Commit 5dbbba4

Browse files
committed
make VectorError picklable
1 parent 136d6b7 commit 5dbbba4

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

can/interfaces/vector/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ class VectorError(CanError):
88
def __init__(self, error_code, error_string, function):
99
self.error_code = error_code
1010
super().__init__(f"{function} failed ({error_string})")
11+
12+
# keep reference to args for pickling
13+
self._args = error_code, error_string, function
14+
15+
def __reduce__(self):
16+
return VectorError, self._args, {}

test/test_vector.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import ctypes
9+
import pickle
910
import time
1011
import logging
1112
import os
@@ -15,7 +16,7 @@
1516
import pytest
1617

1718
import can
18-
from can.interfaces.vector import canlib, xldefine, xlclass
19+
from can.interfaces.vector import canlib, xldefine, xlclass, VectorError
1920

2021

2122
class TestVectorBus(unittest.TestCase):
@@ -255,6 +256,23 @@ def test_called_without_testing_argument(self) -> None:
255256
# do not set the _testing argument, since it supresses the exception
256257
can.Bus(channel=0, bustype="vector")
257258

259+
def test_vector_error_pickle(self) -> None:
260+
error_code = 118
261+
error_string = "XL_ERROR"
262+
function = "function_name"
263+
264+
exc = VectorError(error_code, error_string, function)
265+
266+
# pickle and unpickle
267+
p = pickle.dumps(exc)
268+
exc_unpickled: VectorError = pickle.loads(p)
269+
270+
self.assertEqual(str(exc), str(exc_unpickled))
271+
self.assertEqual(error_code, exc_unpickled.error_code)
272+
273+
with pytest.raises(VectorError):
274+
raise exc_unpickled
275+
258276

259277
def xlGetApplConfig(
260278
app_name_p: ctypes.c_char_p,

0 commit comments

Comments
 (0)