@@ -54,14 +54,14 @@ def test_fig_close():
5454assert init_figs == Gcf .figs
5555
5656
57- class InterruptiblePopen (subprocess .Popen ):
57+ class WaitForStringPopen (subprocess .Popen ):
5858"""
5959 A Popen that passes flags that allow triggering KeyboardInterrupt.
6060 """
6161
6262def __init__ (self ,* args ,** kwargs ):
6363if sys .platform == 'win32' :
64- kwargs ['creationflags' ]= subprocess .CREATE_NEW_PROCESS_GROUP
64+ kwargs ['creationflags' ]= subprocess .CREATE_NEW_CONSOLE
6565super ().__init__ (
6666* args ,** kwargs ,
6767# Force Agg so that each test can switch to its desired Qt backend.
@@ -80,25 +80,35 @@ def wait_for(self, terminator):
8080if buf .endswith (terminator ):
8181return
8282
83- def interrupt (self ):
84- """Interrupt process in a platform-specific way."""
85- if sys .platform == 'win32' :
86- self .send_signal (signal .CTRL_C_EVENT )
87- else :
88- self .send_signal (signal .SIGINT )
89-
9083
9184def _test_sigint_impl (backend ,target_name ,kwargs ):
9285import sys
9386import matplotlib .pyplot as plt
87+ import os
88+ import threading
89+
9490plt .switch_backend (backend )
9591from matplotlib .backends .qt_compat import QtCore
9692
97- target = getattr (plt ,target_name )
93+ def interupter ():
94+ if sys .platform == 'win32' :
95+ import win32api
96+ win32api .GenerateConsoleCtrlEvent (0 ,0 )
97+ else :
98+ import signal
99+ os .kill (os .getpid (),signal .SIGINT )
98100
101+ target = getattr (plt ,target_name )
102+ timer = threading .Timer (1 ,interupter )
99103fig = plt .figure ()
100- fig .canvas .mpl_connect ('draw_event' ,
101- lambda * args :print ('DRAW' ,flush = True ))
104+ fig .canvas .mpl_connect (
105+ 'draw_event' ,
106+ lambda * args :print ('DRAW' ,flush = True )
107+ )
108+ fig .canvas .mpl_connect (
109+ 'draw_event' ,
110+ lambda * args :timer .start ()
111+ )
102112try :
103113target (** kwargs )
104114except KeyboardInterrupt :
@@ -112,13 +122,12 @@ def _test_sigint_impl(backend, target_name, kwargs):
112122])
113123def test_sigint (target ,kwargs ):
114124backend = plt .get_backend ()
115- proc = InterruptiblePopen (
125+ proc = WaitForStringPopen (
116126 [sys .executable ,"-c" ,
117127inspect .getsource (_test_sigint_impl )+
118128f"\n _test_sigint_impl({ backend !r} ,{ target !r} ,{ kwargs !r} )" ])
119129try :
120130proc .wait_for ('DRAW' )
121- proc .interrupt ()
122131stdout ,_ = proc .communicate (timeout = _test_timeout )
123132except :
124133proc .kill ()
@@ -164,7 +173,7 @@ def custom_signal_handler(signum, frame):
164173])
165174def test_other_signal_before_sigint (target ,kwargs ):
166175backend = plt .get_backend ()
167- proc = InterruptiblePopen (
176+ proc = WaitForStringPopen (
168177 [sys .executable ,"-c" ,
169178inspect .getsource (_test_other_signal_before_sigint_impl )+
170179"\n _test_other_signal_before_sigint_impl("
@@ -173,7 +182,7 @@ def test_other_signal_before_sigint(target, kwargs):
173182proc .wait_for ('DRAW' )
174183os .kill (proc .pid ,signal .SIGUSR1 )
175184proc .wait_for ('SIGUSR1' )
176- proc . interrupt ( )
185+ os . kill ( proc . pid , signal . SIGINT )
177186stdout ,_ = proc .communicate (timeout = _test_timeout )
178187except :
179188proc .kill ()