26.07.19 21:52, Bruce Leban пише:

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


Besides an unusual for Python layout (a clause has different indentation than the initial clause of the statement to which it belongs) there is other problem. The exception block is not the part of the "for" or "with" block. After handling an exception in the "for" clause you do not continue to execute the "for" block, but leave the loop. After handling an exception in the "with" clause you do not continue to execute the "with" block and do not call `__exit__` when leave it. To me, this syntax is much more confusing than my initial proposition.