question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nuitka + GTK GI: Fail, because some initializations not called when Nuitka

See original GitHub issue

Nuitka + GTK GI: Fail, because some initializations not called when Nuitka

GI = Global Introspection (GTK+GDK)

Lets consider simple (minimal for reproduce the problem) python3 file:

import gi
import cairo
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk
Gtk.init()
from gi.repository import Gdk

image = cairo.ImageSurface.create_from_png("image.png")
region = Gdk.cairo_region_create_from_surface(image)

print('All OK')

It should work on regular linux python3 if installed package

  • python3-gi-cairo on (Debian/Ubuntu)
  • python3-gobject-base on Fedora

But if we compile it on nuitka, we got KeyError: 'could not find foreign type Surface on line

  region = Gdk.cairo_region_create_from_surface(image)

I did some research. The exception occured on pygi_struct_foreign_convert_to_g_argument (see https://gitlab.gnome.org/GNOME/pygobject/-/blob/3.36.1/gi/pygi-foreign.c#L111 or can be similar error «Couldn’t find foreign struct converter for» on pygi_struct_foreign_lookup_by_name (see https://gitlab.gnome.org/GNOME/pygobject/-/blob/3.36.1/gi/pygi-foreign.c#L74)

This is because when used regular python3 we call PYGLIB_MODULE_START(_gi_cairo, "_gi_cairo") ( https://gitlab.gnome.org/GNOME/pygobject/-/blob/3.36.1/gi/pygi-foreign-cairo.c#L617 ) where called a lot of pygi_register_foreign_struct to register all cairo types as foreigh.

Long stack from https://gitlab.gnome.org/GNOME/pygobject/-/blob/3.36.1/gi/pygi-foreign-cairo.c#L619 when regular python3 called:

/usr/lib64/python3.8/site-packages/gi/_gi_cairo.cpython-38-x86_64-linux-gnu.so(+0x385b) [0x7f83808b185b]
/usr/lib64/python3.8/site-packages/gi/_gi_cairo.cpython-38-x86_64-linux-gnu.so(+0x38fc) [0x7f83808b18fc]
/usr/lib64/python3.8/site-packages/gi/_gi_cairo.cpython-38-x86_64-linux-gnu.so(PyInit__gi_cairo+0x9) [0x7f83808b18d7]
/lib64/libpython3.8.so.1.0(_PyImport_LoadDynamicModuleWithSpec+0x1a9) [0x7f838de63d89]
/lib64/libpython3.8.so.1.0(+0x1a0885) [0x7f838de63885]
/lib64/libpython3.8.so.1.0(+0x11657a) [0x7f838ddd957a]
/lib64/libpython3.8.so.1.0(PyVectorcall_Call+0x70) [0x7f838dde1b80]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x6183) [0x7f838ddd1143]
/lib64/libpython3.8.so.1.0(_PyEval_EvalCodeWithName+0x314) [0x7f838ddc9ec4]
/lib64/libpython3.8.so.1.0(_PyFunction_Vectorcall+0xc3) [0x7f838ddd8f63]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x52c6) [0x7f838ddd0286]
/lib64/libpython3.8.so.1.0(+0x1162b7) [0x7f838ddd92b7]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x7f7) [0x7f838ddcb7b7]
/lib64/libpython3.8.so.1.0(+0x1162b7) [0x7f838ddd92b7]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x3fb) [0x7f838ddcb3bb]
/lib64/libpython3.8.so.1.0(+0x1162b7) [0x7f838ddd92b7]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x3fb) [0x7f838ddcb3bb]
/lib64/libpython3.8.so.1.0(+0x1162b7) [0x7f838ddd92b7]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x3fb) [0x7f838ddcb3bb]
/lib64/libpython3.8.so.1.0(+0x1162b7) [0x7f838ddd92b7]
/lib64/libpython3.8.so.1.0(+0x1157df) [0x7f838ddd87df]
/lib64/libpython3.8.so.1.0(_PyObject_CallMethodIdObjArgs+0xf8) [0x7f838dde2de8]
/lib64/libpython3.8.so.1.0(PyImport_ImportModuleLevelObject+0x4de) [0x7f838dde286e]
/lib64/libpython3.8.so.1.0(+0x127a98) [0x7f838ddeaa98]
/lib64/libpython3.8.so.1.0(+0x12004a) [0x7f838dde304a]
/lib64/libpython3.8.so.1.0(_PyObject_MakeTpCall+0xe1) [0x7f838ddd37a1]
/lib64/libpython3.8.so.1.0(+0x118358) [0x7f838dddb358]
/lib64/libpython3.8.so.1.0(PyObject_CallFunction+0x96) [0x7f838ddea9d6]
/lib64/libpython3.8.so.1.0(PyImport_Import+0xcd) [0x7f838ddea6fd]
/lib64/libpython3.8.so.1.0(PyImport_ImportModule+0x1d) [0x7f838de4bf3d]
/usr/lib64/python3.8/site-packages/gi/_gi.cpython-38-x86_64-linux-gnu.so(+0x2b53a) [0x7f83808e053a]
/usr/lib64/python3.8/site-packages/gi/_gi.cpython-38-x86_64-linux-gnu.so(+0x2b95c) [0x7f83808e095c]
/lib64/libpython3.8.so.1.0(+0x114920) [0x7f838ddd7920]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x52c6) [0x7f838ddd0286]
/lib64/libpython3.8.so.1.0(_PyEval_EvalCodeWithName+0x314) [0x7f838ddc9ec4]
/lib64/libpython3.8.so.1.0(PyEval_EvalCodeEx+0x39) [0x7f838de45109]
/lib64/libpython3.8.so.1.0(PyEval_EvalCode+0x1b) [0x7f838de450cb]
/lib64/libpython3.8.so.1.0(+0x185de0) [0x7f838de48de0]
/lib64/libpython3.8.so.1.0(+0x11657a) [0x7f838ddd957a]
/lib64/libpython3.8.so.1.0(PyVectorcall_Call+0x70) [0x7f838dde1b80]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x6183) [0x7f838ddd1143]
/lib64/libpython3.8.so.1.0(_PyEval_EvalCodeWithName+0x314) [0x7f838ddc9ec4]
/lib64/libpython3.8.so.1.0(_PyFunction_Vectorcall+0xc3) [0x7f838ddd8f63]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x52c6) [0x7f838ddd0286]
/lib64/libpython3.8.so.1.0(+0x1162b7) [0x7f838ddd92b7]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x7f7) [0x7f838ddcb7b7]
/lib64/libpython3.8.so.1.0(+0x1162b7) [0x7f838ddd92b7]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x3fb) [0x7f838ddcb3bb]
/lib64/libpython3.8.so.1.0(+0x1162b7) [0x7f838ddd92b7]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x3fb) [0x7f838ddcb3bb]
/lib64/libpython3.8.so.1.0(+0x1162b7) [0x7f838ddd92b7]
/lib64/libpython3.8.so.1.0(+0x1157df) [0x7f838ddd87df]
/lib64/libpython3.8.so.1.0(_PyObject_CallMethodIdObjArgs+0xf8) [0x7f838dde2de8]
/lib64/libpython3.8.so.1.0(PyImport_ImportModuleLevelObject+0x4de) [0x7f838dde286e]
/lib64/libpython3.8.so.1.0(_PyEval_EvalFrameDefault+0x3a35) [0x7f838ddce9f5]
/lib64/libpython3.8.so.1.0(_PyEval_EvalCodeWithName+0x314) [0x7f838ddc9ec4]
/lib64/libpython3.8.so.1.0(PyEval_EvalCodeEx+0x39) [0x7f838de45109]
/lib64/libpython3.8.so.1.0(PyEval_EvalCode+0x1b) [0x7f838de450cb]
/lib64/libpython3.8.so.1.0(+0x1a3028) [0x7f838de66028]
/lib64/libpython3.8.so.1.0(+0x1a2763) [0x7f838de65763]
/lib64/libpython3.8.so.1.0(PyRun_FileExFlags+0x95) [0x7f838dd5181b]
/lib64/libpython3.8.so.1.0(PyRun_SimpleFileExFlags+0x376) [0x7f838dd5119d]
/lib64/libpython3.8.so.1.0(+0x8553c) [0x7f838dd4853c]
/lib64/libpython3.8.so.1.0(Py_BytesMain+0x39) [0x7f838de38bf9]
/lib64/libc.so.6(__libc_start_main+0xf2) [0x7f838e01e042]
python3(_start+0x2e) [0x55ba8e71507e]

When I compile this sample script with Nuitka, this initialization function (PyInit__gi_cairo) did not called.

Is there any workaround, how I can call this initialization (PyInit__gi_cairo) using Nuitka? (embedding some C-calls in python code…, any ideas are welcomed).

Thanks all in advance.

Sample demo directory attached.

demo-folder.zip

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
actionlesscommented, Oct 16, 2020

i’m having slightly different issue with gi, not sure if it’s a separate issue or not:

for example, Gtk.TreeStore(str, str, str, bool) would raise when trying to run compiled code:

Traceback (most recent call last):
  File "/path/to/file.py", line 50, in __init__
TypeError: GObject.__init__() takes exactly 0 arguments (4 given)

this (and few similar problems with other gtk objects) behavior somehow looks like it ignores the overrides: https://github.com/GNOME/pygobject/tree/master/gi/overrides

1reaction
flozzcommented, May 9, 2021

If someone have the same problem as @actionless, I was able to workaround it by importing the overrides I need before importing anything from gi.repository:

import gi.overrides.Gtk
from gi.repository import Gtk
Read more comments on GitHub >

github_iconTop Results From Across the Web

nuitka Changelog
Nuitka asserted against it failing, where some code handles it failing on non-Windows ... not compile anything yet due to required and at...
Read more >
Index of Portage & Programming - Gentoo Forums
Index of Portage & Programming. "Allow remote access to mySQL" Page:1 · "binary packages for different use flags?" Page:1 · "Question about LVM2...
Read more >
Nuitka compilation error when building on windows
... which for some reason were detecting trojans in the generated .exe files - clang produces an executable that's most compliant with those ......
Read more >
BIRCH/Release To Do List - Bioinformatics.Org Wiki
In part this is because sometimes downloads are incomplete. There is no point in proceeding if these steps fail. CRITICAL!
Read more >
libraries - What would cause the "gi" module to be missing ...
error which appears with some programs too (eg: gnome tweak tool). I have tried re-installing Python both from the software center and from ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found