Skip to content

Collection hangs on Windows when importing from test module #3650

Open
@The-Compiler

Description

@The-Compiler

On Windows, when trying to import from Python's test module, collection seems to hang because assertion rewriting kicks in and the __pycache__ folder isn't writable.

With a file like:

from test import test_file

def test_foo():
    pass

I get a hang. When pressing ctrl-c, I get a big stacktrace, with the relevant part probably being:

PS C:\Users\Florian\proj> pytest .\test_file.py --fulltrace -v
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.2, py-1.5.3, pluggy-0.6.0 -- c:\program files\python36\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\Florian\proj, inifile:
collecting 0 items
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! KeyboardInterrupt !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
name = 'test.test_file', path = ['c:\\program files\\python36\\lib\\test']
target = None
 
>   ???
E   AttributeError: 'AssertionRewritingHook' object has no attribute 'find_spec'
 
<frozen importlib._bootstrap>:888: AttributeError
 
During handling of the above exception, another exception occurred:
 
dir = 'c:\\program files\\python36\\lib\\test\\__pycache__', pre = 'tmp'
suf = '', flags = 34050, output_type = <class 'str'>
 
    def _mkstemp_inner(dir, pre, suf, flags, output_type):
        """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile."""
 
        names = _get_candidate_names()
        if output_type is bytes:
            names = map(_os.fsencode, names)
 
        for seq in range(TMP_MAX):
            name = next(names)
            file = _os.path.join(dir, pre + name + suf)
            try:
>               fd = _os.open(file, flags, 0o600)
E               PermissionError: [Errno 13] Permission denied: 'c:\\program files\\python36\\lib\\test\\__pycache__\\tmppnxn1ld2'
 
c:\program files\python36\lib\tempfile.py:260: PermissionError
 
During handling of the above exception, another exception occurred:
 
[...]
 
c:\program files\python36\lib\site-packages\_pytest\assertion\rewrite.py:146:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
 
state = <_pytest.assertion.AssertionState object at 0x00000273A3FBA400>
co = <code object <module> at 0x00000273A446C030, file "c:\program files\python36\lib\test\test_file.py", line 1>
source_stat = <py._path.local.Stat object at 0x00000273A4458358>
pyc = 'c:\\program files\\python36\\lib\\test\\__pycache__\\test_file.cpython-36-PYTEST.pyc'
 
    def _write_pyc(state, co, source_stat, 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
        # sometime to be able to use imp.load_compiled to load them. (See
        # the comment in load_module above.)
        try:
>           with atomicwrites.atomic_write(pyc, mode="wb", overwrite=True) as fp:
 
c:\program files\python36\lib\site-packages\_pytest\assertion\rewrite.py:265:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
 
[...]
 
c:\program files\python36\lib\tempfile.py:549:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
 
dir = 'c:\\program files\\python36\\lib\\test\\__pycache__', pre = 'tmp'
suf = '', flags = 34050, output_type = <class 'str'>
 
    def _mkstemp_inner(dir, pre, suf, flags, output_type):
        """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile."""
 
        names = _get_candidate_names()
        if output_type is bytes:
            names = map(_os.fsencode, names)
 
        for seq in range(TMP_MAX):
            name = next(names)
            file = _os.path.join(dir, pre + name + suf)
            try:
                fd = _os.open(file, flags, 0o600)
            except FileExistsError:
                continue    # try again
            except PermissionError:
                # This exception is thrown when a directory with the chosen name
                # already exists on windows.
>               if (_os.name == 'nt' and _os.path.isdir(dir) and
                    _os.access(dir, _os.W_OK)):
E                   KeyboardInterrupt
 
c:\program files\python36\lib\tempfile.py:266: KeyboardInterrupt
======================== no tests ran in 7.18 seconds =========================

Note the name of the import doesn't make a difference, i.e. when I do import test_file as tf I get the same issue.

Is there some way I can disable assertion rewriting for that module in the meantime, essentially the opposite of pytest.register_assert_rewrite?

Metadata

Metadata

Assignees

No one assigned

    Labels

    platform: cygwincygwin platform-specific problemtype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions