1+ import threading
2+ import time
13import tkinter
24
3- import pytest
45import numpy as np
6+ import pytest
7+
58from matplotlib import pyplot as plt
69
710
@@ -33,9 +36,10 @@ def evil_blit(photoimage, aggimage, offsets, bboxptr):
3336@pytest .mark .backend ('TkAgg' , skip_on_importerror = True )
3437def test_figuremanager_preserves_host_mainloop ():
3538 success = False
39+
3640 def do_plot ():
3741 plt .figure ()
38- plt .plot ([1 ,2 ],[3 ,5 ])
42+ plt .plot ([1 , 2 ], [3 , 5 ])
3943 plt .close ()
4044 root .after (0 , legitmate_quit )
4145
@@ -49,3 +53,28 @@ def legitmate_quit():
4953 root .mainloop ()
5054
5155 assert success
56+
57+
58+ @pytest .mark .backend ('TkAgg' , skip_on_importerror = True )
59+ def test_figuremanager_cleans_own_mainloop ():
60+ root = tkinter .Tk ()
61+ plt .plot ([1 , 2 , 3 ], [1 , 2 , 5 ])
62+ can_detect_mainloop = False
63+ thread_died_before_quit = True
64+
65+ def target ():
66+ nonlocal can_detect_mainloop
67+ nonlocal thread_died_before_quit
68+ from matplotlib .cbook import _get_running_interactive_framework
69+
70+ time .sleep (0.1 ) # should poll for mainloop being up
71+ can_detect_mainloop = 'tk' == _get_running_interactive_framework ()
72+ plt .close ()
73+ time .sleep (0.1 ) # should poll for mainloop going down
74+ root .quit ()
75+ thread_died_before_quit = False
76+
77+ threading .Thread (target = target , daemon = True ).start ()
78+ plt .show (block = True )
79+ assert can_detect_mainloop
80+ assert thread_died_before_quit
0 commit comments