[llvm] 43d9f2d - [opt viewer] Python compat - decode/encode string
Yuanfang Chen via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 29 14:49:59 PST 2020
Author: Yuanfang Chen
Date: 2020-01-29T14:49:24-08:00
New Revision: 43d9f2d1e8ae2765c1fc02b7cf757c9c05761ac0
URL: https://github.com/llvm/llvm-project/commit/43d9f2d1e8ae2765c1fc02b7cf757c9c05761ac0
DIFF: https://github.com/llvm/llvm-project/commit/43d9f2d1e8ae2765c1fc02b7cf757c9c05761ac0.diff
LOG: [opt viewer] Python compat - decode/encode string
Summary:
Use io.open instead of codecs.open according to here
https://stackoverflow.com/questions/10971033/backporting-python-3-openencoding-utf-8-to-python-2
Add `u` prefix to string literal to make them utf-8 in python2.
Reviewers: anemet, serge-sans-paille
Reviewed by: serge-sans-paille
Differential Revision: https://reviews.llvm.org/D73011
Added:
Modified:
llvm/tools/opt-viewer/opt-viewer.py
llvm/tools/opt-viewer/optrecord.py
Removed:
################################################################################
diff --git a/llvm/tools/opt-viewer/opt-viewer.py b/llvm/tools/opt-viewer/opt-viewer.py
index 4c105886cfdf..e98781595acc 100755
--- a/llvm/tools/opt-viewer/opt-viewer.py
+++ b/llvm/tools/opt-viewer/opt-viewer.py
@@ -4,9 +4,9 @@
import argparse
import cgi
-import codecs
import errno
import functools
+import io
from multiprocessing import cpu_count
import os.path
import re
@@ -54,12 +54,12 @@ def __init__(self, source_dir, output_dir, filename, no_highlight):
existing_filename = fn
self.no_highlight = no_highlight
- self.stream = codecs.open(os.path.join(output_dir, optrecord.html_file_name(filename)), 'w', encoding='utf-8')
+ self.stream = io.open(os.path.join(output_dir, optrecord.html_file_name(filename)), 'w', encoding='utf-8')
if existing_filename:
- self.source_stream = open(existing_filename)
+ self.source_stream = io.open(existing_filename, encoding='utf-8')
else:
self.source_stream = None
- print('''
+ print(u'''
<html>
<h1>Unable to locate file {}</h1>
</html>
@@ -72,10 +72,7 @@ def render_source_lines(self, stream, line_remarks):
file_text = stream.read()
if self.no_highlight:
- if sys.version_info.major >= 3:
- html_highlighted = file_text
- else:
- html_highlighted = file_text.decode('utf-8')
+ html_highlighted = file_text
else:
html_highlighted = highlight(
file_text,
@@ -147,7 +144,7 @@ def render(self, line_remarks):
if not self.source_stream:
return
- print('''
+ print(u'''
<html>
<title>{}</title>
<meta charset="utf-8" />
@@ -186,7 +183,7 @@ def render(self, line_remarks):
<tbody>'''.format(os.path.basename(self.filename)), file=self.stream)
self.render_source_lines(self.source_stream, line_remarks)
- print('''
+ print(u'''
</tbody>
</table>
</body>
@@ -195,7 +192,7 @@ def render(self, line_remarks):
class IndexRenderer:
def __init__(self, output_dir, should_display_hotness, max_hottest_remarks_on_index):
- self.stream = codecs.open(os.path.join(output_dir, 'index.html'), 'w', encoding='utf-8')
+ self.stream = io.open(os.path.join(output_dir, 'index.html'), 'w', encoding='utf-8')
self.should_display_hotness = should_display_hotness
self.max_hottest_remarks_on_index = max_hottest_remarks_on_index
@@ -210,7 +207,7 @@ def render_entry(self, r, odd):
</tr>'''.format(**locals()), file=self.stream)
def render(self, all_remarks):
- print('''
+ print(u'''
<html>
<meta charset="utf-8" />
<head>
@@ -233,7 +230,7 @@ def render(self, all_remarks):
for i, remark in enumerate(all_remarks[:max_entries]):
if not suppress(remark):
self.render_entry(remark, i % 2)
- print('''
+ print(u'''
</table>
</body>
</html>''', file=self.stream)
diff --git a/llvm/tools/opt-viewer/optrecord.py b/llvm/tools/opt-viewer/optrecord.py
index f6fd772689a9..2dff0c847c77 100644
--- a/llvm/tools/opt-viewer/optrecord.py
+++ b/llvm/tools/opt-viewer/optrecord.py
@@ -2,6 +2,7 @@
from __future__ import print_function
+import io
import yaml
# Try to use the C parser.
try:
@@ -272,7 +273,7 @@ def get_remarks(input_file, filter_=None):
all_remarks = dict()
file_remarks = defaultdict(functools.partial(defaultdict, list))
- with open(input_file) as f:
+ with io.open(input_file, encoding = 'utf-8') as f:
docs = yaml.load_all(f, Loader=Loader)
filter_e = None
More information about the llvm-commits
mailing list