New issue 720: Allow testing of bytecode-only modules in Python 3 https://bitbucket.org/pytest-dev/pytest/issue/720/allow-testing-of-bytecode-only-modules-in
Mike Müller: I am testing another programming language that eventually emits Python 3 (or PyPyp 3) bytecode. I would like to use `pytest` for testing. When I import `*.pyc` file somewhere in my test I get this error message: ``` #!shell from factorial import factorial <frozen importlib._bootstrap>:2237: in _find_and_load ??? <frozen importlib._bootstrap>:2222: in _find_and_load_unlocked ??? <frozen importlib._bootstrap>:2160: in _find_spec ??? <frozen importlib._bootstrap>:2141: in _find_spec_legacy ??? .../site-packages/_pytest/assertion/rewrite.py:75: in find_module fn = imp.source_from_cache(fn) ..../lib/python3.4/imp.py:100: in source_from_cache return util.source_from_cache(path) <frozen importlib._bootstrap>:479: in source_from_cache ??? E ValueError: __pycache__ not bottom-level directory in '.../tests/factorial.pyc' ``` I pinned down the problem in the file `assertion/rewrite.py`. This is the code that causes the exception (starting form line 73): ``` #!python if tp == imp.PY_COMPILED: if hasattr(imp, "source_from_cache"): fn = imp.source_from_cache(fn) else: fn = fn[:-1] elif tp != imp.PY_SOURCE: # Don't know what this is. return None ``` My very programmatic, and probably wrong, solution ist to return `None` if this exception occurs: ``` #!python if tp == imp.PY_COMPILED: if hasattr(imp, "source_from_cache"): try: fn = imp.source_from_cache(fn) except ValueError: return None else: fn = fn[:-1] ``` Is there another way to make bytecode-only files work? Could my solution above serve as a basis for a real solution? Maybe a command line option that switches on and off the behavior I want can be useful here. These are my versions: ``` platform darwin -- Python 3.4.3 -- py-1.4.26 -- pytest-2.7.0 plugins: xdist ``` _______________________________________________ pytest-commit mailing list pytest-commit@python.org https://mail.python.org/mailman/listinfo/pytest-commit