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