trolldbois / ctypeslib

Generate python ctypes classes from C headers. Requires LLVM clang
http://trolldbois.blogspot.com/search?q=ctypeslib
MIT License
214 stars 57 forks source link

anonymous union got incorrect name #135

Open dewrfe53535 opened 3 months ago

dewrfe53535 commented 3 months ago

output (case from test)

class struct_s(Structure):
    pass

class union_union (anonymous at test.h:3:5)(Union):
    pass

union_union (anonymous at test.h:3:5)._pack_ = 1 # source:False
union_union (anonymous at test.h:3:5)._fields_ = [
    ('l', ctypes.c_int64),
]

struct_s._pack_ = 1 # source:False
struct_s._anonymous_ = ('_0',)
struct_s._fields_ = [
    ('i', ctypes.c_int32),
    ('PADDING_0', ctypes.c_ubyte * 4),
    ('_0', union_union (anonymous at test.h:3:5)),
]

__all__ = \
    ['struct_s', 'union_union (anonymous at `test.h:3:5)']

clang version:

Ubuntu clang version 17.0.6 (++20231209124227+6009708b4367-1~exp1~20231209124336.77)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
isidroas commented 2 weeks ago

when using them like this:

import ctypeslib

py_module = ctypeslib.translate("""
#include <netinet/in.h>
struct network_setup
{
    sa_family_t version;
    union
    {
        struct in_addr  ipv4;
        struct in6_addr ipv6;
    } ip;
    union
    {
        struct in_addr  ipv4;
        struct in6_addr ipv6;
    } mask;
    union
    {
        struct in_addr  ipv4;
        struct in6_addr ipv6;
    } gateway;
};
""")

it fails with the following traceback:

$ python example.py
Bad source code, bitsize == -16 <0 on __cmsg_data
Traceback (most recent call last):
  File "/tmp/ctypeslib/example.py", line 3, in <module>
    py_module = ctypeslib.translate("""
                ^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/ctypeslib/ctypeslib/codegen/codegenerator.py", line 1151, in translate
    exec(output, namespace)
  File "<string>", line 1215
    'enum (unnamed at /usr/include/netinet/in.h:105:1)', 'enum
                                                         ^