[issue18677] Enhanced context managers with ContextManagerExit and None

2022-01-03 Thread Kristján Valur Jónsson
Kristján Valur Jónsson added the comment: Having given this some thougt, years laters, I believe it _is_ possible to write nested() (and nested_delayed()) in a correct way in python, without the ContextManagerExit function. Behold! import contextlib @contextlib.contextmanager def nested_de

[issue18677] Enhanced context managers with ContextManagerExit and None

2022-01-03 Thread Kristján Valur Jónsson
Kristján Valur Jónsson added the comment: Great throwback. As far as I know, context managers are still not first class citizens. You cannot _compose_ two context managers into a new one programmatically in the language, in the same way that you can, for instance, compose two functions. No

[issue18677] Enhanced context managers with ContextManagerExit and None

2021-12-27 Thread Alex Waygood
Alex Waygood added the comment: Given that this issue has seen no activity for eight years, I am closing it as "rejected". -- nosy: +AlexWaygood resolution: -> rejected stage: -> resolved status: open -> closed ___ Python tracker

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-14 Thread Nick Coghlan
Nick Coghlan added the comment: I think you make a good case, but I already tried and failed to convince Guido of this in PEP 377 (see http://www.python.org/dev/peps/pep-0377/#rationale-for-change) More importantly, see his quoted concerns in http://mail.python.org/pipermail/python-dev/2009-M

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-14 Thread Kristján Valur Jónsson
Kristján Valur Jónsson added the comment: "locally visible" is, I think a very misleading term. How is with ignore_error, acquire_resource as r: doo_stuff_with_resource(r) #can be silently skipped any more locally visible than with acquire_resource_ignore_error as r: doo_stuff_with reso

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-12 Thread Barry A. Warsaw
Changes by Barry A. Warsaw : -- nosy: +barry ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-08 Thread Nick Coghlan
Nick Coghlan added the comment: Allowing a context manager to skip the statement body isn't a new proposal, and I previously argued your side. However, with multiple context managers, there is no invisible flow control. Two context managers are locally visible, which means the outer one completel

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-08 Thread Kristján Valur Jónsson
Kristján Valur Jónsson added the comment: Using my latest patch, the ExitStack inline example can be rewritten: with ExitStack() as stack: files = [stack.enter_context(open(fname)) for fname in filenames] # All opened files will automatically be closed at the end of

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-08 Thread Kristján Valur Jónsson
Kristján Valur Jónsson added the comment: Thanks, Eric. I read that bit and I can't say that I disagree. And I'm not necessarily advocating that "skipping the body" become a standard feature of context managers. But it is a necessary functionality if you want to be able to dynamically nest one

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-08 Thread Kristján Valur Jónsson
Kristján Valur Jónsson added the comment: I've modified the patch. The problem that nested_delayed was trying to solve are "hybrid" context managers, ones that allocate resources during __init__ and release them at exit. A proper context manager should allocate resources during __enter__, an

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread Eric Snow
Eric Snow added the comment: Nick was probably talking about what is further elaborated in PEP 343. I'd recommend taking a particular look at the "Motivation and Summary" section regarding flow control macros. -- nosy: +eric.snow ___ Python tracker

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread Kristján Valur Jónsson
Kristján Valur Jónsson added the comment: Hi there. "allowing suppression of exceptions from __enter__ hides local control flow by blurring the boundaries between with and if statements. " I'm not sure what this means. To me, it is a serious language design flaw that you can write a context man

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread Nick Coghlan
Nick Coghlan added the comment: I pitched the idea of making it possible to skip the with statement body quite some time ago, and Guido convinced me it was a bad idea for much the same reason he chose PEP 343 over his original PEP 340 design: allowing suppression of exceptions from __enter__ hide

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread Kristján Valur Jónsson
Kristján Valur Jónsson added the comment: Simply put, there is no way in the language to nest two context managers, even though we have full access to their implementation model, i.e. can call __enter__ and __exit__ manually. This reflects badly (pun intended) on Python's reflection and intro

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread R. David Murray
R. David Murray added the comment: Raising it on python-ideas sounds like a good idea, then. I must admit that I don't understand what you mean by "combining existing context managers into a nested one" that isn't addressed by ExitStack. -- ___ Pyth

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread Kristján Valur Jónsson
Kristján Valur Jónsson added the comment: IMHO, exitstack is not a very nice construct. It's implementation is far longer than contextlib.nested. And the chief problem still remains, which has not been addressed until this patch (as far as I know): In Python, it is impossible to combine exist

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread R. David Murray
R. David Murray added the comment: Your use cases are either already addressed by contextlib.ExitStack, or should be addressed in the context of its existence. It is the replacement for contextlib.nested. -- nosy: +ncoghlan, r.david.murray ___ Pyth

[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread Kristján Valur Jónsson
New submission from Kristján Valur Jónsson: A proposed patch adds two features to context managers: 1)It has always irked me that it was impossible to assemble nested context managers in the python language. See issue #5251. The main problem, that exceptions in __enter__ cannot be properly hand