This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ctypes on Windows: error calling C function that returns a struct containing 3 bools
Type: behavior Stage: resolved
Components: ctypes Versions: Python 3.7, Python 3.6, Python 3.4, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, belopolsky, mattneri, meador.inge, miss-islington, serhiy.storchaka, v2m, vstinner
Priority: normal Keywords: patch

Created on 2018-09-07 11:15 by mattneri, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
MarshallingTest.zip mattneri, 2018-09-07 11:15
Pull Requests
URL Status Linked Edit
PR 9258 merged v2m, 2018-09-13 15:43
PR 9340 merged miss-islington, 2018-09-16 05:36
PR 9341 merged miss-islington, 2018-09-16 05:36
PR 9342 closed miss-islington, 2018-09-16 05:37
PR 9425 merged v2m, 2018-09-19 18:48
PR 9721 merged serhiy.storchaka, 2018-10-05 17:52
PR 9725 merged serhiy.storchaka, 2018-10-05 18:16
PR 9727 merged miss-islington, 2018-10-05 18:46
Messages (11)
msg324732 - (view) Author: MatteoL (mattneri) Date: 2018-09-07 11:15
Marshalling the following C structure with ctypes in Windows (compiled with VS2015) is not working correctly.
As you can see, in the simple demo program I attached, the integer I pass as a parameter gets somehow corrupted. I use cdecl calling convention.

The corruption happens only if the returned structure contains 3 booleans, if I add a fourth bool to the structure than I get expected results.

In linux (compiling with Gcc) the same code (with 3 booleans) work as expected.


typedef uint64_t CustomHandle;

typedef struct
{
	bool first;
	bool second;
	bool third;
} Flags;

CustomHandle GetHandle();
Flags GetFlags(CustomHandle handle);


As already mention I attached all the files to reproduce the issue with the related cmake file
msg325207 - (view) Author: Vladimir Matveev (v2m) * Date: 2018-09-12 23:03
I think the problem is that FFI layer assumes that MSVC compiler will try to pass any structure less than 8 bytes in registers whereis it is not always true: 
To be returned by value in RAX, user-defined types must have a length of 1, 2, 4, 8, 16, 32, or 64 bits (from https://msdn.microsoft.com/en-us/library/7572ztz4.aspx).
I have a fix, now adding tests.
msg325471 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-09-16 05:36
New changeset 7843caeb909bd907e903606414e238db4082315a by Victor Stinner (Vladimir Matveev) in branch 'master':
bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258)
https://github.com/python/cpython/commit/7843caeb909bd907e903606414e238db4082315a
msg325472 - (view) Author: miss-islington (miss-islington) Date: 2018-09-16 05:53
New changeset e3f6aa7fe48b91f4ff619b2a51d473249d620bcb by Miss Islington (bot) in branch '3.7':
bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258)
https://github.com/python/cpython/commit/e3f6aa7fe48b91f4ff619b2a51d473249d620bcb
msg325473 - (view) Author: miss-islington (miss-islington) Date: 2018-09-16 05:57
New changeset e53632019816749ffd5be0afab2a99d744dbbe35 by Miss Islington (bot) in branch '3.6':
bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258)
https://github.com/python/cpython/commit/e53632019816749ffd5be0afab2a99d744dbbe35
msg325800 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-09-19 20:48
New changeset b63a16febbd1c943c9dbc5c651326b410aa50698 by Victor Stinner (Vladimir Matveev) in branch '2.7':
[2.7] bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258) (GH-9425)
https://github.com/python/cpython/commit/b63a16febbd1c943c9dbc5c651326b410aa50698
msg325801 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-09-19 20:49
Thanks MatteoL for the bug report, it has been fixed in 2.7, 3.6, 3.7 and master. Thanks Vladimir Matveev for the fix!
msg327159 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-05 18:09
assertEquals() is deprecated, use assertEqual() instead.
msg327161 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-05 18:10
New changeset 4642d5f59828e774585e9895b538b24d71b9df8e by Serhiy Storchaka in branch 'master':
Use assertEqual() instead of assertEquals(). (GH-9721)
https://github.com/python/cpython/commit/4642d5f59828e774585e9895b538b24d71b9df8e
msg327170 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-05 18:46
New changeset 6bffe50f5fff8e8a40ae32c3e9c408622a15caf6 by Serhiy Storchaka in branch '3.7':
Use assertEqual() instead of assertEquals(). (GH-9721) (GH-9725)
https://github.com/python/cpython/commit/6bffe50f5fff8e8a40ae32c3e9c408622a15caf6
msg327175 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-05 19:11
New changeset d02490a9a9c238ed7ded1120877fdfdce16364a3 by Serhiy Storchaka (Miss Islington (bot)) in branch '3.6':
Use assertEqual() instead of assertEquals(). (GH-9721) (GH-9725) (GH-9727)
https://github.com/python/cpython/commit/d02490a9a9c238ed7ded1120877fdfdce16364a3
History
Date User Action Args
2022-04-11 14:59:05adminsetgithub: 78784
2018-10-05 19:11:26serhiy.storchakasetmessages: + msg327175
2018-10-05 18:46:36miss-islingtonsetpull_requests: + pull_request9112
2018-10-05 18:46:29serhiy.storchakasetmessages: + msg327170
2018-10-05 18:16:26serhiy.storchakasetpull_requests: + pull_request9110
2018-10-05 18:10:02serhiy.storchakasetmessages: + msg327161
2018-10-05 18:09:03serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg327159
2018-10-05 17:52:10serhiy.storchakasetpull_requests: + pull_request9106
2018-09-19 20:49:26vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg325801

stage: patch review -> resolved
2018-09-19 20:48:26vstinnersetmessages: + msg325800
2018-09-19 18:48:45v2msetpull_requests: + pull_request8843
2018-09-16 05:57:49miss-islingtonsetmessages: + msg325473
2018-09-16 05:53:16miss-islingtonsetnosy: + miss-islington
messages: + msg325472
2018-09-16 05:37:04miss-islingtonsetpull_requests: + pull_request8766
2018-09-16 05:36:53miss-islingtonsetpull_requests: + pull_request8765
2018-09-16 05:36:45miss-islingtonsetpull_requests: + pull_request8764
2018-09-16 05:36:33vstinnersetnosy: + vstinner
messages: + msg325471
2018-09-13 15:43:43v2msetkeywords: + patch
stage: patch review
pull_requests: + pull_request8690
2018-09-12 23:03:18v2msetnosy: + v2m
messages: + msg325207
2018-09-10 07:50:25mattnerisetnosy: + amaury.forgeotdarc, belopolsky, meador.inge
2018-09-07 11:25:52mattnerisettitle: ctypes On Windows: error calling C function that returns a struc containing 3 bools -> ctypes on Windows: error calling C function that returns a struct containing 3 bools
2018-09-07 11:15:17mattnericreate