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

ctypes: subclassed swapped endianness doesn't swap #95384

Open
Celelibi opened this issue Jul 28, 2022 · 1 comment
Open

ctypes: subclassed swapped endianness doesn't swap #95384

Celelibi opened this issue Jul 28, 2022 · 1 comment
Labels
expert-ctypes type-bug An unexpected behavior, bug, or error

Comments

@Celelibi
Copy link

Bug report

Subclassing a swapped simple type and calling from_buffer_copy doesn't swap bytes. Here is a minimal non-working example.

import ctypes

cshort_be = ctypes.c_ushort.__ctype_be__
class Short(cshort_be):
    pass

print(cshort_be.from_buffer_copy(b"\x00\x01").value) # prints 1
print(Short.from_buffer_copy(b"\x00\x01").value) # prints 256

However, there is a simple workaround:

print(Short.__ctype_be__.from_buffer_copy(b"\x00\x01").value) # prints 1

I am not sure what happens, but ctypes behave kinda weird when subclassing native types. So this bug might be related to issue #73456.

My environment

  • CPython versions tested on: 3.10.5
  • Operating system and architecture: Linux Celap 5.18.0-1-amd64 #1 SMP PREEMPT_DYNAMIC Debian 5.18.2-1 (2022-06-06) x86_64 GNU/Linux
@Celelibi Celelibi added the type-bug An unexpected behavior, bug, or error label Jul 28, 2022
@akulakov
Copy link
Contributor

akulakov commented Aug 3, 2022

Confirmed both the issue and workaround on MacOS 11.5 and Python 3.12. But note documentation doesn't promise anything about subclassing simple types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
expert-ctypes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants