@@ -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,35 @@ 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
83
91
84
def _test_sigint_impl (backend , target_name , kwargs ):
92
85
import sys
93
86
import matplotlib .pyplot as plt
87
+ import os
88
+ import threading
89
+
94
90
plt .switch_backend (backend )
95
91
from matplotlib .backends .qt_compat import QtCore
96
92
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 )
98
100
101
+ target = getattr (plt , target_name )
102
+ timer = threading .Timer (1 , interupter )
99
103
fig = 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
+ )
102
112
try :
103
113
target (** kwargs )
104
114
except KeyboardInterrupt :
@@ -112,13 +122,12 @@ def _test_sigint_impl(backend, target_name, kwargs):
112
122
])
113
123
def test_sigint (target , kwargs ):
114
124
backend = plt .get_backend ()
115
- proc = InterruptiblePopen (
125
+ proc = WaitForStringPopen (
116
126
[sys .executable , "-c" ,
117
127
inspect .getsource (_test_sigint_impl ) +
118
128
f"\n _test_sigint_impl({ backend !r} , { target !r} , { kwargs !r} )" ])
119
129
try :
120
130
proc .wait_for ('DRAW' )
121
- proc .interrupt ()
122
131
stdout , _ = proc .communicate (timeout = _test_timeout )
123
132
except :
124
133
proc .kill ()
@@ -164,7 +173,7 @@ def custom_signal_handler(signum, frame):
164
173
])
165
174
def test_other_signal_before_sigint (target , kwargs ):
166
175
backend = plt .get_backend ()
167
- proc = InterruptiblePopen (
176
+ proc = WaitForStringPopen (
168
177
[sys .executable , "-c" ,
169
178
inspect .getsource (_test_other_signal_before_sigint_impl ) +
170
179
"\n _test_other_signal_before_sigint_impl("
@@ -173,7 +182,7 @@ def test_other_signal_before_sigint(target, kwargs):
173
182
proc .wait_for ('DRAW' )
174
183
os .kill (proc .pid , signal .SIGUSR1 )
175
184
proc .wait_for ('SIGUSR1' )
176
- proc . interrupt ( )
185
+ os . kill ( proc . pid , signal . SIGINT )
177
186
stdout , _ = proc .communicate (timeout = _test_timeout )
178
187
except :
179
188
proc .kill ()
0 commit comments