linotp.lib.context module

establish a global context object

linotp.lib.context.context_stack_trace(manager_id, allow_nesting=True)

context_stack_trace is a contextmanager that is used to track usage of other context managers (such as request_context_safety). It pushes a manager_id onto a thread local stack in its entering block and pops it on exit.

usage:

@contextmanager def my_manager():

with context_stack_trace(‘my_manager’):

# do entering yield # do exiting

This way you can check which context managers are active at the moment.

To prevent accidental nesting of managers with the same id, you can set the parameter allow_nesting to False

See also: is_on_context_stack()

linotp.lib.context.is_on_context_stack(manager_id)

returns, if we are inside a traced context manager with id manager_id. can be used to set access rights on some thread local variables, e.g. LocalContainer instances.

:returns bool

See also:

is_on_context_stack()

linotp.lib.context.request_context_safety()

request_context_safety is a context managers that wraps the usage of request_context. to avoid data leakage through thread recycling we have to remove thread local data from request_context when the thread exits. the request_context_safety manager does that for you.

linotp enforces this manager, so using request_context outside of request_context_safety will raise an exception.

usage:

with request_context_safety():

# we can use request_context now, e.g.: request_context[‘config’] = getLinotpConfig # …