本文介绍了捕获 dis.dis 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不重定向 sys.stdout 的情况下获得 dis.dis() 的输出?我试过了:

out=str(dis.dis())

out=""""""out+=str(dis.dis())

但是我很快发现它返回None.有什么办法可以解决这个问题吗?

解决方案

不幸的是,在 Python 3.4 之前的版本中,dis 模块使用打印语句到 stdout,因此它不会返回任何直接有用的东西.要么你必须重新实现 disdisassembledisassemble_string 函数,或者你临时替换 sys.stdout与捕获输出的替代方法:

导入系统从 cStringIO 导入 StringIO输出 = StringIO()标准输出 = sys.stdoutsys.stdout = 输出尝试:dis.dis()最后:sys.stdout = 标准输出out = out.getvalue()

这实际上最好使用上下文管理器来完成:

导入系统从上下文库导入上下文管理器从 cStringIO 导入 StringIO@contextmanagerdef captureStdOut(输出):标准输出 = sys.stdoutsys.stdout = 输出尝试:屈服最后:sys.stdout = 标准输出输出 = StringIO()使用 captureStdOut(out):dis.dis()打印出.getvalue()

这样,即使 dis 出现问题,您也可以保证恢复 stdout.一个小示范:

>>>输出 = StringIO()>>>使用 captureStdOut(out):... dis.dis(captureStdOut)...>>>打印出.getvalue()83 0 LOAD_GLOBAL 0 (GeneratorContextManager)3 LOAD_DEREF 0(功能)6 LOAD_FAST 0(参数)9 LOAD_FAST 1 (kwds)12 CALL_FUNCTION_VAR_KW 015 CALL_FUNCTION 118 RETURN_VALUE

在 Python 3.4 及更高版本中,相关函数采用 file 参数将输出重定向到:

from io import StringIO与 StringIO() 一样:dis.dis(文件=输出)打印(输出.getvalue())

Is there any way to get the output of dis.dis() without redirecting sys.stdout? I have tried:

out=str(dis.dis())

and

out=""""""
out+=str(dis.dis())

However I soon found out that it returns None. Is there any way to fix this?

解决方案

Unfortunately, in Python versions before 3.4 the dis module uses print statements to stdout, so it won't return anything directly useful. Either you have to re-implement the dis, disassemble and disassemble_string functions, or you temporarily replace sys.stdout with an alternative to capture the output:

import sys
from cStringIO import StringIO

out = StringIO()
stdout = sys.stdout
sys.stdout = out
try:
    dis.dis()
finally:
    sys.stdout = stdout
out = out.getvalue()

This is actually best done using a context manager:

import sys
from contextlib import contextmanager
from cStringIO import StringIO

@contextmanager
def captureStdOut(output):
    stdout = sys.stdout
    sys.stdout = output
    try:
        yield
    finally:
        sys.stdout = stdout

out = StringIO()
with captureStdOut(out):
    dis.dis()
print out.getvalue()

That way you are guaranteed to have stdout restored even if something goes wrong with dis. A little demonstration:

>>> out = StringIO()
>>> with captureStdOut(out):
...     dis.dis(captureStdOut)
... 
>>> print out.getvalue()
 83           0 LOAD_GLOBAL              0 (GeneratorContextManager)
              3 LOAD_DEREF               0 (func)
              6 LOAD_FAST                0 (args)
              9 LOAD_FAST                1 (kwds)
             12 CALL_FUNCTION_VAR_KW     0
             15 CALL_FUNCTION            1
             18 RETURN_VALUE        

In Python 3.4 and up, the relevant functions take a file parameter to redirect output to:

from io import StringIO

with StringIO() as out:
    dis.dis(file=out)
    print(out.getvalue())

这篇关于捕获 dis.dis 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 15:01