[3.7] bpo-22273: Update ctypes to correctly handle arrays in small structur… (GH-15839) by miss-islington · Pull Request #16369 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Lib/ctypes/test/test_structures.py
39 changes: 39 additions & 0 deletions Modules/_ctypes/_ctypes_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,45 @@ _testfunc_reg_struct_update_value(TestReg in)
((volatile TestReg *)&in)->second = 0x0badf00d;
}

/*
* See bpo-22273. Structs containing arrays should work on Linux 64-bit.
*/

typedef struct {
unsigned char data[16];
} Test2;

EXPORT(int)
_testfunc_array_in_struct1(Test2 in)
{
int result = 0;

for (unsigned i = 0; i < 16; i++)
result += in.data[i];
/* As the structure is passed by value, changes to it shouldn't be
* reflected in the caller.
*/
memset(in.data, 0, sizeof(in.data));
return result;
}

typedef struct {
double data[2];
} Test3;

EXPORT(double)
_testfunc_array_in_struct2(Test3 in)
{
double result = 0;

for (unsigned i = 0; i < 2; i++)
result += in.data[i];
/* As the structure is passed by value, changes to it shouldn't be
* reflected in the caller.
*/
memset(in.data, 0, sizeof(in.data));
return result;
}

EXPORT(void)testfunc_array(int values[4])
{
Expand Down
107 changes: 107 additions & 0 deletions Modules/_ctypes/stgdict.c