Tarek Ziadé | 895ee24 | 2010-03-06 01:27:09 +0000 | [diff] [blame] | 1 | from distutils.core import Extension as _Extension |
| 2 | from distutils.core import Distribution as _Distribution |
| 3 | |
| 4 | def _get_unpatched(cls): |
| 5 | """Protect against re-patching the distutils if reloaded |
| 6 | |
| 7 | Also ensures that no other distutils extension monkeypatched the distutils |
| 8 | first. |
| 9 | """ |
| 10 | while cls.__module__.startswith('setuptools'): |
| 11 | cls, = cls.__bases__ |
| 12 | if not cls.__module__.startswith('distutils'): |
| 13 | raise AssertionError( |
| 14 | "distutils has already been patched by %r" % cls |
| 15 | ) |
| 16 | return cls |
| 17 | |
| 18 | _Distribution = _get_unpatched(_Distribution) |
| 19 | _Extension = _get_unpatched(_Extension) |
| 20 | |
| 21 | try: |
| 22 | from Pyrex.Distutils.build_ext import build_ext |
| 23 | except ImportError: |
| 24 | have_pyrex = False |
| 25 | else: |
| 26 | have_pyrex = True |
| 27 | |
| 28 | |
| 29 | class Extension(_Extension): |
| 30 | """Extension that uses '.c' files in place of '.pyx' files""" |
| 31 | |
| 32 | if not have_pyrex: |
| 33 | # convert .pyx extensions to .c |
| 34 | def __init__(self,*args,**kw): |
| 35 | _Extension.__init__(self,*args,**kw) |
| 36 | sources = [] |
| 37 | for s in self.sources: |
| 38 | if s.endswith('.pyx'): |
| 39 | sources.append(s[:-3]+'c') |
| 40 | else: |
| 41 | sources.append(s) |
| 42 | self.sources = sources |
| 43 | |
| 44 | class Library(Extension): |
| 45 | """Just like a regular Extension, but built as a library instead""" |
| 46 | |
| 47 | import sys, distutils.core, distutils.extension |
| 48 | distutils.core.Extension = Extension |
| 49 | distutils.extension.Extension = Extension |
| 50 | if 'distutils.command.build_ext' in sys.modules: |
| 51 | sys.modules['distutils.command.build_ext'].Extension = Extension |