From 0e638416284fc238bc2aed19eb9c0483b1bf7f42 Mon Sep 17 00:00:00 2001 From: elicn Date: Fri, 14 Oct 2022 00:01:26 +0300 Subject: [PATCH] Minor PEP8 and linter-friendly changes --- bindings/python/unicorn/arch/arm.py | 2 +- bindings/python/unicorn/arch/arm64.py | 2 -- bindings/python/unicorn/arch/intel.py | 4 ---- bindings/python/unicorn/arch/types.py | 1 - bindings/python/unicorn/unicorn.py | 23 +++++++++++------------ 5 files changed, 12 insertions(+), 20 deletions(-) diff --git a/bindings/python/unicorn/arch/arm.py b/bindings/python/unicorn/arch/arm.py index 4d2b262a..a914654c 100644 --- a/bindings/python/unicorn/arch/arm.py +++ b/bindings/python/unicorn/arch/arm.py @@ -14,6 +14,7 @@ from .types import UcReg128 ARMCPReg = Tuple[int, int, int, int, int, int, int] ARMCPRegValue = Tuple[int, int, int, int, int, int, int, int] + class UcRegCP(ctypes.Structure): """ARM coprocessors registers for instructions MRC, MCR, MRRC, MCRR """ @@ -57,7 +58,6 @@ class UcAArch32(Uc): return next((cls for rng, cls in reg_class if reg_id in rng), None) - def reg_read(self, reg_id: int, aux: Any = None): # select register class for special cases reg_cls = UcAArch32.__select_reg_class(reg_id) diff --git a/bindings/python/unicorn/arch/arm64.py b/bindings/python/unicorn/arch/arm64.py index 571c23e5..8cd217a2 100644 --- a/bindings/python/unicorn/arch/arm64.py +++ b/bindings/python/unicorn/arch/arm64.py @@ -91,7 +91,6 @@ class UcAArch64(Uc): return getattr(self, '_Uc__do_hook_add')(htype, fptr, begin, end, insn) - @staticmethod def __select_reg_class(reg_id: int): """Select class for special architectural registers. @@ -104,7 +103,6 @@ class UcAArch64(Uc): return next((cls for rng, cls in reg_class if reg_id in rng), None) - def reg_read(self, reg_id: int, aux: Any = None): # select register class for special cases reg_cls = UcAArch64.__select_reg_class(reg_id) diff --git a/bindings/python/unicorn/arch/intel.py b/bindings/python/unicorn/arch/intel.py index 2ef7ab0f..2cacec1b 100644 --- a/bindings/python/unicorn/arch/intel.py +++ b/bindings/python/unicorn/arch/intel.py @@ -145,7 +145,6 @@ class UcIntel(Uc): return getattr(self, '_Uc__do_hook_add')(htype, fptr, begin, end, insn) - @staticmethod def __select_reg_class(reg_id: int): """Select class for special architectural registers. @@ -161,7 +160,6 @@ class UcIntel(Uc): return next((cls for rng, cls in reg_class if reg_id in rng), None) - def reg_read(self, reg_id: int, aux: Any = None): # select register class for special cases reg_cls = UcIntel.__select_reg_class(reg_id) @@ -198,10 +196,8 @@ class UcIntel(Uc): else: self._reg_write(reg_id, reg_cls, value) - def msr_read(self, msr_id: int) -> int: return self._reg_read(const.UC_X86_REG_MSR, UcRegMSR, msr_id) - def msr_write(self, msr_id: int, value: int) -> None: self._reg_write(const.UC_X86_REG_MSR, UcRegMSR, (msr_id, value)) diff --git a/bindings/python/unicorn/arch/types.py b/bindings/python/unicorn/arch/types.py index 236c8870..c03f4ff8 100644 --- a/bindings/python/unicorn/arch/types.py +++ b/bindings/python/unicorn/arch/types.py @@ -4,7 +4,6 @@ import ctypes - uc_err = ctypes.c_int uc_mode = ctypes.c_int uc_arch = ctypes.c_int diff --git a/bindings/python/unicorn/unicorn.py b/bindings/python/unicorn/unicorn.py index a79519c4..36f57e58 100644 --- a/bindings/python/unicorn/unicorn.py +++ b/bindings/python/unicorn/unicorn.py @@ -334,7 +334,7 @@ class RegStateManager: return reg.value - def _reg_write(self, reg_id: int, regtype, value) -> None: + def _reg_write(self, reg_id: int, regtype: Type, value) -> None: """Register write helper method. """ @@ -496,7 +496,7 @@ class Uc(RegStateManager): # we have to keep a reference to the callbacks so they do not get gc-ed # see: https://docs.python.org/3/library/ctypes.html#callback-functions self._callbacks: MutableMapping[int, ctypes._FuncPointer] = {} - self._mmio_callbacks: MutableMapping[Tuple[int, int], Tuple[Optional[ctypes._FuncPointer], Optional[ctypes._FuncPointer]]] = {} + self._mmio_callbacks: MutableMapping[Tuple[int, int], Tuple[Optional[MMIO_READ_CFUNC], Optional[MMIO_WRITE_CFUNC]]] = {} self._hook_exception: Optional[Exception] = None @@ -548,7 +548,6 @@ class Uc(RegStateManager): if self._hook_exception is not None: raise self._hook_exception - def emu_stop(self) -> None: """Stop emulation. @@ -752,15 +751,15 @@ class Uc(RegStateManager): def __do_hook_add(self, htype: int, fptr: ctypes._FuncPointer, begin: int, end: int, *args: ctypes.c_int) -> int: handle = uc_hook_h() - # TODO: we do not need a callback counter to reference the callback and user data anymore. - # that said, we could still use the hook handler as auxiliary data - but for that we would - # need to pass a pointer since the handler is set by this very function call. - # - # for now just pass a dummy value + # TODO: we do not need a callback counter to reference the callback and user data anymore, + # so just pass a dummy value. that value will become the unused 'key' argument dummy = 0 status = uclib.uc_hook_add( - self._uch, ctypes.byref(handle), htype, fptr, + self._uch, + ctypes.byref(handle), + htype, + fptr, ctypes.cast(dummy, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end), @@ -1023,10 +1022,10 @@ class Uc(RegStateManager): ) def ctl_get_exits(self) -> Sequence[int]: - l = self.ctl_get_exits_cnt() - arr = (ctypes.c_uint64 * l)() + count = self.ctl_get_exits_cnt() + arr = (ctypes.c_uint64 * count)() - self.ctl(uc.UC_CTL_UC_EXITS, uc.UC_CTL_IO_READ, ctypes.cast(arr, ctypes.c_void_p), ctypes.c_size_t(l)) + self.ctl(uc.UC_CTL_UC_EXITS, uc.UC_CTL_IO_READ, ctypes.cast(arr, ctypes.c_void_p), ctypes.c_size_t(count)) return tuple(i for i in arr)