Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError constructor returned NULL in wayland session #19405

Closed
liuyun88 opened this issue Jan 30, 2021 · 4 comments · Fixed by #19416
Closed

TypeError constructor returned NULL in wayland session #19405

liuyun88 opened this issue Jan 30, 2021 · 4 comments · Fixed by #19416
Labels
Milestone

Comments

@liuyun88
Copy link
Contributor

liuyun88 commented Jan 30, 2021

Bug report

Bug summary

When trying to run matplotlib pyplot, and setting the backend to gtk3agg or gtk3cairo, the following error is encountered in wayland session. The same code works fine in Xorg, or using Qt backend.

Gdk-Message: 12:41:05.918: Unable to load fleur from the cursor theme
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/matplotlib/backends/backend_gtk3.py", line 40, in <module>
    cursors.MOVE:          Gdk.Cursor.new(Gdk.CursorType.FLEUR),
TypeError: constructor returned NULL

Code for reproduction

import matplotlib.pyplot as plt
fig, ax0 = plt.subplots()
ax0.plot(1,1)
plt.show()

Actual outcome

Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/matplotlib/backends/backend_gtk3.py", line 40, in <module>
    cursors.MOVE:          Gdk.Cursor.new(Gdk.CursorType.FLEUR),
TypeError: constructor returned NULL

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/yl398/test.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "/usr/lib/python3.9/site-packages/matplotlib/pyplot.py", line 2336, in <module>
    switch_backend(rcParams["backend"])
  File "/usr/lib/python3.9/site-packages/matplotlib/pyplot.py", line 276, in switch_backend
    class backend_mod(matplotlib.backend_bases._Backend):
  File "/usr/lib/python3.9/site-packages/matplotlib/pyplot.py", line 277, in backend_mod
    locals().update(vars(importlib.import_module(backend_name)))
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/lib/python3.9/site-packages/matplotlib/backends/backend_gtk3agg.py", line 8, in <module>
    from . import backend_agg, backend_gtk3
  File "/usr/lib/python3.9/site-packages/matplotlib/backends/backend_gtk3.py", line 49, in <module>
    raise ImportError(exc) from exc
ImportError: constructor returned NULL

Expected outcome

No error should be encountered and the figure should show on the screen

Matplotlib version

  • Operating system: Manjaro Linux 20.2.1
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.3.3
  • Matplotlib backend (print(matplotlib.get_backend())): GTK3AGG or GTK3Cairo
  • Python version: 3.9.1
  • Jupyter version (if applicable):
  • Other libraries:

Installed matplotlib from default manjaro/arch linux repository.

@anntzer
Copy link
Contributor

anntzer commented Jan 30, 2021

Looks like the fix is

diff --git i/lib/matplotlib/backends/backend_gtk3.py w/lib/matplotlib/backends/backend_gtk3.py
index e9e78b43e..486ba9c1e 100644
--- i/lib/matplotlib/backends/backend_gtk3.py
+++ w/lib/matplotlib/backends/backend_gtk3.py
@@ -36,12 +36,13 @@ backend_version = "%s.%s.%s" % (
     Gtk.get_major_version(), Gtk.get_micro_version(), Gtk.get_minor_version())
 
 try:
+    _display = Gdk.Display.get_default()
     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),
+        cursors.MOVE:          Gdk.Cursor.new_from_name(_display, "move"),
+        cursors.HAND:          Gdk.Cursor.new_from_name(_display, "pointer"),
+        cursors.POINTER:       Gdk.Cursor.new_from_name(_display, "default"),
+        cursors.SELECT_REGION: Gdk.Cursor.new_from_name(_display, "crosshair"),
+        cursors.WAIT:          Gdk.Cursor.new_from_name(_display, "wait"),
     }
 except TypeError as exc:
     # Happens when running headless.  Convert to ImportError to cooperate with

(per https://developer.gnome.org/gdk3/stable/gdk3-Cursors.html, new_from_name exists since gtk2.8, and is preferred over using GdkCursorType (for which a note says "these IDs are directly taken from the X cursor font, and many of these cursors are either not useful, or are not available on other platforms.").)

@liuyun88
Copy link
Contributor Author

liuyun88 commented Jan 30, 2021

Thanks. Can confirm the fix works. The cursor changes to a different theme from the system default when hovering over the pyplot window, but that is a minor issue.

Edit: everything works fine. No longer have the different cursor theme issue.

@anntzer anntzer added the status: has patch patch suggested, PR still needed label Jan 30, 2021
@tacaswell
Copy link
Member

@liuyun88 Would you be willing to open a PR with that patch?

@liuyun88
Copy link
Contributor Author

liuyun88 commented Feb 1, 2021

@liuyun88 Would you be willing to open a PR with that patch?

Just opened a PR. First time doing it and welcoming all suggestions and advice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants