From 590cd608d39829f693356be6ecc355cafd7982f3 Mon Sep 17 00:00:00 2001 From: Ash Holland Date: Wed, 6 Jan 2021 15:54:01 +0000 Subject: [PATCH] Fix logging from mangled methods where class name starts with "_" Python's name-mangling rules are quite complex. Previously, EDMC was incorrectly mangling names where the class name starts with one or more underscores; if the class name starts with any underscores, they should be removed before prepending to the identifier being mangled. If the class name contains *only* underscores, no mangling should be performed. --- EDMCLogging.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/EDMCLogging.py b/EDMCLogging.py index b1cac32e..c0898941 100644 --- a/EDMCLogging.py +++ b/EDMCLogging.py @@ -309,9 +309,11 @@ class EDMCContextFilter(logging.Filter): if frame_class: # See https://en.wikipedia.org/wiki/Name_mangling#Python for how name mangling works. + # For more detail, see _Py_Mangle in CPython's Python/compile.c. name = frame_info.function - if name.startswith("__") and not name.endswith("__"): - name = f'_{frame_class.__class__.__name__}{frame_info.function}' + class_name = frame_class.__class__.__name__.lstrip("_") + if name.startswith("__") and not name.endswith("__") and class_name: + name = f'_{class_name}{frame_info.function}' # Find __qualname__ of the caller fn = inspect.getattr_static(frame_class, name, None)