From 21f7006d22ceb4535c6991042940fe4034a6584c Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Tue, 2 Jul 2024 22:41:48 +0100 Subject: [PATCH] Improve ``get_location()`` (#12502) --- sphinx/util/docutils.py | 18 ++++++++++++++++-- sphinx/util/logging.py | 6 ++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/sphinx/util/docutils.py b/sphinx/util/docutils.py index bfdeeb362..7b2364a84 100644 --- a/sphinx/util/docutils.py +++ b/sphinx/util/docutils.py @@ -425,7 +425,14 @@ class SphinxDirective(Directive): def get_location(self) -> str: """Get current location info for logging.""" - return ':'.join(str(s) for s in self.get_source_info()) + source, line = self.get_source_info() + if source and line: + return f'{source}:{line}' + if source: + return f'{source}:' + if line: + return f':{line}' + return '' def parse_content_to_nodes(self) -> list[Node]: """Parse the directive's content into nodes.""" @@ -529,7 +536,14 @@ class SphinxRole: def get_location(self) -> str: """Get current location info for logging.""" - return ':'.join(str(s) for s in self.get_source_info()) + source, line = self.get_source_info() + if source and line: + return f'{source}:{line}' + if source: + return f'{source}:' + if line: + return f':{line}' + return '' class ReferenceRole(SphinxRole): diff --git a/sphinx/util/logging.py b/sphinx/util/logging.py index e107a568d..88a25b8a0 100644 --- a/sphinx/util/logging.py +++ b/sphinx/util/logging.py @@ -589,12 +589,10 @@ class WarningLogRecordTranslator(SphinxLogRecordTranslator): def get_node_location(node: Node) -> str | None: source, line = get_source_line(node) - if source: - source = abspath(source) if source and line: - return f"{source}:{line}" + return f"{abspath(source)}:{line}" if source: - return f"{source}:" + return f"{abspath(source)}:" if line: return f":{line}" return None