[Python-Dev] Possible context managers in stdlib

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Fri Jul 8 23:26:25 CEST 2005


Phillip J. Eby wrote:
> At 10:24 PM 7/8/2005 +0200, Reinhold Birkenfeld wrote:
>>        with sys.trace
> 
> Note that it's currently not possible to inspect the trace/profile hooks 
> from Python code, only from C, so that might be, um, interesting to implement.

That was beyond my short view... if it can't be implemented, it won't.

>>* pprint: with pprint.printer (used for print)
> 
> Interesting; I'm not sure if I like it.
> 
>>* os: with os.current_directory
> 
> What does this do?  Oh, I get it.  The name's not very obvious.  I would've 
> expected a more imperative name, something like 'with os.setcwd()' or 'with 
> os.pushdir()'.

I didn't think about the names too long. ;)

>>       with os.modified_env
>>       with os.umask/uid/gid etc.
> 
> Yeah, again I would expect more verbish names, but these are at least 
> easier to grasp than current_directory was.

Names can be found, of course.

>>* curses: with curses.wrapper
>>            with logging.Logger
>>* tty: with tty.raw
>>        with tty.cbreak
>>* cgitb: with cgitb.enabled
> 
> What do these do?

curses: curses.wrapper currently is a function which restores sane terminal
settings after the curses program bails out. It could be a context manager too.
Similar is tty.raw and tty.cbreak. These switch terminal modes, and in case
of an uncaught exception the terminal will stay in this state. Context managers
would restore the "sane" mode on exit.

logging.Logger: hm, I didn't think about that properly.
cgitb: enables or disables the full traceback view (Ok, it wouldn't be so useful).

Reinhold


-- 
Mail address is perfectly valid!



More information about the Python-Dev mailing list