[Greg Ewing] > I don't think you'd really be breaking any promises. > After all, if someone wrote > > def asdf(): > try: > something_that_never_returns() > finally: > ... > > they wouldn't have much ground for complaint that the > finally never got executed. The case we're talking about > seems much the same situation. Not to me -- you can't write something_that_never_returns() in Python unless the program runs forever, you crash the system, you get the thread stuck in deadlock or permanent starvation, or you're anti-social by calling os._exit() (sys.exit() is fine: it raises SystemExit, and pending finally blocks get run then). All of those are highly exceptional use cases; everyone else is guaranteed their finally block will eventually run. > I take it you usually provide a method for explicit cleanup. Yup. > How about giving generator-iterators one, then, called > maybe close() or abort(). The effect would be to raise > an appropriate exception at the point of the yield, > triggering any except or finally blocks. As before, I'm already happy; sharing state via instance variables is all "the solution" I've felt a need for. If consensus is that something needs to be done here anyway, I'd rather think of generators more as threads of control than as lumps of data with attributes. From that view, I think it would be easier to make a coherent case that generators should support a termination protocol involving raising SystemExit. But then that should apply to all thread-like objects too, and there's no way now for one thread to raise SystemExit in another (but it's arguable that there should be). > This method could even be added to the general iterator > protocol (implementing it would be optional). It would > then provide a standard name for people to use for > cleanup methods in their own iterator classes. Generalizing from zero examples <wink>?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4