You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.6KB

  1. import re
  2. import functools
  3. import distutils.core
  4. import distutils.errors
  5. import distutils.extension
  6. from .monkey import get_unpatched
  7. def _have_cython():
  8. """
  9. Return True if Cython can be imported.
  10. """
  11. cython_impl = 'Cython.Distutils.build_ext'
  12. try:
  13. # from (cython_impl) import build_ext
  14. __import__(cython_impl, fromlist=['build_ext']).build_ext
  15. return True
  16. except Exception:
  17. pass
  18. return False
  19. # for compatibility
  20. have_pyrex = _have_cython
  21. _Extension = get_unpatched(distutils.core.Extension)
  22. class Extension(_Extension):
  23. """Extension that uses '.c' files in place of '.pyx' files"""
  24. def __init__(self, name, sources, *args, **kw):
  25. # The *args is needed for compatibility as calls may use positional
  26. # arguments. py_limited_api may be set only via keyword.
  27. self.py_limited_api = kw.pop("py_limited_api", False)
  28. _Extension.__init__(self, name, sources, *args, **kw)
  29. def _convert_pyx_sources_to_lang(self):
  30. """
  31. Replace sources with .pyx extensions to sources with the target
  32. language extension. This mechanism allows language authors to supply
  33. pre-converted sources but to prefer the .pyx sources.
  34. """
  35. if _have_cython():
  36. # the build has Cython, so allow it to compile the .pyx files
  37. return
  38. lang = self.language or ''
  39. target_ext = '.cpp' if lang.lower() == 'c++' else '.c'
  40. sub = functools.partial(re.sub, '.pyx$', target_ext)
  41. self.sources = list(map(sub, self.sources))
  42. class Library(Extension):
  43. """Just like a regular Extension, but built as a library instead"""