[Pytest-commit] commit/pytest: hpk42: fix issue305 - ignore any problems in writing a pyc file, but print out a trace.

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Thu Aug 1 15:44:29 CEST 2013


1 new commit in pytest:

https://bitbucket.org/hpk42/pytest/commits/daa1e4c25b57/
Changeset:   daa1e4c25b57
User:        hpk42
Date:        2013-08-01 15:43:42
Summary:     fix issue305 - ignore any problems in writing a pyc file, but print out a trace.
Affected #:  3 files

diff -r 03f54633875fb0717c70ee9d8308aa501667d7e0 -r daa1e4c25b578fdfa98a122922ea13bf80c8592a CHANGELOG
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -6,6 +6,8 @@
 
 - remove implicit distribute_setup support from setup.py.
 
+- fix issue305: ignore any problems when writing pyc files.
+
 - integrate tab-completion on options through use of "argcomplete".
   Thanks Anthon van der Neut for the PR.
 

diff -r 03f54633875fb0717c70ee9d8308aa501667d7e0 -r daa1e4c25b578fdfa98a122922ea13bf80c8592a _pytest/assertion/rewrite.py
--- a/_pytest/assertion/rewrite.py
+++ b/_pytest/assertion/rewrite.py
@@ -156,7 +156,7 @@
             raise
         return sys.modules[name]
 
-def _write_pyc(co, source_path, pyc):
+def _write_pyc(state, co, source_path, pyc):
     # Technically, we don't have to have the same pyc format as
     # (C)Python, since these "pycs" should never be seen by builtin
     # import. However, there's little reason deviate, and I hope
@@ -167,15 +167,11 @@
         fp = open(pyc, "wb")
     except IOError:
         err = sys.exc_info()[1].errno
-        if err in [errno.ENOENT, errno.ENOTDIR]:
-            # This happens when we get a EEXIST in find_module creating the
-            # __pycache__ directory and __pycache__ is by some non-dir node.
-            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
+        state.trace("error writing pyc file at %s: errno=%s" %(pyc, err))
+        # we ignore any failure to write the cache file
+        # there are many reasons, permission-denied, __pycache__ being a
+        # file etc.
+        return False
     try:
         fp.write(imp.get_magic())
         fp.write(struct.pack("<l", mtime))
@@ -249,12 +245,12 @@
     if sys.platform.startswith("win"):
         # Windows grants exclusive access to open files and doesn't have atomic
         # rename, so just write into the final file.
-        _write_pyc(co, fn, pyc)
+        _write_pyc(state, co, fn, pyc)
     else:
         # When not on windows, assume rename is atomic. Dump the code object
         # into a file specific to this process and atomically replace it.
         proc_pyc = pyc + "." + str(os.getpid())
-        if _write_pyc(co, fn, proc_pyc):
+        if _write_pyc(state, co, fn, proc_pyc):
             os.rename(proc_pyc, pyc)
 
 def _read_pyc(source, pyc):

diff -r 03f54633875fb0717c70ee9d8308aa501667d7e0 -r daa1e4c25b578fdfa98a122922ea13bf80c8592a testing/test_assertrewrite.py
--- a/testing/test_assertrewrite.py
+++ b/testing/test_assertrewrite.py
@@ -419,3 +419,23 @@
         res = testdir.runpytest()
         assert res.ret != 0
         assert "SyntaxError: Non-ASCII character" in res.stdout.str()
+
+
+    def test_write_pyc(self, testdir, tmpdir, monkeypatch):
+        from _pytest.assertion.rewrite import _write_pyc
+        from _pytest.assertion import AssertionState
+        try:
+            import __builtin__ as b
+        except ImportError:
+            import builtins as b
+        config = testdir.parseconfig([])
+        state = AssertionState(config, "rewrite")
+        source_path = tmpdir.ensure("source.py")
+        pycpath = tmpdir.join("pyc").strpath
+        assert _write_pyc(state, [1], source_path, pycpath)
+        def open(*args):
+            e = IOError()
+            e.errno = 10
+            raise e
+        monkeypatch.setattr(b, "open", open)
+        assert not _write_pyc(state, [1], source_path, pycpath)

Repository URL: https://bitbucket.org/hpk42/pytest/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the pytest-commit mailing list