GNOME Bugzilla – Bug 546120
make gio.File more Pythonic
Last modified: 2008-08-10 15:54:26 UTC
gio.File contains a lot of methods, but not much else. It feels unpythonic. 1. Add __repr__ >>> gio.File ('/home/paul/svn/pygobject/INSTALL') <__main__.GLocalFile object at 0x407d611c (GLocalFile at 0x82329c0)> I think something along the lines of <...: file:///home/paul/svn/pygobject/INSTALL> will be a lot more useful than current repr. get_uri() is documented to not do blocking IO, so this will not be a problem. 2. Wrap hash() >>> x = gio.File ('/home/paul/svn/pygobject/INSTALL') >>> y = gio.File ('/home/paul/svn/pygobject/INSTALL') >>> hash (x), hash (y) (136546432, 136546496) There is function g_file_hash() in GLib which will map seemlessly to Python __hash__. As I understand, functions g_file_set_* won't affect hash value, so it is fine to have __hash__ on "somewhat mutable" type. 3. Wrap equal() in more Pythonic way >>> x.equal (y) True >>> x == y False First statement is fine, but feels alien to do in Python. Normal comparison operator (==) must do The Right Thing.
Created attachment 116235 [details] [review] patch with problems This patch adds the slots, but is unfortunately problematic. GLocalFile type (created at runtime) inherits __eq__ etc. slots, but not __repr__ (with this we can live) or __hash__ (this one is unacceptable). It seems some work needs to be done on runtime type wrapper creation.
Comment on attachment 116235 [details] [review] patch with problems Looks good, please commit
Done, thanks. Sending ChangeLog Sending gio/gfile.override Sending tests/test_gio.py Transmitting file data ... Committed revision 939. 2008-08-10 Paul Pogonyshev <pogonyshev@gmx.net> Bug 546120 – make gio.File more Pythonic * gio/gfile.override (_wrap_g_file_tp_richcompare) (_wrap_g_file_tp_hash, _wrap_g_file_tp_repr): New functions. * tests/test_gio.py (TestFile.test_eq, TestFile.test_hash): New tests.