Skip to content

Commit 8d52ece

Browse files
authored
Replaced custom deprecated decorator with simple DeprecationWarning (#2110)
1 parent bf5e335 commit 8d52ece

File tree

2 files changed

+15
-31
lines changed

2 files changed

+15
-31
lines changed

bindings/python/unicorn/unicorn_py3/unicorn.py

+10-30
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
import functools
1010
import weakref
1111
import warnings
12-
1312
from unicorn import unicorn_const as uc
1413
from .arch.types import uc_err, uc_engine, uc_context, uc_hook_h, UcReg, VT
1514

16-
# __version__ = f'{uc.UC_VERSION_MAJOR}.{uc.UC_VERSION_MINOR}.{uc.UC_VERSION_PATCH}'
1715

1816
MemRegionStruct = Tuple[int, int, int]
1917
TBStruct = Tuple[int, int, int]
@@ -632,25 +630,6 @@ class Uc(RegStateManager):
632630
"""Unicorn Engine class.
633631
"""
634632

635-
# Code snippet modified from:
636-
# https://stackoverflow.com/questions/2536307/decorators-in-the-python-standard-lib-deprecated-specifically
637-
@staticmethod
638-
def __deprecated(msg: str):
639-
def __deprecated_inner(func: Callable) -> Callable:
640-
"""Use this decorator to mark functions as deprecated.
641-
Every time the decorated function runs, it will emit
642-
a "deprecation" warning."""
643-
@functools.wraps(func)
644-
def new_func(*args, **kwargs):
645-
warnings.simplefilter('always', DeprecationWarning) # turn off filter
646-
warnings.warn("Call to a deprecated function {}. {}".format(func.__name__, msg),
647-
category=DeprecationWarning,
648-
stacklevel=2)
649-
warnings.simplefilter('default', DeprecationWarning) # reset filter
650-
return func(*args, **kwargs)
651-
return new_func
652-
return __deprecated_inner
653-
654633
@staticmethod
655634
def __is_compliant() -> bool:
656635
"""Checks whether Unicorn binding version complies with Unicorn library.
@@ -1455,15 +1434,6 @@ def ctl_flush_tb(self) -> None:
14551434

14561435
self.__ctl_w(uc.UC_CTL_TB_FLUSH)
14571436

1458-
@__deprecated("You should use ctl_set_tlb_mode instead.")
1459-
def ctl_tlb_mode(self, mode: int) -> None:
1460-
"""Deprecated, please use ctl_set_tlb_mode instead.
1461-
1462-
Args:
1463-
mode: tlb mode to use (see UC_TLB_* constants)
1464-
"""
1465-
self.ctl_set_tlb_mode(mode)
1466-
14671437
def ctl_set_tlb_mode(self, mode: int) -> None:
14681438
"""Set TLB mode.
14691439
@@ -1475,6 +1445,16 @@ def ctl_set_tlb_mode(self, mode: int) -> None:
14751445
(ctypes.c_uint, mode)
14761446
)
14771447

1448+
# For backward compatibility...
1449+
def ctl_tlb_mode(self, mode: int) -> None:
1450+
"""Deprecated, please use ctl_set_tlb_mode instead.
1451+
1452+
Args:
1453+
mode: tlb mode to use (see UC_TLB_* constants)
1454+
"""
1455+
warnings.warn('Deprecated method, use ctl_set_tlb_mode', DeprecationWarning)
1456+
self.ctl_set_tlb_mode(mode)
1457+
14781458
def ctl_get_tcg_buffer_size(self) -> int:
14791459
"""Retrieve TCG buffer size.
14801460

tests/regress/test_old_ctl.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import regress
2+
import sys
3+
import unittest
24
from unicorn import *
35
from unicorn.x86_const import *
46

7+
58
# Very basic testing to ensure the old api exists
6-
# and we correctly implement __deprecated
9+
@unittest.skipIf(sys.version_info < (3, 7), reason="requires python3.7 or higher")
710
class OldCtl(regress.RegressTest):
811
def runTest(self):
912
mu = Uc(UC_ARCH_X86, UC_MODE_32)
1013
mu.ctl_tlb_mode(UC_TLB_CPU)
1114
mu.ctl_set_tlb_mode(UC_TLB_VIRTUAL)
1215

16+
1317
if __name__ == '__main__':
1418
regress.main()

0 commit comments

Comments
 (0)