[issue13585] Add contextlib.CleanupManager

2011-12-21 Thread Nick Coghlan
Nick Coghlan 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 way that doesn't ne

[issue13585] Add contextlib.CleanupManager

2011-12-21 Thread Nikolaus Rath
Nikolaus Rath added the comment: On 12/21/2011 12:03 PM, Éric Araujo wrote: > > Éric Araujo 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 sti

[issue13585] Add contextlib.CleanupManager

2011-12-21 Thread Éric Araujo
Éric Araujo 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 the OS-level

[issue13585] Add contextlib.CleanupManager

2011-12-21 Thread Nikolaus Rath
Nikolaus Rath added the comment: On 12/21/2011 11:46 AM, Éric Araujo wrote: > > Éric Araujo 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. > > In the passage I quoted,

[issue13585] Add contextlib.CleanupManager

2011-12-21 Thread Éric Araujo
Éric Araujo 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 a feeling that i

[issue13585] Add contextlib.CleanupManager

2011-12-13 Thread Sven Marnach
Sven Marnach 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 files:

[issue13585] Add contextlib.CleanupManager

2011-12-13 Thread Nick Coghlan
Nick Coghlan 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 of others) once tha

[issue13585] Add contextlib.CleanupManager

2011-12-13 Thread Raymond Hettinger
Raymond Hettinger added the comment: Thanks Nick. You're awesome. -- ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubs

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nick Coghlan
Nick Coghlan 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

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nick Coghlan
Nick Coghlan 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 thoroughly vetted befo

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nick Coghlan
Nick Coghlan 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 unittest API. For i

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Raymond Hettinger
Changes by Raymond Hettinger : -- assignee: -> rhettinger resolution: -> later ___ Python tracker ___ ___ Python-bugs-list mailing l

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Raymond Hettinger
Raymond Hettinger 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 like this being used in prod

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nick Coghlan
Nick Coghlan 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 precedent of both t

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Julian Berman
Julian Berman 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: with contextfactory

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Eric Snow
Eric Snow added the comment: Check out: http://code.activestate.com/recipes/ -- nosy: +eric.snow ___ Python tracker ___ ___ Python-bu

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nikolaus Rath
Nikolaus Rath 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" means :-). Is t

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Sven Marnach
Sven Marnach 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. You can add a clean

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Giampaolo Rodola'
Changes by Giampaolo Rodola' : -- nosy: +giampaolo.rodola ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: http:

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nikolaus Rath
Changes by Nikolaus Rath : Added file: http://bugs.python.org/file23933/CleanupManager_patch_v2.diff ___ Python tracker ___ ___ Python-bugs-li

[issue13585] Add contextlib.CleanupManager

2011-12-12 Thread Nikolaus Rath
Nikolaus Rath added the comment: On 12/11/2011 10:17 PM, Raymond Hettinger wrote: > > Raymond Hettinger added the comment: > > ''' > Example code: > > with CleanupManager() als mngr: > tmpdir = tempfile.mkdtemp() > mngr.register(shutil.rmtree(tmpdir)) <-- this makes the call right aw

[issue13585] Add contextlib.CleanupManager

2011-12-11 Thread Raymond Hettinger
Raymond Hettinger 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 is that the idea and code n

[issue13585] Add contextlib.CleanupManager

2011-12-11 Thread Nikolaus Rath
Nikolaus Rath 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? Try/finally: I think th

[issue13585] Add contextlib.CleanupManager

2011-12-11 Thread Raymond Hettinger
Raymond Hettinger 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 registration of shutil.rmtree

[issue13585] Add contextlib.CleanupManager

2011-12-11 Thread Nikolaus Rath
Nikolaus Rath 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: http://bugs.python.org/file23923/CleanupManager_patch_v1.diff

[issue13585] Add contextlib.CleanupManager

2011-12-11 Thread Nikolaus Rath
New submission from Nikolaus Rath : 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