3 # We cannot use a generator because we rely on the finally block to
4 # be sure finalize() will be called. Python 2.5 allows finally block
5 # in generator, but it relies on the GC so it's not sure it will be
6 # called at the right time.
8 # The expected use of these browsing functions is:
10 # cookie = chdir_browsing.init(path)
12 # for child in chdir_browsing.listdir(cookie):
13 # .. Do something with child ..
15 # chdir_browsing.finalize(cookie)
18 previous_cwd
= os
.open('.', os
.O_RDONLY
)
23 os
.close(previous_cwd
)
26 def listdir(unused_cookie
):
28 # We want the order to be always so same so that hard links
29 # detection will be consistent across filesystems
33 def finalize(previous_cwd
):
35 os
.fchdir(previous_cwd
)
37 os
.close(previous_cwd
)