Skip to content

Commit

Permalink
Improved tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
vsajip authored and methane committed Oct 16, 2019
1 parent d83fc65 commit a3cf72d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Lib/ctypes/test/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ class Test5(Structure):
_fields_ = [
('an_int', c_int),
('nested', Nested2),
('another_int', c_int),
]

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

# passing by reference should be OK
test4.a_long = 12345;
func = dll._testfunc_union_by_reference1
func.restype = c_long
func.argtypes = (POINTER(Test4),)
result = func(byref(test4))
self.assertEqual(result, 12345)
self.assertEqual(test4.a_long, 0)
self.assertEqual(test4.a_struct.an_int, 0)
self.assertEqual(test4.a_struct.another_int, 0)
test4.a_struct.an_int = 0x12340000
test4.a_struct.another_int = 0x5678
func = dll._testfunc_union_by_reference2
func.restype = c_long
func.argtypes = (POINTER(Test4),)
result = func(byref(test4))
self.assertEqual(result, 0x12345678)
self.assertEqual(test4.a_long, 0)
self.assertEqual(test4.a_struct.an_int, 0)
self.assertEqual(test4.a_struct.another_int, 0)
test5.an_int = 0x12000000
test5.nested.an_int = 0x345600
test5.another_int = 0x78
func = dll._testfunc_union_by_reference3
func.restype = c_long
func.argtypes = (POINTER(Test5),)
result = func(byref(test5))
self.assertEqual(result, 0x12345678)
self.assertEqual(test5.an_int, 0)
self.assertEqual(test5.nested.an_int, 0)
self.assertEqual(test5.another_int, 0)

class PointerMemberTestCase(unittest.TestCase):

def test(self):
Expand Down
25 changes: 25 additions & 0 deletions Modules/_ctypes/_ctypes_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ typedef struct {
int an_int;
Test4 a_union;
} nested;
int another_int;
} Test5;

EXPORT(long)
Expand All @@ -169,6 +170,30 @@ _testfunc_union_by_value2(Test5 in) {
return result;
}

EXPORT(long)
_testfunc_union_by_reference1(Test4 *in) {
long result = in->a_long;

memset(in, 0, sizeof(Test4));
return result;
}

EXPORT(long)
_testfunc_union_by_reference2(Test4 *in) {
long result = in->a_struct.an_int + in->a_struct.another_int;

memset(in, 0, sizeof(Test4));
return result;
}

EXPORT(long)
_testfunc_union_by_reference3(Test5 *in) {
long result = in->an_int + in->nested.an_int + in->another_int;

memset(in, 0, sizeof(Test5));
return result;
}

EXPORT(void)testfunc_array(int values[4])
{
printf("testfunc_array %d %d %d %d\n",
Expand Down

0 comments on commit a3cf72d

Please sign in to comment.