python-autopxd2
python-autopxd2 copied to clipboard
Fixed an issue where character values in enum could not be handled in some cases.
Fixed an issue where character values in enum could not be handled in some cases like
// test.h
typedef enum
{
SDL_SCANCODE_CAPSLOCK = 57,
} SDL_Scancode;
#define SDLK_SCANCODE_MASK (1<<30)
#define SDL_SCANCODE_TO_KEYCODE(X) (X | SDLK_SCANCODE_MASK)
typedef enum
{
SDLK_z = 'z',
SDLK_CAPSLOCK = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_CAPSLOCK),
} SDL_KeyCode;
File "D:/msys64/ucrt64/lib/python3.11/site-packages/autopxd/writer.py", line 125, in visit_Enum
value = str(int(value, base=0) + 1)
^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 0: "'z'"
Thanks for the contribution! Please make sure this code passes the linting check, and add or update tests so that each line in your changeset has test coverage:
if "'" in value:
# Handle characters
value = str(int.from_bytes(value[1:-1].encode('raw_unicode_escape')))
else:
# Remove type suffixes
for suffix in "lLuU":
value = value.replace(suffix, "")
@Poiuzy thanks for your work on this PR :+1:
I've reworked it into PR #47 (adding tests and also basic support for binary expression)