Use logging.suppress_logging()

This commit is contained in:
Jakob Lykke Andersen 2020-03-17 18:30:52 +01:00
parent 02c4a0d02f
commit 91e536597c
5 changed files with 27 additions and 47 deletions

View File

@ -3054,7 +3054,7 @@ class CObject(ObjectDescription):
def handle_signature(self, sig: str, signode: TextElement) -> ASTDeclaration: def handle_signature(self, sig: str, signode: TextElement) -> ASTDeclaration:
parentSymbol = self.env.temp_data['c:parent_symbol'] # type: Symbol parentSymbol = self.env.temp_data['c:parent_symbol'] # type: Symbol
parser = DefinitionParser(sig, location=signode, emitWarnings=True) parser = DefinitionParser(sig, location=signode)
try: try:
ast = self.parse_definition(parser) ast = self.parse_definition(parser)
parser.assert_end() parser.assert_end()
@ -3187,8 +3187,7 @@ class CExprRole(SphinxRole):
def run(self) -> Tuple[List[Node], List[system_message]]: def run(self) -> Tuple[List[Node], List[system_message]]:
text = self.text.replace('\n', ' ') text = self.text.replace('\n', ' ')
parser = DefinitionParser(text, location=self.get_source_info(), parser = DefinitionParser(text, location=self.get_source_info())
emitWarnings=True)
# attempt to mimic XRefRole classes, except that... # attempt to mimic XRefRole classes, except that...
classes = ['xref', 'c', self.class_type] classes = ['xref', 'c', self.class_type]
try: try:
@ -3316,15 +3315,14 @@ class CDomain(Domain):
ourObjects[fullname] = (fn, id_, objtype) ourObjects[fullname] = (fn, id_, objtype)
def _resolve_xref_inner(self, env: BuildEnvironment, fromdocname: str, builder: Builder, def _resolve_xref_inner(self, env: BuildEnvironment, fromdocname: str, builder: Builder,
typ: str, target: str, node: pending_xref, contnode: Element, typ: str, target: str, node: pending_xref,
emitWarnings: bool = True) -> Tuple[Element, str]: contnode: Element) -> Tuple[Element, str]:
parser = DefinitionParser(target, location=node, emitWarnings=emitWarnings) parser = DefinitionParser(target, location=node)
try: try:
name = parser.parse_xref_object() name = parser.parse_xref_object()
except DefinitionError as e: except DefinitionError as e:
if emitWarnings: logger.warning('Unparseable C cross-reference: %r\n%s', target, e,
logger.warning('Unparseable C cross-reference: %r\n%s', target, e, location=node)
location=node)
return None, None return None, None
parentKey = node.get("c:parent_key", None) # type: LookupKey parentKey = node.get("c:parent_key", None) # type: LookupKey
rootSymbol = self.data['root_symbol'] rootSymbol = self.data['root_symbol']
@ -3354,17 +3352,17 @@ class CDomain(Domain):
), declaration.objectType ), declaration.objectType
def resolve_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder, def resolve_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder,
typ: str, target: str, node: pending_xref, contnode: Element, typ: str, target: str, node: pending_xref,
emitWarnings: bool = True) -> Element: contnode: Element) -> Element:
return self._resolve_xref_inner(env, fromdocname, builder, typ, return self._resolve_xref_inner(env, fromdocname, builder, typ,
target, node, contnode)[0] target, node, contnode)[0]
def resolve_any_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder, def resolve_any_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder,
target: str, node: pending_xref, contnode: Element target: str, node: pending_xref, contnode: Element
) -> List[Tuple[str, Element]]: ) -> List[Tuple[str, Element]]:
retnode, objtype = self._resolve_xref_inner(env, fromdocname, builder, with logging.suppress_logging():
'any', target, node, contnode, retnode, objtype = self._resolve_xref_inner(env, fromdocname, builder,
emitWarnings=False) 'any', target, node, contnode)
if retnode: if retnode:
return [('c:' + self.role_for_objtype(objtype), retnode)] return [('c:' + self.role_for_objtype(objtype), retnode)]
return [] return []

View File

@ -4410,10 +4410,8 @@ class DefinitionParser(BaseParser):
def __init__(self, definition: str, *, def __init__(self, definition: str, *,
location: Union[nodes.Node, Tuple[str, int]], location: Union[nodes.Node, Tuple[str, int]],
emitWarnings: bool,
config: "Config") -> None: config: "Config") -> None:
super().__init__(definition, super().__init__(definition, location=location)
location=location, emitWarnings=emitWarnings)
self.config = config self.config = config
def _parse_string(self): def _parse_string(self):
@ -6366,9 +6364,7 @@ class CPPObject(ObjectDescription):
def handle_signature(self, sig: str, signode: desc_signature) -> ASTDeclaration: def handle_signature(self, sig: str, signode: desc_signature) -> ASTDeclaration:
parentSymbol = self.env.temp_data['cpp:parent_symbol'] parentSymbol = self.env.temp_data['cpp:parent_symbol']
parser = DefinitionParser(sig, location=signode, parser = DefinitionParser(sig, location=signode, config=self.env.config)
emitWarnings=True,
config=self.env.config)
try: try:
ast = self.parse_definition(parser) ast = self.parse_definition(parser)
parser.assert_end() parser.assert_end()
@ -6478,7 +6474,6 @@ class CPPNamespaceObject(SphinxDirective):
else: else:
parser = DefinitionParser(self.arguments[0], parser = DefinitionParser(self.arguments[0],
location=self.get_source_info(), location=self.get_source_info(),
emitWarnings=True,
config=self.config) config=self.config)
try: try:
ast = parser.parse_namespace_object() ast = parser.parse_namespace_object()
@ -6507,7 +6502,6 @@ class CPPNamespacePushObject(SphinxDirective):
return [] return []
parser = DefinitionParser(self.arguments[0], parser = DefinitionParser(self.arguments[0],
location=self.get_source_info(), location=self.get_source_info(),
emitWarnings=True,
config=self.config) config=self.config)
try: try:
ast = parser.parse_namespace_object() ast = parser.parse_namespace_object()
@ -6579,9 +6573,7 @@ class AliasTransform(SphinxTransform):
sig = node.sig sig = node.sig
parentKey = node.parentKey parentKey = node.parentKey
try: try:
parser = DefinitionParser(sig, parser = DefinitionParser(sig, location=node,
location=node,
emitWarnings=True,
config=self.env.config) config=self.env.config)
ast, isShorthand = parser.parse_xref_object() ast, isShorthand = parser.parse_xref_object()
parser.assert_end() parser.assert_end()
@ -6734,7 +6726,6 @@ class CPPExprRole(SphinxRole):
text = self.text.replace('\n', ' ') text = self.text.replace('\n', ' ')
parser = DefinitionParser(text, parser = DefinitionParser(text,
location=self.get_source_info(), location=self.get_source_info(),
emitWarnings=True,
config=self.config) config=self.config)
# attempt to mimic XRefRole classes, except that... # attempt to mimic XRefRole classes, except that...
classes = ['xref', 'cpp', self.class_type] classes = ['xref', 'cpp', self.class_type]
@ -6875,13 +6866,12 @@ class CPPDomain(Domain):
ourNames[name] = docname ourNames[name] = docname
def _resolve_xref_inner(self, env: BuildEnvironment, fromdocname: str, builder: Builder, def _resolve_xref_inner(self, env: BuildEnvironment, fromdocname: str, builder: Builder,
typ: str, target: str, node: pending_xref, contnode: Element, typ: str, target: str, node: pending_xref,
emitWarnings: bool = True) -> Tuple[Element, str]: contnode: Element) -> Tuple[Element, str]:
# add parens again for those that could be functions # add parens again for those that could be functions
if typ == 'any' or typ == 'func': if typ == 'any' or typ == 'func':
target += '()' target += '()'
parser = DefinitionParser(target, location=node, parser = DefinitionParser(target, location=node,
emitWarnings=emitWarnings,
config=env.config) config=env.config)
try: try:
ast, isShorthand = parser.parse_xref_object() ast, isShorthand = parser.parse_xref_object()
@ -6893,7 +6883,6 @@ class CPPDomain(Domain):
# hax on top of the paren hax to try to get correct errors # hax on top of the paren hax to try to get correct errors
parser2 = DefinitionParser(target[:-2], parser2 = DefinitionParser(target[:-2],
location=node, location=node,
emitWarnings=emitWarnings,
config=env.config) config=env.config)
try: try:
parser2.parse_xref_object() parser2.parse_xref_object()
@ -6902,9 +6891,8 @@ class CPPDomain(Domain):
# strange, that we don't get the error now, use the original # strange, that we don't get the error now, use the original
return target, e return target, e
t, ex = findWarning(e) t, ex = findWarning(e)
if emitWarnings: logger.warning('Unparseable C++ cross-reference: %r\n%s', t, ex,
logger.warning('Unparseable C++ cross-reference: %r\n%s', t, ex, location=node)
location=node)
return None, None return None, None
parentKey = node.get("cpp:parent_key", None) # type: LookupKey parentKey = node.get("cpp:parent_key", None) # type: LookupKey
rootSymbol = self.data['root_symbol'] rootSymbol = self.data['root_symbol']
@ -6977,7 +6965,7 @@ class CPPDomain(Domain):
return declTyp in objtypes return declTyp in objtypes
print("Type is %s (originally: %s), declType is %s" % (typ, origTyp, declTyp)) print("Type is %s (originally: %s), declType is %s" % (typ, origTyp, declTyp))
assert False assert False
if not checkType() and emitWarnings: if not checkType():
logger.warning("cpp:%s targets a %s (%s).", logger.warning("cpp:%s targets a %s (%s).",
origTyp, s.declaration.objectType, origTyp, s.declaration.objectType,
s.get_full_nested_name(), s.get_full_nested_name(),
@ -7044,9 +7032,9 @@ class CPPDomain(Domain):
def resolve_any_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder, def resolve_any_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder,
target: str, node: pending_xref, contnode: Element target: str, node: pending_xref, contnode: Element
) -> List[Tuple[str, Element]]: ) -> List[Tuple[str, Element]]:
retnode, objtype = self._resolve_xref_inner(env, fromdocname, builder, with logging.suppress_logging():
'any', target, node, contnode, retnode, objtype = self._resolve_xref_inner(env, fromdocname, builder,
emitWarnings=False) 'any', target, node, contnode)
if retnode: if retnode:
if objtype == 'templateParam': if objtype == 'templateParam':
return [('cpp:templateParam', retnode)] return [('cpp:templateParam', retnode)]

View File

@ -130,11 +130,9 @@ class DefinitionError(Exception):
class BaseParser: class BaseParser:
def __init__(self, definition: str, *, def __init__(self, definition: str, *,
location: Union[nodes.Node, Tuple[str, int]], location: Union[nodes.Node, Tuple[str, int]]) -> None:
emitWarnings: bool) -> None:
self.definition = definition.strip() self.definition = definition.strip()
self.location = location # for warnings self.location = location # for warnings
self.emitWarnings = emitWarnings
self.pos = 0 self.pos = 0
self.end = len(self.definition) self.end = len(self.definition)
@ -185,8 +183,7 @@ class BaseParser:
raise self._make_multi_error(errors, '') raise self._make_multi_error(errors, '')
def warn(self, msg: str) -> None: def warn(self, msg: str) -> None:
if self.emitWarnings: logger.warning(msg, location=self.location)
logger.warning(msg, location=self.location)
def match(self, regex: Pattern) -> bool: def match(self, regex: Pattern) -> bool:
match = regex.match(self.definition, self.pos) match = regex.match(self.definition, self.pos)

View File

@ -28,7 +28,7 @@ from sphinx.util import docutils
def parse(name, string): def parse(name, string):
parser = DefinitionParser(string, location=None, emitWarnings=True) parser = DefinitionParser(string, location=None)
parser.allowFallbackExpressionParsing = False parser.allowFallbackExpressionParsing = False
ast = parser.parse_declaration(name, name) ast = parser.parse_declaration(name, name)
parser.assert_end() parser.assert_end()
@ -87,7 +87,7 @@ def check(name, input, idDict, output=None):
def test_expressions(): def test_expressions():
def exprCheck(expr, output=None): def exprCheck(expr, output=None):
parser = DefinitionParser(expr, location=None, emitWarnings=True) parser = DefinitionParser(expr, location=None)
parser.allowFallbackExpressionParsing = False parser.allowFallbackExpressionParsing = False
ast = parser.parse_expression() ast = parser.parse_expression()
parser.assert_end() parser.assert_end()

View File

@ -24,7 +24,6 @@ def parse(name, string):
cpp_id_attributes = ["id_attr"] cpp_id_attributes = ["id_attr"]
cpp_paren_attributes = ["paren_attr"] cpp_paren_attributes = ["paren_attr"]
parser = DefinitionParser(string, location=None, parser = DefinitionParser(string, location=None,
emitWarnings=True,
config=Config()) config=Config())
parser.allowFallbackExpressionParsing = False parser.allowFallbackExpressionParsing = False
ast = parser.parse_declaration(name, name) ast = parser.parse_declaration(name, name)
@ -118,7 +117,6 @@ def test_expressions():
cpp_paren_attributes = ["paren_attr"] cpp_paren_attributes = ["paren_attr"]
parser = DefinitionParser(expr, location=None, parser = DefinitionParser(expr, location=None,
emitWarnings=True,
config=Config()) config=Config())
parser.allowFallbackExpressionParsing = False parser.allowFallbackExpressionParsing = False
ast = parser.parse_expression() ast = parser.parse_expression()
@ -792,7 +790,6 @@ def test_xref_parsing():
cpp_id_attributes = ["id_attr"] cpp_id_attributes = ["id_attr"]
cpp_paren_attributes = ["paren_attr"] cpp_paren_attributes = ["paren_attr"]
parser = DefinitionParser(target, location=None, parser = DefinitionParser(target, location=None,
emitWarnings=True,
config=Config()) config=Config())
ast, isShorthand = parser.parse_xref_object() ast, isShorthand = parser.parse_xref_object()
parser.assert_end() parser.assert_end()