Skip to main content

Python module to modify bytecode

Project description

bytecode is a Python module to modify bytecode.

Install bytecode: pip install bytecode.

bytecode only works on Python 3.

Hello World

Example running print('Hello World!'):

from bytecode import Instr, Bytecode

bytecode = Bytecode()
bytecode.extend([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)

Output:

Hello World!

API

bytecode module version string: bytecode.__version__ (ex: '0.1').

Instruction

  • Instr: abstract instruction, argument is not validated

  • ConcreteInstr: concrete instruction, argument must be an integer

Create instructions

  • Instr(name, arg=UNSET, *, lineno=None)

  • ConcreteInstr(name, arg=UNSET, *, lineno=None)

  • Instr.disassemble(code, offset)

  • ConcreteInstr.disassemble(code, offset)

Pseudo instructions

  • Label: target of jumps for Bytecode, must not be used in ConcreteBytecode

  • SetLineno: set the line number of following instructions

Bytecode

  • Bytecode: list of Instr

  • BytecodeBlocks: list of blocks, a block is a list of Instr and has a label

  • ConcreteBytecode: list of ConcreteInstr

Create bytecode

  • Bytecode.from_code(*, extended_arg_op=False)

  • BytecodeBlocks.from_code()

  • ConcreteBytecode.from_code()

Conversions

  • bytecode.to_bytecode() -> Bytecode

  • bytecode.to_concrete_bytecode() -> ConcreteBytecode

  • bytecode.to_bytecode_blocks() -> BytecodeBlocks

  • bytecode.to_code() -> types.CodeType

ChangeLog

  • 2016-02-26: Version 0.1

    • Rewrite completely the API!

  • 2016-02-23: Release 0.0

    • First public release

See also

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bytecode-0.1.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

bytecode-0.1-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

Details for the file bytecode-0.1.tar.gz.

File metadata

  • Download URL: bytecode-0.1.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for bytecode-0.1.tar.gz
Algorithm Hash digest
SHA256 99cffd74e782b888f1bead4deb5605d65538eea49bda62097ca4f8c73654f843
MD5 8f253e1ffa48dbc5b391b0ce468e0ff6
BLAKE2b-256 f4fef52a8fe674b8e821a4316182636ffefc89264648c198f53e3f653c2322cd

See more details on using hashes here.

Provenance

File details

Details for the file bytecode-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for bytecode-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c1fb58ecd99580ce64f04aa286866c475487bc53eedd868f16b30b70a184f436
MD5 36d3727f39d1238ee2b818cbbac6f225
BLAKE2b-256 f0d7a898beb731b53aede96ba3eff2eca6fe5b38660e02abea05c9fa33096a88

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page