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 vstinner
Recipients vstinner
Date 2022-04-01.09:31:33
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1648805493.81.0.634478965764.issue47185@roundup.psfhosted.org>
In-reply-to
Content
Since bpo-40222 "Zero cost exception handling", code object created by from bytecode with code.replace(co_code=new_code) no longer catch exceptions on Python 3.11, unless an exception table is set explicitly.

Example:
---
def f():
    try:
        print("raise")
        raise ValueError
    except ValueError:
        print("except")
    else:
        print("else")
    print("exit func")

def g(): pass

if 1:
    code = f.__code__
    g.__code__ = g.__code__.replace(
        co_code=code.co_code,
        co_consts=code.co_consts,
        co_names=code.co_names,
        co_flags=code.co_flags,
        co_stacksize=code.co_stacksize)
else:
    g.__code__ = f.__code__  # this code path works on Python 3.11

g()
---

Output with Python 3.10 (ok):
---
raise
except
exit func
---

Output with Python 3.11 (oops):
---
raise
Traceback (most recent call last):
  ...
ValueError
---

Would it make sense to automatically compute co_exceptiontable on code.replace(co_code=new_code)? If it's computed automatically, I don't know if it makes still sense to call code.replace(co_code=new_code, co_exceptiontable=new_table).

It seems like currently, the only implementation to build an exception table lives in Python/compile.c which compiles AST to bytecode. It cannot be reused for code.replace() which takes bytecode as input, not AST.

--

If code.replace() is not updated to recompute co_exceptiontable, at least, it would be nice to document the bpo-40222 changes in What's New in Python 3.11 and in the CodeType documentation:

* https://docs.python.org/dev/library/types.html#types.CodeType
* https://docs.python.org/dev/whatsnew/3.11.html
History
Date User Action Args
2022-04-01 09:31:33vstinnersetrecipients: + vstinner
2022-04-01 09:31:33vstinnersetmessageid: <1648805493.81.0.634478965764.issue47185@roundup.psfhosted.org>
2022-04-01 09:31:33vstinnerlinkissue47185 messages
2022-04-01 09:31:33vstinnercreate