python-autopxd2 icon indicating copy to clipboard operation
python-autopxd2 copied to clipboard

Fixed an issue where character values in enum could not be handled in some cases.

Open Poiuzy opened this issue 2 years ago • 1 comments

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'"

Poiuzy avatar Oct 14 '23 14:10 Poiuzy

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, "")

elijahr avatar Oct 27 '23 23:10 elijahr

@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)

touilleMan avatar Sep 10 '24 07:09 touilleMan