This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients Mark.Shannon, benjamin.peterson, pitrou, serhiy.storchaka
Date 2018-02-25.13:43:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1519566194.29.0.467229070634.issue32949@psf.upfronthosting.co.za>
In-reply-to
Content
There are some issues with "with"-related opcodes.

All other opcodes has constant stack effect for particular control flow. For example FOR_ITER always has the stack effect 1 if not jump (pushes the next item) and -1 if jumps (pops the iterator). The only exceptions are WITH_CLEANUP_START which pushes 1 or 2 values depending on TOS, and WITH_CLEANUP_FINISH which pops 2 or 3 values depending on values pushed by preceding WITH_CLEANUP_START. This breaks consistency and may make debugging harder.

WITH_CLEANUP_START duplicates a one of values on the stack without good reasons. Even the comment in the initial commit exposed uncertainty in this.

The proposed PR simplifies WITH_CLEANUP_START and WITH_CLEANUP_FINISH. They will be now executed only when the exception is raised. In normal case they will be replaced with calling a  function `__exit__(None, None, None)`.

    LOAD_CONST               0 ((None, None, None))
    CALL_FUNCTION_EX         0
    POP_TOP

WITH_CLEANUP_FINISH will be merged with the following END_FINALLY.

This PR is inspired by PR 5112 by Mark Shannon, but Mark goes further.

In addition to simplifying the implementation and the mental model, the PR adds a tiny bit of performance gain.

$ ./python -m perf timeit -s 'class CM:' -s '  def __enter__(s): pass' -s '  def __exit__(*args): pass' -s 'cm = CM()' -- 'with cm: pass'

Unpatched:  Mean +- std dev: 227 ns +- 6 ns
Patched:    Mean +- std dev: 205 ns +- 10 ns
History
Date User Action Args
2018-02-25 13:43:14serhiy.storchakasetrecipients: + serhiy.storchaka, pitrou, benjamin.peterson, Mark.Shannon
2018-02-25 13:43:14serhiy.storchakasetmessageid: <1519566194.29.0.467229070634.issue32949@psf.upfronthosting.co.za>
2018-02-25 13:43:14serhiy.storchakalinkissue32949 messages
2018-02-25 13:43:13serhiy.storchakacreate