The problem is in the while-loop.
You're assigning the name
In the while loop you call this function again.
Here my extension to your try_wrapper.
It's one level deeper and allows to set a return_value and is using logging.
def split_url(url): pass def validate_scheme(url): pass while True: url = 'foo' split = split_url(url) split_url = validate_scheme(split)It doesn't matter what
validate_scheme
returns.You're assigning the name
split_url
, which has been assigned before to the function.In the while loop you call this function again.
Here my extension to your try_wrapper.
It's one level deeper and allows to set a return_value and is using logging.
import logging import functools logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) def try_wrapper(*, logger=None, traceback=False, retval=None): def func_wrapper(func): @functools.wraps(func) def inner(*args, **kwargs): try: result = func(*args, **kwargs) return result except Exception as e: if logger and not traceback: logger.debug(e) elif logger and traceback: logger.exception(traceback) return retval else: return result return inner return func_wrapper @try_wrapper() def foo(): return 1/0 @try_wrapper(logger=logger) def foo_with_logger(): return 1/0 @try_wrapper(logger=logger, retval=42) def foo_with_logger_and_retval(): return 1/0 print('Foo call') ret = foo() print('Retval:', ret, 'Type:', type(ret)) print() print('foo_with_logger') ret = foo_with_logger() print('Retval:', ret, 'Type:', type(ret)) print() print('foo_with_logger in logging level debug') logger.setLevel(logging.DEBUG) ret = foo_with_logger() print('Retval:', ret, 'Type:', type(ret)) print() print('foo_with_logger_and_retval 42') ret = foo_with_logger_and_retval() print('Retval:', ret, 'Type:', type(ret))Maybe you can also log tracebacks if you want. Set traceback=True
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!