I sent this message earlier but it was rejected by the mailer.


On Fri, Jul 26, 2019 at 11:27 AM Serhiy Storchaka <storchaka@gmail.com> wrote:

So you will be able to add errors handling like in:

     with connect() as stream:
         for data in stream:
             try:
                 write(data)
             except OSError:
                 handle_write_error()
         except OSError:
             handle_read_error()
     except OSError:
         handle_connection_error()

To put this in a simpler way: the proposal is to add an except clause that applies ONLY to the direct operation of the with or for statement and not to the block. That's an interesting idea.

The one thing I find confusing about your proposal is that the proposed syntax does not imply the behavior. In a try statement, the except appears at the end and after all possible statements that it could cover. The proposal mimics that syntax but with different semantics. Something like this would be much more clear what is going on:

    for VARIABLE in EXPRESSION:
        except EXCEPTION:
            BLOCK
        BLOCK

    with EXPRESSION as VARIABLE:
        except EXCEPTION:
            BLOCK
        BLOCK

    while EXPRESSION:
        except EXCEPTION:
            BLOCK
        BLOCK

or rewriting your example:

    with connect() as stream:
        except OSError:
            handle_connection_error()
        for data in stream:
            except OSError:
                handle_read_error()
            try:
                write(data)
            except OSError:
                handle_write_error()