From fdcca005a4dd62a55476f013945a5930d4f88f53 Mon Sep 17 00:00:00 2001 From: A_D Date: Sun, 18 Oct 2020 09:48:23 +0200 Subject: [PATCH] Added name mangling support to EDMCContextFilter Python name mangling is more about name collisions than actually making things private. This commit adds a check to resolve mangled names to their runtime names, and adds a just-in-case default to fetching the attribute. Fixes #764 --- EDMCLogging.py | 8 ++++++-- EDMarketConnector.py | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/EDMCLogging.py b/EDMCLogging.py index 246ee78d..846dba84 100644 --- a/EDMCLogging.py +++ b/EDMCLogging.py @@ -287,11 +287,15 @@ class EDMCContextFilter(logging.Filter): frame_info = inspect.getframeinfo(frame) args, _, _, value_dict = inspect.getargvalues(frame) if len(args) and args[0] in ('self', 'cls'): - frame_class = value_dict[args[0]] + frame_class: 'object' = value_dict[args[0]] if frame_class: + # See https://en.wikipedia.org/wiki/Name_mangling#Python for how name mangling works. + if (name := frame_info.function).startswith("__") and not name.endswith("__"): + name = f'_{frame_class.__class__.__name__}{frame_info.function}' + # Find __qualname__ of the caller - fn = getattr(frame_class, frame_info.function) + fn = getattr(frame_class, name, None) if fn and fn.__qualname__: caller_qualname = fn.__qualname__ diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 90ff631f..63cc2f6f 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -1165,6 +1165,10 @@ sys.path: {sys.path}''' def __init__(self): logger.debug('A call from A.B.__init__') + self.__test() + + def __test(self): + logger.debug("A call from A.B.__test") # abinit = A.B()