New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Python 3, importing a .pyc with no .py fails in the assert rewriter #2038

Closed
nedbat opened this Issue Nov 1, 2016 · 7 comments

Comments

Projects
None yet
3 participants
@nedbat
Contributor

nedbat commented Nov 1, 2016

Pytest 3.0.3, Mac OS/X

To reproduce: get commit 694165cbea4c of coverage.py: https://bitbucket.org/ned/coveragepy/get/694165cbea4c.zip

$ pip install tox
$ COVERAGE_PYTEST=1 tox -e py35 -- tests/test_summary.py
py35 develop-inst-nodeps: /Users/ned/coverage/trunk
py35 installed: -e hg+ssh://hg@bitbucket.org/ned/coveragepy@694165cbea4ca7c0e2103c453836aea192abf02a#egg=coverage,covtestegg1==0.0.0,decorator==4.0.10,eventlet==0.19.0,gevent==1.1.2,greenlet==0.4.10,mock==2.0.0,nose==1.3.7,pbr==1.10.0,py==1.4.31,PyContracts==1.7.9,pyparsing==2.1.10,pytest==3.0.3,six==1.10.0,unittest-mixins==1.1.1
py35 runtests: PYTHONHASHSEED='2429763551'
py35 runtests: commands[0] | python setup.py --quiet clean develop
no previously-included directories found matching 'tests/eggsrc/dist'
no previously-included directories found matching 'tests/eggsrc/*.egg-info'
py35 runtests: commands[1] | python igor.py zip_mods install_egg remove_extension
py35 runtests: commands[2] | python igor.py test_with_tracer py tests/test_summary.py
=== CPython 3.5.2 with Python tracer (.tox/py35/bin/python) ===
.......Fs.........................
=========================================================================== FAILURES ===========================================================================
_________________________________________________________ SummaryTest.test_missing_py_file_during_run __________________________________________________________

name = 'mod', path = None, target = None

>   ???
E   AttributeError: 'AssertionRewritingHook' object has no attribute 'find_spec'

<frozen importlib._bootstrap>:890: AttributeError

During handling of the above exception, another exception occurred:

self = <tests.test_summary.SummaryTest testMethod=test_missing_py_file_during_run>

    def test_missing_py_file_during_run(self):
        # PyPy2 doesn't run bare .pyc files.
        if env.PYPY and env.PY2:
            self.skipTest("PyPy2 doesn't run bare .pyc files")

        # Create two Python files.
        self.make_file("mod.py", "a = 1\n")
        self.make_file("main.py", "import mod\n")

        # Make one into a .pyc, and remove the .py.
        py_compile.compile("mod.py")
        os.remove("mod.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("mod.pyc"):
            pycs = glob.glob("__pycache__/mod.*.pyc")
            self.assertEqual(len(pycs), 1)
            os.rename(pycs[0], "mod.pyc")

        # Run the program.
        cov = coverage.Coverage()
        cov.start()
>       import main     # pragma: nested # pylint: disable=import-error, unused-variable

tests/test_summary.py:575:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/private/var/folders/j2/gr3cj3jn63s5q8g3bjvw57hm0000gp/T/test_cover_18448310/main.py:1: in <module>
    ???
<frozen importlib._bootstrap>:969: in _find_and_load
    ???
<frozen importlib._bootstrap>:954: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:892: in _find_spec
    ???
<frozen importlib._bootstrap>:873: in _find_spec_legacy
    ???
.tox/py35/lib/python3.5/site-packages/_pytest/assertion/rewrite.py:82: in find_module
    fn = imp.source_from_cache(fn)
.tox/py35/lib/python3.5/imp.py:102: in source_from_cache
    return util.source_from_cache(path)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

path = '/private/var/folders/j2/gr3cj3jn63s5q8g3bjvw57hm0000gp/T/test_cover_18448310/mod.pyc'

>   ???
E   ValueError: __pycache__ not bottom-level directory in '/private/var/folders/j2/gr3cj3jn63s5q8g3bjvw57hm0000gp/T/test_cover_18448310/mod.pyc'

<frozen importlib._bootstrap_external>:305: ValueError
1 failed, 32 passed, 1 skipped in 24.72 seconds

This is with Python 3.5, py34 or py36 will also fail, with slightly different stack traces.

This test is designed to test how coverage.py handles missing .py files, so it is intentional that there is a .pyc with no .py file.

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Nov 1, 2016

wow, that one is a nasty edge-case

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Nov 1, 2016

I'm curious, why was this labeled as "infrastructure"? Mind you I have no idea what's the problem. 😄

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Nov 1, 2016

FWIW I can reproduce the issue on Windows, thanks @nedbat for packaging it so neatly:

   Creating library build\temp.win32-3.5\Release\coverage/ctracer\tracer.cp35-win32.lib and object build\temp.win32-3.5\Release\coverage/ctracer\tracer.cp35-win32.exp
Generating code
Finished generating code
py35 runtests: commands[1] | python igor.py zip_mods install_egg remove_extension
py35 runtests: commands[2] | python igor.py test_with_tracer py tests\test_summary.py
=== CPython 3.5.0 with Python tracer (.tox\py35\Scripts\python.EXE) ===
============================= test session starts =============================
platform win32 -- Python 3.5.0, pytest-3.0.3, py-1.4.31, pluggy-0.4.0
rootdir: C:\pytest, inifile: tox.ini
collected 34 items

tests\test_summary.py .......F..........................

================================== FAILURES ===================================
_________________ SummaryTest.test_missing_py_file_during_run _________________

name = 'mod', path = None, target = None

>   ???
E   AttributeError: 'AssertionRewritingHook' object has no attribute 'find_spec'

<frozen importlib._bootstrap>:890: AttributeError

During handling of the above exception, another exception occurred:

self = <tests.test_summary.SummaryTest testMethod=test_missing_py_file_during_run>

    def test_missing_py_file_during_run(self):
        # PyPy2 doesn't run bare .pyc files.
        if env.PYPY and env.PY2:
            self.skipTest("PyPy2 doesn't run bare .pyc files")

        # Create two Python files.
        self.make_file("mod.py", "a = 1\n")
        self.make_file("main.py", "import mod\n")

        # Make one into a .pyc, and remove the .py.
        py_compile.compile("mod.py")
        os.remove("mod.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("mod.pyc"):
            pycs = glob.glob("__pycache__/mod.*.pyc")
            self.assertEqual(len(pycs), 1)
            os.rename(pycs[0], "mod.pyc")

        # Run the program.
        cov = coverage.Coverage()
        cov.start()
>       import main     # pragma: nested # pylint: disable=import-error, unused-variable

tests\test_summary.py:575:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
C:\Users\Bruno\AppData\Local\Temp\test_cover_15357924\main.py:1: in <module>
    ???
<frozen importlib._bootstrap>:969: in _find_and_load
    ???
<frozen importlib._bootstrap>:954: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:892: in _find_spec
    ???
<frozen importlib._bootstrap>:873: in _find_spec_legacy
    ???
.tox\py35\lib\site-packages\_pytest\assertion\rewrite.py:82: in find_module
    fn = imp.source_from_cache(fn)
.tox\py35\lib\imp.py:102: in source_from_cache
    return util.source_from_cache(path)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

path = 'C:\\Users\\Bruno\\AppData\\Local\\Temp\\test_cover_15357924\\mod.pyc'

>   ???
E   ValueError: __pycache__ not bottom-level directory in 'C:\\Users\\Bruno\\AppData\\Local\\Temp\\test_cover_15357924\\mod.pyc'

<frozen importlib._bootstrap_external>:302: ValueError
==================== 1 failed, 33 passed in 39.60 seconds =====================
@nedbat

This comment has been minimized.

Contributor

nedbat commented Nov 8, 2016

Is there a way to disable assert rewriting for just one test?

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Nov 8, 2016

Not AFAIK...

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Nov 8, 2016

And sadly I can't think of a workaround to recommend either.

@nedbat

This comment has been minimized.

Contributor

nedbat commented Nov 20, 2016

Turns out it wasn't hard to fix: #2070

This was referenced Mar 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment