Assembling Bytecode - PyCode New
<p>The following code produces bytecode output as string. I want to replace some lines of the bytecode and reassemble and exec it. Do I need something like the ByteCodeAssember or can I do this with PyCode New?</p>

<p><a href="http://docs.python.org/2/c-api/code.html#PyCode_New" rel="nofollow">http://docs.python.org/2/c-api/code.html#PyCode_New</a></p>

<p><a href="http://pypi.python.org/pypi/BytecodeAssembler" rel="nofollow">http://pypi.python.org/pypi/BytecodeAssembler</a></p>

<p>output</p>

<pre><code>**********      code    **********                                                                                   

type: &lt;type 'str'&gt;                                                                                                   
def test(a,b):                                                                                                       
    return a*b                                                                                                       
test(2,3)                                                                                                            

**********      compiles into the bytecode      **********                                                           

type: &lt;type 'code'&gt;                                                                                                  
  1           0 LOAD_CONST               0 (&lt;code object test at 0x101d1ca30, file "&lt;string&gt;", line 1&gt;)              
              3 MAKE_FUNCTION            0                                                                           
              6 STORE_NAME               0 (test)                                                                    

  3           9 LOAD_NAME                0 (test)                                                                    
             12 LOAD_CONST               1 (2)                                                                       
             15 LOAD_CONST               2 (3)                                                                       
             18 CALL_FUNCTION            2                                                                           
             21 POP_TOP                                                                                              
             22 LOAD_CONST               3 (None)                                                                    
             25 RETURN_VALUE 3  

**********  bytecode    **********

'd\x00\x00\x84\x00\x00Z\x00\x00e\x00\x00d\x01\x00d\x02\x00\x83\x02\x00\x01d\x03\x00S'
</code></pre>

<p>code</p>

<pre><code>import dis                                                                                                           
import sys                                                                                                           
import inspect                                                                                                       
import new                                                                                                           

class writer:                                                                                                        
    def __init__(self):                                                                                              
        self.s = ""                                                                                                  

    def write(self,text):                                                                                            
        #print ': ',text                                                                                             
        self.s += text                                                                                               

#save stdout                                                                                                                     
origstdout = sys.stdout                                                                                              
w = writer()                                                                                                         
sys.stdout = w                                                                                                       


s = "def test(a,b):\n\treturn a*b\ntest(2,3)"                                                                        

c = compile(s,'&lt;string&gt;','exec')                                                                                     
# dis calls stdout, so output is in w                                                                                                                         
bytecode = dis.dis(c)                                                                                                

sys.stdout = origstdout                                                                                              

def h(x):                                                                                                            
    print '*'*10 + '\t' + x + '\t' + '*'*10 + '\n'*1                                                                 

h('code')                                                                                                            
print 'type: '+str(type(s))                                                                                          
print s + '\n'                                                                                                       

h('compiles into the bytecode')                                                                                      
print 'type: '+str(type(c))                                                                                          
print w.s

h('bytecode')                                                                                                        
print repr(c.co_code)  
</code></pre>
