[MERGE] Add exit() on progressbars from TestUIFactory

John Arbash Meinel john at arbash-meinel.com
Tue Mar 17 02:04:13 GMT 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Martin Pool wrote:
> 2009/3/17 Robert Collins <robert.collins at canonical.com>:
>> It has the disadvantage that it doesn't work :(.
>>
>> Python simply fails to tell you that it won't.
> 
> I was a bit worried by this because we already have some code that
> does this and claims to do it because of python2.4.  But it looks like
> it does work:
> 
> mbp at grace% cat ~/tmp/generator.py
> def a(crash):
>     try:
>         for i in range(5):
>             yield 2*i
>         if crash:
>             raise AssertionError()
>     except:
>         print 'foo'
>         raise
>     else:
>         print 'done'
> 
> print list(a(False))
> print list(a(True))
> mbp at grace% python2.4 ~/tmp/generator.py
> done
> [0, 2, 4, 6, 8]
> foo
> Traceback (most recent call last):
>   File "/home/mbp/tmp/generator.py", line 14, in ?
>     print list(a(True))
>   File "/home/mbp/tmp/generator.py", line 6, in a
>     raise AssertionError()
> AssertionError
> 
> 

Try to do that without fully consuming the generator.

do_a = a()
print do_a.next()


Note that we've only yielded 1 value, and then when we stop iterating
the generator, none of the cleanup code gets called.

Python2.5 handles this by explicitly raising GeneratorExit when a
generator is killed before it is fully consumed.

So *if* you always fully consume your generators, then try/except/else
works for python2.4+.

John
=:->
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkm/BR0ACgkQJdeBCYSNAAODeACfTDrrCAlAzY1pCk2sXcULhFX9
FIcAoIjyBhyi8NObQ9o36fuCXwhA4Owc
=8M1P
-----END PGP SIGNATURE-----



More information about the bazaar mailing list