Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.7] bpo-10746: Fix ctypes PEP 3118 type codes for c_long, c_bool, c_int (GH-31) #3242

Merged
merged 2 commits into from
Sep 2, 2017

Conversation

pv
Copy link
Contributor

@pv pv commented Aug 30, 2017

Ctypes currently produces wrong pep3118 type codes for several types.
E.g. memoryview(ctypes.c_long()).format gives "<l" on 64-bit platforms,
but it should be "<q" instead for sizeof(c_long) == 8

The problem is that the '<>' endian specification in the struct syntax
also turns on the "standard size" mode, which makes type characters have
a platform-independent meaning, which does not match with the codes used
internally in ctypes. The struct module format syntax also does not
allow specifying native-size non-native-endian items.

This commit adds a converter function that maps the internal ctypes
codes to appropriate struct module standard-size codes in the pep3118
format strings. The tests are modified to check for this..
(cherry picked from commit 07f1658)

https://bugs.python.org/issue10746

…_int (pythonGH-31)

Ctypes currently produces wrong pep3118 type codes for several types.
E.g. memoryview(ctypes.c_long()).format gives "<l" on 64-bit platforms,
but it should be "<q" instead for sizeof(c_long) == 8

The problem is that the '<>' endian specification in the struct syntax
also turns on the "standard size" mode, which makes type characters have
a platform-independent meaning, which does not match with the codes used
internally in ctypes.  The struct module format syntax also does not
allow specifying native-size non-native-endian items.

This commit adds a converter function that maps the internal ctypes
codes to appropriate struct module standard-size codes in the pep3118
format strings. The tests are modified to check for this..
(cherry picked from commit 07f1658)
@pitrou
Copy link
Member

pitrou commented Aug 30, 2017

This PR failed building on AppVeyor, see: https://ci.appveyor.com/project/python/cpython/build/2.7.13+.5702#L306

#elif SIZEOF__BOOL == 8
case '?': pep_code = 'Q'; break;
#else
# error SIZEOF__BOOL has an unexpected value
Copy link
Contributor

@eamanu eamanu Aug 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @pv,

The problems is with the win compilation. SIZEOF__BOOL has an unexpected value

I'm really new, but that sound how if you don't define SIZEOF__BOOL variable. Searching where is that defining, I found its defintion in PC/pyconfig.h, but you never including that. I cannot test if that is the solution, because I don`t have my tools here.

regard!

@pv
Copy link
Contributor Author

pv commented Aug 30, 2017 via email

@pv
Copy link
Contributor Author

pv commented Sep 2, 2017

Should be fixed now. In Python 2.7, SIZEOF__BOOL is defined to 1 if HAVE_C99_BOOL is undefined in cfield.c. I now duplicated that definition in ctypes.c. On Python 3.x, there are no such ifdefs for SIZEOF__BOOL in either file, as the variable seems to be always defined.

@pitrou
Copy link
Member

pitrou commented Sep 2, 2017

Thank you! Now merging.

@pitrou pitrou merged commit 990b2d0 into python:2.7 Sep 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants