Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fffeb9b

Browse files
committed
BUG: fix IPython's %pylab mode detection
There is no pyplot module in the system registry, as the module is imported as matplotlib.pyplot. This code tries to detect when matplotlib.pyplot.show() is invoked in a script run via the %run IPython's magic while in interactive mode as enabled by the %pylab or %matplotlib IPyhton's magic commands. In these modes, matplotlib.pyplot.show() should not block. Failing do detect these modes, a simple test.py script as this import matplotlib.pyplot as plt plt.figure() plt.show() run in IPython as follows %matplotlib %run test.py is not expected to block on matplotlib.pyplot.show(). Without this patch it does. Additionally, the redraw implemented by IPython at the end of %run caused an empty figure to be displayed when the script terminates. Fixing the module lookup in sys.modules fixes the issue. Fixes 86f26a0. Fixes #25485.
1 parent 78bf53c commit fffeb9b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,8 +2930,8 @@ def pyplot_show(cls, *, block=None):
29302930
# Hack: Are we in IPython's %pylab mode? In pylab mode, IPython
29312931
# (>= 0.10) tacks a _needmain attribute onto pyplot.show (always
29322932
# set to False).
2933-
ipython_pylab = hasattr(
2934-
getattr(sys.modules.get("pyplot"), "show", None), "_needmain")
2933+
pyplot_show = getattr(sys.modules.get("matplotlib.pyplot"), "show", None)
2934+
ipython_pylab = hasattr(pyplot_show, "_needmain")
29352935
block = not ipython_pylab and not is_interactive()
29362936
if block:
29372937
cls.start_main_loop()
@@ -3645,8 +3645,8 @@ def show(cls, *, block=None):
36453645
# Hack: Are we in IPython's %pylab mode? In pylab mode, IPython
36463646
# (>= 0.10) tacks a _needmain attribute onto pyplot.show (always
36473647
# set to False).
3648-
ipython_pylab = hasattr(
3649-
getattr(sys.modules.get("pyplot"), "show", None), "_needmain")
3648+
pyplot_show = getattr(sys.modules.get("matplotlib.pyplot"), "show", None)
3649+
ipython_pylab = hasattr(pyplot_show, "_needmain")
36503650
block = not ipython_pylab and not is_interactive()
36513651
if block:
36523652
cls.mainloop()

0 commit comments

Comments
 (0)