Skip to content

Commit 9180607

Browse files
test for thread safety
1 parent b63950e commit 9180607

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/matplotlib/backends/_backend_tk.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ def _blit(argsid):
5454
Thin wrapper to pass arguments to blit via tkapp.call
5555
"""
5656
photoimage, dataptr, offsets, bboxptr, blank = _blit_args.pop(argsid)
57-
args = (
58-
photoimage.tk.interpaddr(), str(photoimage), dataptr, offsets, bboxptr)
5957
if blank:
6058
photoimage.blank()
61-
_tkagg.blit(*args)
59+
_tkagg.blit(
60+
photoimage.tk.interpaddr(), str(photoimage), dataptr, offsets, bboxptr)
6261

6362

6463
def blit(photoimage, aggimage, offsets, bbox=None):

lib/matplotlib/tests/test_backends_interactive.py

+23
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def _get_testable_interactive_backends():
7272
import io
7373
import json
7474
import sys
75+
import threading
7576
from unittest import TestCase
7677
7778
import matplotlib as mpl
@@ -146,6 +147,28 @@ def check_alt_backend(alt_backend):
146147
# FIXME: This should be enabled everywhere once Qt5 is fixed on macOS to
147148
# not resize incorrectly.
148149
assert_equal(result.getvalue(), result_after.getvalue())
150+
151+
# Test artists and drawing does not crash from thread (no other guarantees)
152+
fig, ax = plt.subplots()
153+
# plt.pause needed vs plt.show(block=False) at least on toolbar2-tkagg
154+
plt.pause(0.1)
155+
156+
def thread_artist_work():
157+
ax.plot([1,3,6])
158+
159+
def thread_draw_work():
160+
fig.canvas.draw()
161+
fig.canvas.stop_event_loop()
162+
163+
t = threading.Thread(target=thread_artist_work)
164+
t.start()
165+
# artists never wait for the event loop to run, so just join
166+
t.join()
167+
168+
t = threading.Thread(target=thread_draw_work)
169+
t.start()
170+
fig.canvas.start_event_loop()
171+
t.join()
149172
"""
150173
_test_timeout = 10 # Empirically, 1s is not enough on Travis.
151174

0 commit comments

Comments
 (0)