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

Skip to content

Commit 5a01cb8

Browse files
committed
Raise ImportError on failure to import backends.
This will make backend fallback logic easier (try a backend, if it raises ImportError, move to the next one).
1 parent 65158f9 commit 5a01cb8

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Changes to backend loading
2+
``````````````````````````
3+
4+
Failure to load backend modules (``macosx`` on non-framework builds and
5+
``gtk3`` when running headless) now raises `ImportError` (instead of
6+
`RuntimeError` and `TypeError`, respectively.

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@
2323
# see http://groups.google.com/groups?q=screen+dpi+x11&hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=7077.26e81ad5%40swift.cs.tcd.ie&rnum=5 for some info about screen dpi
2424
PIXELS_PER_INCH = 96
2525

26-
cursord = {
27-
cursors.MOVE : Gdk.Cursor.new(Gdk.CursorType.FLEUR),
28-
cursors.HAND : Gdk.Cursor.new(Gdk.CursorType.HAND2),
29-
cursors.POINTER : Gdk.Cursor.new(Gdk.CursorType.LEFT_PTR),
30-
cursors.SELECT_REGION : Gdk.Cursor.new(Gdk.CursorType.TCROSS),
31-
cursors.WAIT : Gdk.Cursor.new(Gdk.CursorType.WATCH),
26+
try:
27+
cursord = {
28+
cursors.MOVE : Gdk.Cursor.new(Gdk.CursorType.FLEUR),
29+
cursors.HAND : Gdk.Cursor.new(Gdk.CursorType.HAND2),
30+
cursors.POINTER : Gdk.Cursor.new(Gdk.CursorType.LEFT_PTR),
31+
cursors.SELECT_REGION : Gdk.Cursor.new(Gdk.CursorType.TCROSS),
32+
cursors.WAIT : Gdk.Cursor.new(Gdk.CursorType.WATCH),
3233
}
34+
except TypeError as exc:
35+
# Happens when running headless. Convert to ImportError to cooperate with
36+
# backend switching.
37+
raise ImportError(exc)
3338

3439

3540
class TimerGTK3(TimerBase):

src/_macosx.m

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3025,23 +3025,26 @@ static bool verify_framework(void)
30253025
&& GetCurrentProcess(&psn)==noErr
30263026
&& SetFrontProcess(&psn)==noErr) return true;
30273027
#endif
3028-
PyErr_SetString(PyExc_RuntimeError,
3028+
PyErr_SetString(PyExc_ImportError,
30293029
"Python is not installed as a framework. The Mac OS X backend will "
30303030
"not be able to function correctly if Python is not installed as a "
30313031
"framework. See the Python documentation for more information on "
30323032
"installing Python as a framework on Mac OS X. Please either reinstall "
30333033
"Python as a framework, or try one of the other backends. If you are "
3034-
"using (Ana)Conda please install python.app and replace the use of 'python' "
3035-
"with 'pythonw'. See 'Working with Matplotlib on OSX' "
3036-
"in the Matplotlib FAQ for more information.");
3034+
"using (Ana)Conda please install python.app and replace the use of "
3035+
"'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the "
3036+
"Matplotlib FAQ for more information.");
30373037
return false;
30383038
}
30393039

30403040
static struct PyMethodDef methods[] = {
30413041
{"show",
30423042
(PyCFunction)show,
30433043
METH_NOARGS,
3044-
"Show all the figures and enter the main loop.\nThis function does not return until all Matplotlib windows are closed,\nand is normally not needed in interactive sessions."
3044+
"Show all the figures and enter the main loop.\n"
3045+
"\n"
3046+
"This function does not return until all Matplotlib windows are closed,\n"
3047+
"and is normally not needed in interactive sessions."
30453048
},
30463049
{"choose_save_file",
30473050
(PyCFunction)choose_save_file,
@@ -3053,7 +3056,7 @@ static bool verify_framework(void)
30533056
METH_VARARGS,
30543057
"Sets the active cursor."
30553058
},
3056-
{NULL, NULL, 0, NULL}/* sentinel */
3059+
{NULL, NULL, 0, NULL} /* sentinel */
30573060
};
30583061

30593062
static struct PyModuleDef moduledef = {

0 commit comments

Comments
 (0)