ATLAS Offline Software
Loading...
Searching...
No Matches
DebuggingContext.py
Go to the documentation of this file.
1# Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2
3
4# set of utilities allowing to get a better context information when issues with CA occur
6 """
7 Keeps the context info as a stack
8 Usage:
9 context = Context("context string") # this is it
10 """
11 hint="Unknown context (enable it with ComponentAccumulator.debugMode = \"trackCA trackEventAlgo etc. see ComponentAccumulator documentation\")"
12 __context = []
13
14 def __init__(self, c):
15 Context.__context.append(c)
16
17 def __del__(self):
18 Context.__context.pop()
19
20 @classmethod
21 def current(cls):
22 return Context.__context[-1]
23
24 @classmethod
25 def complete(cls):
26 return Context.hint if not Context.__context else "\n".join(Context.__context)
27
28
30 """
31 Obtains (a very abbreviated) context of the current call stack (from the top, excluding the code using this function)
32 """
33 import inspect
34 compact = ' >>> '
35 for frameInfo in inspect.stack()[2:][::-1]:
36 compact += "{}:{}({}) ".format(frameInfo.filename.split('/')[-1], frameInfo.frame.f_lineno, frameInfo.function)
37 return compact
38
39
40def createContextForDeduplication(message, compName, destContext):
41 return Context(f"{message} : {compName} : {destContext.get(compName, Context.hint)}")
42
43
45 """
46 Raises the exception with the message supplemented with context information
47 """
48 raise type(exception)(str(exception) + '\nWith the context:\n{}'.format(Context.complete()) )
createContextForDeduplication(message, compName, destContext)