From 8d52ece48bbb521060a5234ba1326d601be78315 Mon Sep 17 00:00:00 2001 From: "@Antelox" Date: Sun, 16 Feb 2025 16:04:42 +0100 Subject: [PATCH] Replaced custom deprecated decorator with simple DeprecationWarning (#2110) --- .../python/unicorn/unicorn_py3/unicorn.py | 40 +++++-------------- tests/regress/test_old_ctl.py | 6 ++- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/bindings/python/unicorn/unicorn_py3/unicorn.py b/bindings/python/unicorn/unicorn_py3/unicorn.py index 085fb113..8684a391 100644 --- a/bindings/python/unicorn/unicorn_py3/unicorn.py +++ b/bindings/python/unicorn/unicorn_py3/unicorn.py @@ -9,11 +9,9 @@ import ctypes import functools import weakref import warnings - from unicorn import unicorn_const as uc from .arch.types import uc_err, uc_engine, uc_context, uc_hook_h, UcReg, VT -# __version__ = f'{uc.UC_VERSION_MAJOR}.{uc.UC_VERSION_MINOR}.{uc.UC_VERSION_PATCH}' MemRegionStruct = Tuple[int, int, int] TBStruct = Tuple[int, int, int] @@ -632,25 +630,6 @@ class Uc(RegStateManager): """Unicorn Engine class. """ - # Code snippet modified from: - # https://stackoverflow.com/questions/2536307/decorators-in-the-python-standard-lib-deprecated-specifically - @staticmethod - def __deprecated(msg: str): - def __deprecated_inner(func: Callable) -> Callable: - """Use this decorator to mark functions as deprecated. - Every time the decorated function runs, it will emit - a "deprecation" warning.""" - @functools.wraps(func) - def new_func(*args, **kwargs): - warnings.simplefilter('always', DeprecationWarning) # turn off filter - warnings.warn("Call to a deprecated function {}. {}".format(func.__name__, msg), - category=DeprecationWarning, - stacklevel=2) - warnings.simplefilter('default', DeprecationWarning) # reset filter - return func(*args, **kwargs) - return new_func - return __deprecated_inner - @staticmethod def __is_compliant() -> bool: """Checks whether Unicorn binding version complies with Unicorn library. @@ -1455,15 +1434,6 @@ class Uc(RegStateManager): self.__ctl_w(uc.UC_CTL_TB_FLUSH) - @__deprecated("You should use ctl_set_tlb_mode instead.") - def ctl_tlb_mode(self, mode: int) -> None: - """Deprecated, please use ctl_set_tlb_mode instead. - - Args: - mode: tlb mode to use (see UC_TLB_* constants) - """ - self.ctl_set_tlb_mode(mode) - def ctl_set_tlb_mode(self, mode: int) -> None: """Set TLB mode. @@ -1475,6 +1445,16 @@ class Uc(RegStateManager): (ctypes.c_uint, mode) ) + # For backward compatibility... + def ctl_tlb_mode(self, mode: int) -> None: + """Deprecated, please use ctl_set_tlb_mode instead. + + Args: + mode: tlb mode to use (see UC_TLB_* constants) + """ + warnings.warn('Deprecated method, use ctl_set_tlb_mode', DeprecationWarning) + self.ctl_set_tlb_mode(mode) + def ctl_get_tcg_buffer_size(self) -> int: """Retrieve TCG buffer size. diff --git a/tests/regress/test_old_ctl.py b/tests/regress/test_old_ctl.py index 5d84aa42..0da4725d 100644 --- a/tests/regress/test_old_ctl.py +++ b/tests/regress/test_old_ctl.py @@ -1,14 +1,18 @@ import regress +import sys +import unittest from unicorn import * from unicorn.x86_const import * + # Very basic testing to ensure the old api exists -# and we correctly implement __deprecated +@unittest.skipIf(sys.version_info < (3, 7), reason="requires python3.7 or higher") class OldCtl(regress.RegressTest): def runTest(self): mu = Uc(UC_ARCH_X86, UC_MODE_32) mu.ctl_tlb_mode(UC_TLB_CPU) mu.ctl_set_tlb_mode(UC_TLB_VIRTUAL) + if __name__ == '__main__': regress.main()