Skip to content

Commit a8293ea

Browse files
authored
Merge pull request #22724 from github/tausbn/python-normalise-tsg-python-attributes-in-logged-errors
Python: Normalise attribute ordering in `tsg-python` errors
2 parents a3db94d + 3ebbfc4 commit a8293ea

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

‎python/extractor/semmle/python/parser/tsg_parser.py‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def _decode_tsg_node_attributes(encoded_attrs, path, logger):
147147
)
148148
return attrs
149149

150+
def _format_node_attributes(attrs):
151+
return repr(dict(sorted(attrs.items())))
152+
150153
def read_tsg_python_output(path, logger):
151154
command_args = tsg_command + [path]
152155
p = subprocess.Popen(command_args, stdout=subprocess.PIPE)
@@ -209,7 +212,11 @@ def get_context(id, node_attr, path, logger):
209212

210213
while "ctx" not in node_attr[id]:
211214
if "_inherited_ctx" not in node_attr[id]:
212-
logger.error("No context for node {} in file {} with attributes {}\n".format(id, path, node_attr[id]))
215+
logger.error(
216+
"No context for node {} in file {} with attributes {}\n".format(
217+
id, path, _format_node_attributes(node_attr[id])
218+
)
219+
)
213220
# A missing context is most likely to be a "load", so return that.
214221
return ast.Load()
215222
id = node_attr[id]["_inherited_ctx"].id
@@ -321,7 +328,11 @@ def parse(path, logger):
321328
nodes[id] = attrs["_is_literal"]
322329
continue
323330
if "_kind" not in attrs:
324-
logger.error("Error: Graph node {} with attributes {} has no `_kind`!\n".format(id, attrs))
331+
logger.error(
332+
"Error: Graph node {} with attributes {} has no `_kind`!\n".format(
333+
id, _format_node_attributes(attrs)
334+
)
335+
)
325336
continue
326337
# This is not the node we are looking for (so don't bother creating it).
327338
if "_skip_to" in attrs:
@@ -353,7 +364,7 @@ def parse(path, logger):
353364
if field.startswith("_"): continue
354365
if field == "ctx": continue
355366
if field != "parenthesised" and field not in expected_fields:
356-
logger.warning("Unknown field {} found among {} in node {}\n".format(field, attrs, id))
367+
logger.warning("Unknown field {} found among {} in node {}\n".format(field, _format_node_attributes(attrs), id))
357368

358369
# For fields that point to other AST nodes.
359370
if isinstance(val, Node):

0 commit comments

Comments
 (0)