Message149268
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. |
|
Date |
User |
Action |
Args |
2011-12-12 01:49:50 | nikratio | set | recipients:
+ nikratio |
2011-12-12 01:49:50 | nikratio | set | messageid: <1323654590.86.0.53041507768.issue13585@psf.upfronthosting.co.za> |
2011-12-12 01:49:50 | nikratio | link | issue13585 messages |
2011-12-12 01:49:49 | nikratio | create | |
|