| Python-Bytecode documentation | view source | Contained in the Python-Bytecode distribution. |
Python::Bytecode - Disassemble and investigate Python bytecode
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";
}
}
}
Python::Bytecode accepts a string or filehandle contain Python
bytecode and puts it into a format you can manipulate.
disassembleThis 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.
constantsThis 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.
labelsSimilar to constants, some operations branch to labels by index
in this table.
varnamesAgain, when variables are referred to by name, the names are stored as an index into this table.
filenameThe 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.
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.
Simon Cozens, simon@cpan.org. Mutation for Python 2.3 by Dan
Sugalski dan@sidhe.org
This code is licensed under the same terms as Perl itself.
| Python-Bytecode documentation | view source | Contained in the Python-Bytecode distribution. |