pycstruct icon indicating copy to clipboard operation
pycstruct copied to clipboard

Descriptive struct

Open vallsv opened this issue 4 years ago • 3 comments

When i saw your library, i was searching something to define cstrucs as descriptive way.

With the last python typing annotation there is probably way to provide something like that:

class Foo(pycstruct.Struct):
    foo: pycstruct.uint32
    foo2: pycstruct.uint16
    foo3: pycstruct.array[pycstruct.float64, 32]

...or...

class Foo:
    foo: typing.Annotated[int, pycstruct.type("uint32")]
    foo2: typing.Annotated[int, pycstruct.type("uint16")]
    foo3: typing.Annotated[float, pycstruct.type("float64, length=32")]

...or...

class Foo:
    @pycstruct.tpye("uint32")
    foo: int
    @pycstruct.tpye("uint16")
    foo2: int
    @pycstruct.tpye("uint16", length=32)
    foo3: float

No idea if that stuff could work or not.

I would be very happy to use such thing. Do you think it could interest you?

vallsv avatar Feb 15 '21 20:02 vallsv

Hi,

Thank you for your suggestion. It would probably be possible to implement any of these alternatives in a separate file (like annotations.py). You are welcome with a suggestion in a pull request.

However, it seems that most users are utilizing the cparser and standard C header files (or strings) to define there structs, bitfields etc.

/Joel

midstar avatar Feb 20 '21 15:02 midstar

Yes you are right. But i was thinking about getting ride of castxml for pure python lib. Just because it it's extra dependency which is more difficult to install.

There is probably an opportunity to provide a tool to convert .h file to a pure python file using your lib. As result any project could remove the castxml dependency easily if needed.

I will maybe play around if i have some time.

vallsv avatar Feb 20 '21 18:02 vallsv

It would be great if pycstruct had a .h parser that did not depend on castxml.

castxml is extremly powerful and builds on a real compiler which supports the complete standard with all complex corner cases. Thus it would not be feasible to implement such a powerful parser.

However, it should be possible to implement the basic cases for enum, struct/bitfield and union. But it gets quite complex when the source includes typedefs, macros, includes etc.

midstar avatar Feb 21 '21 06:02 midstar