Possible File iteration bug

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jul 15 23:42:40 EDT 2011

Billy Mays wrote:

> I was thinking that a convenient solution to this problem would be to
> introduce a new Exception call PauseIteration, which would signal to the
> caller that there is no more data for now, but not to close down the
> generator entirely.

It never fails to amuse me how often people consider it "convenient" to add
new built-in functionality to Python to solve every little issue. As
pie-in-the-sky wishful-thinking, it can be fun, but people often mean it to
be taken seriously.

Okay, we've come up with the solution of a new exception, PauseIteration,
that the iterator protocol will recognise. Now we have to:

- write a PEP for it, setting out the case for it;
- convince the majority of CPython developers that the idea is a good one,
  which might mean writing a proof-of-concept version;
- avoid having the Jython, IronPython and PyPy developers come back and say
  that it is impossible under their implementations;
- avoid having Guido veto it;
- write an implementation or patch adding that functionality;
- try to ensure it doesn't cause any regressions in the CPython tests;
- fix the regressions that do occur despite our best efforts;
- ensure that there are no backwards compatibility issues to be dealt with;
- write a test suite for it;
- write documentation for it;
- unless we're some of the most senior Python developers, have the patch
  reviewed before it is accepted;
- fix the bugs that have come to light since the first version;
- make sure copyright is assigned to the Python Software Foundation;
- wait anything up to a couple of years for the latest version of Python,
  including the patch, to be released as production-ready software;
- upgrade our own Python installation to use the latest version, if we can
  and aren't forced to stick with an older version

and now, at long last, we can use this convenient feature in our own code!
Pretty convenient, yes?

(If you think I exaggerate, consider the "yield from" construct, which has
Guido's support and was pretty uncontroversial. Two and a half years later,
it is now on track to be added to Python 3.3.)

Or you can look at the various recipes on the Internet for writing tail-like
file viewers in Python, and solve the problem the boring old fashioned way.
Here's one that blocks while the file is unchanged:


Modifying it to be non-blocking should be pretty straightforward -- just add
a `yield ""` after the `if not line`.


More information about the Python-list mailing list