|
4 | 4 | from the installed astropy. It makes use of the `pytest` testing framework. |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import errno |
7 | 8 | import shlex |
8 | 9 | import sys |
9 | 10 | import base64 |
|
54 | 55 | pytest = importer.load_module('pytest') |
55 | 56 |
|
56 | 57 |
|
| 58 | +# Monkey-patch py.test to work around issue #811 |
| 59 | +# https://github.com/astropy/astropy/issues/811 |
| 60 | +from _pytest.assertion import rewrite as _rewrite |
| 61 | +_orig_write_pyc = _rewrite._write_pyc |
| 62 | +def _write_pyc_wrapper(co, source_path, pyc): |
| 63 | + """Wraps the internal _write_pyc method in py.test to recognize |
| 64 | + PermissionErrors and just stop trying to cache its generated pyc files if |
| 65 | + it can't write them to the __pycache__ directory. |
| 66 | +
|
| 67 | + When py.test scans for test modules, it actually rewrites the bytecode |
| 68 | + of each test module it discovers--this is how it manages to add extra |
| 69 | + instrumentation to the assert builtin. Normally it caches these |
| 70 | + rewritten bytecode files--``_write_pyc()`` is just a function that handles |
| 71 | + writing the rewritten pyc file to the cache. If it returns ``False`` for |
| 72 | + any reason py.test will stop trying to cache the files altogether. The |
| 73 | + original function catches some cases, but it has a long-standing bug of |
| 74 | + not catching permission errors on the ``__pycache__`` directory in Python |
| 75 | + 3. Hence this patch. |
| 76 | + """ |
| 77 | + |
| 78 | + try: |
| 79 | + return _orig_write_pyc(co, source_path, pyc) |
| 80 | + except IOError as e: |
| 81 | + if e.errno == errno.EACCES: |
| 82 | + return False |
| 83 | +_rewrite._write_pyc = _write_pyc_wrapper |
| 84 | + |
| 85 | + |
57 | 86 | # pytest marker to mark tests which get data from the web |
58 | 87 | remote_data = pytest.mark.remote_data |
59 | 88 |
|
@@ -299,7 +328,7 @@ def run(self): |
299 | 328 | raise SystemExit(retcode) |
300 | 329 |
|
301 | 330 |
|
302 | | -class raises: |
| 331 | +class raises(object): |
303 | 332 | """ |
304 | 333 | A decorator to mark that a test should raise a given exception. |
305 | 334 | Use as follows:: |
|
0 commit comments