From 27ee11a8d77f71f329d5cb6e4e58ce12ca148500 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Thu, 11 Apr 2019 18:15:43 -0500 Subject: [PATCH 1/2] get_dot_dependency_graph(): Don't use repr() to get statement labels Sometimes, it is useful to have labels that include escape sequences significant to the dot language. The string that comes from repr() doesn't allow us to do that because it escapes all the backslashes. >>> print(str("a\lb")) a\lb >>> print(repr("a\lb")) 'a\\lb' This change removes the use of repr() and documents that the statements are turned into strings using str(). --- pymbolic/imperative/utils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pymbolic/imperative/utils.py b/pymbolic/imperative/utils.py index fa13d57..bb69269 100644 --- a/pymbolic/imperative/utils.py +++ b/pymbolic/imperative/utils.py @@ -50,6 +50,8 @@ def get_dot_dependency_graph( """Return a string in the `dot `_ language depicting dependencies among kernel statements. + :arg statements: A sequence of statements, each of which is stringified by + calling :func:`str`. :arg preamble_hook: A function that returns an iterable of lines to add at the beginning of the graph :arg additional_lines_hook: A function that returns an iterable @@ -73,10 +75,7 @@ def get_dot_dependency_graph( stmt_label = str(stmt) tooltip = stmt.id - return "label=\"%s\",shape=\"box\",tooltip=\"%s\"" % ( - repr(stmt_label)[1:-1], - repr(tooltip)[1:-1], - ) + return "label=\"%s\",shape=\"box\",tooltip=\"%s\"" % (stmt_label, tooltip) lines = list(preamble_hook()) lines.append("rankdir=BT;") -- GitLab From 27f4b00eeecbe3363160d6f01daff2e3f8e4e36f Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Thu, 11 Apr 2019 21:57:44 -0500 Subject: [PATCH 2/2] Allow user control over stringification of statments by adding a *statement_stringifier* argument. The default stringifier escapes double quotes, which are the only escaped characters in the graphviz language. --- pymbolic/imperative/utils.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pymbolic/imperative/utils.py b/pymbolic/imperative/utils.py index bb69269..16e538e 100644 --- a/pymbolic/imperative/utils.py +++ b/pymbolic/imperative/utils.py @@ -44,6 +44,7 @@ def get_dot_dependency_graph( statements, use_stmt_ids=None, preamble_hook=_default_preamble_hook, additional_lines_hook=list, + statement_stringifier=lambda s: str(s).replace("\"", "\\\""), # deprecated use_insn_ids=None,): @@ -51,11 +52,15 @@ def get_dot_dependency_graph( dependencies among kernel statements. :arg statements: A sequence of statements, each of which is stringified by - calling :func:`str`. + calling *statement_stringifier*. + :arg statement_stringifier: The function to use for stringifying the + statements. The default stringifier uses :func:`str` and escapes all + double quotes (``"``) in the string representation. :arg preamble_hook: A function that returns an iterable of lines to add at the beginning of the graph :arg additional_lines_hook: A function that returns an iterable of lines to add at the end of the graph + """ if use_stmt_ids is not None and use_insn_ids is not None: @@ -70,9 +75,9 @@ def get_dot_dependency_graph( def get_node_attrs(stmt): if use_stmt_ids: stmt_label = stmt.id - tooltip = str(stmt) + tooltip = statement_stringifier(stmt) else: - stmt_label = str(stmt) + stmt_label = statement_stringifier(stmt) tooltip = stmt.id return "label=\"%s\",shape=\"box\",tooltip=\"%s\"" % (stmt_label, tooltip) -- GitLab