Skip to content

Commit 8105e60

Browse files
authored
Merge pull request #7937 from bluetech/testing-fixes
testing: fix pexpect hang
2 parents 65e6e39 + ca82214 commit 8105e60

File tree

7 files changed

+226
-183
lines changed

7 files changed

+226
-183
lines changed

changelog/7913.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a crash or hang in ``pytester.spawn`` when the ``readline`` module is involved.

src/_pytest/pytester.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,11 @@ def __take_sys_modules_snapshot(self) -> SysModulesSnapshot:
707707
# Some zope modules used by twisted-related tests keep internal state
708708
# and can't be deleted; we had some trouble in the past with
709709
# `zope.interface` for example.
710+
#
711+
# Preserve readline due to https://bugs.python.org/issue41033.
712+
# pexpect issues a SIGWINCH.
710713
def preserve_module(name):
711-
return name.startswith("zope")
714+
return name.startswith(("zope", "readline"))
712715

713716
return SysModulesSnapshot(preserve=preserve_module)
714717

testing/test_assertrewrite.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,8 @@ def test_rewritten():
786786
sub.chmod(old_mode)
787787

788788
def test_dont_write_bytecode(self, testdir, monkeypatch):
789+
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
790+
789791
testdir.makepyfile(
790792
"""
791793
import os
@@ -797,7 +799,10 @@ def test_no_bytecode():
797799
monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1")
798800
assert testdir.runpytest_subprocess().ret == 0
799801

800-
def test_orphaned_pyc_file(self, testdir):
802+
def test_orphaned_pyc_file(self, testdir, monkeypatch):
803+
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
804+
monkeypatch.setattr(sys, "pycache_prefix", None, raising=False)
805+
801806
testdir.makepyfile(
802807
"""
803808
import orphan
@@ -826,6 +831,7 @@ def test_it():
826831
def test_cached_pyc_includes_pytest_version(self, testdir, monkeypatch):
827832
"""Avoid stale caches (#1671)"""
828833
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
834+
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
829835
testdir.makepyfile(
830836
test_foo="""
831837
def test_foo():
@@ -852,11 +858,13 @@ def test_optimized():
852858
tmp = "--basetemp=%s" % p
853859
monkeypatch.setenv("PYTHONOPTIMIZE", "2")
854860
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
861+
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
855862
assert testdir.runpytest_subprocess(tmp).ret == 0
856863
tagged = "test_pyc_vs_pyo." + PYTEST_TAG
857864
assert tagged + ".pyo" in os.listdir("__pycache__")
858865
monkeypatch.undo()
859866
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
867+
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
860868
assert testdir.runpytest_subprocess(tmp).ret == 1
861869
assert tagged + ".pyc" in os.listdir("__pycache__")
862870

@@ -1592,10 +1600,11 @@ class TestPyCacheDir:
15921600
],
15931601
)
15941602
def test_get_cache_dir(self, monkeypatch, prefix, source, expected):
1595-
if prefix:
1596-
if sys.version_info < (3, 8):
1597-
pytest.skip("pycache_prefix not available in py<38")
1598-
monkeypatch.setattr(sys, "pycache_prefix", prefix)
1603+
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
1604+
1605+
if prefix is not None and sys.version_info < (3, 8):
1606+
pytest.skip("pycache_prefix not available in py<38")
1607+
monkeypatch.setattr(sys, "pycache_prefix", prefix, raising=False)
15991608

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

0 commit comments

Comments
 (0)