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.

extension.py 1.6 KiB

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