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

Commitb0a840d

Browse files
authored
Merge pull request#19601 from anntzer/modulesnone
Handle None entries in sys.modules.
2 parentsd5d2b2a +1a6f9f2 commitb0a840d

File tree

4 files changed

+44
-51
lines changed

4 files changed

+44
-51
lines changed

‎lib/matplotlib/backends/qt_compat.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@
3535
_ETS= {"pyqt5":QT_API_PYQT5,"pyside2":QT_API_PYSIDE2,
3636
"pyqt":QT_API_PYQTv2,"pyside":QT_API_PYSIDE,
3737
None:None}
38-
# First, check if anything is already imported.
39-
if"PyQt5.QtCore"insys.modules:
38+
# First, check if anything is already imported. Use ``sys.modules.get(name)``
39+
# rather than ``name in sys.modules`` as entries can also have been explicitly
40+
# set to None.
41+
ifsys.modules.get("PyQt5.QtCore"):
4042
QT_API=QT_API_PYQT5
41-
elif"PySide2.QtCore"insys.modules:
43+
elifsys.modules.get("PySide2.QtCore"):
4244
QT_API=QT_API_PYSIDE2
43-
elif"PyQt4.QtCore"insys.modules:
45+
elifsys.modules.get("PyQt4.QtCore"):
4446
QT_API=QT_API_PYQTv2
45-
elif"PySide.QtCore"insys.modules:
47+
elifsys.modules.get("PySide.QtCore"):
4648
QT_API=QT_API_PYSIDE
4749
# Otherwise, check the QT_API environment variable (from Enthought). This can
4850
# only override the binding, not the backend (in other words, we check that the

‎lib/matplotlib/cbook/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def _get_running_interactive_framework():
5454
One of the following values: "qt5", "qt4", "gtk3", "wx", "tk",
5555
"macosx", "headless", ``None``.
5656
"""
57+
# Use ``sys.modules.get(name)`` rather than ``name in sys.modules`` as
58+
# entries can also have been explicitly set to None.
5759
QtWidgets= (sys.modules.get("PyQt5.QtWidgets")
5860
orsys.modules.get("PySide2.QtWidgets"))
5961
ifQtWidgetsandQtWidgets.QApplication.instance():
@@ -76,9 +78,9 @@ def _get_running_interactive_framework():
7678
ifframe.f_codeincodes:
7779
return"tk"
7880
frame=frame.f_back
79-
if'matplotlib.backends._macosx'insys.modules:
80-
ifsys.modules["matplotlib.backends._macosx"].event_loop_is_running():
81-
return"macosx"
81+
macosx=sys.modules.get("matplotlib.backends._macosx")
82+
ifmacosxandmacosx.event_loop_is_running():
83+
return"macosx"
8284
ifnot_c_internal_utils.display_is_valid():
8385
return"headless"
8486
returnNone

‎lib/matplotlib/pyplot.py

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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'insys.modules:
129-
fromIPythonimportget_ipython
130-
ip=get_ipython()
131-
ifipisNone:
132-
raise_NotIPython()
133-
134-
if_IP_REGISTERED:
135-
return
136-
137-
defpost_execute():
138-
ifmatplotlib.is_interactive():
139-
draw_all()
140-
141-
# IPython >= 2
142-
try:
143-
ip.events.register('post_execute',post_execute)
144-
exceptAttributeError:
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+
ifnotmod_ipython:
129+
_INSTALL_FIG_OBSERVER=True
130+
return
131+
ip=mod_ipython.get_ipython()
132+
ifnotip:
133+
_INSTALL_FIG_OBSERVER=True
134+
return
150135

151-
# trigger IPython's eventloop integration, if available
152-
fromIPython.core.pylabtoolsimportbackend2gui
136+
defpost_execute():
137+
ifmatplotlib.is_interactive():
138+
draw_all()
153139

154-
ipython_gui_name=backend2gui.get(get_backend())
155-
ifipython_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+
exceptAttributeError:# 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+
fromIPython.core.pylabtoolsimportbackend2gui
148+
# trigger IPython's eventloop integration, if available
149+
ipython_gui_name=backend2gui.get(get_backend())
150+
ifipython_gui_name:
151+
ip.enable_gui(ipython_gui_name)
163152

164153

165154
defuninstall_repl_displayhook():
166155
"""
167-
Uninstall thematplotlib display hook.
156+
Uninstall theMatplotlib 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 whenmatplotlib installedit's displayhook,
166+
display hook, this will reset ``sys.displayhook`` to what ever
167+
function was there whenMatplotlib installedits displayhook,
179168
possibly discarding your changes.
180169
"""
181170
global_IP_REGISTERED

‎lib/matplotlib/testing/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def mpl_test_settings(request):
4848

4949
# special case Qt backend importing to avoid conflicts
5050
ifbackend.lower().startswith('qt4'):
51-
ifany(kinsys.modulesforkin ('PyQt5','PySide2')):
51+
ifany(sys.modules.get(k)forkin ('PyQt5','PySide2')):
5252
pytest.skip('Qt5 binding already imported')
5353
try:
5454
importPyQt4
@@ -59,7 +59,7 @@ def mpl_test_settings(request):
5959
exceptImportError:
6060
pytest.skip("Failed to import a Qt4 binding.")
6161
elifbackend.lower().startswith('qt5'):
62-
ifany(kinsys.modulesforkin ('PyQt4','PySide')):
62+
ifany(sys.modules.get(k)forkin ('PyQt4','PySide')):
6363
pytest.skip('Qt4 binding already imported')
6464
try:
6565
importPyQt5

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp