On Thu, 19 Aug 2010 16:58:30 +0100, Chris Withers wrote: > Hi All, > > Am I right in thinking this is a bug: > > class MyContextManager: > > def __enter__(self): > pass > > def __exit__(self,t,e,tb): > print type(t),t > print type(e),e > > > with MyContextManager(): > import foo.bar.baz > > ...when executed, gives me: > > <type 'type'> <type 'exceptions.ImportError'> > <type 'str'> No module named foo.bar.baz > > Why is 'e' ending up as a string rather than the ImportError object?
Because e is the exception value, not an exception instance. In other words, if you call t(e) you will get the instance you're expecting. See the docs: http://docs.python.org/library/stdtypes.html#contextmanager.__exit__ The three arguments exc_type, exc_value, exc_tb are the same three arguments you can pass to the raise statement: http://docs.python.org/reference/simple_stmts.html#the-raise-statement (BTW, I'm not suggesting you should do that from inside the __exit__ method.) So, no, this is not a bug. -- Steven -- http://mail.python.org/mailman/listinfo/python-list