pythongtkgobject

How can I store integers wider than 32 bits in a Gtk.ListStore?


It seems that DirEntry's f.stat().st_size can return values larger than 2147483647 for the file size, which - in my case - is correct. But when I try to store it in a Gtk.ListStore which has the corresponding column set to int, I get an OverflowError:

OverflowError: 2200517068 not in range -2147483648 to 2147483647

How do I solve this? Is int in ListStore limited to 4 bytes?

BTW, I'm using Python 3.9.5 and GUdev 237.

        self.file_store = Gtk.ListStore(str,    # File name
                                        int,    # File size
                                        str)    # Mod time, as YYYY/MM/DD hh:mm:ss
        ...
        self.file_store.append(
                        (f.name,
                         f.stat().st_size,
                         ftime))

Solution

  • Use GObject.TYPE_INT64 to allow signed 64-bit integers in the list store:

    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import GObject, Gtk
    
    list_store = Gtk.ListStore(GObject.TYPE_INT64)
    list_store.append((1 << 48,))   # look ma! no error!
    

    You will probably want signed 64-bit integers given that off_t (what st_size is defined as) is defined to be a signed type, and on most platforms it is at most 64 bits wide; 8 pebibytes ought to be enough for everyone.

    You can refer to other primitive GObject types in similar way, using TYPE_name constants in the GObject namespace.