@@ -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,33 @@ 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-
90-
9183def _test_sigint_impl (backend ,target_name ,kwargs ):
9284import sys
9385import matplotlib .pyplot as plt
86+ import os
87+ import threading
88+
9489plt .switch_backend (backend )
9590from matplotlib .backends .qt_compat import QtCore
9691
97- target = getattr (plt ,target_name )
92+ def interupter ():
93+ if sys .platform == 'win32' :
94+ import win32api
95+ win32api .GenerateConsoleCtrlEvent (0 ,0 )
96+ else :
97+ os .kill (os .getpid (),signal .SIGINT )
9898
99+ target = getattr (plt ,target_name )
100+ timer = threading .Timer (1 ,interupter )
99101fig = plt .figure ()
100- fig .canvas .mpl_connect ('draw_event' ,
101- lambda * args :print ('DRAW' ,flush = True ))
102+ fig .canvas .mpl_connect (
103+ 'draw_event' ,
104+ lambda * args :print ('DRAW' ,flush = True )
105+ )
106+ fig .canvas .mpl_connect (
107+ 'draw_event' ,
108+ lambda * args :timer .start ()
109+ )
102110try :
103111target (** kwargs )
104112except KeyboardInterrupt :
@@ -112,13 +120,12 @@ def _test_sigint_impl(backend, target_name, kwargs):
112120])
113121def test_sigint (target ,kwargs ):
114122backend = plt .get_backend ()
115- proc = InterruptiblePopen (
123+ proc = WaitForStringPopen (
116124 [sys .executable ,"-c" ,
117125inspect .getsource (_test_sigint_impl )+
118126f"\n _test_sigint_impl({ backend !r} ,{ target !r} ,{ kwargs !r} )" ])
119127try :
120128proc .wait_for ('DRAW' )
121- proc .interrupt ()
122129stdout ,_ = proc .communicate (timeout = _test_timeout )
123130except :
124131proc .kill ()
@@ -164,7 +171,7 @@ def custom_signal_handler(signum, frame):
164171])
165172def test_other_signal_before_sigint (target ,kwargs ):
166173backend = plt .get_backend ()
167- proc = InterruptiblePopen (
174+ proc = WaitForStringPopen (
168175 [sys .executable ,"-c" ,
169176inspect .getsource (_test_other_signal_before_sigint_impl )+
170177"\n _test_other_signal_before_sigint_impl("
@@ -173,7 +180,7 @@ def test_other_signal_before_sigint(target, kwargs):
173180proc .wait_for ('DRAW' )
174181os .kill (proc .pid ,signal .SIGUSR1 )
175182proc .wait_for ('SIGUSR1' )
176- proc . interrupt ( )
183+ os . kill ( proc . pid , signal . SIGINT )
177184stdout ,_ = proc .communicate (timeout = _test_timeout )
178185except :
179186proc .kill ()