79537969

Date: 2025-03-27 06:13:23
Score: 0.5
Natty:
Report link

Firstly, do NOT directly use types.CodeType and use pyobject.Code instead, as constructing types.CodeType is too complex and not compatible across multiple Python versions.
The pyobject library, which can be installed via pip install pyobject, provides a high-level wrapper for code objects.

According to devguide.python.org, Python 3.11 introduced _co_code_adaptive attribute for code objects: devguide.python.org/internals/interpreter

E:\>py311
Python 3.11.8 (tags/v3.11.8:db85d51, Feb  6 2024, 22:03:32) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyobject import Code
>>> import random
>>> def f(a):return a+1
...
>>> c=Code(f.__code__)
>>> new_co_code = bytes([random.randint(0, 255) for _ in range(24)])
>>> new_c=c.copy()
>>> new_c.co_code=new_co_code
>>> new_c._code.co_code
b'2R\xaf\x19X\xd1\x00\xa1}P\x00M2\xf5z\xed\x00\x00\xab\xbf\x00\x00\x00\x00'
>>> new_c._code._co_code_adaptive
b'2R\xaf\x19X\xd1\xdb\xa1}P\xc3M2\xf5\x06\xed%\xeb\xab\xbf\x18\x0f*\xff'
>>>

The inputted co_code attribute is stored in _co_code_adaptive, while the decoded bytecode is stored in co_code.

Note: I'm the original developer of pyobject.

Reasons:
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: qfcy