trolldbois / ctypeslib

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

ran clang2py on a header that had the "raise" symbol #97

Open bogen85 opened 3 years ago

bogen85 commented 3 years ago

Since that is a python keyword, loading the resulting file is problematic....

I was able to fix and a few other issues with:

sed 's/^raise/#_raise/'
sed "s,^_libraries\['FIXME_STUB'].*,_libraries['FIXME_STUB'] = ctypes.CDLL('libc.so.6'),"
sed 's/^pthread/#pthread/'
bogen85 commented 3 years ago

In general clang2py does not like the headers I give it. I was trying to use cffi on some headers and it did not them either. The following made them more palatable, and clang2py likes headers resulting from this as well:

result = subprocess.check_output(f"clang -E -I. {header}", shell=True)
sanitized = []
for line in result.decode(encoding='utf-8').split('\n'):
  for bad in ['__asm__', '__attribute__']:
    if bad in line:
      line = f'{line.split(bad)[0]};'.replace(';;',';')
    sanitized.append(line)
lines = "\n".join(sanitized)

With sanitized headers and the above sed commands I'm having a lot more success with ctypeslib than I was having with cffi I'm passing in my library and getting most symbols resolved by it. When I try to pass in libc.so.6 as well, I get no output from clang2py, but that is a different issue.

bogen85 commented 3 years ago

Well, actually I'm fixing it all up in python now, the sed stuff was just temporary from the command line to get it working.

mara004 commented 9 months ago

Sounds like clang2py would need a conflicting names resolver?