[PATCH] D71746: Fix the "TypeError: a bytes-like object is required, not 'str'" in exploded-graph-rewriter.py on Python 3.5+

Pavel Samolysov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 20 02:49:06 PST 2019


psamolysov created this revision.
psamolysov added reviewers: dergachev.a, clang.
psamolysov added a project: clang.
Herald added a subscriber: cfe-commits.

When I run the 'exploded-graph-rewriter.py' tool on Windows using Python 3.5 and above, the following error and stack trace occurs:

  Traceback (most recent call last):
    File "C:\Work\Dev\llvm\llvm-monorepo\clang\utils\analyzer\exploded-graph-rewriter.py", line 1061, in <module>
      main()
    File "C:\Work\Dev\llvm\llvm-monorepo\clang\utils\analyzer\exploded-graph-rewriter.py", line 1057, in main
      explorer.explore(graph, visitor)
    File "C:\Work\Dev\llvm\llvm-monorepo\clang\utils\analyzer\exploded-graph-rewriter.py", line 911, in explore
      visitor.visit_end_of_graph()
    File "C:\Work\Dev\llvm\llvm-monorepo\clang\utils\analyzer\exploded-graph-rewriter.py", line 879, in visit_end_of_graph
      svg = graphviz.pipe('dot', 'svg', self.output())
    File "C:\Program Files\Python37\lib\site-packages\graphviz\backend.py", line 229, in pipe
      out, _ = run(cmd, input=data, capture_output=True, check=True, quiet=quiet)
    File "C:\Program Files\Python37\lib\site-packages\graphviz\backend.py", line 166, in run
      out, err = proc.communicate(input)
    File "C:\Program Files\Python37\lib\subprocess.py", line 920, in communicate
      stdout, stderr = self._communicate(input, endtime, timeout)
    File "C:\Program Files\Python37\lib\subprocess.py", line 1238, in _communicate
      self._stdin_write(input)
    File "C:\Program Files\Python37\lib\subprocess.py", line 854, in _stdin_write
      self.stdin.write(input)
  TypeError: a bytes-like object is required, not 'str'

Due to work with Unicode in Python begin from 3.5, the output string must be encoded, so I put the code to detect whether the script works on Python 3.5+ and use the encode() method if so. After this manipulations, the exploded-graph-rewriter.py script works fine on Windows and Python 3.7.

I haven't tried the script on Python 2


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71746

Files:
  clang/utils/analyzer/exploded-graph-rewriter.py


Index: clang/utils/analyzer/exploded-graph-rewriter.py
===================================================================
--- clang/utils/analyzer/exploded-graph-rewriter.py
+++ clang/utils/analyzer/exploded-graph-rewriter.py
@@ -18,6 +18,7 @@
 import logging
 import os
 import re
+import sys
 
 
 #===-----------------------------------------------------------------------===#
@@ -425,7 +426,10 @@
 
     def output(self):
         assert not self._dump_dot_only
-        return ''.join(self._output)
+        if sys.version_info[0] > 2 and sys.version_info[1] >= 5:
+            return ''.join(self._output).encode()
+        else:
+            return ''.join(self._output)
 
     def _dump(self, s):
         s = s.replace('&', '&') \


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71746.234847.patch
Type: text/x-patch
Size: 744 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191220/21abf0c3/attachment.bin>


More information about the cfe-commits mailing list