Skip to content

Commit 0feb25a

Browse files
committed
Merge pull request #811 from iguananaut/issue-811
astropy 0.2 tests write to site-packages for Python 3
2 parents 61093e5 + 9a7865e commit 0feb25a

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎CHANGES.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ Bug Fixes
190190

191191
- Misc
192192

193+
- Fixed a permission error that could occur when running ``astropy.test()``
194+
on Python 3 when Astropy is installed as root. [#811]
195+
193196
- Made it easier to filter warnings from the ``convolve()`` function and
194197
from ``Quantity`` objects. [#853]
195198

‎astropy/tests/helper.py‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from the installed astropy. It makes use of the `pytest` testing framework.
55
"""
66

7+
import errno
78
import shlex
89
import sys
910
import base64
@@ -54,6 +55,34 @@
5455
pytest = importer.load_module('pytest')
5556

5657

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+
5786
# pytest marker to mark tests which get data from the web
5887
remote_data = pytest.mark.remote_data
5988

@@ -299,7 +328,7 @@ def run(self):
299328
raise SystemExit(retcode)
300329

301330

302-
class raises:
331+
class raises(object):
303332
"""
304333
A decorator to mark that a test should raise a given exception.
305334
Use as follows::

0 commit comments

Comments
 (0)