Adds a test for and fixes #112. If attempting to write to the __pycache__ directory raises a permission error _write_pyc() should just return False to prevent any further write attempts.

This commit is contained in:
Erik Bray 2013-05-28 18:11:12 -04:00
parent c294a417bd
commit b1595d3f61
2 changed files with 22 additions and 1 deletions

View File

@ -177,6 +177,10 @@ def _write_pyc(co, source_path, pyc):
# This happens when we get a EEXIST in find_module creating the # This happens when we get a EEXIST in find_module creating the
# __pycache__ directory and __pycache__ is by some non-dir node. # __pycache__ directory and __pycache__ is by some non-dir node.
return False return False
elif err == errno.EACCES:
# The directory is read-only; this can happen for example when
# running the tests in a package installed as root
return False
raise raise
try: try:
fp.write(imp.get_magic()) fp.write(imp.get_magic())

View File

@ -1,4 +1,5 @@
import os import os
import stat
import sys import sys
import zipfile import zipfile
import py import py
@ -323,6 +324,18 @@ def test_rewritten():
assert "@py_builtins" in globals()""") assert "@py_builtins" in globals()""")
assert testdir.runpytest().ret == 0 assert testdir.runpytest().ret == 0
def test_pycache_is_readonly(self, testdir):
cache = testdir.tmpdir.mkdir("__pycache__")
old_mode = cache.stat().mode
cache.chmod(old_mode ^ stat.S_IWRITE)
testdir.makepyfile("""
def test_rewritten():
assert "@py_builtins" in globals()""")
try:
assert testdir.runpytest().ret == 0
finally:
cache.chmod(old_mode)
def test_zipfile(self, testdir): def test_zipfile(self, testdir):
z = testdir.tmpdir.join("myzip.zip") z = testdir.tmpdir.join("myzip.zip")
z_fn = str(z) z_fn = str(z)
@ -346,8 +359,12 @@ import test_gum.test_lizard""" % (z_fn,))
def test_rewritten(): def test_rewritten():
assert "@py_builtins" in globals() assert "@py_builtins" in globals()
""").encode("utf-8"), "wb") """).encode("utf-8"), "wb")
old_mode = sub.stat().mode
sub.chmod(320) sub.chmod(320)
assert testdir.runpytest().ret == 0 try:
assert testdir.runpytest().ret == 0
finally:
sub.chmod(old_mode)
def test_dont_write_bytecode(self, testdir, monkeypatch): def test_dont_write_bytecode(self, testdir, monkeypatch):
testdir.makepyfile(""" testdir.makepyfile("""