Пример #1
0
 def wrap(self, fn, ofn):
     '''Wraps the function to provide conversion.'''
     argnames=get_function_argument_names(ofn)
     @wraps(fn)
     def wrapper(*args, **kw):
         '''Wrapper to implement actual argument and return value
         conversion. The @wraps decorator is required to preserve
         function module, name and docstring.'''
         cooperation=self.cooperate(ofn.__annotations__)
         # Convert positional arguments
         args=[self.convertArgument(fn, cooperation, name, value) for name, value in zip(argnames, args)]
         # Convert keyword arguments
         kw=dict([(name, self.convertArgument(fn, cooperation, name, value)) for name, value in kw.items()])
         # Call original function
         return_value=fn(*args, **kw)
         # Convert return value
         return self.convertArgument(fn, cooperation, 'return', return_value)
     return wrapper
Пример #2
0
 def wrap(self, fn, ofn):
     '''Wraps the function to provide runtime type checking.'''
     argnames=get_function_argument_names(ofn)
     @wraps(fn) 
     def wrapper(*args, **kw):
         '''Wrapper to implement actual argument and return value
         type checking. The @wraps decorator is required to preserve
         function module, name and docstring.'''
         cooperation=self.cooperate(ofn.__annotations__)
         # Validate positional arguments
         for name, value in zip(argnames, args):
             self.checkArgument(fn, cooperation, name, value)
         # Validate keyword arguments
         for name, value in kw.items():
             self.checkArgument(fn, cooperation, name, value)
         # Call original function
         return_value=fn(*args, **kw)
         # Validate return value
         self.checkArgument(fn, cooperation, 'return', return_value)
         # Returns with the return value of the original function
         return return_value
     return wrapper