Why ctypes.wintypes.BYTE is signed, but native windows BYTE is unsigned?

283 Views Asked by At

Python 2.7 on Windows:

from ctypes import wintypes
print wintypes.BYTE  # ctypes.c_byte

MSDN

A BYTE - 8 bit.

typedef unsigned char BYTE
1

There are 1 best solutions below

0
On

As @ErykSun mentioned in his comment, it's an old bug which somehow got away.
It was however spotted (here are a couple of URLs - surely there are more):

But it was fixed by [GitHub]: python/cpython - gh-60580: Fix a wrong type of ctypes.wintypes.BYTE (on 20230126 - I was just about to submit a PR myself, if I didn't find this one).

So, the first version that will have it right, is Python 3.12.

Usually (when it really matters) I do:

from ctypes import wintypes as wts

# Other imports

wts.BYTE = cts.c_ubyte

# ...

Things (CTypes related) that would (probably) worth reading: