Skip to contentSkip to conversation

perf flamegraph: explicitly set utf-8 encoding

PATCH Superseded Andreas Gerstmayr 2 participants 4 messages Jun 19, 2020 – Jun 22, 2020 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf flamegraph: explicitly set utf-8 encoding

on some platforms the default encoding is not utf-8,
which causes an UnicodeDecodeError when reading the flamegraph template

---
Tested with Python 2.7 and 3.

 tools/perf/scripts/python/flamegraph.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/perf/scripts/python/flamegraph.py +3 -2
index 61f3be9add6b..8979db910a8f 100755
@@ -17,6 +17,7 @@
 from __future__ import print_function
 import sys
 import os
+import io
 import argparse
 import json
 
@@ -81,7 +82,7 @@ class FlameGraphCLI:
 
         if self.args.format == "html":
             try:
-                with open(self.args.template) as f:
+                with io.open(self.args.template, encoding="utf-8") as f:
                     output_str = f.read().replace("/** @flamegraph_json **/",
                                                   json_str)
             except IOError as e:
@@ -97,7 +98,7 @@ class FlameGraphCLI:
         else:
             print("dumping data to {}".format(output_fn))
             try:
-                with open(output_fn, "w") as out:
+                with io.open(output_fn, "w", encoding="utf-8") as out:
                     out.write(output_str)
             except IOError as e:
                 print("Error writing output file: {}".format(e), file=sys.stderr)
Signature · 1 line
2.25.4
#1 lore raw

PATCH v2 1/1 perf flamegraph: explicitly set utf-8 encoding

on some platforms the default encoding is not utf-8,
which causes an UnicodeDecodeError when reading the flamegraph template and writing the flamegraph

---

changelog v2: also write to stdout with utf-8 encoding

 tools/perf/scripts/python/flamegraph.py | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/scripts/python/flamegraph.py +5 -3
index 61f3be9add6b..65780013f745 100755
@@ -17,6 +17,7 @@
 from __future__ import print_function
 import sys
 import os
+import io
 import argparse
 import json
 
@@ -81,7 +82,7 @@ class FlameGraphCLI:
 
         if self.args.format == "html":
             try:
-                with open(self.args.template) as f:
+                with io.open(self.args.template, encoding="utf-8") as f:
                     output_str = f.read().replace("/** @flamegraph_json **/",
                                                   json_str)
             except IOError as e:
@@ -93,11 +94,12 @@ class FlameGraphCLI:
             output_fn = self.args.output or "stacks.json"
 
         if output_fn == "-":
-            sys.stdout.write(output_str)
+            with io.open(sys.stdout.fileno(), "w", encoding="utf-8", closefd=False) as out:
+                out.write(output_str)
         else:
             print("dumping data to {}".format(output_fn))
             try:
-                with open(output_fn, "w") as out:
+                with io.open(output_fn, "w", encoding="utf-8") as out:
                     out.write(output_str)
             except IOError as e:
                 print("Error writing output file: {}".format(e), file=sys.stderr)
Signature · 1 line
2.25.4
maintainer Applied
#2 lore raw

Em Fri, Jun 19, 2020 at 05:32:31PM +0200, Andreas Gerstmayr escreveu:

Quoted 3 lines

on some platforms the default encoding is not utf-8,
which causes an UnicodeDecodeError when reading the flamegraph template and writing the flamegraph

Thanks, applied.

- Arnaldo

Quoted 47 lines

---

changelog v2: also write to stdout with utf-8 encoding

 tools/perf/scripts/python/flamegraph.py | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/scripts/python/flamegraph.py +5 -3
index 61f3be9add6b..65780013f745 100755
@@ -17,6 +17,7 @@
 from __future__ import print_function
 import sys
 import os
+import io
 import argparse
 import json
 
@@ -81,7 +82,7 @@ class FlameGraphCLI:
 
         if self.args.format == "html":
             try:
-                with open(self.args.template) as f:
+                with io.open(self.args.template, encoding="utf-8") as f:
                     output_str = f.read().replace("/** @flamegraph_json **/",
                                                   json_str)
             except IOError as e:
@@ -93,11 +94,12 @@ class FlameGraphCLI:
             output_fn = self.args.output or "stacks.json"
 
         if output_fn == "-":
-            sys.stdout.write(output_str)
+            with io.open(sys.stdout.fileno(), "w", encoding="utf-8", closefd=False) as out:
+                out.write(output_str)
         else:
             print("dumping data to {}".format(output_fn))
             try:
-                with open(output_fn, "w") as out:
+                with io.open(output_fn, "w", encoding="utf-8") as out:
                     out.write(output_str)
             except IOError as e:
                 print("Error writing output file: {}".format(e), file=sys.stderr)
Signature · 1 line
2.25.4
Signature · 1 line
- Arnaldo
#3 lore raw
Quoted 6 lines · Arnaldo Carvalho de Melo

Em Fri, Jun 19, 2020 at 05:32:31PM +0200, Andreas Gerstmayr escreveu:

Quoted 3 lines

on some platforms the default encoding is not utf-8,
which causes an UnicodeDecodeError when reading the flamegraph template and writing the flamegraph

Thanks, applied.

Thank you!

Cheers,
Andreas

Quoted 51 lines

- Arnaldo

Quoted 47 lines

---

changelog v2: also write to stdout with utf-8 encoding

  tools/perf/scripts/python/flamegraph.py | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/scripts/python/flamegraph.py +5 -3
index 61f3be9add6b..65780013f745 100755
@@ -17,6 +17,7 @@
  from __future__ import print_function
  import sys
  import os
+import io
  import argparse
  import json
 
@@ -81,7 +82,7 @@ class FlameGraphCLI:
 
          if self.args.format == "html":
              try:
-                with open(self.args.template) as f:
+                with io.open(self.args.template, encoding="utf-8") as f:
                      output_str = f.read().replace("/** @flamegraph_json **/",
                                                    json_str)
              except IOError as e:
@@ -93,11 +94,12 @@ class FlameGraphCLI:
              output_fn = self.args.output or "stacks.json"
 
          if output_fn == "-":
-            sys.stdout.write(output_str)
+            with io.open(sys.stdout.fileno(), "w", encoding="utf-8", closefd=False) as out:
+                out.write(output_str)
          else:
              print("dumping data to {}".format(output_fn))
              try:
-                with open(output_fn, "w") as out:
+                with io.open(output_fn, "w", encoding="utf-8") as out:
                      out.write(output_str)
              except IOError as e:
                  print("Error writing output file: {}".format(e), file=sys.stderr)
Signature · 1 line
2.25.4