Skip to content

Commit 2f8705d

Browse files
committed
test_set_type_updates_format
1 parent 1391ee6 commit 2f8705d

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Lib/test/test_ctypes/test_incomplete.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ctypes
22
import unittest
3-
from ctypes import Structure, POINTER, pointer, c_char_p
3+
from ctypes import Structure, POINTER, pointer, c_char_p, c_int
44

55
# String-based "incomplete pointers" were implemented in ctypes 0.6.3 (2003, when
66
# ctypes was an external project). They made obsolete by the current
@@ -50,6 +50,29 @@ class cell(Structure):
5050
lpcell.set_type(cell)
5151
self.assertIs(POINTER(cell), lpcell)
5252

53+
def test_set_type_updates_format(self):
54+
# gh-142966: set_type should update StgInfo.format
55+
# to match the element type's format
56+
with self.assertWarns(DeprecationWarning):
57+
lp = POINTER("node")
58+
59+
class node(Structure):
60+
_fields_ = [("value", c_int)]
61+
62+
# Get the expected format before set_type
63+
node_format = memoryview(node()).format
64+
expected_format = "&" + node_format
65+
66+
lp.set_type(node)
67+
68+
# Create instance to check format via memoryview
69+
n = node(42)
70+
p = lp(n)
71+
actual_format = memoryview(p).format
72+
73+
# After set_type, the pointer's format should be "&<element_format>"
74+
self.assertEqual(actual_format, expected_format)
75+
5376

5477
if __name__ == '__main__':
5578
unittest.main()

0 commit comments

Comments
 (0)