extension.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_pyrex():
  12. """
  13. Return True if Cython or Pyrex can be imported.
  14. """
  15. pyrex_impls = 'Cython.Distutils.build_ext', 'Pyrex.Distutils.build_ext'
  16. for pyrex_impl in pyrex_impls:
  17. try:
  18. # from (pyrex_impl) import build_ext
  19. __import__(pyrex_impl, fromlist=['build_ext']).build_ext
  20. return True
  21. except Exception:
  22. pass
  23. return False
  24. class Extension(_Extension):
  25. """Extension that uses '.c' files in place of '.pyx' files"""
  26. def __init__(self, *args, **kw):
  27. _Extension.__init__(self, *args, **kw)
  28. self._convert_pyx_sources_to_lang()
  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_pyrex():
  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"""
  44. distutils.core.Extension = Extension
  45. distutils.extension.Extension = Extension
  46. if 'distutils.command.build_ext' in sys.modules:
  47. sys.modules['distutils.command.build_ext'].Extension = Extension