how to import my function to python file and get input from it?
我有一个名为analyze()的函数,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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() |
此代码函数获取以下输入并尝试分析它:
1 2 | 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做了以下事情:
1 2 3 4 5 | from analyze import analyze CODE = b"\xc1\xe0\x05" irsb = pyvex.block.IRSB(CODE, 0x80482f0, archinfo.ArchAMD64()) analyze() |
但是,当我运行array.py时,它会因为错误而阻止我;
1 | NameError: name 'CODE' is not defined |
我该如何解决这个问题? 解决办法是什么?
在功能上进行简单更改,添加参数:
1 2 3 | def analyze(irsb): # irsb here called parameter ... # The rest is the same |
然后在调用它时传递参数:
1 2 3 4 5 | 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 |
我刚刚将