I just ran into another difference between fixtures and context managers while evaluating a feature request from James Westby.
On Wed, Aug 25, 2010 at 8:00 AM, Robert Collins <[email protected]> wrote: > On Wed, Aug 25, 2010 at 7:55 AM, Aaron Bentley <[email protected]> wrote: > >> I could argue that foo.__len__() is ugly if you use it directly, too. I >> think it is ugly to have two ways of doing the same thing. I really do. >> Context managers are inevitable, so having a Fixture concept that is >> equivalent but doesn't work the same is cognitive overhead and >> finger-typing overhead. >> >> Anything that is equivalent to a context manager should actually be a >> context manager, because that's what Python standardized on, and >> therefore it'll be compatible with everything going forward. > > Oh, and I did forget something, lp:python-fixtures also defines a > reset(), which context managers do not define, and which is useful for > handling things like layers. Layers can be implemented on a fixture > with __enter__, __exit__ and reset(), but not on a bare context > manager. > > I do agree that if something is equivalent to a context manager, > having it implement the contract is sensible - thats why Fixture > implements it :). This shows a behaviour of context managers that is undesirable for test suites: from contextlib import contextmanager @contextmanager def raiser(message): yield print "cleanup", message raise RuntimeError(message) with raiser("outer"): with raiser("inner"): print "test code" :!python demo.py test code cleanup inner Traceback (most recent call last): File "demo.py", line 11, in <module> print "test code" File "/usr/lib/python2.6/contextlib.py", line 23, in __exit__ self.gen.next() File "demo.py", line 7, in raiser raise RuntimeError(message) RuntimeError: inner In a test suite we want to run all the cleanups, *and* we want to report on all the exceptions. -Rob _______________________________________________ Mailing list: https://launchpad.net/~launchpad-dev Post to : [email protected] Unsubscribe : https://launchpad.net/~launchpad-dev More help : https://help.launchpad.net/ListHelp

