python: cs_disasm*() now use generator rather than a list to return disassembled instructions. suggested by Mario Vilas.

This commit is contained in:
Nguyen Anh Quynh 2013-12-31 22:55:27 +08:00
parent 19b0de3c8d
commit ef709f0272

View File

@ -242,15 +242,15 @@ def cs_disasm_quick(arch, mode, code, offset, count = 0):
if status != CS_ERR_OK:
raise CsError(status)
insns = []
all_insn = ctypes.POINTER(_cs_insn)()
res = _cs.cs_disasm_ex(csh, code, len(code), offset, count, ctypes.byref(all_insn))
if res > 0:
for i in xrange(res):
insns.append(CsInsn(_dummy_cs(csh, arch), all_insn[i]))
yield CsInsn(_dummy_cs(csh, arch), all_insn[i])
_cs.cs_free(all_insn, res)
else:
yield []
status = _cs.cs_errno(csh)
if status != CS_ERR_OK:
raise CsError(status)
@ -259,7 +259,6 @@ def cs_disasm_quick(arch, mode, code, offset, count = 0):
if status != CS_ERR_OK:
raise CsError(status)
return insns
# Python-style class to disasm code
class CsInsn(object):
@ -400,17 +399,15 @@ class Cs(object):
self._mode = opt
def disasm(self, code, offset, count = 0):
insns = []
all_insn = ctypes.POINTER(_cs_insn)()
res = _cs.cs_disasm_ex(self.csh, code, len(code), offset, count, ctypes.byref(all_insn))
if res > 0:
for i in xrange(res):
insns.append(CsInsn(self, all_insn[i]))
yield CsInsn(self, all_insn[i])
_cs.cs_free(all_insn, res)
else:
yield []
status = _cs.cs_errno(self.csh)
if status != CS_ERR_OK:
raise CsError(status)
return insns