On some platforms the default encoding is not utf-8, which causes an UnicodeDecodeError when reading the flamegraph template and writing the flamegraph
Showing the changes to tools/perf/scripts/python (1 of 1 file). Show all
Unified · Split
| @@ -17,6 +17,7 @@ · 16 unmodified lines | ||
| 17 | 17 | from __future__ import print_function |
| 18 | 18 | import sys |
| 19 | 19 | import os |
| 20 | +import io | |
| 20 | 21 | import argparse |
| 21 | 22 | import json |
| 22 | 23 | |
| @@ -81,7 +82,7 @@ class FlameGraphCLI: · 58 unmodified lines | ||
| 81 | 82 | |
| 82 | 83 | if self.args.format == "html": |
| 83 | 84 | try: |
| 84 | − with open(self.args.template) as f: | |
| 85 | + with io.open(self.args.template, encoding="utf-8") as f: | |
| 85 | 86 | output_str = f.read().replace("/** @flamegraph_json **/", |
| 86 | 87 | json_str) |
| 87 | 88 | except IOError as e: |
| @@ -93,11 +94,12 @@ class FlameGraphCLI: · 5 unmodified lines | ||
| 93 | 94 | output_fn = self.args.output or "stacks.json" |
| 94 | 95 | |
| 95 | 96 | if output_fn == "-": |
| 96 | − sys.stdout.write(output_str) | |
| 97 | + with io.open(sys.stdout.fileno(), "w", encoding="utf-8", closefd=False) as out: | |
| 98 | + out.write(output_str) | |
| 97 | 99 | else: |
| 98 | 100 | print("dumping data to {}".format(output_fn)) |
| 99 | 101 | try: |
| 100 | − with open(output_fn, "w") as out: | |
| 102 | + with io.open(output_fn, "w", encoding="utf-8") as out: | |
| 101 | 103 | out.write(output_str) |
| 102 | 104 | except IOError as e: |
| 103 | 105 | print("Error writing output file: {}".format(e), file=sys.stderr) |