如何将我的函数导入python文件并从中获取输入?

时间:2017-09-21 16:10:37

标签: python

我有一个名为analyze()的函数,如下所示:

def analyze():
    for stmt in irsb.statements:
        if isinstance(stmt, pyvex.IRStmt.WrTmp):
           wrtmp(stmt)
        if isinstance(stmt, pyvex.IRStmt.Store):
           address = stmt.addr
           address1 = '{}'.format(address)[1:]
           print address1
           data = stmt.data
           data1 = '{}'.format(data)[1:]
           tmp3 = store64(address1, int64(data1))
        if isinstance(stmt, pyvex.IRStmt.Put):
           expr = stmt.expressions[0]
           putoffset = stmt.offset
           data =  stmt.data
           data4 = '{}'.format(data)[1:]
           if (str(data).startswith("0x")):
             #const_1 = ir.Constant(int64, data4)
             tmp = put64(putoffset, ZERO_TAG)
           else:
             put64(putoffset, int64(data4))
             if isinstance(stmt.data, pyvex.IRExpr.Const):
                reg_name = irsb.arch.translate_register_name(stmt.offset, stmt.data.result_size(stmt.data.tag))
                print reg_name
        stmt.pp()

此代码函数获取以下输入并尝试分析它:

CODE = b"\xc1\xe0\x05"  
irsb = pyvex.block.IRSB(CODE, 0x80482f0, archinfo.ArchAMD64())

当这个输入在我的代码中的同一个文件中(让我们称为analyze.py)时,它可以工作,python analyze.py将使我成为输出。但是,我想创建一个单独的文件(调用array.py),在那里调用analyze并将输入放在其中并运行python array.py以获得相同的结果。我为array.py执行了以下操作:

from analyze import analyze

CODE = b"\xc1\xe0\x05" 
irsb = pyvex.block.IRSB(CODE, 0x80482f0, archinfo.ArchAMD64())
analyze()

但是,当我运行array.py时,它会因为错误而阻止我;

NameError: name 'CODE' is not defined

我该如何解决这个问题?解决方案是什么?

1 个答案:

答案 0 :(得分:2)

功能的简单更改,添加参数:

def analyze(irsb):   # irsb here called parameter
  ...
  # The rest is the same

然后在调用它时传递参数:

from analyze import analyze

CODE = b"\xc1\xe0\x05" 
irsb_as_arg = pyvex.block.IRSB(CODE, 0x80482f0, archinfo.ArchAMD64())
analyze(irsb_as_arg)   # irsb_as_arg is an argument

我刚刚将irsb更改为irsb_as_arg以引起注意,但它可以是同一个名称