Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/7913.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a crash or hang in ``pytester.spawn`` when the ``readline`` module is involved.
5 changes: 4 additions & 1 deletion src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,11 @@ def __take_sys_modules_snapshot(self) -> SysModulesSnapshot:
# Some zope modules used by twisted-related tests keep internal state
# and can't be deleted; we had some trouble in the past with
# `zope.interface` for example.
#
# Preserve readline due to https://bugs.python.org/issue41033.
# pexpect issues a SIGWINCH.
def preserve_module(name):
return name.startswith("zope")
return name.startswith(("zope", "readline"))

return SysModulesSnapshot(preserve=preserve_module)

Expand Down
19 changes: 14 additions & 5 deletions testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,8 @@ def test_rewritten():
sub.chmod(old_mode)

def test_dont_write_bytecode(self, testdir, monkeypatch):
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)

testdir.makepyfile(
"""
import os
Expand All @@ -797,7 +799,10 @@ def test_no_bytecode():
monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1")
assert testdir.runpytest_subprocess().ret == 0

def test_orphaned_pyc_file(self, testdir):
def test_orphaned_pyc_file(self, testdir, monkeypatch):
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
monkeypatch.setattr(sys, "pycache_prefix", None, raising=False)

testdir.makepyfile(
"""
import orphan
Expand Down Expand Up @@ -826,6 +831,7 @@ def test_it():
def test_cached_pyc_includes_pytest_version(self, testdir, monkeypatch):
"""Avoid stale caches (#1671)"""
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
testdir.makepyfile(
test_foo="""
def test_foo():
Expand All @@ -852,11 +858,13 @@ def test_optimized():
tmp = "--basetemp=%s" % p
monkeypatch.setenv("PYTHONOPTIMIZE", "2")
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
assert testdir.runpytest_subprocess(tmp).ret == 0
tagged = "test_pyc_vs_pyo." + PYTEST_TAG
assert tagged + ".pyo" in os.listdir("__pycache__")
monkeypatch.undo()
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
assert testdir.runpytest_subprocess(tmp).ret == 1
assert tagged + ".pyc" in os.listdir("__pycache__")

Expand Down Expand Up @@ -1592,10 +1600,11 @@ class TestPyCacheDir:
],
)
def test_get_cache_dir(self, monkeypatch, prefix, source, expected):
if prefix:
if sys.version_info < (3, 8):
pytest.skip("pycache_prefix not available in py<38")
monkeypatch.setattr(sys, "pycache_prefix", prefix)
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)

if prefix is not None and sys.version_info < (3, 8):
pytest.skip("pycache_prefix not available in py<38")
monkeypatch.setattr(sys, "pycache_prefix", prefix, raising=False)

assert get_cache_dir(Path(source)) == Path(expected)

Expand Down
Loading