Don't fail if imp can't find the source for a .pyc file. #2038
This commit is contained in:
parent
cbf261c74e
commit
06bb61bbe3
1
AUTHORS
1
AUTHORS
|
@ -101,6 +101,7 @@ Michael Birtwell
|
||||||
Michael Droettboom
|
Michael Droettboom
|
||||||
Michael Seifert
|
Michael Seifert
|
||||||
Mike Lundy
|
Mike Lundy
|
||||||
|
Ned Batchelder
|
||||||
Nicolas Delaby
|
Nicolas Delaby
|
||||||
Oleg Pidsadnyi
|
Oleg Pidsadnyi
|
||||||
Oliver Bestwalter
|
Oliver Bestwalter
|
||||||
|
|
|
@ -5,12 +5,17 @@
|
||||||
|
|
||||||
*
|
*
|
||||||
|
|
||||||
*
|
* Cope gracefully with a .pyc file with no matching .py file (`#2038`_). Thanks
|
||||||
|
`@nedbat`_.
|
||||||
|
|
||||||
*
|
*
|
||||||
|
|
||||||
*
|
*
|
||||||
|
|
||||||
|
.. _@nedbat: https://github.com/nedbat
|
||||||
|
|
||||||
|
.. _#2038: https://github.com/pytest-dev/pytest/issues/2038
|
||||||
|
|
||||||
|
|
||||||
3.0.4
|
3.0.4
|
||||||
=====
|
=====
|
||||||
|
|
|
@ -80,7 +80,12 @@ class AssertionRewritingHook(object):
|
||||||
tp = desc[2]
|
tp = desc[2]
|
||||||
if tp == imp.PY_COMPILED:
|
if tp == imp.PY_COMPILED:
|
||||||
if hasattr(imp, "source_from_cache"):
|
if hasattr(imp, "source_from_cache"):
|
||||||
fn = imp.source_from_cache(fn)
|
try:
|
||||||
|
fn = imp.source_from_cache(fn)
|
||||||
|
except ValueError:
|
||||||
|
# Python 3 doesn't like orphaned but still-importable
|
||||||
|
# .pyc files.
|
||||||
|
fn = fn[:-1]
|
||||||
else:
|
else:
|
||||||
fn = fn[:-1]
|
fn = fn[:-1]
|
||||||
elif tp != imp.PY_SOURCE:
|
elif tp != imp.PY_SOURCE:
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
import glob
|
||||||
import os
|
import os
|
||||||
|
import py_compile
|
||||||
import stat
|
import stat
|
||||||
import sys
|
import sys
|
||||||
import zipfile
|
import zipfile
|
||||||
|
|
||||||
import py
|
import py
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
@ -480,6 +483,31 @@ def test_rewritten():
|
||||||
monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1")
|
monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1")
|
||||||
assert testdir.runpytest_subprocess().ret == 0
|
assert testdir.runpytest_subprocess().ret == 0
|
||||||
|
|
||||||
|
def test_orphaned_pyc_file(self, testdir):
|
||||||
|
if sys.version_info < (3, 0) and hasattr(sys, 'pypy_version_info'):
|
||||||
|
pytest.skip("pypy2 doesn't run orphaned pyc files")
|
||||||
|
|
||||||
|
testdir.makepyfile("""
|
||||||
|
import orphan
|
||||||
|
def test_it():
|
||||||
|
assert orphan.value == 17
|
||||||
|
""")
|
||||||
|
testdir.makepyfile(orphan="""
|
||||||
|
value = 17
|
||||||
|
""")
|
||||||
|
py_compile.compile("orphan.py")
|
||||||
|
os.remove("orphan.py")
|
||||||
|
|
||||||
|
# Python 3 puts the .pyc files in a __pycache__ directory, and will
|
||||||
|
# not import from there without source. It will import a .pyc from
|
||||||
|
# the source location though.
|
||||||
|
if not os.path.exists("orphan.pyc"):
|
||||||
|
pycs = glob.glob("__pycache__/orphan.*.pyc")
|
||||||
|
assert len(pycs) == 1
|
||||||
|
os.rename(pycs[0], "orphan.pyc")
|
||||||
|
|
||||||
|
assert testdir.runpytest().ret == 0
|
||||||
|
|
||||||
@pytest.mark.skipif('"__pypy__" in sys.modules')
|
@pytest.mark.skipif('"__pypy__" in sys.modules')
|
||||||
def test_pyc_vs_pyo(self, testdir, monkeypatch):
|
def test_pyc_vs_pyo(self, testdir, monkeypatch):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
|
|
Loading…
Reference in New Issue