Skip to content

Commit a3cf72d

Browse files
vsajipmethane
authored andcommitted
Improved tests.
1 parent d83fc65 commit a3cf72d

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Lib/ctypes/test/test_structures.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ class Test5(Structure):
603603
_fields_ = [
604604
('an_int', c_int),
605605
('nested', Nested2),
606+
('another_int', c_int),
606607
]
607608

608609
test4 = Test4()
@@ -623,6 +624,38 @@ class Test5(Structure):
623624
self.assertEqual(ctx.exception.args[0], 'item 1 in _argtypes_ passes '
624625
'a union by value, which is unsupported.')
625626

627+
# passing by reference should be OK
628+
test4.a_long = 12345;
629+
func = dll._testfunc_union_by_reference1
630+
func.restype = c_long
631+
func.argtypes = (POINTER(Test4),)
632+
result = func(byref(test4))
633+
self.assertEqual(result, 12345)
634+
self.assertEqual(test4.a_long, 0)
635+
self.assertEqual(test4.a_struct.an_int, 0)
636+
self.assertEqual(test4.a_struct.another_int, 0)
637+
test4.a_struct.an_int = 0x12340000
638+
test4.a_struct.another_int = 0x5678
639+
func = dll._testfunc_union_by_reference2
640+
func.restype = c_long
641+
func.argtypes = (POINTER(Test4),)
642+
result = func(byref(test4))
643+
self.assertEqual(result, 0x12345678)
644+
self.assertEqual(test4.a_long, 0)
645+
self.assertEqual(test4.a_struct.an_int, 0)
646+
self.assertEqual(test4.a_struct.another_int, 0)
647+
test5.an_int = 0x12000000
648+
test5.nested.an_int = 0x345600
649+
test5.another_int = 0x78
650+
func = dll._testfunc_union_by_reference3
651+
func.restype = c_long
652+
func.argtypes = (POINTER(Test5),)
653+
result = func(byref(test5))
654+
self.assertEqual(result, 0x12345678)
655+
self.assertEqual(test5.an_int, 0)
656+
self.assertEqual(test5.nested.an_int, 0)
657+
self.assertEqual(test5.another_int, 0)
658+
626659
class PointerMemberTestCase(unittest.TestCase):
627660

628661
def test(self):

Modules/_ctypes/_ctypes_test.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ typedef struct {
145145
int an_int;
146146
Test4 a_union;
147147
} nested;
148+
int another_int;
148149
} Test5;
149150

150151
EXPORT(long)
@@ -169,6 +170,30 @@ _testfunc_union_by_value2(Test5 in) {
169170
return result;
170171
}
171172

173+
EXPORT(long)
174+
_testfunc_union_by_reference1(Test4 *in) {
175+
long result = in->a_long;
176+
177+
memset(in, 0, sizeof(Test4));
178+
return result;
179+
}
180+
181+
EXPORT(long)
182+
_testfunc_union_by_reference2(Test4 *in) {
183+
long result = in->a_struct.an_int + in->a_struct.another_int;
184+
185+
memset(in, 0, sizeof(Test4));
186+
return result;
187+
}
188+
189+
EXPORT(long)
190+
_testfunc_union_by_reference3(Test5 *in) {
191+
long result = in->an_int + in->nested.an_int + in->another_int;
192+
193+
memset(in, 0, sizeof(Test5));
194+
return result;
195+
}
196+
172197
EXPORT(void)testfunc_array(int values[4])
173198
{
174199
printf("testfunc_array %d %d %d %d\n",

0 commit comments

Comments
 (0)