Hello, I wanted to tell you that I wrote a Python bytecode assembler/disassembler, and would be happy if people tried it and said what they think.
I send this message to this list because this module deals with pretty low-level Python, so I thought it might interest the audience here. If I was wrong - please forgive me. The idea is to define an object which is equivalent to Python's code object, but which is easy to work with. To explain what I mean, I'll show a quick example. We can define this stupid function: >>> def f(a, b): ... print (a, b) We can convert it to an equivalent object, and see how it stores the byte code: >>> from byteplay import * >>> c = Code.from_code(f.func_code) >>> from pprint import pprint; pprint(c.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: >>> c.code[3:3] = [(ROT_TWO, None)] >>> f.func_code = c.to_code() >>> f(3, 5) (5, 3) The idea is basically the same as that of Michael Hudson's bytecodehacks, but this one works with Python 2.4 and 2.5. I also think that it's simpler to use. I borrowed some code from Phillip J. Eby's peak.util.assembler - the main difference between his package and mine is that mine lets you play with existing bytecode, not only create new code objects. I learned a lot about Python's bytecode from writing this, and I think that other may learn from it as well - I think it's much easier to understand how code objects work by understanding equivalent objects which were meant to be as simple as possible, instead of as fast as possible. I think it got pretty good testing - I patched __import__ so that after a module is imported, all function objects (found by the gc module) were disassembled and assembled again. I then managed to get the complete test suite to pass! You can download the module from http://byteplay.googlecode.com/svn/trunk/byteplay.py . I wrote a documentation (which goes into some length in purpose of explaining how bytecode works). It's on the wiki: http://wiki.python.org/moin/ByteplayDoc . I even thought that it might get to the standard library, because it seemed to me to be a pretty good documentation of bytecode details, and because it has a pretty straightforward interface. It also makes meddling with bytecode much less dangerous - for example, you can see Raymond's original recipe for binding constants at compile time (I reposted it at http://python.pastebin.com/768312) and how using byteplay makes it simple (posted at http://python.pastebin.com/768318 so you can view the diff). But, of course - it's up to you. I will be entirely satisfied if people simply find it useful. Have a good day, Noam _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com