On Tue, Jul 01, 2014 at 06:38:37PM +1000, Chris Angelico wrote: [...]
- Do I really think anyone *should* do this? Your subsequent comments
support this question, and the answer is resoundingly NO. CPython is
"This" being trying to micro-optimize code by bytecode-hacking.
not the sort of platform on which that kind of thing is ever worth doing. You'll get far more performance by using Cython for parts, or in some other way improving your code, than you will by hand-tweaking the Python bytecode.
I think that micro-optimization is probably the wrong reason to hack bytecodes. What I'm more interested in is exploring potential new features, or to add functionality, for example:
Adding the ability to trace individual expressions, not just lines: http://nedbatchelder.com/blog/200804/wicked_hack_python_bytecode_tracing.htm...
Exploring dynamic scoping: http://www.voidspace.org.uk/python/articles/code_blocks.shtml
A proposal from Python 2.3 days for a brand-new decorator syntax: http://code.activestate.com/recipes/286147
A (serious!) defence of GOTO in Python: http://www.dr-josiah.com/2012/04/python-bytecode-hacks-gotos-revisited.html
(although even Josiah doesn't suggest using COMEFROM :-)
I don't know that such bytecode manipulations should be provided in the standard library, and certainly not as a built-in "asm" command. But, I think that we ought to acknowledge that bytecode hacking has a role to play in the wider Python ecosystem.
I'm lead to understand that in the Java community, bytecode hacking is, perhaps not common, but accepted as something that powerusers do when all else fails:
[Aside: does Python do any sort of verification of the bytecode before executing it, as Java does?]