GNOME Bugzilla – Bug 623278
Make GFileEnumerator iteratable
Last modified: 2011-01-19 14:00:41 UTC
We should override that class to make it iteratable so it is easier to use.
Created attachment 165430 [details] [review] file enumerator v1 Far from finished. I can't really make a good test and finish it as I get an error: Traceback (most recent call last):
+ Trace 222735
suite.addTest(loader.loadTestsFromName(name))
module = __import__('.'.join(parts_copy))
from gi.repository import Gio
overrides_modules = __import__('gi.overrides', fromlist=[namespace])
FileEnumerator = override(FileEnumerator)
g_type = type_.__info__.get_g_type()
*** Bug 634636 has been marked as a duplicate of this bug. ***
Created attachment 175867 [details] Testing iterability of a GFileEnumerator in a standalone script I'm a little confused at what the situation of this enhancement request is. If I try to iterate a GFileEnumerator as in the attached script, I get a "TypeError: '__main__.GLocalFileEnumerator' object is not iterable". However, if I turn the very same code into a unit test for the overrides test suite, it runs smoothly. I have no idea what the test suite does differently.
We want it to be iterable so we can do things like: for info in enumerator: print info.get_name()
Created attachment 176567 [details] [review] Patch to override GFileEnumerator behavior. As part of a Google Code-in task (see: http://www.google-melange.com/gci/task/show/google/gci2010/gnome/t129121625299), I've amended the patch (with help from the people at #python@irc.gnome.org, pbor and J5 in particular) to work correctly. This patch was written for commit b03cc9e0d66d8caea3cd6a63db198c43de9267e9. Tell me if you run into any problems with it.
Hi Tony, thanks for working on this. For every override we also add unit tests to test_overrides.py. The original patch by nachio had a first test. Can you please add tests?
Created attachment 176678 [details] [review] Revised patch. Here's a revised version of the patch which adds a new test case that compares the output of iterating through a FileEnumerator with FileEnumerator.next_file behavior.
Review of attachment 176678 [details] [review]: Patch looks good to me except I'd call the unit test function just "test_file_enumerator" for consistency with the other tests.
Created attachment 176687 [details] [review] Amended patch. Final revision of the patch which names the test test_file_enumerator.
Btw pbor, should we also need to add?: def __bool__(self): return True # alias for Python 2.x object protocol __nonzero__ = __bool__
No, I do not think we should override bool, in this case it makes sense that if the list is empty the enumerator is false.
Created attachment 176759 [details] [review] file enumerator v2 Fixed some minor issues. Can somebody test it? Here the test doesn't pass due to not finding the override. Do I need to add the Gio module somewhere else to be detected?
Comment on attachment 176759 [details] [review] file enumerator v2 looks good
Patch works fine for me.
Just to clarify: With this patch applied, I can iterate over GFileEnumerators in a project using pygobject. However, the corresponding test in "make check" fails for me too.
Tests pass once we remove the gio tests.
This problem has been fixed in the development version. The fix will be available in the next major software release. Thank you for your bug report.