1 | n/a | import unittest |
---|
2 | n/a | import pickle |
---|
3 | n/a | from ctypes import * |
---|
4 | n/a | import _ctypes_test |
---|
5 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
6 | n/a | |
---|
7 | n/a | class X(Structure): |
---|
8 | n/a | _fields_ = [("a", c_int), ("b", c_double)] |
---|
9 | n/a | init_called = 0 |
---|
10 | n/a | def __init__(self, *args, **kw): |
---|
11 | n/a | X.init_called += 1 |
---|
12 | n/a | self.x = 42 |
---|
13 | n/a | |
---|
14 | n/a | class Y(X): |
---|
15 | n/a | _fields_ = [("str", c_char_p)] |
---|
16 | n/a | |
---|
17 | n/a | class PickleTest: |
---|
18 | n/a | def dumps(self, item): |
---|
19 | n/a | return pickle.dumps(item, self.proto) |
---|
20 | n/a | |
---|
21 | n/a | def loads(self, item): |
---|
22 | n/a | return pickle.loads(item) |
---|
23 | n/a | |
---|
24 | n/a | def test_simple(self): |
---|
25 | n/a | for src in [ |
---|
26 | n/a | c_int(42), |
---|
27 | n/a | c_double(3.14), |
---|
28 | n/a | ]: |
---|
29 | n/a | dst = self.loads(self.dumps(src)) |
---|
30 | n/a | self.assertEqual(src.__dict__, dst.__dict__) |
---|
31 | n/a | self.assertEqual(memoryview(src).tobytes(), |
---|
32 | n/a | memoryview(dst).tobytes()) |
---|
33 | n/a | |
---|
34 | n/a | def test_struct(self): |
---|
35 | n/a | X.init_called = 0 |
---|
36 | n/a | |
---|
37 | n/a | x = X() |
---|
38 | n/a | x.a = 42 |
---|
39 | n/a | self.assertEqual(X.init_called, 1) |
---|
40 | n/a | |
---|
41 | n/a | y = self.loads(self.dumps(x)) |
---|
42 | n/a | |
---|
43 | n/a | # loads must NOT call __init__ |
---|
44 | n/a | self.assertEqual(X.init_called, 1) |
---|
45 | n/a | |
---|
46 | n/a | # ctypes instances are identical when the instance __dict__ |
---|
47 | n/a | # and the memory buffer are identical |
---|
48 | n/a | self.assertEqual(y.__dict__, x.__dict__) |
---|
49 | n/a | self.assertEqual(memoryview(y).tobytes(), |
---|
50 | n/a | memoryview(x).tobytes()) |
---|
51 | n/a | |
---|
52 | n/a | def test_unpickable(self): |
---|
53 | n/a | # ctypes objects that are pointers or contain pointers are |
---|
54 | n/a | # unpickable. |
---|
55 | n/a | self.assertRaises(ValueError, lambda: self.dumps(Y())) |
---|
56 | n/a | |
---|
57 | n/a | prototype = CFUNCTYPE(c_int) |
---|
58 | n/a | |
---|
59 | n/a | for item in [ |
---|
60 | n/a | c_char_p(), |
---|
61 | n/a | c_wchar_p(), |
---|
62 | n/a | c_void_p(), |
---|
63 | n/a | pointer(c_int(42)), |
---|
64 | n/a | dll._testfunc_p_p, |
---|
65 | n/a | prototype(lambda: 42), |
---|
66 | n/a | ]: |
---|
67 | n/a | self.assertRaises(ValueError, lambda: self.dumps(item)) |
---|
68 | n/a | |
---|
69 | n/a | def test_wchar(self): |
---|
70 | n/a | self.dumps(c_char(b"x")) |
---|
71 | n/a | # Issue 5049 |
---|
72 | n/a | self.dumps(c_wchar("x")) |
---|
73 | n/a | |
---|
74 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
75 | n/a | name = 'PickleTest_%s' % proto |
---|
76 | n/a | globals()[name] = type(name, |
---|
77 | n/a | (PickleTest, unittest.TestCase), |
---|
78 | n/a | {'proto': proto}) |
---|
79 | n/a | |
---|
80 | n/a | if __name__ == "__main__": |
---|
81 | n/a | unittest.main() |
---|