[issue13585] Add contextlib.CleanupManager

2011-12-21 Thread Éric Araujo
Éric Araujo mer...@netwok.org added the comment: 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. I read the thread back on python-ideas and didn’t like the idea, without knowing exactly why—maybe

[issue13585] Add contextlib.CleanupManager

2011-12-21 Thread Nikolaus Rath
Nikolaus Rath nikol...@rath.org added the comment: On 12/21/2011 11:46 AM, Éric Araujo wrote: Éric Araujo mer...@netwok.org added the comment: 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.

[issue13585] Add contextlib.CleanupManager

2011-12-21 Thread Éric Araujo
Éric Araujo mer...@netwok.org added the comment: In the passage I quoted, I don’t understand what is meant by “non-Python resources”. Think about e.g. mounting a file system. Ah, ok. In that case there would still be a Python-level object (just like a Python file object will also release

[issue13585] Add contextlib.CleanupManager

2011-12-21 Thread Nikolaus Rath
Nikolaus Rath nikol...@rath.org added the comment: On 12/21/2011 12:03 PM, Éric Araujo wrote: Éric Araujo mer...@netwok.org added the comment: In the passage I quoted, I don’t understand what is meant by “non-Python resources”. Think about e.g. mounting a file system. Ah, ok. In

[issue13585] Add contextlib.CleanupManager

2011-12-21 Thread Nick Coghlan
Nick Coghlan ncogh...@gmail.com added the comment: My earlier descriptions here aren't really adequate - as soon as I started putting contextlib2 together, this CleanupManager idea quickly morphed into ContextStack [1], which is a far more powerful tool for manipulating context managers in a

[issue13585] Add contextlib.CleanupManager

2011-12-13 Thread Raymond Hettinger
Raymond Hettinger raymond.hettin...@gmail.com added the comment: Thanks Nick. You're awesome. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13585 ___

[issue13585] Add contextlib.CleanupManager

2011-12-13 Thread Nick Coghlan
Nick Coghlan ncogh...@gmail.com added the comment: And the backport: http://contextlib2.readthedocs.org/ I haven't tested on anything other than 2.7 as yet - I have an account request in train with the Shining Panda folks, so I'll set up multi-version CI for this project (along with a couple

[issue13585] Add contextlib.CleanupManager

2011-12-13 Thread Sven Marnach
Sven Marnach s...@marnach.net added the comment: I think that the fact that Nick got the code to close multiple files wrong underlines that it is difficult to get right currently. Nick's code try: files = [open(fname) for fname in names] # ... finally: for f in

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nikolaus Rath
Nikolaus Rath nikol...@rath.org added the comment: On 12/11/2011 10:17 PM, Raymond Hettinger wrote: Raymond Hettinger raymond.hettin...@gmail.com added the comment: ''' Example code: with CleanupManager() als mngr: tmpdir = tempfile.mkdtemp()

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nikolaus Rath
Changes by Nikolaus Rath nikol...@rath.org: Added file: http://bugs.python.org/file23933/CleanupManager_patch_v2.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13585 ___

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Giampaolo Rodola'
Changes by Giampaolo Rodola' g.rod...@gmail.com: -- nosy: +giampaolo.rodola ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13585 ___ ___

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Sven Marnach
Sven Marnach s...@marnach.net added the comment: There is actually a second thread on python-ideas on a very similar topic, see http://mail.python.org/pipermail/python-ideas/2011-December/013021.html The two main advantages of the proposed CleanupManager over try/finally blocks are 1.

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nikolaus Rath
Nikolaus Rath nikol...@rath.org added the comment: On 12/12/2011 03:31 PM, Sven Marnach wrote: Adding this as a cookbook recipe first seems like a good idea. This is the second time that this is mentioned, so I would be very grateful if someone could elucidate what adding as a cookbook recipe

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Eric Snow
Eric Snow ericsnowcurren...@gmail.com added the comment: Check out: http://code.activestate.com/recipes/ -- nosy: +eric.snow ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13585 ___

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Julian Berman
Julian Berman julian+python@grayvines.com added the comment: For reference, the implementation that I posted in the other thread is: @contextlib.contextmanager def maybe(got, contextfactory, *args, checkif=bool, **kwargs): if checkif(got): yield got else:

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nick Coghlan
Nick Coghlan ncogh...@gmail.com added the comment: Given the existence of tempfile.TemporaryDirectory in recent Python versions, I suggest finding a new cleanup function example that doesn't duplicate native stdlib functionality :) I do see value in the feature itself though - I believe the

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Raymond Hettinger
Raymond Hettinger raymond.hettin...@gmail.com added the comment: I think you guys need to post your code somewhere (perhaps on PyPi or the ASPN Cookbook). It needs to mature beyond the stage of I just whipped-up this code and think it would be great if everybody used it. I've seen nothing

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Raymond Hettinger
Changes by Raymond Hettinger raymond.hettin...@gmail.com: -- assignee: - rhettinger resolution: - later ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13585 ___

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nick Coghlan
Nick Coghlan ncogh...@gmail.com added the comment: TestCase.setUp() and TestCase.tearDown() were amongst the precursors to__enter__() and __exit__(). addCleanUp() fills exactly the same role here - and I've seen *plenty* of positive feedback directed towards Michael for that addition to the

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nick Coghlan
Nick Coghlan ncogh...@gmail.com added the comment: Given the history of API design errors in contextlib (cf. contextlib.nested in general, making contextlib._GeneratorContextManager a subclass of contextlib.ContextDecorator), I've realised Raymond is right in wanting to see this idea more

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nick Coghlan
Nick Coghlan ncogh...@gmail.com added the comment: In the meantime, I put my version up as a cookbook recipe: http://code.activestate.com/recipes/577981-cleanupmanager-for-with-statements/ -- ___ Python tracker rep...@bugs.python.org

[issue13585] Add contextlib.CleanupManager

2011-12-11 Thread Nikolaus Rath
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)

[issue13585] Add contextlib.CleanupManager

2011-12-11 Thread Nikolaus Rath
Nikolaus Rath nikol...@rath.org added the comment: Here's the first part of the patch with the implementation. I'll add tests and documentation as soon as someone confirms that the idea API is okay. -- keywords: +patch Added file:

[issue13585] Add contextlib.CleanupManager

2011-12-11 Thread Raymond Hettinger
Raymond Hettinger raymond.hettin...@gmail.com added the comment: I would like to see this posted as a recipe before being put in the standard library. It needs a chance to mature and to demonstrate that people will want to use it. FWIW, the example is problematic in a couple of ways. The

[issue13585] Add contextlib.CleanupManager

2011-12-11 Thread Nikolaus Rath
Nikolaus Rath nikol...@rath.org added the comment: Not sure what you mean with posted as a recipe -- are you thinking of a specific website/mailing list? Example: which one do you mean? The one in the issue or the one in the patch? With statement: what advantages do you have in mind?

[issue13585] Add contextlib.CleanupManager

2011-12-11 Thread Raymond Hettinger
Raymond Hettinger raymond.hettin...@gmail.com added the comment: ''' Example code: with CleanupManager() als mngr: tmpdir = tempfile.mkdtemp() mngr.register(shutil.rmtree(tmpdir)) -- this makes the call right away # do stuff with tmpdir ''' The part of my note that should be clear