linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf flamegraph: explicitly set utf-8 encoding
@ 2020-06-19 13:07 Andreas Gerstmayr
  2020-06-19 15:32 ` [PATCH v2] " Andreas Gerstmayr
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Gerstmayr @ 2020-06-19 13:07 UTC (permalink / raw)
  To: linux-perf-users
  Cc: Michael Petlan, Andreas Gerstmayr, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, linux-kernel

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

Signed-off-by: Andreas Gerstmayr <agerstmayr@redhat.com>
---
Tested with Python 2.7 and 3.

 tools/perf/scripts/python/flamegraph.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tools/perf/scripts/python/flamegraph.py b/tools/perf/scripts/python/flamegraph.py
index 61f3be9add6b..8979db910a8f 100755
--- a/tools/perf/scripts/python/flamegraph.py
+++ b/tools/perf/scripts/python/flamegraph.py
@@ -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)
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2] perf flamegraph: explicitly set utf-8 encoding
  2020-06-19 13:07 [PATCH] perf flamegraph: explicitly set utf-8 encoding Andreas Gerstmayr
@ 2020-06-19 15:32 ` Andreas Gerstmayr
  2020-06-22 16:31   ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Gerstmayr @ 2020-06-19 15:32 UTC (permalink / raw)
  To: linux-perf-users
  Cc: Michael Petlan, Andreas Gerstmayr, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, linux-kernel

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

Signed-off-by: Andreas Gerstmayr <agerstmayr@redhat.com>
---

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

 tools/perf/scripts/python/flamegraph.py | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tools/perf/scripts/python/flamegraph.py b/tools/perf/scripts/python/flamegraph.py
index 61f3be9add6b..65780013f745 100755
--- a/tools/perf/scripts/python/flamegraph.py
+++ b/tools/perf/scripts/python/flamegraph.py
@@ -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)
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] perf flamegraph: explicitly set utf-8 encoding
  2020-06-19 15:32 ` [PATCH v2] " Andreas Gerstmayr
@ 2020-06-22 16:31   ` Arnaldo Carvalho de Melo
  2020-06-22 17:45     ` Andreas Gerstmayr
  0 siblings, 1 reply; 4+ messages in thread
From: Arnaldo Carvalho de Melo @ 2020-06-22 16:31 UTC (permalink / raw)
  To: Andreas Gerstmayr
  Cc: linux-perf-users, Michael Petlan, Peter Zijlstra, Ingo Molnar,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	linux-kernel

Em Fri, Jun 19, 2020 at 05:32:31PM +0200, Andreas Gerstmayr escreveu:
> 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
 
> Signed-off-by: Andreas Gerstmayr <agerstmayr@redhat.com>
> ---
> 
> changelog v2: also write to stdout with utf-8 encoding
> 
>  tools/perf/scripts/python/flamegraph.py | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/perf/scripts/python/flamegraph.py b/tools/perf/scripts/python/flamegraph.py
> index 61f3be9add6b..65780013f745 100755
> --- a/tools/perf/scripts/python/flamegraph.py
> +++ b/tools/perf/scripts/python/flamegraph.py
> @@ -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)
> -- 
> 2.25.4
> 

-- 

- Arnaldo

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] perf flamegraph: explicitly set utf-8 encoding
  2020-06-22 16:31   ` Arnaldo Carvalho de Melo
@ 2020-06-22 17:45     ` Andreas Gerstmayr
  0 siblings, 0 replies; 4+ messages in thread
From: Andreas Gerstmayr @ 2020-06-22 17:45 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: linux-perf-users, Michael Petlan, Peter Zijlstra, Ingo Molnar,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	linux-kernel

On 22.06.20 18:31, Arnaldo Carvalho de Melo wrote:
> Em Fri, Jun 19, 2020 at 05:32:31PM +0200, Andreas Gerstmayr escreveu:
>> 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


> 
> - Arnaldo
>   
>> Signed-off-by: Andreas Gerstmayr <agerstmayr@redhat.com>
>> ---
>>
>> changelog v2: also write to stdout with utf-8 encoding
>>
>>   tools/perf/scripts/python/flamegraph.py | 8 +++++---
>>   1 file changed, 5 insertions(+), 3 deletions(-)
>>
>> diff --git a/tools/perf/scripts/python/flamegraph.py b/tools/perf/scripts/python/flamegraph.py
>> index 61f3be9add6b..65780013f745 100755
>> --- a/tools/perf/scripts/python/flamegraph.py
>> +++ b/tools/perf/scripts/python/flamegraph.py
>> @@ -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)
>> -- 
>> 2.25.4
>>
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-06-22 17:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-19 13:07 [PATCH] perf flamegraph: explicitly set utf-8 encoding Andreas Gerstmayr
2020-06-19 15:32 ` [PATCH v2] " Andreas Gerstmayr
2020-06-22 16:31   ` Arnaldo Carvalho de Melo
2020-06-22 17:45     ` Andreas Gerstmayr

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).