@@ -54,14 +54,14 @@ def test_fig_close():
54
54
assert init_figs == Gcf .figs
55
55
56
56
57
- class InterruptiblePopen (subprocess .Popen ):
57
+ class WaitForStringPopen (subprocess .Popen ):
58
58
"""
59
59
A Popen that passes flags that allow triggering KeyboardInterrupt.
60
60
"""
61
61
62
62
def __init__ (self , * args , ** kwargs ):
63
63
if sys .platform == 'win32' :
64
- kwargs ['creationflags' ] = subprocess .CREATE_NEW_PROCESS_GROUP
64
+ kwargs ['creationflags' ] = subprocess .CREATE_NEW_CONSOLE
65
65
super ().__init__ (
66
66
* args , ** kwargs ,
67
67
# Force Agg so that each test can switch to its desired Qt backend.
@@ -80,25 +80,33 @@ def wait_for(self, terminator):
80
80
if buf .endswith (terminator ):
81
81
return
82
82
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
-
91
83
def _test_sigint_impl (backend , target_name , kwargs ):
92
84
import sys
93
85
import matplotlib .pyplot as plt
86
+ import os
87
+ import threading
88
+
94
89
plt .switch_backend (backend )
95
90
from matplotlib .backends .qt_compat import QtCore
96
91
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 )
98
98
99
+ target = getattr (plt , target_name )
100
+ timer = threading .Timer (1 , interupter )
99
101
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
+ )
102
110
try :
103
111
target (** kwargs )
104
112
except KeyboardInterrupt :
@@ -112,13 +120,12 @@ def _test_sigint_impl(backend, target_name, kwargs):
112
120
])
113
121
def test_sigint (target , kwargs ):
114
122
backend = plt .get_backend ()
115
- proc = InterruptiblePopen (
123
+ proc = WaitForStringPopen (
116
124
[sys .executable , "-c" ,
117
125
inspect .getsource (_test_sigint_impl ) +
118
126
f"\n _test_sigint_impl({ backend !r} , { target !r} , { kwargs !r} )" ])
119
127
try :
120
128
proc .wait_for ('DRAW' )
121
- proc .interrupt ()
122
129
stdout , _ = proc .communicate (timeout = _test_timeout )
123
130
except :
124
131
proc .kill ()
@@ -164,7 +171,7 @@ def custom_signal_handler(signum, frame):
164
171
])
165
172
def test_other_signal_before_sigint (target , kwargs ):
166
173
backend = plt .get_backend ()
167
- proc = InterruptiblePopen (
174
+ proc = WaitForStringPopen (
168
175
[sys .executable , "-c" ,
169
176
inspect .getsource (_test_other_signal_before_sigint_impl ) +
170
177
"\n _test_other_signal_before_sigint_impl("
@@ -173,7 +180,7 @@ def test_other_signal_before_sigint(target, kwargs):
173
180
proc .wait_for ('DRAW' )
174
181
os .kill (proc .pid , signal .SIGUSR1 )
175
182
proc .wait_for ('SIGUSR1' )
176
- proc . interrupt ( )
183
+ os . kill ( proc . pid , signal . SIGINT )
177
184
stdout , _ = proc .communicate (timeout = _test_timeout )
178
185
except :
179
186
proc .kill ()
0 commit comments