Source: matplotlib Followup-For: Bug #1101565 X-Debbugs-Cc: francesco.balla...@unicatt.it, j...@jp-hosting.net Control: severity -1 important Control: tags -1 patch
Dear Maintainer, Please find attached a patch to resolve the problem encountered here: that the presence of a package that installs gobject-introspection overrides[1] allows Python / matplotlib to import the 'gi' module successfully despite the absence of the python3-gi module that provides GObject functionality. The fix is detects an empty/implicit module import by checking for a __file__ attribute. I'm re-raising the severity to important, because I think many Debian systems could have installed packages that include these overrides, and the effect on matplotlib is non-intuitive and fairly severe. I have tested the patch by running the following command both with and without the patch applied, on a system that has gedit installed: $ python3 -c 'from matplotlib.backends import backend_gtk3' Without the patch applied (replicating the bugreport) the result is: Traceback (most recent call last): File "<string>", line 1, in <module> from matplotlib.backends import backend_gtk3 File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_gtk3.py", line 20, in <module> gi.require_version("Gtk", "3.0") ^^^^^^^^^^^^^^^^^^ AttributeError: module 'gi' has no attribute 'require_version' After applying the patch, the result is: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_gtk3.py", line 16, in <module> raise ImportError("Error: PyGObject module is empty") ImportError: Error: PyGObject module is empty The above exception was the direct cause of the following exception: Traceback (most recent call last): File "<string>", line 1, in <module> from matplotlib.backends import backend_gtk3 File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_gtk3.py", line 18, in <module> raise ImportError("The GTK3 backends require PyGObject") from err ImportError: The GTK3 backends require PyGObject The ImportError is intended to allow matplotlib to detect that GObject is not available on the system, so that it can choose an alternate backend automatically. Thanks, James [1] - https://wiki.gnome.org/Projects/PyGObject/IntrospectionPorting#Overrides
From: James Addison <j...@jp-hosting.net> Date: Tue, 01 Apr 2025 09:17:28 +0000 Subject: Raise import error if PyGObject module is empty . When GI overrides are installed, the dist-packages/gi directory is non-empty, and this allows Python3 to import the module. This can happen when Debian packages other than python3-gi are installed. . Detect an empty __file__ attribute on the imported module to check whether it is genuinely the expected GObject Python module. . Credit to Francesco Ballarin for the test condition in #1101565. Bug: http://bugs.debian.org/1101565 --- --- a/lib/matplotlib/backends/backend_gtk3.py +++ b/lib/matplotlib/backends/backend_gtk3.py @@ -11,6 +11,9 @@ try: import gi + + if gi.__file__ is None: + raise ImportError("Error: PyGObject module is empty") except ImportError as err: raise ImportError("The GTK3 backends require PyGObject") from err --- a/lib/matplotlib/backends/backend_gtk4.py +++ b/lib/matplotlib/backends/backend_gtk4.py @@ -10,6 +10,9 @@ try: import gi + + if gi.__file__ is None: + raise ImportError("Error: PyGObject module is empty") except ImportError as err: raise ImportError("The GTK4 backends require PyGObject") from err