Skip to contentSkip to conversation

decodetree: Open files with encoding='utf-8'

PATCH Superseded Philippe Mathieu-Daudé 7 participants 12 messages Jan 8, 2021 – Jan 12, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 decodetree: Open files with encoding='utf-8'

When decodetree.py was added in commit 568ae7efae7, QEMU was using Python 2 which happily reads UTF-8 files in text mode. Python 3 requires either UTF-8 locale or an explicit encoding passed to open(). Now that Python 3 is required, explicit UTF-8 encoding for decodetree sources.

This fixes:

  $ /usr/bin/python3 scripts/decodetree.py test.decode
  Traceback (most recent call last):
    File "scripts/decodetree.py", line 1397, in <module>
      main()
    File "scripts/decodetree.py", line 1308, in main
      parse_file(f, toppat)
    File "scripts/decodetree.py", line 994, in parse_file
      for line in f:
    File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode
      return codecs.ascii_decode(input, self.errors)[0]
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 80:
  ordinal not in range(128)
---
 scripts/decodetree.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/decodetree.py +1 -1
index 47aa9caf6d1..fa40903cff1 100644
@@ -1304,7 +1304,7 @@ def main():
 
     for filename in args:
         input_file = filename
-        f = open(filename, 'r')
+        f = open(filename, 'r', encoding='utf-8')
         parse_file(f, toppat)
         f.close()
 
Signature · 1 line
2.26.2
Reviewed-by Yonggang Luo on v1 1/1
#1 lore raw
Quoted 42 lines · Philippe Mathieu-Daudé

When decodetree.py was added in commit 568ae7efae7, QEMU was using Python 2 which happily reads UTF-8 files in text mode. Python 3 requires either UTF-8 locale or an explicit encoding passed to open(). Now that Python 3 is required, explicit UTF-8 encoding for decodetree sources.

This fixes:

  $ /usr/bin/python3 scripts/decodetree.py test.decode
  Traceback (most recent call last):
    File "scripts/decodetree.py", line 1397, in <module>
      main()
    File "scripts/decodetree.py", line 1308, in main
      parse_file(f, toppat)
    File "scripts/decodetree.py", line 994, in parse_file
      for line in f:
    File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode
      return codecs.ascii_decode(input, self.errors)[0]
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 80:
  ordinal not in range(128)
---
 scripts/decodetree.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/decodetree.py +1 -2
index 47aa9caf6d1..fa40903cff1 100644
@@ -1304,7 +1304,7 @@ def main():
 
     for filename in args:
         input_file = filename
-        f = open(filename, 'r')
+        f = open(filename, 'r', encoding='utf-8')
         parse_file(f, toppat)
         f.close()
 
--
Quoted 3 lines

2.26.2

Reviewed-by Yonggang Luo <l <[email protected]>[email protected]
Quoted 1 line

--
此致
礼
罗勇刚
Yours
sincerely,
Yonggang Luo

#2 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 33 lines · Philippe Mathieu-Daudé

When decodetree.py was added in commit 568ae7efae7, QEMU was using Python 2 which happily reads UTF-8 files in text mode. Python 3 requires either UTF-8 locale or an explicit encoding passed to open(). Now that Python 3 is required, explicit UTF-8 encoding for decodetree sources.

This fixes:

  $ /usr/bin/python3 scripts/decodetree.py test.decode
  Traceback (most recent call last):
    File "scripts/decodetree.py", line 1397, in <module>
      main()
    File "scripts/decodetree.py", line 1308, in main
      parse_file(f, toppat)
    File "scripts/decodetree.py", line 994, in parse_file
      for line in f:
    File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode
      return codecs.ascii_decode(input, self.errors)[0]
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 80:
  ordinal not in range(128)
---
 scripts/decodetree.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/decodetree.py +0 -0
index 47aa9caf6d1..fa40903cff1 100644

scripts/decodetree.py def main(): · line 1309

Quoted 7 lines
scripts/decodetree.py +1 -1
@@ -1304,7 +1304,7 @@ def main():
 
     for filename in args:
         input_file = filename
-        f = open(filename, 'r')
+        f = open(filename, 'r', encoding='utf-8')
         parse_file(f, toppat)
         f.close()

Should we also be opening the output file explicitly as utf-8 ? (How do we say "write to sys.stdout as utf-8" for the case where we're doing that?)

thanks
-- PMM

#3 lore raw
Quoted 2 lines · Peter Maydell

On Fri, 8 Jan 2021 at 15:16, Philippe Mathieu-Daudé <[email protected]>

Quoted 9 lines · wrote:
Quoted 9 lines

When decodetree.py was added in commit 568ae7efae7, QEMU was using Python 2 which happily reads UTF-8 files in text mode. Python 3 requires either UTF-8 locale or an explicit encoding passed to open(). Now that Python 3 is required, explicit UTF-8 encoding for decodetree sources.

This fixes:

Quoted 11 lines
Quoted 11 lines
  $ /usr/bin/python3 scripts/decodetree.py test.decode
  Traceback (most recent call last):
    File "scripts/decodetree.py", line 1397, in <module>
      main()
    File "scripts/decodetree.py", line 1308, in main
      parse_file(f, toppat)
    File "scripts/decodetree.py", line 994, in parse_file
      for line in f:
    File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode
      return codecs.ascii_decode(input, self.errors)[0]
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position

80:

Quoted 14 lines
Quoted 14 lines

ordinal not in range(128)

---
 scripts/decodetree.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/decodetree.py +0 -0
index 47aa9caf6d1..fa40903cff1 100644
@@ -1304,7 +1304,7 @@ def main():
 
Quoted 10 lines

scripts/decodetree.py def main(): · line 1309

Quoted 6 lines
scripts/decodetree.py +1 -1
     for filename in args:
         input_file = filename
-        f = open(filename, 'r')
+        f = open(filename, 'r', encoding='utf-8')
         parse_file(f, toppat)
         f.close()

Should we also be opening the output file explicitly as utf-8 ? (How do we say "write to sys.stdout as utf-8" for the case where we're doing that?)

Can be done with

```
        sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf8",
errors="ignore")
```
Quoted 4 lines

thanks
-- PMM

--
此致
礼
罗勇刚
Yours
sincerely,
Yonggang Luo

#4 lore raw
Quoted 51 lines · 罗勇刚(Yonggang Luo)
Quoted 2 lines · Peter Maydell

On Fri, 8 Jan 2021 at 15:16, Philippe Mathieu-Daudé <[email protected]>

Quoted 9 lines · wrote:
Quoted 9 lines

When decodetree.py was added in commit 568ae7efae7, QEMU was using Python 2 which happily reads UTF-8 files in text mode. Python 3 requires either UTF-8 locale or an explicit encoding passed to open(). Now that Python 3 is required, explicit UTF-8 encoding for decodetree sources.

This fixes:

Quoted 11 lines
Quoted 11 lines
  $ /usr/bin/python3 scripts/decodetree.py test.decode
  Traceback (most recent call last):
    File "scripts/decodetree.py", line 1397, in <module>
      main()
    File "scripts/decodetree.py", line 1308, in main
      parse_file(f, toppat)
    File "scripts/decodetree.py", line 994, in parse_file
      for line in f:
    File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode
      return codecs.ascii_decode(input, self.errors)[0]
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position

80:

Quoted 14 lines
Quoted 14 lines

ordinal not in range(128)

---
 scripts/decodetree.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/decodetree.py +0 -0
index 47aa9caf6d1..fa40903cff1 100644
@@ -1304,7 +1304,7 @@ def main():
 
Quoted 10 lines

scripts/decodetree.py def main(): · line 1309

Quoted 6 lines
scripts/decodetree.py +1 -1
     for filename in args:
         input_file = filename
-        f = open(filename, 'r')
+        f = open(filename, 'r', encoding='utf-8')
         parse_file(f, toppat)
         f.close()

Should we also be opening the output file explicitly as utf-8 ? (How do we say "write to sys.stdout as utf-8" for the case where we're doing that?)

Quoted 5 lines

Can be done with

```
        sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf8",
errors="ignore")
```

In the specific case of decodetree, just assigning this to output_fd is enough, and less hacky than overwriting sys.stdout.

Signature · 1 line
Eduardo
#5 lore raw
In reply to Peter Maydell
Quoted 35 lines · Peter Maydell
Quoted 34 lines · Philippe Mathieu-Daudé

When decodetree.py was added in commit 568ae7efae7, QEMU was using Python 2 which happily reads UTF-8 files in text mode. Python 3 requires either UTF-8 locale or an explicit encoding passed to open(). Now that Python 3 is required, explicit UTF-8 encoding for decodetree sources.

This fixes:

  $ /usr/bin/python3 scripts/decodetree.py test.decode
  Traceback (most recent call last):
    File "scripts/decodetree.py", line 1397, in <module>
      main()
    File "scripts/decodetree.py", line 1308, in main
      parse_file(f, toppat)
    File "scripts/decodetree.py", line 994, in parse_file
      for line in f:
    File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode
      return codecs.ascii_decode(input, self.errors)[0]
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 80:
  ordinal not in range(128)
---
 scripts/decodetree.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/decodetree.py +0 -0
index 47aa9caf6d1..fa40903cff1 100644
@@ -1304,7 +1304,7 @@ def main():
 
Quoted 10 lines

scripts/decodetree.py def main(): · line 1309

Quoted 6 lines
scripts/decodetree.py +1 -1
     for filename in args:
         input_file = filename
-        f = open(filename, 'r')
+        f = open(filename, 'r', encoding='utf-8')
         parse_file(f, toppat)
         f.close()

Should we also be opening the output file explicitly as utf-8 ? (How do we say "write to sys.stdout as utf-8" for the case where we're doing that?)

I have been wondering about it, but the content written in the output file is plain C code using only ASCII, which any locale is able to process. But indeed maybe we prefer ignore the user locale... I'm not sure.

#6 lore raw
Quoted 10 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Peter Maydell

Should we also be opening the output file explicitly as utf-8 ? (How do we say "write to sys.stdout as utf-8" for the case where we're doing that?)

I have been wondering about it, but the content written in the output file is plain C code using only ASCII, which any locale is able to process. But indeed maybe we prefer ignore the user locale... I'm not sure.

I'm not a python expert so I don't know what the usual thing is here, but it seems to me better to insulate our build process from what the user's locale happens to be set to, even if it happens that we currently only output ASCII.

thanks
-- PMM

#7 lore raw
Quoted 12 lines · Peter Maydell
Quoted 10 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Peter Maydell

Should we also be opening the output file explicitly as utf-8 ? (How do we say "write to sys.stdout as utf-8" for the case where we're doing that?)

I have been wondering about it, but the content written in the output file is plain C code using only ASCII, which any locale is able to process. But indeed maybe we prefer ignore the user locale... I'm not sure.

Quoted 4 lines

I'm not a python expert so I don't know what the usual thing is here, but it seems to me better to insulate our build process from what the user's locale happens to be set to, even if it happens that we currently only output ASCII.

OK, I'll respin.

#8 lore raw
In reply to Philippe Mathieu-Daudé

I had a similar issue in the past with the acceptance tests. Some VMs send UTF-8 output in their console and the acceptance test script would bail out if the locale was not UTF-8.

I sent a patch on the ml but it probably got lost:
https://lists.gnu.org/archive/html/qemu-devel/2020-07/msg06086.html

I can re-spin it if you guys are interested

Quoted 40 lines · Philippe Mathieu-Daudé

When decodetree.py was added in commit 568ae7efae7, QEMU was using Python 2 which happily reads UTF-8 files in text mode. Python 3 requires either UTF-8 locale or an explicit encoding passed to open(). Now that Python 3 is required, explicit UTF-8 encoding for decodetree sources.

This fixes:

   $ /usr/bin/python3 scripts/decodetree.py test.decode
   Traceback (most recent call last):
     File "scripts/decodetree.py", line 1397, in <module>
       main()
     File "scripts/decodetree.py", line 1308, in main
       parse_file(f, toppat)
     File "scripts/decodetree.py", line 994, in parse_file
       for line in f:
     File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode
       return codecs.ascii_decode(input, self.errors)[0]
   UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 80:
   ordinal not in range(128)
---
  scripts/decodetree.py | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
scripts/decodetree.py +1 -1
index 47aa9caf6d1..fa40903cff1 100644
@@ -1304,7 +1304,7 @@ def main():
 
      for filename in args:
          input_file = filename
-        f = open(filename, 'r')
+        f = open(filename, 'r', encoding='utf-8')
          parse_file(f, toppat)
          f.close()
 
#9 lore raw

[CCing John, Wainer]

Quoted 8 lines · Daniele Buono

I had a similar issue in the past with the acceptance tests. Some VMs send UTF-8 output in their console and the acceptance test script would bail out if the locale was not UTF-8.

I sent a patch on the ml but it probably got lost:
https://lists.gnu.org/archive/html/qemu-devel/2020-07/msg06086.html

I can re-spin it if you guys are interested

The mbox at
https://lore.kernel.org/qemu-devel/[email protected]/
can still be applied cleanly, I don't think you need to resubmit.

However, we have no owner for tests/acceptance/avocado_qemu in MAINTAINERS. Is anybody currently taking care of tests/acceptance patches and making sure they are merged?

Signature · 1 line
Eduardo
#10 lore raw
Quoted 12 lines · Eduardo Habkost

[CCing John, Wainer]

Quoted 8 lines · Daniele Buono

I had a similar issue in the past with the acceptance tests. Some VMs send UTF-8 output in their console and the acceptance test script would bail out if the locale was not UTF-8.

I sent a patch on the ml but it probably got lost:
https://lists.gnu.org/archive/html/qemu-devel/2020-07/msg06086.html

I can re-spin it if you guys are interested

Quoted 8 lines

The mbox at
https://lore.kernel.org/qemu-devel/[email protected]/
can still be applied cleanly, I don't think you need to resubmit.

However, we have no owner for tests/acceptance/avocado_qemu in MAINTAINERS. Is anybody currently taking care of tests/acceptance patches and making sure they are merged?

I touch these tests sometimes, but I know very little about avocado overall, so I don't think it's going to be me taking point here.

(I don't mind taking a reviewer stanza for something like *.py, though.)

Acceptance (Integration) Testing with the Avocado framework
W: https://trello.com/b/6Qi1pxVn/avocado-qemu
R: Cleber Rosa <[email protected]>
R: Philippe Mathieu-Daudé <[email protected]>
R: Wainer dos Santos Moschetta <[email protected]>
S: Odd Fixes
F: tests/acceptance/

Why is this only "Odd Fixes"? Isn't it new within the last ~2y? The avocado_qemu module itself was largely written by Cleber, Philippe and Caio.

--js

#11 lore raw
Quoted 13 lines · John Snow
Quoted 12 lines · Eduardo Habkost

[CCing John, Wainer]

Quoted 8 lines · Daniele Buono

I had a similar issue in the past with the acceptance tests. Some VMs send UTF-8 output in their console and the acceptance test script would bail out if the locale was not UTF-8.

I sent a patch on the ml but it probably got lost:
https://lists.gnu.org/archive/html/qemu-devel/2020-07/msg06086.html

I can re-spin it if you guys are interested

Quoted 8 lines
Quoted 8 lines

The mbox at
https://lore.kernel.org/qemu-devel/[email protected]/

can still be applied cleanly, I don't think you need to resubmit.

However, we have no owner for tests/acceptance/avocado_qemu in MAINTAINERS.  Is anybody currently taking care of tests/acceptance patches and making sure they are merged?

[1] The answer to this question is below in [2]...

Quoted 13 lines

I touch these tests sometimes, but I know very little about avocado overall, so I don't think it's going to be me taking point here.

(I don't mind taking a reviewer stanza for something like *.py, though.)

Acceptance (Integration) Testing with the Avocado framework
W: https://trello.com/b/6Qi1pxVn/avocado-qemu
R: Cleber Rosa <[email protected]>
R: Philippe Mathieu-Daudé <[email protected]>
R: Wainer dos Santos Moschetta <[email protected]>
S: Odd Fixes
F: tests/acceptance/

Quoted 3 lines

Why is this only "Odd Fixes"? Isn't it new within the last ~2y? The avocado_qemu module itself was largely written by Cleber, Philippe and Caio.

[2] The answer to this question is above in [1] :)

Quoted 4 lines

--js