Meatboy 106 has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/53523 )
Change subject: util-m5: Define deprecated and callOnce decorators
......................................................................
util-m5: Define deprecated and callOnce decorators
Change-Id: I85d52a65308b9d5068a9aaa46597e5eaf8175064
---
M src/python/m5/util/__init__.py
1 file changed, 41 insertions(+), 0 deletions(-)
diff --git a/src/python/m5/util/__init__.py b/src/python/m5/util/__init__.py
index a63fa81..bd11760 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:
+ funcName = lambda f: f.__module__ + '.' + f.__name__
+ message = f'Function {funcName(func)} is deprecated.'
+ if replacement:
+ message += f' Prefer {funcName(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: 1
Gerrit-Owner: Meatboy 106 <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s