Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita51424a

Browse files
authored
Merge pull request#22906 from anntzer/repldisplayhook3state
Clarify logic for repl displayhook.
2 parents925b27f +6c7cf65 commita51424a

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

‎lib/matplotlib/pyplot.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
implicit and explicit interfaces.
3636
"""
3737

38+
fromenumimportEnum
3839
importfunctools
3940
importimportlib
4041
importinspect
@@ -108,8 +109,15 @@ def _copy_docstring_and_deprecators(method, func=None):
108109

109110
## Global ##
110111

111-
_IP_REGISTERED=None
112-
_INSTALL_FIG_OBSERVER=False
112+
113+
# The state controlled by {,un}install_repl_displayhook().
114+
_ReplDisplayHook=Enum("_ReplDisplayHook", ["NONE","PLAIN","IPYTHON"])
115+
_REPL_DISPLAYHOOK=_ReplDisplayHook.NONE
116+
117+
118+
def_draw_all_if_interactive():
119+
ifmatplotlib.is_interactive():
120+
draw_all()
113121

114122

115123
definstall_repl_displayhook():
@@ -119,30 +127,25 @@ def install_repl_displayhook():
119127
120128
This works both with IPython and with vanilla python shells.
121129
"""
122-
global_IP_REGISTERED
123-
global_INSTALL_FIG_OBSERVER
130+
global_REPL_DISPLAYHOOK
124131

125-
if_IP_REGISTERED:
132+
if_REPL_DISPLAYHOOKis_ReplDisplayHook.IPYTHON:
126133
return
134+
127135
# See if we have IPython hooks around, if so use them.
128136
# Use ``sys.modules.get(name)`` rather than ``name in sys.modules`` as
129137
# entries can also have been explicitly set to None.
130138
mod_ipython=sys.modules.get("IPython")
131139
ifnotmod_ipython:
132-
_INSTALL_FIG_OBSERVER=True
140+
_REPL_DISPLAYHOOK=_ReplDisplayHook.PLAIN
133141
return
134142
ip=mod_ipython.get_ipython()
135143
ifnotip:
136-
_INSTALL_FIG_OBSERVER=True
144+
_REPL_DISPLAYHOOK=_ReplDisplayHook.PLAIN
137145
return
138146

139-
defpost_execute():
140-
ifmatplotlib.is_interactive():
141-
draw_all()
142-
143-
ip.events.register("post_execute",post_execute)
144-
_IP_REGISTERED=post_execute
145-
_INSTALL_FIG_OBSERVER=False
147+
ip.events.register("post_execute",_draw_all_if_interactive)
148+
_REPL_DISPLAYHOOK=_ReplDisplayHook.IPYTHON
146149

147150
fromIPython.core.pylabtoolsimportbackend2gui
148151
# trigger IPython's eventloop integration, if available
@@ -161,16 +164,12 @@ def uninstall_repl_displayhook():
161164
this will reset `sys.displayhook` to what ever function was there when
162165
Matplotlib installed its displayhook, possibly discarding your changes.
163166
"""
164-
global_IP_REGISTERED
165-
global_INSTALL_FIG_OBSERVER
166-
if_IP_REGISTERED:
167+
global_REPL_DISPLAYHOOK
168+
if_REPL_DISPLAYHOOKis_ReplDisplayHook.IPYTHON:
167169
fromIPythonimportget_ipython
168170
ip=get_ipython()
169-
ip.events.unregister('post_execute',_IP_REGISTERED)
170-
_IP_REGISTERED=None
171-
172-
if_INSTALL_FIG_OBSERVER:
173-
_INSTALL_FIG_OBSERVER=False
171+
ip.events.unregister("post_execute",_draw_all_if_interactive)
172+
_REPL_DISPLAYHOOK=_ReplDisplayHook.NONE
174173

175174

176175
draw_all=_pylab_helpers.Gcf.draw_all
@@ -810,7 +809,7 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
810809
# FigureManager base class.
811810
draw_if_interactive()
812811

813-
if_INSTALL_FIG_OBSERVER:
812+
if_REPL_DISPLAYHOOKis_ReplDisplayHook.PLAIN:
814813
fig.stale_callback=_auto_draw_if_interactive
815814

816815
ifclear:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp