Skip to content

Commit

Permalink
python: add error message to CsError and modify all tests to use CsEr…
Browse files Browse the repository at this point in the history
…ror exception
  • Loading branch information
aquynh committed Dec 6, 2013
1 parent 73a6dba commit f1618bc
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 25 deletions.
2 changes: 1 addition & 1 deletion bindings/python/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from capstone import Cs, cs_disasm_quick, CS_ARCH_ARM, CS_ARCH_ARM64, CS_ARCH_MIPS, CS_ARCH_X86, CS_MODE_LITTLE_ENDIAN, CS_MODE_ARM, CS_MODE_THUMB, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL, CS_MODE_16, CS_MODE_32, CS_MODE_64, CS_OPT_SYNTAX_ATT, CS_MODE_BIG_ENDIAN
from capstone import Cs, CsError, cs_disasm_quick, CS_ARCH_ARM, CS_ARCH_ARM64, CS_ARCH_MIPS, CS_ARCH_X86, CS_MODE_LITTLE_ENDIAN, CS_MODE_ARM, CS_MODE_THUMB, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL, CS_MODE_16, CS_MODE_32, CS_MODE_64, CS_OPT_SYNTAX_ATT, CS_MODE_BIG_ENDIAN
2 changes: 1 addition & 1 deletion bindings/python/capstone/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from capstone import Cs, cs_disasm_quick, cs_version, CS_ARCH_ARM, CS_ARCH_ARM64, CS_ARCH_MIPS, CS_ARCH_X86, CS_MODE_LITTLE_ENDIAN, CS_MODE_ARM, CS_MODE_THUMB, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL, CS_MODE_16, CS_MODE_32, CS_MODE_64, CS_OPT_SYNTAX_ATT, CS_MODE_BIG_ENDIAN, CS_MODE_MICRO, CS_MODE_N64
from capstone import Cs, CsError, cs_disasm_quick, cs_version, CS_ARCH_ARM, CS_ARCH_ARM64, CS_ARCH_MIPS, CS_ARCH_X86, CS_MODE_LITTLE_ENDIAN, CS_MODE_ARM, CS_MODE_THUMB, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL, CS_MODE_16, CS_MODE_32, CS_MODE_64, CS_OPT_SYNTAX_ATT, CS_MODE_BIG_ENDIAN, CS_MODE_MICRO, CS_MODE_N64
19 changes: 16 additions & 3 deletions bindings/python/capstone/capstone.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ class CsError(Exception):
def __init__(self, errno):
self.errno = errno

def __str__(self):
messages = { \
CS_ERR_MEM: "Out of memory (CsError)",
CS_ERR_ARCH: "Invalid architecture (CsError)",
CS_ERR_HANDLE: "Invalid handle (CsError)",
CS_ERR_CSH: "Invalid csh (CsError)",
CS_ERR_MODE: "Invalid mode (CsError)",
CS_ERR_OPTION: "Invalid option (CsError)",
}
return messages[self.errno]


# quick & dirty Python function to disasm raw binary code
def cs_disasm_quick(arch, mode, code, offset, count = 0):
Expand Down Expand Up @@ -268,6 +279,7 @@ def __init__(self, arch, mode):
self.csh = ctypes.c_size_t()
status = _cs.cs_open(arch, mode, ctypes.byref(self.csh))
if status != CS_ERR_OK:
self.csh = None
raise CsError(status)

if arch == CS_ARCH_X86:
Expand All @@ -277,9 +289,10 @@ def __init__(self, arch, mode):
self._syntax = None

def __del__(self):
status = _cs.cs_close(self.csh)
if status != CS_ERR_OK:
raise CsError(status)
if self.csh:
status = _cs.cs_close(self.csh)
if status != CS_ERR_OK:
raise CsError(status)

#def option(self, opt_type, opt_value):
# return _cs.cs_option(self.csh, opt_type, opt_value)
Expand Down
4 changes: 2 additions & 2 deletions bindings/python/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def test_class():

print("0x%x:" % (insn.address + insn.size))
print
except:
print("ERROR: Arch or mode unsupported!")
except CsError as e:
print("ERROR: %s" %e)


#test_cs_disasm_quick()
Expand Down
4 changes: 2 additions & 2 deletions bindings/python/test_arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def print_insn_detail(insn):
print_insn_detail(insn)
print
print "0x%x:\n" % (insn.address + insn.size)
except:
print("ERROR: Arch or mode unsupported!")
except CsError as e:
print("ERROR: %s" %e)


test_class()
4 changes: 2 additions & 2 deletions bindings/python/test_arm64.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def print_insn_detail(insn):
print_insn_detail(insn)
print
print "0x%x:\n" % (insn.address + insn.size)
except:
print("ERROR: Arch or mode unsupported!")
except CsError as e:
print("ERROR: %s" %e)


test_class()
5 changes: 2 additions & 3 deletions bindings/python/test_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,15 @@ def print_detail(insn):
if syntax != 0:
md.syntax = syntax


for insn in md.disasm(code, 0x1000):
print("0x%x:\t%s\t%s // insn-ID: %u, insn-mnem: %s" \
%(insn.address, insn.mnemonic, insn.op_str, insn.id, \
insn.insn_name()))
print_detail(insn)

print
except:
print("ERROR: Arch or mode unsupported!")
except CsError as e:
print("ERROR: %s" %e)


test_class()
4 changes: 2 additions & 2 deletions bindings/python/test_mips.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def print_insn_detail(insn):
print

print "0x%x:\n" %(insn.address + insn.size)
except:
print("ERROR: Arch or mode unsupported!")
except CsError as e:
print("ERROR: %s" %e)


test_class()
21 changes: 12 additions & 9 deletions bindings/python/test_x86.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,18 @@ def print_insn_detail(mode, insn):
print("Code: %s" % to_hex(code))
print("Disasm:")

md = Cs(arch, mode)

if syntax != 0:
md.syntax = syntax

for insn in md.disasm(code, 0x1000):
print_insn_detail(mode, insn)
print
print ("0x%x:\n" % (insn.address + insn.size))
try:
md = Cs(arch, mode)

if syntax != 0:
md.syntax = syntax

for insn in md.disasm(code, 0x1000):
print_insn_detail(mode, insn)
print
print ("0x%x:\n" % (insn.address + insn.size))
except CsError as e:
print("ERROR: %s" %e)


test_class()

0 comments on commit f1618bc

Please sign in to comment.