classification
Title: Simplify bytecodes for try-finally, try-except and with blocks.
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Mark.Shannon, cheryl.sabella, gregory.p.smith, iritkatriel, miss-islington, ncoghlan, serhiy.storchaka, vstinner, xmorel
Priority: normal Keywords: patch

Created on 2018-04-29 18:28 by Mark.Shannon, last changed 2021-01-26 10:27 by iritkatriel. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6641 merged Mark.Shannon, 2018-04-29 18:36
PR 17318 merged Mark.Shannon, 2019-11-21 10:31
PR 18099 merged vstinner, 2020-01-21 10:25
PR 22723 merged Mark.Shannon, 2020-10-16 16:28
PR 22765 merged miss-islington, 2020-10-19 13:08
PR 24334 merged iritkatriel, 2021-01-25 21:03
Messages (14)
msg315905 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2018-04-29 18:28
The six complex bytecodes currently used for implementing 'with' and 'try' statements can be replaced with just two simpler bytecodes.
The six bytecodes are WITH_CLEANUP_START, WITH_CLEANUP_FINISH,
BEGIN_FINALLY, END_FINALLY, CALL_FINALLY and POP_FINALLY.
They can be replaced with RERAISE and WITH_EXCEPT_FINISH.

See https://bugs.python.org/issue32949 for more details of the new bytecodes and how they are used in the 'with' statement.

The try-finally statement can be implemented broadly as 
   SETUP_FINALLY except
   try-body
   POP_BLOCK
   finally-body
   JUMP exit
except:
   finally-body
exit:
msg339817 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-04-09 23:32
What would be the best way to resolve the discussion between this and issue 32949?  Would it be good to try to get one of them into 3.8?
msg357147 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-11-21 10:06
Mark merged his PR 6641 but forgot to mention bpo-33387:

commit fee552669f21ca294f57fe0df826945edc779090
Author: Mark Shannon <mark@hotpy.org>
Date:   Thu Nov 21 09:11:43 2019 +0000

    Produce cleaner bytecode for 'with' and 'async with' by generating separate code for normal and exceptional paths. (#6641)
    
    Remove BEGIN_FINALLY, END_FINALLY, CALL_FINALLY and POP_FINALLY bytecodes. Implement finally blocks by code duplication.
    Reimplement frame.lineno setter using line numbers rather than bytecode offsets.
msg357148 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-11-21 10:07
The new doc seems to use Python version 3.8, but the change was merged into master which is the future Python 3.9, no?

 .. opcode:: RERAISE 

     Re-raises the exception currently on top of the stack. 

     .. versionadded:: 3.8
msg357155 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2019-11-21 10:32
Thanks for noticing.
https://github.com/python/cpython/pull/17318
msg357172 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2019-11-21 14:47
New changeset 82f897bf8f72d09f537054d64a94e645ad23d8d6 by Mark Shannon in branch 'master':
Correct release version to 3.9 for RERAISE and WITH_EXCEPT_START bytecodes. (#17318)
https://github.com/python/cpython/commit/82f897bf8f72d09f537054d64a94e645ad23d8d6
msg357246 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2019-11-22 06:35
sweet! =)
msg357279 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-11-22 13:28
Can this issue be closed now?
msg360393 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-21 11:47
New changeset 629023c05be24fa2f01c914c739aaa5a61a0304c by Victor Stinner in branch 'master':
bpo-33387: Fix compiler warning in frame_block_unwind() (GH-18099)
https://github.com/python/cpython/commit/629023c05be24fa2f01c914c739aaa5a61a0304c
msg378715 - (view) Author: Xavier Morel (xmorel) * Date: 2020-10-16 14:06
The 3.9 changelog mentions WITH_EXCEPT_FINISH but that seems to have ultimately been called WITH_EXCEPT_START, maybe it shoudl be updated also?
msg378720 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2020-10-16 16:47
Yes, thanks for pointing that out.
msg385664 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-01-25 21:03
I'm reopening this to delete an obsolete comment left behind.
msg385679 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-01-25 23:23
There's another place that needs to be updated: https://docs.python.org/3/library/dis.html#opcode-SETUP_WITH

need to replace WITH_CLEANUP_START by WITH_EXCEPT_START
msg385703 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-01-26 10:17
New changeset dea5bf9d15999bfcc58095b157c0678d45b00bdd by Irit Katriel in branch 'master':
bpo-33387: update documentation for exception handling opcode changes (GH-24334)
https://github.com/python/cpython/commit/dea5bf9d15999bfcc58095b157c0678d45b00bdd
History
Date User Action Args
2021-01-26 10:27:14iritkatrielsetstatus: open -> closed
2021-01-26 10:17:17Mark.Shannonsetmessages: + msg385703
2021-01-25 23:23:07iritkatrielsetmessages: + msg385679
2021-01-25 23:14:57iritkatrielsetstatus: closed -> open
2021-01-25 23:10:52iritkatrielsetstatus: open -> closed
stage: patch review -> resolved
2021-01-25 21:03:21iritkatrielsetstage: resolved -> patch review
pull_requests: + pull_request23153
2021-01-25 21:03:11iritkatrielsetstatus: closed -> open
nosy: + iritkatriel
messages: + msg385664

2020-10-19 13:08:51miss-islingtonsetnosy: + miss-islington

pull_requests: + pull_request21729
2020-10-16 16:47:32Mark.Shannonsetmessages: + msg378720
2020-10-16 16:28:49Mark.Shannonsetpull_requests: + pull_request21689
2020-10-16 14:06:24xmorelsetnosy: + xmorel
messages: + msg378715
2020-01-21 11:47:33vstinnersetmessages: + msg360393
2020-01-21 10:25:20vstinnersetpull_requests: + pull_request17488
2019-12-30 11:19:02Mark.Shannonsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-11-22 13:28:55vstinnersetmessages: + msg357279
2019-11-22 06:35:20gregory.p.smithsetmessages: + msg357246
versions: + Python 3.9, - Python 3.8
2019-11-21 14:47:56Mark.Shannonsetmessages: + msg357172
2019-11-21 10:32:28Mark.Shannonsetmessages: + msg357155
2019-11-21 10:31:23Mark.Shannonsetpull_requests: + pull_request16805
2019-11-21 10:07:05vstinnersetmessages: + msg357148
2019-11-21 10:06:22vstinnersetnosy: + vstinner
messages: + msg357147
2019-04-09 23:32:02cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg339817
2018-07-08 19:44:06gregory.p.smithsetnosy: + gregory.p.smith, ncoghlan, serhiy.storchaka
2018-04-29 18:36:03Mark.Shannonsetkeywords: + patch
stage: patch review
pull_requests: + pull_request6337
2018-04-29 18:31:10Mark.Shannonsetmessages: - msg315906
2018-04-29 18:30:39Mark.Shannonsetmessages: + msg315906
2018-04-29 18:28:42Mark.Shannoncreate