@@ -54,14 +54,14 @@ def test_fig_close():
5454 assert 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
6262 def __init__ (self , * args , ** kwargs ):
6363 if sys .platform == 'win32' :
64- kwargs ['creationflags' ] = subprocess .CREATE_NEW_PROCESS_GROUP
64+ kwargs ['creationflags' ] = subprocess .CREATE_NEW_CONSOLE
6565 super ().__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):
8080 if buf .endswith (terminator ):
8181 return
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 ):
9284 import sys
9385 import matplotlib .pyplot as plt
86+ import os
87+ import threading
88+
9489 plt .switch_backend (backend )
9590 from 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 )
99101 fig = 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+ )
102110 try :
103111 target (** kwargs )
104112 except KeyboardInterrupt :
@@ -112,13 +120,12 @@ def _test_sigint_impl(backend, target_name, kwargs):
112120])
113121def test_sigint (target , kwargs ):
114122 backend = plt .get_backend ()
115- proc = InterruptiblePopen (
123+ proc = WaitForStringPopen (
116124 [sys .executable , "-c" ,
117125 inspect .getsource (_test_sigint_impl ) +
118126 f"\n _test_sigint_impl({ backend !r} , { target !r} , { kwargs !r} )" ])
119127 try :
120128 proc .wait_for ('DRAW' )
121- proc .interrupt ()
122129 stdout , _ = proc .communicate (timeout = _test_timeout )
123130 except :
124131 proc .kill ()
@@ -164,7 +171,7 @@ def custom_signal_handler(signum, frame):
164171])
165172def test_other_signal_before_sigint (target , kwargs ):
166173 backend = plt .get_backend ()
167- proc = InterruptiblePopen (
174+ proc = WaitForStringPopen (
168175 [sys .executable , "-c" ,
169176 inspect .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):
173180 proc .wait_for ('DRAW' )
174181 os .kill (proc .pid , signal .SIGUSR1 )
175182 proc .wait_for ('SIGUSR1' )
176- proc . interrupt ( )
183+ os . kill ( proc . pid , signal . SIGINT )
177184 stdout , _ = proc .communicate (timeout = _test_timeout )
178185 except :
179186 proc .kill ()
0 commit comments