New submission from Nikolaus Rath <nikol...@rath.org>:

I'd like to propose addding the CleanupManager class described in 
http://article.gmane.org/gmane.comp.python.ideas/12447 to the contextlib 
module. The idea is to add a general-purpose context manager to manage (python 
or non-python) resources that don't come with their own context manager.

Example code:

with CleanupManager() als mngr:
    tmpdir = tempfile.mkdtemp()
    mngr.register(shutil.rmtree(tmpdir))
    # do stuff with tmpdir

# shutil.rmtree() get's called automatically when the block is over

Note that mkdtemp() could of course also be changed to become its own context 
manager. The idea is to provide a general facility for this kind of problem, so 
it doesn't have to be reinvented whenever a module provides a ressource without 
its own context manager. Other possible uses are of course ressources that are 
completely external to Python,
e.g. anything allocated with a subprocess (think of 
subprocess.check_call('mount'))/


I'll be happy to make a proper patch with documentation and testcases from 
Jan's code. As a matter of fact, I'll probably start working out it right now, 
so please let me know quickly if this doesn't have a chance of getting accepted.

----------
components: Library (Lib)
messages: 149268
nosy: Nikratio
priority: normal
severity: normal
status: open
title: Add contextlib.CleanupManager
type: feature request
versions: Python 3.3, Python 3.4

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue13585>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to