From f0ae2554557808dff1e431c346e54c03ae7e2101 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Wed, 14 Sep 2022 17:51:16 +0200 Subject: [PATCH] Improve warning stack trace detection Handle the cases where: * Several useless "importlib_bootstrap" frames are inserted in between. * The warning is about syntax, so the file location wouldn't even show up in the stack trace. --- mkdocs/__main__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mkdocs/__main__.py b/mkdocs/__main__.py index bd726cfa..b7d1af0f 100644 --- a/mkdocs/__main__.py +++ b/mkdocs/__main__.py @@ -33,7 +33,11 @@ def _showwarning(message, category, filename, lineno, file=None, line=None): # * Location of call to warn() <-- include this # * (stdlib) Location of call to showwarning function # * (this function) Location of call to extract_stack() - stack = traceback.extract_stack()[-4:-2] + stack = [frame for frame in traceback.extract_stack() if frame.line][-4:-2] + # Make sure the actual affected file's name is still present (the case of syntax warning): + if not any(frame.filename == filename for frame in stack): + stack = stack[-1:] + [traceback.FrameSummary(filename, lineno, '')] + tb = ''.join(traceback.format_list(stack)) except Exception: tb = f' File "{filename}", line {lineno}'