Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

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
Release History

Release History

History Node

0.5

History Node

0.4

History Node

0.3

History Node

0.2

This version
History Node

0.1

History Node

0.0

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
bytecode-0.1-py3-none-any.whl (20.7 kB) Copy SHA256 Checksum SHA256 3.4 Wheel Feb 26, 2016
bytecode-0.1.tar.gz (15.4 kB) Copy SHA256 Checksum SHA256 Source Feb 26, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting