|
3 | 3 | import os
|
4 | 4 | import shutil
|
5 | 5 | from tempfile import mkstemp, mkdtemp
|
| 6 | +from subprocess import Popen, PIPE |
| 7 | +from distutils.errors import DistutilsError |
6 | 8 |
|
7 | 9 | from numpy.distutils import ccompiler
|
8 | 10 | from numpy.testing import TestCase, run_module_suite, assert_, assert_equal
|
@@ -54,6 +56,27 @@ def get_class(name, notfound_action=1):
|
54 | 56 | }
|
55 | 57 | """
|
56 | 58 |
|
| 59 | +def have_compiler(): |
| 60 | + """ Return True if there appears to be an executable compiler |
| 61 | + """ |
| 62 | + compiler = ccompiler.new_compiler() |
| 63 | + try: |
| 64 | + cmd = compiler.compiler # Unix compilers |
| 65 | + except AttributeError: |
| 66 | + try: |
| 67 | + compiler.initialize() # MSVC is different |
| 68 | + except DistutilsError: |
| 69 | + return False |
| 70 | + cmd = [compiler.cc] |
| 71 | + try: |
| 72 | + Popen(cmd, stdout=PIPE, stderr=PIPE) |
| 73 | + except OSError: |
| 74 | + return False |
| 75 | + return True |
| 76 | + |
| 77 | + |
| 78 | +HAVE_COMPILER = have_compiler() |
| 79 | + |
57 | 80 |
|
58 | 81 | class test_system_info(system_info):
|
59 | 82 |
|
@@ -171,38 +194,39 @@ def test_temp2(self):
|
171 | 194 | extra = tsi.calc_extra_info()
|
172 | 195 | assert_equal(extra['extra_link_args'], ['-Wl,-rpath=' + self._lib2])
|
173 | 196 |
|
| 197 | + @skipif(not HAVE_COMPILER) |
174 | 198 | def test_compile1(self):
|
175 | 199 | # Compile source and link the first source
|
176 | 200 | c = ccompiler.new_compiler()
|
| 201 | + previousDir = os.getcwd() |
177 | 202 | try:
|
178 | 203 | # Change directory to not screw up directories
|
179 |
| - previousDir = os.getcwd() |
180 | 204 | os.chdir(self._dir1)
|
181 | 205 | c.compile([os.path.basename(self._src1)], output_dir=self._dir1)
|
182 | 206 | # Ensure that the object exists
|
183 | 207 | assert_(os.path.isfile(self._src1.replace('.c', '.o')) or
|
184 | 208 | os.path.isfile(self._src1.replace('.c', '.obj')))
|
| 209 | + finally: |
185 | 210 | os.chdir(previousDir)
|
186 |
| - except OSError: |
187 |
| - pass |
188 | 211 |
|
| 212 | + @skipif(not HAVE_COMPILER) |
189 | 213 | @skipif('msvc' in repr(ccompiler.new_compiler()))
|
190 | 214 | def test_compile2(self):
|
191 | 215 | # Compile source and link the second source
|
192 | 216 | tsi = self.c_temp2
|
193 | 217 | c = ccompiler.new_compiler()
|
194 | 218 | extra_link_args = tsi.calc_extra_info()['extra_link_args']
|
| 219 | + previousDir = os.getcwd() |
195 | 220 | try:
|
196 | 221 | # Change directory to not screw up directories
|
197 |
| - previousDir = os.getcwd() |
198 | 222 | os.chdir(self._dir2)
|
199 | 223 | c.compile([os.path.basename(self._src2)], output_dir=self._dir2,
|
200 | 224 | extra_postargs=extra_link_args)
|
201 | 225 | # Ensure that the object exists
|
202 | 226 | assert_(os.path.isfile(self._src2.replace('.c', '.o')))
|
| 227 | + finally: |
203 | 228 | os.chdir(previousDir)
|
204 |
| - except OSError: |
205 |
| - pass |
| 229 | + |
206 | 230 |
|
207 | 231 | if __name__ == '__main__':
|
208 | 232 | run_module_suite()
|
0 commit comments