Improve `get_location()` (#12502)

This commit is contained in:
Adam Turner 2024-07-02 22:41:48 +01:00 committed by GitHub
parent 00cc5f6585
commit 21f7006d22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 6 deletions

View File

@ -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'<unknown>:{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'<unknown>:{line}'
return ''
class ReferenceRole(SphinxRole):

View File

@ -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"<unknown>:{line}"
return None