Skip to content

Commit 9bcdd60

Browse files
authored
Merge pull request #24178 from QuLogic/pypy-threads
Fall back to Python-level Thread for GUI warning
2 parents 8be23b6 + d4cf0f7 commit 9bcdd60

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lib/matplotlib/pyplot.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,21 @@ def draw_if_interactive():
332332

333333

334334
def _warn_if_gui_out_of_main_thread():
335-
# This compares native thread ids because even if python-level Thread
336-
# objects match, the underlying OS thread (which is what really matters)
337-
# may be different on Python implementations with green threads.
338-
if (_get_required_interactive_framework(_get_backend_mod()) and
339-
threading.get_native_id() != threading.main_thread().native_id):
335+
warn = False
336+
if _get_required_interactive_framework(_get_backend_mod()):
337+
if hasattr(threading, 'get_native_id'):
338+
# This compares native thread ids because even if Python-level
339+
# Thread objects match, the underlying OS thread (which is what
340+
# really matters) may be different on Python implementations with
341+
# green threads.
342+
if threading.get_native_id() != threading.main_thread().native_id:
343+
warn = True
344+
else:
345+
# Fall back to Python-level Thread if native IDs are unavailable,
346+
# mainly for PyPy.
347+
if threading.current_thread() is not threading.main_thread():
348+
warn = True
349+
if warn:
340350
_api.warn_external(
341351
"Starting a Matplotlib GUI outside of the main thread will likely "
342352
"fail.")

0 commit comments

Comments
 (0)