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

Question:

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))
Asked By: jcoppens

||

Answers:

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!

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.

Answered By: user3840170
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.