ANN: byteplay - a bytecode assembler/disassembler

G

Guest

Hello,

I would like to present a module that I have wrote, called byteplay.
It's a Python bytecode assembler/disassembler, which means that you can
take Python code object, disassemble them into equivalent objects which
are easy to play with, play with them, and then assemble a new,
modified, code object.

I think it's pretty useful if you like to learn more about Python's
bytecode - playing with things and seeing what happens is a nice way to
learn, I think.

Here's a quick example. We can define this stupid function:
(3, 5)

We can convert it to an equivalent object, and see how it stores the
byte code:
[(SetLineno, 2),
(LOAD_FAST, 'a'),
(LOAD_FAST, 'b'),
(BUILD_TUPLE, 2),
(PRINT_ITEM, None),
(PRINT_NEWLINE, None),
(LOAD_CONST, None),
(RETURN_VALUE, None)]

We can change the bytecode easily, and see what happens. Let's insert a
ROT_TWO opcode, that will swap the two arguments:
c.code[3:3] = [(ROT_TWO, None)]
f.func_code = c.to_code()
f(3, 5)
(5, 3)

You can download byteplay from
http://byteplay.googlecode.com/svn/trunk/byteplay.py and you can read
(and edit) the documentation at http://wiki.python.org/moin/ByteplayDoc
.. I will be happy to hear if you find it useful, or if you have any
comments or ideas.

Have a good day,
Noam Raphael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top