From 5a01cb8d278e178df12b29aa8dd337b0eee05e26 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 27 Jun 2018 15:00:03 +0200 Subject: [PATCH] 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). --- doc/api/next_api_changes/2018-06-27-AL.rst | 6 ++++++ lib/matplotlib/backends/backend_gtk3.py | 17 +++++++++++------ src/_macosx.m | 15 +++++++++------ 3 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 doc/api/next_api_changes/2018-06-27-AL.rst diff --git a/doc/api/next_api_changes/2018-06-27-AL.rst b/doc/api/next_api_changes/2018-06-27-AL.rst new file mode 100644 index 000000000000..74c32d357386 --- /dev/null +++ b/doc/api/next_api_changes/2018-06-27-AL.rst @@ -0,0 +1,6 @@ +Changes to backend loading +`````````````````````````` + +Failure to load backend modules (``macosx`` on non-framework builds and +``gtk3`` when running headless) now raises `ImportError` (instead of +`RuntimeError` and `TypeError`, respectively. diff --git a/lib/matplotlib/backends/backend_gtk3.py b/lib/matplotlib/backends/backend_gtk3.py index 08aaaea2be9a..68af0c19a301 100644 --- a/lib/matplotlib/backends/backend_gtk3.py +++ b/lib/matplotlib/backends/backend_gtk3.py @@ -23,13 +23,18 @@ # 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 PIXELS_PER_INCH = 96 -cursord = { - cursors.MOVE : Gdk.Cursor.new(Gdk.CursorType.FLEUR), - cursors.HAND : Gdk.Cursor.new(Gdk.CursorType.HAND2), - cursors.POINTER : Gdk.Cursor.new(Gdk.CursorType.LEFT_PTR), - cursors.SELECT_REGION : Gdk.Cursor.new(Gdk.CursorType.TCROSS), - cursors.WAIT : Gdk.Cursor.new(Gdk.CursorType.WATCH), +try: + cursord = { + cursors.MOVE : Gdk.Cursor.new(Gdk.CursorType.FLEUR), + cursors.HAND : Gdk.Cursor.new(Gdk.CursorType.HAND2), + cursors.POINTER : Gdk.Cursor.new(Gdk.CursorType.LEFT_PTR), + cursors.SELECT_REGION : Gdk.Cursor.new(Gdk.CursorType.TCROSS), + cursors.WAIT : Gdk.Cursor.new(Gdk.CursorType.WATCH), } +except TypeError as exc: + # Happens when running headless. Convert to ImportError to cooperate with + # backend switching. + raise ImportError(exc) class TimerGTK3(TimerBase): diff --git a/src/_macosx.m b/src/_macosx.m index 09c80e616cce..2cc9182aa383 100644 --- a/src/_macosx.m +++ b/src/_macosx.m @@ -3025,15 +3025,15 @@ static bool verify_framework(void) && GetCurrentProcess(&psn)==noErr && SetFrontProcess(&psn)==noErr) return true; #endif - PyErr_SetString(PyExc_RuntimeError, + PyErr_SetString(PyExc_ImportError, "Python is not installed as a framework. The Mac OS X backend will " "not be able to function correctly if Python is not installed as a " "framework. See the Python documentation for more information on " "installing Python as a framework on Mac OS X. Please either reinstall " "Python as a framework, or try one of the other backends. If you are " - "using (Ana)Conda please install python.app and replace the use of 'python' " - "with 'pythonw'. See 'Working with Matplotlib on OSX' " - "in the Matplotlib FAQ for more information."); + "using (Ana)Conda please install python.app and replace the use of " + "'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the " + "Matplotlib FAQ for more information."); return false; } @@ -3041,7 +3041,10 @@ static bool verify_framework(void) {"show", (PyCFunction)show, METH_NOARGS, - "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." + "Show all the figures and enter the main loop.\n" + "\n" + "This function does not return until all Matplotlib windows are closed,\n" + "and is normally not needed in interactive sessions." }, {"choose_save_file", (PyCFunction)choose_save_file, @@ -3053,7 +3056,7 @@ static bool verify_framework(void) METH_VARARGS, "Sets the active cursor." }, - {NULL, NULL, 0, NULL}/* sentinel */ + {NULL, NULL, 0, NULL} /* sentinel */ }; static struct PyModuleDef moduledef = {