python - 组装字节码 - PyCode New

以下代码将字节码输出生成为字符串。我想替换字节码的一些行并重新组装并执行它。我是否需要像 ByteCodeAssember 之类的东西,或者我可以使用 PyCode New 来做到这一点吗?

http://docs.python.org/2/c-api/code.html#PyCode_New

http://pypi.python.org/pypi/BytecodeAssembler

输出

**********      code    **********                                                                                   

type: <type 'str'>                                                                                                   
def test(a,b):                                                                                                       
    return a*b                                                                                                       
test(2,3)                                                                                                            

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

type: <type 'code'>                                                                                                  
  1           0 LOAD_CONST               0 (<code object test at 0x101d1ca30, file "<string>", line 1>)              
              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'

代码

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,'<string>','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)  

最佳答案

Byteplay 是 python 代码对象的一个​​很好的包装器。它有自己的代码类。代码对象是 CPython 特定的并且更复杂。要开始使用 byteplay 肯定会更好。

from byteplay import *                                                                                 

def foo():                                                                                             
    x = 10                                                                                             
    print 'inside ',x                                                                                  
    return 42                                                                                          

c = Code.from_code(foo.func_code)                                                                      
print c.code                                                                                           

ret = foo()                                                                                            
print 'outside: ',ret                                                                                  

c.code[10] = (LOAD_CONST,1000)                                                                         

foo.func_code = c.to_code()                                                                            
print '*'*20                                                                                           
print c.code                                                                                           

ret = foo()                                                                                            

print 'outside: ',ret  

产生

  4           1 LOAD_CONST           10
              2 STORE_FAST           x

  5           4 LOAD_CONST           'inside '
              5 PRINT_ITEM           
              6 LOAD_FAST            x
              7 PRINT_ITEM           
              8 PRINT_NEWLINE        

  6          10 LOAD_CONST           42
             11 RETURN_VALUE         

inside  10
outside:  42
********************

  4           1 LOAD_CONST           10
              2 STORE_FAST           x

  5           4 LOAD_CONST           'inside '
              5 PRINT_ITEM           
              6 LOAD_FAST            x
              7 PRINT_ITEM           
              8 PRINT_NEWLINE        

  6          10 LOAD_CONST           1000
             11 RETURN_VALUE         

inside  10
outside:  1000

https://stackoverflow.com/questions/13251882/

相关文章:

sql - 当一个值为 null 时未拾取不匹配

.net - 即使所有新发布者都有签名证书,未知发布者警告是否真的会出现?

php - 在 foreach 循环中使用数组变量

c# - 将 DependencyProperty 转发到用户控件中包含的控件

ruby-on-rails - 在 rails 3 中连接两个表

python - 将 Python 正则表达式用于 Twitter 数据

authentication - 如何使用 JavaMail 调试 SMTP 身份验证错误?

google-chrome - "Extension Install Failure: Packag

c# - 执行相机预览镜像模式的正确方法

python - 从 `scipy.ndimage.map_coordinates` 输出向量/数组