From 7deb613a3c32a0be0ae8d44e048eff9e9e04ff14 Mon Sep 17 00:00:00 2001 From: elicn Date: Thu, 19 Sep 2024 16:58:03 +0300 Subject: [PATCH] Reduce namespace clutter --- bindings/python/unicorn/unicorn.py | 14 ++++++++++---- .../python/unicorn/unicorn_py3/arch/__init__.py | 4 ---- bindings/python/unicorn/unicorn_py3/arch/arm.py | 8 ++++++-- bindings/python/unicorn/unicorn_py3/arch/arm64.py | 11 +++++++---- bindings/python/unicorn/unicorn_py3/arch/intel.py | 14 +++++++++----- bindings/python/unicorn/unicorn_py3/arch/types.py | 3 +++ bindings/python/unicorn/unicorn_py3/unicorn.py | 14 ++++++-------- 7 files changed, 41 insertions(+), 27 deletions(-) diff --git a/bindings/python/unicorn/unicorn.py b/bindings/python/unicorn/unicorn.py index 04b3e295..3ad3cb37 100644 --- a/bindings/python/unicorn/unicorn.py +++ b/bindings/python/unicorn/unicorn.py @@ -1,8 +1,14 @@ -import sys +import sys as _sys -if sys.version_info[0] == 2: +from .unicorn_const import ( + UC_VERSION_MAJOR as __MAJOR, + UC_VERSION_MINOR as __MINOR, + UC_VERSION_PATCH as __PATCH +) + +__version__ = "%u.%u.%u" % (__MAJOR, __MINOR, __PATCH) + +if _sys.version_info.major == 2: from .unicorn_py2 import * else: from .unicorn_py3 import * - -__version__ = "%u.%u.%u" % (uc.UC_VERSION_MAJOR, uc.UC_VERSION_MINOR, uc.UC_VERSION_PATCH) diff --git a/bindings/python/unicorn/unicorn_py3/arch/__init__.py b/bindings/python/unicorn/unicorn_py3/arch/__init__.py index 1330905d..e69de29b 100644 --- a/bindings/python/unicorn/unicorn_py3/arch/__init__.py +++ b/bindings/python/unicorn/unicorn_py3/arch/__init__.py @@ -1,4 +0,0 @@ -# from .arm import UcRegCP -# from .arm64 import UcRegCP64 -# from .intel import UcRegFPR, UcRegMMR, UcRegMSR -# from .types import UcReg128, UcReg256, UcReg512, UcReg, UcLargeReg, UcTupledReg \ No newline at end of file diff --git a/bindings/python/unicorn/unicorn_py3/arch/arm.py b/bindings/python/unicorn/unicorn_py3/arch/arm.py index 7ec019b3..98b5c91e 100644 --- a/bindings/python/unicorn/unicorn_py3/arch/arm.py +++ b/bindings/python/unicorn/unicorn_py3/arch/arm.py @@ -6,9 +6,11 @@ from typing import Any, Tuple import ctypes -from .. import Uc -from .. import arm_const as const +# traditional unicorn imports +from unicorn import arm_const as const +# newly introduced unicorn imports +from ..unicorn import Uc from .types import UcTupledReg, UcReg128 ARMCPReg = Tuple[int, int, int, int, int, int, int, int] @@ -83,3 +85,5 @@ class UcAArch32(Uc): else: self._reg_write(reg_id, reg_cls, value) + +__all__ = ['UcRegCP', 'UcAArch32'] diff --git a/bindings/python/unicorn/unicorn_py3/arch/arm64.py b/bindings/python/unicorn/unicorn_py3/arch/arm64.py index e6dcdb27..c2b94f59 100644 --- a/bindings/python/unicorn/unicorn_py3/arch/arm64.py +++ b/bindings/python/unicorn/unicorn_py3/arch/arm64.py @@ -6,11 +6,12 @@ from typing import Any, Callable, NamedTuple, Tuple import ctypes -from .. import Uc, UcError -from .. import arm64_const as const +# traditional unicorn imports +from unicorn import arm64_const as const +from unicorn.unicorn_const import UC_ERR_ARG, UC_HOOK_INSN -from ..unicorn import uccallback -from unicorn import UC_ERR_ARG, UC_HOOK_INSN +# newly introduced unicorn imports +from ..unicorn import Uc, UcError, uccallback from .types import uc_engine, UcTupledReg, UcReg128 ARM64CPReg = Tuple[int, int, int, int, int, int] @@ -124,3 +125,5 @@ class UcAArch64(Uc): else: self._reg_write(reg_id, reg_cls, value) + +__all__ = ['UcRegCP64', 'UcAArch64'] diff --git a/bindings/python/unicorn/unicorn_py3/arch/intel.py b/bindings/python/unicorn/unicorn_py3/arch/intel.py index ec3da95a..ea341bc3 100644 --- a/bindings/python/unicorn/unicorn_py3/arch/intel.py +++ b/bindings/python/unicorn/unicorn_py3/arch/intel.py @@ -2,15 +2,16 @@ # # @author elicn -from typing import Any, Callable, Tuple +from typing import Any, Callable, Sequence, Tuple import ctypes -from .. import Uc, UcError -from .. import x86_const as const - -from unicorn.unicorn_py3 import uccallback +# traditional unicorn imports +from unicorn import x86_const as const from unicorn.unicorn_const import UC_ERR_ARG, UC_HOOK_INSN + +# newly introduced unicorn imports +from ..unicorn import Uc, UcError, uccallback from .types import uc_engine, UcTupledReg, UcReg128, UcReg256, UcReg512 X86MMRReg = Tuple[int, int, int, int] @@ -176,3 +177,6 @@ class UcIntel(Uc): def msr_write(self, msr_id: int, value: int) -> None: self._reg_write(const.UC_X86_REG_MSR, UcRegMSR, (msr_id, value)) + + +__all__ = ['UcRegMMR', 'UcRegMSR', 'UcRegFPR', 'UcIntel'] diff --git a/bindings/python/unicorn/unicorn_py3/arch/types.py b/bindings/python/unicorn/unicorn_py3/arch/types.py index f5793b91..ea6f471f 100644 --- a/bindings/python/unicorn/unicorn_py3/arch/types.py +++ b/bindings/python/unicorn/unicorn_py3/arch/types.py @@ -94,3 +94,6 @@ class UcReg256(UcLargeReg): class UcReg512(UcLargeReg): _fields_ = [('qwords', ctypes.c_uint64 * 8)] + + +__all__ = ['uc_err', 'uc_engine', 'uc_context', 'uc_hook_h', 'UcReg', 'UcTupledReg', 'UcLargeReg', 'UcReg128', 'UcReg256', 'UcReg512'] diff --git a/bindings/python/unicorn/unicorn_py3/unicorn.py b/bindings/python/unicorn/unicorn_py3/unicorn.py index 88d9d6c5..7a1db01d 100644 --- a/bindings/python/unicorn/unicorn_py3/unicorn.py +++ b/bindings/python/unicorn/unicorn_py3/unicorn.py @@ -5,10 +5,10 @@ import ctypes import functools import weakref -from unicorn import x86_const, arm_const, arm64_const, unicorn_const as uc -from .arch.types import * +from unicorn import unicorn_const as uc +from .arch.types import uc_err, uc_engine, uc_context, uc_hook_h, UcReg -__version__ = f'{uc.UC_VERSION_MAJOR}.{uc.UC_VERSION_MINOR}.{uc.UC_VERSION_PATCH}' +# __version__ = f'{uc.UC_VERSION_MAJOR}.{uc.UC_VERSION_MINOR}.{uc.UC_VERSION_PATCH}' def _structure_repr(self): return "%s(%s)" % (self.__class__.__name__, ", ".join("%s=%s" % (k, getattr(self, k)) for (k, _) in self._fields_)) @@ -457,7 +457,7 @@ class Uc(RegStateManager): """ def __wrapped() -> Type[Uc]: - archmod = importlib.import_module(f'.arch.{pkgname}', f'unicorn.unicorn_py3') + archmod = importlib.import_module(f'.arch.{pkgname}', 'unicorn.unicorn_py3') return getattr(archmod, clsname) @@ -1163,8 +1163,6 @@ UC_MMIO_READ_TYPE = Callable[[Uc, int, int, Any], int] UC_MMIO_WRITE_TYPE = Callable[[Uc, int, int, int, Any], None] +__all__ = ['Uc', 'UcContext', 'ucsubclass', 'UcError', 'uc_version', 'version_bind', 'uc_arch_supported', 'debug'] + -__all__ = [ - 'Uc', 'UcContext', 'ucsubclass', 'uc_version', 'uc_arch_supported', 'version_bind', 'UcError', 'uc', 'debug', 'uccallback', - 'x86_const', 'arm_const', 'arm64_const' -] \ No newline at end of file