After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 546120 - make gio.File more Pythonic
make gio.File more Pythonic
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: gio
Git master
Other All
: Normal enhancement
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2008-08-03 17:49 UTC by Paul Pogonyshev
Modified: 2008-08-10 15:54 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
patch with problems (2.55 KB, patch)
2008-08-09 15:39 UTC, Paul Pogonyshev
committed Details | Review

Description Paul Pogonyshev 2008-08-03 17:49:54 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.
Comment 1 Paul Pogonyshev 2008-08-09 15:39:51 UTC
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 2 Johan (not receiving bugmail) Dahlin 2008-08-10 15:43:59 UTC
Comment on attachment 116235 [details] [review]
patch with problems

Looks good, please commit
Comment 3 Paul Pogonyshev 2008-08-10 15:54:26 UTC
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.