@@ -119,52 +119,41 @@ def install_repl_displayhook():
119119 global _IP_REGISTERED
120120 global _INSTALL_FIG_OBSERVER
121121
122- class _NotIPython (Exception ):
123- pass
124-
125- # see if we have IPython hooks around, if use them
126-
127- try :
128- if 'IPython' in sys .modules :
129- from IPython import get_ipython
130- ip = get_ipython ()
131- if ip is None :
132- raise _NotIPython ()
133-
134- if _IP_REGISTERED :
135- return
136-
137- def post_execute ():
138- if matplotlib .is_interactive ():
139- draw_all ()
140-
141- # IPython >= 2
142- try :
143- ip .events .register ('post_execute' , post_execute )
144- except AttributeError :
145- # IPython 1.x
146- ip .register_post_execute (post_execute )
147-
148- _IP_REGISTERED = post_execute
149- _INSTALL_FIG_OBSERVER = False
122+ if _IP_REGISTERED :
123+ return
124+ # See if we have IPython hooks around, if so use them.
125+ # Use ``sys.modules.get(name)`` rather than ``name in sys.modules`` as
126+ # entries can also have been explicitly set to None.
127+ mod_ipython = sys .modules .get ("IPython" )
128+ if not mod_ipython :
129+ _INSTALL_FIG_OBSERVER = True
130+ return
131+ ip = mod_ipython .get_ipython ()
132+ if not ip :
133+ _INSTALL_FIG_OBSERVER = True
134+ return
150135
151- # trigger IPython's eventloop integration, if available
152- from IPython .core .pylabtools import backend2gui
136+ def post_execute ():
137+ if matplotlib .is_interactive ():
138+ draw_all ()
153139
154- ipython_gui_name = backend2gui .get (get_backend ())
155- if ipython_gui_name :
156- ip .enable_gui (ipython_gui_name )
157- else :
158- _INSTALL_FIG_OBSERVER = True
140+ try : # IPython >= 2
141+ ip .events .register ("post_execute" , post_execute )
142+ except AttributeError : # IPython 1.x
143+ ip .register_post_execute (post_execute )
144+ _IP_REGISTERED = post_execute
145+ _INSTALL_FIG_OBSERVER = False
159146
160- # import failed or ipython is not running
161- except (ImportError , _NotIPython ):
162- _INSTALL_FIG_OBSERVER = True
147+ from IPython .core .pylabtools import backend2gui
148+ # trigger IPython's eventloop integration, if available
149+ ipython_gui_name = backend2gui .get (get_backend ())
150+ if ipython_gui_name :
151+ ip .enable_gui (ipython_gui_name )
163152
164153
165154def uninstall_repl_displayhook ():
166155 """
167- Uninstall the matplotlib display hook.
156+ Uninstall the Matplotlib display hook.
168157
169158 .. warning::
170159
@@ -174,8 +163,8 @@ def uninstall_repl_displayhook():
174163 .. warning::
175164
176165 If you are using vanilla python and have installed another
177- display hook this will reset ``sys.displayhook`` to what ever
178- function was there when matplotlib installed it's displayhook,
166+ display hook, this will reset ``sys.displayhook`` to what ever
167+ function was there when Matplotlib installed its displayhook,
179168 possibly discarding your changes.
180169 """
181170 global _IP_REGISTERED
0 commit comments