-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtest_incomplete.py
More file actions
59 lines (46 loc) · 1.82 KB
/
test_incomplete.py
File metadata and controls
59 lines (46 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import ctypes
import unittest
import warnings
from ctypes import Structure, POINTER, pointer, c_char_p
# String-based "incomplete pointers" were implemented in ctypes 0.6.3 (2003, when
# ctypes was an external project). They made obsolete by the current
# incomplete *types* (setting `_fields_` late) in 0.9.5 (2005).
# ctypes was added to Python 2.5 (2006), without any mention in docs.
# This tests incomplete pointer example from the old tutorial
# (https://svn.python.org/projects/ctypes/tags/release_0_6_3/ctypes/docs/tutorial.stx)
class TestSetPointerType(unittest.TestCase):
def tearDown(self):
ctypes._pointer_type_cache_fallback.clear()
def test_incomplete_example(self):
with self.assertWarns(DeprecationWarning):
lpcell = POINTER("cell")
class cell(Structure):
_fields_ = [("name", c_char_p),
("next", lpcell)]
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
ctypes.SetPointerType(lpcell, cell)
self.assertIs(POINTER(cell), lpcell)
c1 = cell()
c1.name = b"foo"
c2 = cell()
c2.name = b"bar"
c1.next = pointer(c2)
c2.next = pointer(c1)
p = c1
result = []
for i in range(8):
result.append(p.name)
p = p.next[0]
self.assertEqual(result, [b"foo", b"bar"] * 4)
def test_deprecation(self):
with self.assertWarns(DeprecationWarning):
lpcell = POINTER("cell")
class cell(Structure):
_fields_ = [("name", c_char_p),
("next", lpcell)]
with self.assertWarns(DeprecationWarning):
ctypes.SetPointerType(lpcell, cell)
self.assertIs(POINTER(cell), lpcell)
if __name__ == '__main__':
unittest.main()