Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c8b3e18

Browse files
committed
TST: Wrap _WaitForStringPopen in a context manager
We do attempt to kill the subprocess in all cases, but this should also ensure that file descriptors are closed.
1 parent e6640db commit c8b3e18

1 file changed

Lines changed: 25 additions & 27 deletions

File tree

lib/matplotlib/tests/test_backends_interactive.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -737,18 +737,17 @@ def test_sigint(env, target, kwargs):
737737
backend = env.get("MPLBACKEND")
738738
if not backend.startswith(("qt", "macosx")):
739739
pytest.skip("SIGINT currently only tested on qt and macosx")
740-
proc = _WaitForStringPopen(
741-
[sys.executable, "-c",
742-
inspect.getsource(_test_sigint_impl) +
743-
f"\n_test_sigint_impl({backend!r}, {target!r}, {kwargs!r})"])
744-
try:
745-
proc.wait_for('DRAW')
746-
stdout, _ = proc.communicate(timeout=_test_timeout)
747-
except Exception:
748-
proc.kill()
749-
stdout, _ = proc.communicate()
750-
raise
751-
assert 'SUCCESS' in stdout
740+
source = (inspect.getsource(_test_sigint_impl) +
741+
f"\n_test_sigint_impl({backend!r}, {target!r}, {kwargs!r})")
742+
with _WaitForStringPopen([sys.executable, "-c", source]) as proc:
743+
try:
744+
proc.wait_for('DRAW')
745+
stdout, _ = proc.communicate(timeout=_test_timeout)
746+
except Exception:
747+
proc.kill()
748+
stdout, _ = proc.communicate()
749+
raise
750+
assert 'SUCCESS' in stdout
752751

753752

754753
def _test_other_signal_before_sigint_impl(backend, target_name, kwargs):
@@ -796,20 +795,19 @@ def test_other_signal_before_sigint(env, target, kwargs, request):
796795
# https://github.com/matplotlib/matplotlib/issues/27984
797796
request.node.add_marker(
798797
pytest.mark.xfail(reason="Qt backend is buggy on macOS"))
799-
proc = _WaitForStringPopen(
800-
[sys.executable, "-c",
801-
inspect.getsource(_test_other_signal_before_sigint_impl) +
802-
"\n_test_other_signal_before_sigint_impl("
803-
f"{backend!r}, {target!r}, {kwargs!r})"])
804-
try:
805-
proc.wait_for('DRAW')
806-
os.kill(proc.pid, signal.SIGUSR1)
807-
proc.wait_for('SIGUSR1')
808-
os.kill(proc.pid, signal.SIGINT)
809-
stdout, _ = proc.communicate(timeout=_test_timeout)
810-
except Exception:
811-
proc.kill()
812-
stdout, _ = proc.communicate()
813-
raise
798+
source = (inspect.getsource(_test_other_signal_before_sigint_impl) +
799+
"\n_test_other_signal_before_sigint_impl("
800+
f"{backend!r}, {target!r}, {kwargs!r})")
801+
with _WaitForStringPopen([sys.executable, "-c", source]) as proc:
802+
try:
803+
proc.wait_for('DRAW')
804+
os.kill(proc.pid, signal.SIGUSR1)
805+
proc.wait_for('SIGUSR1')
806+
os.kill(proc.pid, signal.SIGINT)
807+
stdout, _ = proc.communicate(timeout=_test_timeout)
808+
except Exception:
809+
proc.kill()
810+
stdout, _ = proc.communicate()
811+
raise
814812
print(stdout)
815813
assert 'SUCCESS' in stdout

0 commit comments

Comments
 (0)