Skip to contentSkip to conversation

Update check-python-tox test for pylint 2.10

PATCH Superseded John Snow 2 participants 4 messages Sep 15, 2021

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

John Snow

author
#0 lore raw

v2 cover Update check-python-tox test for pylint 2.10

V2: It's not safe to use sys.stderr.encoding to determine a "console encoding", because that uses the "current" stderr and not a hypothetically generic one -- and doing this causes the acceptance tests to fail.

Use UTF-8 instead.

Question: What encoding do terminal programs use? Is there an inherent encoding to fprintf et al, or does it just push whatever bytes you put into it straight into the stdout/stderr pipe?

John Snow (1):
python: Update for pylint 2.10

 python/qemu/machine/machine.py | 3 ++-
 python/setup.cfg               | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v2 1/1 python: Update for pylint 2.10

A few new annoyances. Of note is the new warning for an unspecified encoding when opening a text file, which actually does indicate a potentially real problem; see
https://www.python.org/dev/peps/pep-0597/#motivation

It's not clear to me what the "right" encoding is; it depends on whatever encoding QEMU is using when it prints to terminal. I'm going to assume UTF-8 works.

---
 python/qemu/machine/machine.py | 3 ++-
 python/setup.cfg               | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)
python/qemu/machine/machine.py +2 -1
index a7081b1845..a27a80497d 100644
@@ -291,7 +291,8 @@ def get_pid(self) -> Optional[int]:
 
     def _load_io_log(self) -> None:
         if self._qemu_log_path is not None:
-            with open(self._qemu_log_path, "r") as iolog:
+            with open(self._qemu_log_path, "r",
+                      encoding='utf-8') as iolog:
                 self._iolog = iolog.read()
 
     @property
python/setup.cfg +1 -0
index 83909c1c97..0f0cab098f 100644
@@ -104,6 +104,7 @@ good-names=i,
 [pylint.similarities]
 # Ignore imports when computing similarities.
 ignore-imports=yes
+ignore-signatures=yes
 
 # Minimum lines number of a similarity.
 # TODO: Remove after we opt in to Pylint 2.8.3. See commit msg.
Signature · 1 line
2.31.1
#2 lore raw
In reply to John Snow
Quoted 10 lines · John Snow

V2: It's not safe to use sys.stderr.encoding to determine a "console encoding", because that uses the "current" stderr and not a hypothetically generic one -- and doing this causes the acceptance tests to fail.

Use UTF-8 instead.

Question: What encoding do terminal programs use? Is there an inherent encoding to fprintf et al, or does it just push whatever bytes you put into it straight into the stdout/stderr pipe?

Programs are expected to output data in the encoding that is set in the various env variables LC_ALL/LC_CTYPE/LANG.

In traditional end user scenarios this almost always means UTF-8 charset.

There's plenty of cases which end up with the C locale though, which would mean 7-bit ASCII on Linux, though apps are supposed to be 8-bit clean allow data with the high bit to pass through without interpretation. The latter is what python3 gets very wrong complaining if you output 8-bit high data in C locale.

There is increasing support for a C.UTF-8 locale to bring it closer to other locales which are all UTF-8.

On macOS the C locale has been UTF-8 by default indefinitely.

Windows is a whole other world of fun and IIRC isn't UTF-8 by default, but I don't recall details.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
author
#3 lore raw
Quoted 32 lines · Daniel P. Berrangé
Quoted 10 lines · John Snow

V2: It's not safe to use sys.stderr.encoding to determine a "console encoding", because that uses the "current" stderr and not a hypothetically generic one -- and doing this causes the acceptance tests to fail.

Use UTF-8 instead.

Question: What encoding do terminal programs use? Is there an inherent encoding to fprintf et al, or does it just push whatever bytes you put into it straight into the stdout/stderr pipe?

Programs are expected to output data in the encoding that is set in the various env variables LC_ALL/LC_CTYPE/LANG.

In traditional end user scenarios this almost always means UTF-8 charset.

There's plenty of cases which end up with the C locale though, which would mean 7-bit ASCII on Linux, though apps are supposed to be 8-bit clean allow data with the high bit to pass through without interpretation. The latter is what python3 gets very wrong complaining if you output 8-bit high data in C locale.

There is increasing support for a C.UTF-8 locale to bring it closer to other locales which are all UTF-8.

On macOS the C locale has been UTF-8 by default indefinitely.

Windows is a whole other world of fun and IIRC isn't UTF-8 by default, but I don't recall details.

Quoted 11 lines

Regards,
Daniel
--
|: https://berrange.com -o-
https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o-
https://fstop138.berrange.com :|
|: https://entangle-photo.org -o-
https://www.instagram.com/dberrange :|

Hm, I believe I can use lang, encoding = locale.getlocale() in this case -- I believe it follows LC_CTYPE. This ought to accurately match the console output from QEMU.
I'll respin, actually. We don't test the Python packages on Windows, but I see no reason to introduce a nasty timebomb.

Thanks!
--js