parse.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import os
  2. def parse_distributions_h(ffi, inc_dir):
  3. """
  4. Parse distributions.h located in inc_dir for CFFI, filling in the ffi.cdef
  5. Read the function declarations without the "#define ..." macros that will
  6. be filled in when loading the library.
  7. """
  8. with open(os.path.join(inc_dir, 'random', 'bitgen.h')) as fid:
  9. s = []
  10. for line in fid:
  11. # massage the include file
  12. if line.strip().startswith('#'):
  13. continue
  14. s.append(line)
  15. ffi.cdef('\n'.join(s))
  16. with open(os.path.join(inc_dir, 'random', 'distributions.h')) as fid:
  17. s = []
  18. in_skip = 0
  19. for line in fid:
  20. # massage the include file
  21. if line.strip().startswith('#'):
  22. continue
  23. # skip any inlined function definition
  24. # which starts with 'static NPY_INLINE xxx(...) {'
  25. # and ends with a closing '}'
  26. if line.strip().startswith('static NPY_INLINE'):
  27. in_skip += line.count('{')
  28. continue
  29. elif in_skip > 0:
  30. in_skip += line.count('{')
  31. in_skip -= line.count('}')
  32. continue
  33. # replace defines with their value or remove them
  34. line = line.replace('DECLDIR', '')
  35. line = line.replace('NPY_INLINE', '')
  36. line = line.replace('RAND_INT_TYPE', 'int64_t')
  37. s.append(line)
  38. ffi.cdef('\n'.join(s))