Rock It 《ML》JupyterLab 【丁】Code《七》語義【六】解讀‧三中

藉著

dis.Bytecode

class dis.Bytecode(x, *, first_line=None, current_offset=None)

Analyse the bytecode corresponding to a function, generator, asynchronous generator, coroutine, method, string of source code, or a code object (as returned by compile()).

This is a convenience wrapper around many of the functions listed below, most notably get_instructions(), as iterating over a Bytecode instance yields the bytecode operations as Instruction instances.

If first_line is not None, it indicates the line number that should be reported for the first source line in the disassembled code. Otherwise, the source line information (if any) is taken directly from the disassembled code object.

If current_offset is not None, it refers to an instruction offset in the disassembled code. Setting this means dis()will display a “current instruction” marker against the specified opcode.

classmethod from_traceback(tb)
Construct a Bytecode instance from the given traceback, setting current_offset to the instruction responsible for the exception.
codeobj
The compiled code object.
first_line
The first source line of the code object (if available)
dis()
Return a formatted view of the bytecode operations (the same as printed by dis.dis(), but returned as a multi-line string).
info()
Return a formatted multi-line string with detailed information about the code object, like code_info().

Changed in version 3.7: This can now handle coroutine and asynchronous generator objects.

 

玩轉『位元組碼』已不容易!還難作逆向工程實驗哩!

此所以介紹

/bytecode

Python module to modify bytecode

bytecode

Latest release on the Python Cheeseshop (PyPI) Build status of bytecode on Travis CI Code coverage of bytecode on codecov.io

bytecode is a Python module to generate and modify bytecode.

Install bytecode: python3 -m pip install bytecode. It requires Python 3.4 or newer.

Example executing print('Hello World!'):

from bytecode import Instr, Bytecode

bytecode = Bytecode([Instr("LOAD_NAME", 'print'),
                     Instr("LOAD_CONST", 'Hello World!'),
                     Instr("CALL_FUNCTION", 1),
                     Instr("POP_TOP"),
                     Instr("LOAD_CONST", None),
                     Instr("RETURN_VALUE")])
code = bytecode.to_code()
exec(code)

 

 

程式庫也。

駭客者流可以大刀闊斧呦☺

bytecode

bytecode is a Python module to generate and modify bytecode.