[Python-Dev] Possible context managers in stdlib (original) (raw)

James Y Knight foom at fuhm.net
Fri Jul 8 23:11:10 CEST 2005


On Jul 8, 2005, at 4:46 PM, Brett Cannon wrote:

I think having basic context managers in a stdlib module that we know for a fact that will be handy is a good idea. We should keep the list short and poignant, but we should have something for people to work off of. The ones I like below for a 'context' module are:

* builtins: with open/file * sys: with sys.redirectedstd[in|out|err] * decimal: with decimal.Context * os: with os.currentdirectory * mutex: with mutexobj * threading: with threading.Lock with threading.Condition with threading.Event * bz2/zipfile/tarfile: with ...open

It is a really bad idea to codify the practice of modifying non- threadlocal global state like sys.std[in|out|err] and current
directory with a context manager. A user can do it to themselves now,
true, but by putting a context manager for it in the stdlib, you make
it look like it'd be a local modification when it is not. I can only
see confusion resulting from this. Users will inevitably try to use
it like with sys.redirected_stderr(f): print "hello" print "there" instead of explicitly writing to f with print>> or write(). And that
is just a terribly bad idea. It looks pretty, yes, but unless
stdinouterr are made thread-local, it's just asking for trouble.

James



More information about the Python-Dev mailing list