classification
Title: Turn contextlib.{redirect_stdout,suppress} into ContextDecorators
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Antony.Lee, ncoghlan, r.david.murray
Priority: normal Keywords:

Created on 2014-10-01 09:50 by Antony.Lee, last changed 2014-10-01 14:55 by ncoghlan. This issue is now closed.

Messages (3)
msg228065 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-10-01 09:50
A small lib improvement suggestion could be to make contextlib.redirect_stdout and contextlib.suppress inherit from ContextDecorator.

As a side note, the source of contextlib has some classes inheriting explicitly from object while others don't, so perhaps this can be harmonized at the same time.
msg228075 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-10-01 14:45
-100 on doing this for suppress.  That would be exactly the kind of wrong-headed code that I was worried that context manager would invite.
msg228078 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2014-10-01 14:55
Aye, suppress is only intended for use around a single line of code. Using it for an entire function would be "OnError Resume Next" levels of poor coding style.

I'm also -1 on implicitly wrapping redirect_stdout around functions due to the immediate thread safety problem doing so introduces. The stdout redirection really only makes sense in a single-threaded scripting context, and using it as a decorator rather than inline makes it far too easy to inadvertently violate that constraint.

As far as the "explicitly inherit from object or not" goes, it wouldn't surprise me if that's just a "migrated from Python 2" vs "first introduced in Python 3" distinction. It's not something I would change solely for the sake of consistency.
History
Date User Action Args
2014-10-01 14:55:08ncoghlansetstatus: open -> closed
resolution: rejected
messages: + msg228078

stage: resolved
2014-10-01 14:45:11r.david.murraysetnosy: + r.david.murray
messages: + msg228075
2014-10-01 11:52:28berker.peksagsetnosy: + ncoghlan
2014-10-01 09:50:23Antony.Leecreate