Skip to content

Commit 774b0b0

Browse files
committed
Fix #9481: autosummary: some warnings contain non-existing filenames
`SphinxDirective.get_source_info()` returns the pair of fullpath of source and current line number. But our logging module expects one of these: * A string consists of fullpath and current line number * A pair of docname and current line number To show correct location for warnings, this adds `get_location()` method that returns the former one. Note: This also modifies C/C++ domains.
1 parent ebb03cd commit 774b0b0

File tree

6 files changed

+36
-25
lines changed

6 files changed

+36
-25
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Bugs fixed
2121
----------
2222

2323
* #9487: autodoc: typehint for cached_property is not shown
24+
* #9481: autosummary: some warnings contain non-existing filenames
25+
* #9481: c domain: some warnings contain non-existing filenames
26+
* #9481: cpp domain: some warnings contain non-existing filenames
2427

2528
Testing
2629
--------

sphinx/domains/c.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3388,13 +3388,13 @@ def run(self) -> List[Node]:
33883388
stack: List[Symbol] = []
33893389
else:
33903390
parser = DefinitionParser(self.arguments[0],
3391-
location=self.get_source_info(),
3391+
location=self.get_location(),
33923392
config=self.env.config)
33933393
try:
33943394
name = parser.parse_namespace_object()
33953395
parser.assert_end()
33963396
except DefinitionError as e:
3397-
logger.warning(e, location=self.get_source_info())
3397+
logger.warning(e, location=self.get_location())
33983398
name = _make_phony_error_name()
33993399
symbol = rootSymbol.add_name(name)
34003400
stack = [symbol]
@@ -3415,13 +3415,13 @@ def run(self) -> List[Node]:
34153415
if self.arguments[0].strip() in ('NULL', '0', 'nullptr'):
34163416
return []
34173417
parser = DefinitionParser(self.arguments[0],
3418-
location=self.get_source_info(),
3418+
location=self.get_location(),
34193419
config=self.env.config)
34203420
try:
34213421
name = parser.parse_namespace_object()
34223422
parser.assert_end()
34233423
except DefinitionError as e:
3424-
logger.warning(e, location=self.get_source_info())
3424+
logger.warning(e, location=self.get_location())
34253425
name = _make_phony_error_name()
34263426
oldParent = self.env.temp_data.get('c:parent_symbol', None)
34273427
if not oldParent:
@@ -3446,7 +3446,7 @@ def run(self) -> List[Node]:
34463446
stack = self.env.temp_data.get('c:namespace_stack', None)
34473447
if not stack or len(stack) == 0:
34483448
logger.warning("C namespace pop on empty stack. Defaulting to gobal scope.",
3449-
location=self.get_source_info())
3449+
location=self.get_location())
34503450
stack = []
34513451
else:
34523452
stack.pop()
@@ -3628,7 +3628,7 @@ def run(self) -> List[Node]:
36283628
" Requested 'noroot' but 'maxdepth' 1."
36293629
" When skipping the root declaration,"
36303630
" need 'maxdepth' 0 for infinite or at least 2.",
3631-
location=self.get_source_info())
3631+
location=self.get_location())
36323632
signatures = self.get_signatures()
36333633
for i, sig in enumerate(signatures):
36343634
node.append(AliasNode(sig, aliasOptions, self.state.document, env=self.env))
@@ -3661,7 +3661,7 @@ def run(self) -> Tuple[List[Node], List[system_message]]:
36613661
return super().run()
36623662

36633663
text = self.text.replace('\n', ' ')
3664-
parser = DefinitionParser(text, location=self.get_source_info(),
3664+
parser = DefinitionParser(text, location=self.get_location(),
36653665
config=self.env.config)
36663666
try:
36673667
parser.parse_xref_object()
@@ -3686,7 +3686,7 @@ def run(self) -> Tuple[List[Node], List[system_message]]:
36863686
msg = "{}: Pre-v3 C type role ':c:type:`{}`' converted to ':c:expr:`{}`'."
36873687
msg += "\nThe original parsing error was:\n{}"
36883688
msg = msg.format(RemovedInSphinx50Warning.__name__, text, text, eOrig)
3689-
logger.warning(msg, location=self.get_source_info())
3689+
logger.warning(msg, location=self.get_location())
36903690
return [signode], []
36913691

36923692

@@ -3702,14 +3702,14 @@ def __init__(self, asCode: bool) -> None:
37023702

37033703
def run(self) -> Tuple[List[Node], List[system_message]]:
37043704
text = self.text.replace('\n', ' ')
3705-
parser = DefinitionParser(text, location=self.get_source_info(),
3705+
parser = DefinitionParser(text, location=self.get_location(),
37063706
config=self.env.config)
37073707
# attempt to mimic XRefRole classes, except that...
37083708
try:
37093709
ast = parser.parse_expression()
37103710
except DefinitionError as ex:
37113711
logger.warning('Unparseable C expression: %r\n%s', text, ex,
3712-
location=self.get_source_info())
3712+
location=self.get_location())
37133713
# see below
37143714
return [addnodes.desc_inline('c', text, text, classes=[self.class_type])], []
37153715
parentSymbol = self.env.temp_data.get('c:parent_symbol', None)

sphinx/domains/cpp.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7005,7 +7005,7 @@ def add_target_and_index(self, ast: ASTDeclaration, sig: str,
70057005
if not re.compile(r'^[a-zA-Z0-9_]*$').match(newestId):
70067006
logger.warning('Index id generation for C++ object "%s" failed, please '
70077007
'report as bug (id=%s).', ast, newestId,
7008-
location=self.get_source_info())
7008+
location=self.get_location())
70097009

70107010
name = ast.symbol.get_full_nested_name().get_display_string().lstrip(':')
70117011
# Add index entry, but not if it's a declaration inside a concept
@@ -7088,7 +7088,7 @@ def run(self) -> List[Node]:
70887088
logger.warning(msg.format(
70897089
str(parentSymbol.get_full_nested_name()),
70907090
self.name, self.arguments[0]
7091-
), location=self.get_source_info())
7091+
), location=self.get_location())
70927092
name = _make_phony_error_name()
70937093
symbol = parentSymbol.add_name(name)
70947094
env.temp_data['cpp:last_symbol'] = symbol
@@ -7216,13 +7216,13 @@ def run(self) -> List[Node]:
72167216
stack: List[Symbol] = []
72177217
else:
72187218
parser = DefinitionParser(self.arguments[0],
7219-
location=self.get_source_info(),
7219+
location=self.get_location(),
72207220
config=self.config)
72217221
try:
72227222
ast = parser.parse_namespace_object()
72237223
parser.assert_end()
72247224
except DefinitionError as e:
7225-
logger.warning(e, location=self.get_source_info())
7225+
logger.warning(e, location=self.get_location())
72267226
name = _make_phony_error_name()
72277227
ast = ASTNamespace(name, None)
72287228
symbol = rootSymbol.add_name(ast.nestedName, ast.templatePrefix)
@@ -7244,13 +7244,13 @@ def run(self) -> List[Node]:
72447244
if self.arguments[0].strip() in ('NULL', '0', 'nullptr'):
72457245
return []
72467246
parser = DefinitionParser(self.arguments[0],
7247-
location=self.get_source_info(),
7247+
location=self.get_location(),
72487248
config=self.config)
72497249
try:
72507250
ast = parser.parse_namespace_object()
72517251
parser.assert_end()
72527252
except DefinitionError as e:
7253-
logger.warning(e, location=self.get_source_info())
7253+
logger.warning(e, location=self.get_location())
72547254
name = _make_phony_error_name()
72557255
ast = ASTNamespace(name, None)
72567256
oldParent = self.env.temp_data.get('cpp:parent_symbol', None)
@@ -7276,7 +7276,7 @@ def run(self) -> List[Node]:
72767276
stack = self.env.temp_data.get('cpp:namespace_stack', None)
72777277
if not stack or len(stack) == 0:
72787278
logger.warning("C++ namespace pop on empty stack. Defaulting to gobal scope.",
7279-
location=self.get_source_info())
7279+
location=self.get_location())
72807280
stack = []
72817281
else:
72827282
stack.pop()
@@ -7480,7 +7480,7 @@ def run(self) -> List[Node]:
74807480
" Requested 'noroot' but 'maxdepth' 1."
74817481
" When skipping the root declaration,"
74827482
" need 'maxdepth' 0 for infinite or at least 2.",
7483-
location=self.get_source_info())
7483+
location=self.get_location())
74847484
signatures = self.get_signatures()
74857485
for i, sig in enumerate(signatures):
74867486
node.append(AliasNode(sig, aliasOptions, env=self.env))
@@ -7537,14 +7537,14 @@ def __init__(self, asCode: bool) -> None:
75377537
def run(self) -> Tuple[List[Node], List[system_message]]:
75387538
text = self.text.replace('\n', ' ')
75397539
parser = DefinitionParser(text,
7540-
location=self.get_source_info(),
7540+
location=self.get_location(),
75417541
config=self.config)
75427542
# attempt to mimic XRefRole classes, except that...
75437543
try:
75447544
ast = parser.parse_expression()
75457545
except DefinitionError as ex:
75467546
logger.warning('Unparseable C++ expression: %r\n%s', text, ex,
7547-
location=self.get_source_info())
7547+
location=self.get_location())
75487548
# see below
75497549
return [addnodes.desc_inline('cpp', text, text, classes=[self.class_type])], []
75507550
parentSymbol = self.env.temp_data.get('cpp:parent_symbol', None)

sphinx/ext/autosummary/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def run(self) -> List[Node]:
280280
msg = __('autosummary: stub file not found %r. '
281281
'Check your autosummary_generate setting.')
282282

283-
logger.warning(msg, real_name, location=self.get_source_info())
283+
logger.warning(msg, real_name, location=self.get_location())
284284
continue
285285

286286
docnames.append(docname)
@@ -344,7 +344,7 @@ def get_items(self, names: List[str]) -> List[Tuple[str, str, str, str]]:
344344
real_name, obj, parent, modname = self.import_by_name(name, prefixes=prefixes)
345345
except ImportError:
346346
logger.warning(__('autosummary: failed to import %s'), name,
347-
location=self.get_source_info())
347+
location=self.get_location())
348348
continue
349349

350350
self.bridge.result = StringList() # initialize for each documenter
@@ -358,12 +358,12 @@ def get_items(self, names: List[str]) -> List[Tuple[str, str, str, str]]:
358358
documenter = self.create_documenter(self.env.app, obj, parent, full_name)
359359
if not documenter.parse_name():
360360
logger.warning(__('failed to parse name %s'), real_name,
361-
location=self.get_source_info())
361+
location=self.get_location())
362362
items.append((display_name, '', '', real_name))
363363
continue
364364
if not documenter.import_object():
365365
logger.warning(__('failed to import object %s'), real_name,
366-
location=self.get_source_info())
366+
location=self.get_location())
367367
items.append((display_name, '', '', real_name))
368368
continue
369369
if documenter.options.members and not documenter.check_module():

sphinx/util/cfamily.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class DefinitionError(Exception):
216216

217217
class BaseParser:
218218
def __init__(self, definition: str, *,
219-
location: Union[nodes.Node, Tuple[str, int]],
219+
location: Union[nodes.Node, Tuple[str, int], str],
220220
config: "Config") -> None:
221221
self.definition = definition.strip()
222222
self.location = location # for warnings

sphinx/util/docutils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ def set_source_info(self, node: Node) -> None:
339339
"""Set source and line number to the node."""
340340
node.source, node.line = self.get_source_info()
341341

342+
def get_location(self) -> str:
343+
"""Get current location info for logging."""
344+
return ':'.join(str(s) for s in self.get_source_info())
345+
342346

343347
class SphinxRole:
344348
"""A base class for Sphinx roles.
@@ -401,6 +405,10 @@ def get_source_info(self, lineno: int = None) -> Tuple[str, int]:
401405
def set_source_info(self, node: Node, lineno: int = None) -> None:
402406
node.source, node.line = self.get_source_info(lineno)
403407

408+
def get_location(self) -> str:
409+
"""Get current location info for logging."""
410+
return ':'.join(str(s) for s in self.get_source_info())
411+
404412

405413
class ReferenceRole(SphinxRole):
406414
"""A base class for reference roles.

0 commit comments

Comments
 (0)