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

Skip to content

Commit f4efac1

Browse files
committed
MNT : add guards to only register repl hook once
Multiple calls to install_repl_displayhook are effectively no-ops.
1 parent a555475 commit f4efac1

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

lib/matplotlib/pyplot.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ def _backend_selection():
108108
from matplotlib.backends import pylab_setup
109109
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
110110

111+
_BASE_DH = None
112+
_IP_REGISTERED = False
113+
111114

112115
def install_repl_displayhook():
116+
global _BASE_DH
117+
global _IP_REGISTERED
113118

114119
class _NotIPython(Exception):
115120
pass
@@ -122,16 +127,25 @@ class _NotIPython(Exception):
122127
if ip is None:
123128
raise _NotIPython()
124129

130+
if _IP_REGISTERED:
131+
return
132+
125133
# IPython >= 2
126134
try:
127135
ip.events.register('post_execute', draw_all)
128136
except AttributeError:
129137
# IPython 1.x
130138
ip.register_post_execute(draw_all)
139+
finally:
140+
_IP_REGISTERED = True
131141

132142
# import failed or ipython is not running
133143
except (ImportError, _NotIPython):
134-
dh = sys.displayhook
144+
145+
if _BASE_DH is not None:
146+
return
147+
148+
dh = _BASE_DH = sys.displayhook
135149

136150
def displayhook(*args):
137151
dh(*args)

0 commit comments

Comments
 (0)