Emitting a signal with integer data greater than INT_MAX

Although Python3 integers are unlimited, the following test application fails to emit a signal with an integer value greater that INT_MAX.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject, GLib

class Test(GObject.GObject):
    __gsignals__ = {'dummy': (GObject.SignalFlags.RUN_LAST, None, (int, )), }

    def __init__(self):
        GObject.GObject.__init__(self)
        self.emit('dummy',  GLib.MAXINT)     # OK
        self.emit('dummy',  GLib.MAXINT + 1) # NOT OK

test = Test()

Error

Traceback (most recent call last):
  File "Test.py", line 14, in <module>
    test = Test()
           ^^^^^^
  File "Test.py", line 12, in __init__
    self.emit('dummy',  GLib.MAXINT + 1)
TypeError: could not convert type int to gint required for parameter 0

Is there a known workaround?

It doesn’t really matter what Python does: the signal code is implemented in C, and the int type in pygobject it turned into G_TYPE_INT, which maps to 32 bit signed integers.

Use GObject.TYPE_INT64 instead of int in the signal argument tuple, if you’re passing a signed integer of (at most) 64 bits in size (or GObject.TYPE_UINT64 for unsigned integers).

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.