proper return value in handler read_* methods

wrapped read_* methods must always return a value

+ do not copy __name__ attribute of handler method to wrapped method

Change-Id: I54cd4b37cf7452621ee734be393aec4611fe809b
Reviewed-on: https://forge.frm2.tum.de/review/c/sine2020/secop/playground/+/27870
Tested-by: Jenkins Automated Tests <pedersen+jenkins@frm2.tum.de>
Reviewed-by: Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
Reviewed-by: Markus Zolliker <markus.zolliker@psi.ch>
This commit is contained in:
2022-03-01 09:52:49 +01:00
parent c2596a9629
commit aa82bc580d
2 changed files with 50 additions and 8 deletions

View File

@@ -53,11 +53,19 @@ Example 2: addressable HW parameters
return self.get_hw_register(HW_ADDR[pname])
"""
from functools import wraps
import functools
from secop.modules import Done
from secop.errors import ProgrammingError
def wraps(func):
"""decorator to copy function attributes of wrapped function"""
# we modify the default here:
# copy __doc__ , __module___ and attributes from __dict__
# but not __name__ and __qualname__
return functools.wraps(func, assigned=('__doc__', '__module__'))
class Handler:
func = None
method_names = set() # this is shared among all instances of handlers!
@@ -120,9 +128,11 @@ class ReadHandler(Handler):
def wrap(self, key):
def method(module, pname=key, func=self.func):
value = func(module, pname)
if value is not Done:
setattr(module, pname, value)
if value is Done:
return getattr(module, pname)
setattr(module, pname, value)
return value
return wraps(self.func)(method)
@@ -137,13 +147,14 @@ class CommonReadHandler(ReadHandler):
self.first_key = next(iter(keys))
def wrap(self, key):
def method(module, func=self.func):
def method(module, pname=key, func=self.func):
ret = func(module)
if ret not in (None, Done):
raise ProgrammingError('a method wrapped with CommonReadHandler must not return any value')
return getattr(module, pname)
method = wraps(self.func)(method)
method.poll = self.poll if key == self.first_key else False
method.poll = self.poll and getattr(method, 'poll', True) if key == self.first_key else False
return method