1from distutils.core import Extension as _Extension
2from distutils.core import Distribution as _Distribution
3
4def _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
21try:
22    from Pyrex.Distutils.build_ext import build_ext
23except ImportError:
24    have_pyrex = False
25else:
26    have_pyrex = True
27
28
29class 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
44class Library(Extension):
45    """Just like a regular Extension, but built as a library instead"""
46
47import sys, distutils.core, distutils.extension
48distutils.core.Extension = Extension
49distutils.extension.Extension = Extension
50if 'distutils.command.build_ext' in sys.modules:
51    sys.modules['distutils.command.build_ext'].Extension = Extension
52