diff options
| author | Roman Lebedev <lebedev.ri@gmail.com> | 2017-10-10 19:34:15 +0000 |
|---|---|---|
| committer | Roman Lebedev <lebedev.ri@gmail.com> | 2017-10-10 19:34:15 +0000 |
| commit | afd049b6a4bf59228e1f79bffb8a06199d1444a7 (patch) | |
| tree | 87c08de649531fbff2c3832285279ea2ea151562 | |
| parent | 8ed119877426a4dfe60997b8aa9752e635c53962 (diff) | |
| download | bcm5719-llvm-afd049b6a4bf59228e1f79bffb8a06199d1444a7.tar.gz bcm5719-llvm-afd049b6a4bf59228e1f79bffb8a06199d1444a7.zip | |
[opt-viewer] Don't Decode HTML bytes for Python 2
Summary:
D36624 added some python3 compatibility. But that fix has a problem..
With python2 (which is specified by `#!/usr/bin/env python2.7`), if the env variables do not specify the UTF8,
and the source file is UTF8 (contains non-ASCII symbols), then the `.decode('utf-8')` causes the following exception:
```
Reading YAML files...
Rendering HTML files...
8 of 41Traceback (most recent call last):
File "/build/llvm/tools/opt-viewer/opt-viewer.py", line 277, in <module>
print_progress)
File "/build/llvm/tools/opt-viewer/opt-viewer.py", line 213, in generate_report
should_print_progress)
File "/build/llvm/tools/opt-viewer/optpmap.py", line 45, in pmap
result = map(_wrapped_func, func_and_args, *args, **kwargs)
File "/build/llvm/tools/opt-viewer/optpmap.py", line 25, in _wrapped_func
return func(argument)
File "/build/llvm/tools/opt-viewer/opt-viewer.py", line 174, in _render_file
SourceFileRenderer(source_dir, output_dir, filename).render(remarks)
File "/build/llvm/tools/opt-viewer/opt-viewer.py", line 125, in render
self.render_source_lines(self.source_stream, line_remarks)
File "/build/llvm/tools/opt-viewer/opt-viewer.py", line 79, in render_source_lines
</tr>'''.format(**locals()), file=self.stream)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf4' in position 47: ordinal not in range(128)
```
This is similar to https://bugs.llvm.org/show_bug.cgi?id=33548, which was fixed by https://reviews.llvm.org/D37661
Unlike that fix, here, *removing* `.decode('utf-8')` actually fixes it.
Since i assume that the original fix is needed, i simply made
that fix conditional, since for python2 it actually breaks things.
Reviewers: modocache, anemet
Reviewed By: anemet
Subscribers: fhahn, llvm-commits
Differential Revision: https://reviews.llvm.org/D38289
llvm-svn: 315350
| -rwxr-xr-x | llvm/tools/opt-viewer/opt-viewer.py | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/llvm/tools/opt-viewer/opt-viewer.py b/llvm/tools/opt-viewer/opt-viewer.py index 08fe2d585e9..15e76d65bf3 100755 --- a/llvm/tools/opt-viewer/opt-viewer.py +++ b/llvm/tools/opt-viewer/opt-viewer.py @@ -10,6 +10,7 @@ from multiprocessing import cpu_count import os.path import re import shutil +import sys from pygments import highlight from pygments.lexers.c_cpp import CppLexer @@ -62,7 +63,11 @@ class SourceFileRenderer: html_highlighted = highlight( file_text, self.cpp_lexer, - self.html_formatter).decode('utf-8') + self.html_formatter) + + # On Python 3, pygments.highlight() returns a bytes object, not a str. + if sys.version_info >= (3, 0): + html_highlighted = html_highlighted.decode('utf-8') # Take off the header and footer, these must be # reapplied line-wise, within the page structure |

