1- import pytest
1+ import os
2+ import subprocess
3+ import sys
4+ import tkinter
5+
26import numpy as np
7+ import pytest
8+
39from matplotlib import pyplot as plt
410
511
@@ -26,3 +32,71 @@ def evil_blit(photoimage, aggimage, offsets, bboxptr):
2632 np .ones ((4 , 4 , 4 )),
2733 (0 , 1 , 2 , 3 ),
2834 bad_boxes )
35+
36+
37+ @pytest .mark .backend ('TkAgg' , skip_on_importerror = True )
38+ def test_figuremanager_preserves_host_mainloop ():
39+ success = False
40+
41+ def do_plot ():
42+ plt .figure ()
43+ plt .plot ([1 , 2 ], [3 , 5 ])
44+ plt .close ()
45+ root .after (0 , legitimate_quit )
46+
47+ def legitimate_quit ():
48+ root .quit ()
49+ nonlocal success
50+ success = True
51+
52+ root = tkinter .Tk ()
53+ root .after (0 , do_plot )
54+ root .mainloop ()
55+
56+ assert success
57+
58+
59+ @pytest .mark .backend ('TkAgg' , skip_on_importerror = True )
60+ @pytest .mark .flaky (reruns = 3 )
61+ def test_figuremanager_cleans_own_mainloop ():
62+ script = '''
63+ import tkinter
64+ import time
65+ import matplotlib.pyplot as plt
66+ import threading
67+ from matplotlib.cbook import _get_running_interactive_framework
68+
69+ root = tkinter.Tk()
70+ plt.plot([1, 2, 3], [1, 2, 5])
71+
72+ def target():
73+ while not 'tk' == _get_running_interactive_framework():
74+ time.sleep(.01)
75+ plt.close()
76+ if show_finished_event.wait():
77+ print('success')
78+
79+ show_finished_event = threading.Event()
80+ thread = threading.Thread(target=target, daemon=True)
81+ thread.start()
82+ plt.show(block=True) # testing if this function hangs
83+ show_finished_event.set()
84+ thread.join()
85+
86+ '''
87+ try :
88+ proc = subprocess .run (
89+ [sys .executable , "-c" , script ],
90+ env = {** os .environ ,
91+ "MPLBACKEND" : "TkAgg" ,
92+ "SOURCE_DATE_EPOCH" : "0" },
93+ timeout = 10 ,
94+ stdout = subprocess .PIPE ,
95+ universal_newlines = True ,
96+ check = True
97+ )
98+ except subprocess .TimeoutExpired :
99+ pytest .fail ("Most likely plot.show(block=True) hung" )
100+ except subprocess .CalledProcessError :
101+ pytest .fail ("Subprocess failed to test intended behavior" )
102+ assert proc .stdout .count ("success" ) == 1
0 commit comments