Meatboy 106 has submitted this change. (
https://gem5-review.googlesource.com/c/public/gem5/+/53523 )
Change subject: python: Define deprecated and callOnce decorators
......................................................................
python: Define deprecated and callOnce decorators
Change-Id: I85d52a65308b9d5068a9aaa46597e5eaf8175064
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/53523
Reviewed-by: Jason Lowe-Power <[email protected]>
Maintainer: Jason Lowe-Power <[email protected]>
Reviewed-by: Andreas Sandberg <[email protected]>
Maintainer: Andreas Sandberg <[email protected]>
Tested-by: kokoro <[email protected]>
---
M src/python/m5/util/__init__.py
1 file changed, 47 insertions(+), 0 deletions(-)
Approvals:
Jason Lowe-Power: Looks good to me, approved; Looks good to me, approved
Andreas Sandberg: Looks good to me, approved; Looks good to me, approved
kokoro: Regressions pass
diff --git a/src/python/m5/util/__init__.py b/src/python/m5/util/__init__.py
index a63fa81..f5b5c79 100644
--- a/src/python/m5/util/__init__.py
+++ b/src/python/m5/util/__init__.py
@@ -41,6 +41,8 @@
import re
import sys
+from functools import wraps
+
from . import convert
from .attrdict import attrdict, multiattrdict, optiondict
@@ -71,6 +73,36 @@
def inform(fmt, *args):
print('info:', fmt % args, file=sys.stdout)
+def callOnce(func):
+ """Decorator that enables to run a given function only once. Subsequent
+ calls are discarded."""
+ @wraps(func)
+ def wrapper(*args, **kwargs):
+ if not wrapper.has_run:
+ wrapper.has_run = True
+ return func(*args, **kwargs)
+ wrapper.has_run = False
+ return wrapper
+
+def deprecated(replacement=None, logger=warn):
+ """This decorator warns the user about a deprecated function."""
+ def decorator(func):
+ @callOnce
+ def notifyDeprecation():
+ try:
+ func_name = lambda f: f.__module__ + '.' + f.__qualname__
+ message = f'Function {func_name(func)} is deprecated.'
+ if replacement:
+ message += f' Prefer {func_name(replacement)} instead.'
+ except AttributeError:
+ message = f'Function {func} is deprecated.'
+ if replacement:
+ message += f' Prefer {replacement} instead.'
+ logger(message)
+ notifyDeprecation()
+ return func
+ return decorator
+
class Singleton(type):
def __call__(cls, *args, **kwargs):
if hasattr(cls, '_instance'):
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/53523
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings
Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I85d52a65308b9d5068a9aaa46597e5eaf8175064
Gerrit-Change-Number: 53523
Gerrit-PatchSet: 6
Gerrit-Owner: Meatboy 106 <[email protected]>
Gerrit-Reviewer: Andreas Sandberg <[email protected]>
Gerrit-Reviewer: Bobby Bruce <[email protected]>
Gerrit-Reviewer: Gabe Black <[email protected]>
Gerrit-Reviewer: Jason Lowe-Power <[email protected]>
Gerrit-Reviewer: Jason Lowe-Power <[email protected]>
Gerrit-Reviewer: Meatboy 106 <[email protected]>
Gerrit-Reviewer: kokoro <[email protected]>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s