Skip to content

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

Closed
@nedbat

Description

@nedbat

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugproblem that needs to be addressedtype: infrastructureimprovement to development/releases/CI structure

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions