Dan Sugalski > Python-Bytecode-2.7 > Python::Bytecode

Download:
Python-Bytecode-2.7.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 2.7   Source  

NAME ^

Python::Bytecode - Disassemble and investigate Python bytecode

SYNOPSIS ^

    use Python::Bytecode
    my $bytecode = Python::Bytecode->new($bytecode);
    my $bytecode = Python::Bytecode->new(FH);
    for ($bytecode->disassemble) {
        print $_->[0],"\n"; # Textual representation of disassembly
    }

    foreach my $constant (@{$bytecode->constants()}) {
      if ($constant->can('disassemble')) {
        print "code constant:\n";
        for ($constant->disassemble) {
          print $_->[0], "\n";
        }
      }
    }

DESCRIPTION ^

Python::Bytecode accepts a string or filehandle contain Python bytecode and puts it into a format you can manipulate.

METHODS ^

disassemble

This is the basic method for getting at the actual code. It returns an array representing the individual operations in the bytecode stream. Each element is a reference to a three-element array containing a textual representation of the disassembly, the opcode number, (the opname() function can be used to turn this into an op name) and the argument to the op, if any.

constants

This returns an array reflecting the constants table of the code object. Some operations such as LOAD_CONST refer to constants by index in this array.

labels

Similar to constants, some operations branch to labels by index in this table.

varnames

Again, when variables are referred to by name, the names are stored as an index into this table.

filename

The filename from which this compiled bytecode is derived.

There are other methods, but you can read the code to find them. It's not hard, and besides, it's probably easiest to work off the textual representation of the disassembly anyway.

STRUCTURE ^

The structure of the decoded bytecode file is reasonably simple.

The output of the new method is an object that represents the fully parsed bytecode file. This object contains the information about the bytecode file, as well as the top-level code object for the file.

Each python code object in the bytecode file has its own perl object that represents it. This object can be disassembled, has its own constants (which themselves may be code objects) and its own variables.

The module completely decodes the bytecode object when the bytecode file is handed to the new method, but to get all the pieces of the bytecode may require digging into the constants of each code object.

PERPETRATORS ^

Simon Cozens, simon@cpan.org. Mutation for Python 2.3 by Dan Sugalski dan@sidhe.org

LICENSE ^

This code is licensed under the same terms as Perl itself.

syntax highlighting: