Update check-python-tox test for pylint 2.10
v3 cover Update check-python-tox test for pylint 2.10
V3: Try to use locale settings to determine the most likely output encoding for a QEMU terminal session.
This ought to fix the warning for check-python-tox on Gitlab CI.
John Snow (1):
python: Update for pylint 2.10
python/qemu/machine/machine.py | 9 ++++++++- python/setup.cfg | 1 + 2 files changed, 9 insertions(+), 1 deletion(-)
Signature · 1 line
2.31.1
PATCH v3 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
Use LC_CTYPE to determine an encoding to use for interpreting QEMU's terminal output. Note that Python states: "language code and encoding may be None if their values cannot be determined" -- use a platform default as a backup.
--- python/qemu/machine/machine.py | 9 ++++++++- python/setup.cfg | 1 + 2 files changed, 9 insertions(+), 1 deletion(-)
python/qemu/machine/machine.py +8 -1
@@ -19,6 +19,7 @@ import errno from itertools import chain +import locale import logging import os import shutil
@@ -290,8 +291,14 @@ def get_pid(self) -> Optional[int]: return self._subp.pid def _load_io_log(self) -> None: + # Assume that the output encoding of QEMU's terminal output + # is defined by our locale. If indeterminate, use a platform default. + _, encoding = locale.getlocale() + if encoding is None: + encoding = locale.getpreferredencoding(do_setlocale=False) 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=encoding) as iolog: self._iolog = iolog.read() @property
python/setup.cfg +1 -0
@@ -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
Re: [PATCH v3 1/1] python: Update for pylint 2.10
Quoted 29 lines · John Snow
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/#motivationUse
LC_CTYPEto determine an encoding to use for interpreting QEMU's terminal output. Note that Python states: "language code and encoding may be None if their values cannot be determined" -- use a platform default as a backup.Signed-off-by John Snow <[email protected]>--- python/qemu/machine/machine.py | 9 ++++++++- python/setup.cfg | 1 + 2 files changed, 9 insertions(+), 1 deletion(-)python/qemu/machine/machine.py +1
-0@@ -19,6 +19,7 @@ import errno from itertools import chain +import locale import logging import os import shutil
python/qemu/machine/machine.py def get_pid(self) -> Optional[int]: · line 298
Quoted 8 lines
python/qemu/machine/machine.py +5
-0@@ -290,8 +291,14 @@ def get_pid(self) -> Optional[int]: return self._subp.pid def _load_io_log(self) -> None: + # Assume that the output encoding of QEMU's terminal output + # is defined by our locale. If indeterminate, use a platform default. + _, encoding = locale.getlocale() + if encoding is None: + encoding = locale.getpreferredencoding(do_setlocale=False)
Do we really need this getpreferredencoding ? IIUC, this is a sign that the application is buggy by not calling
locale.setlocale(locale.LC_ALL, '')
during its main() method, which I think we can just delegate to the code in question to fix. Missing setlocale will affect everything they run, so doing workarounds in only 1 place is not worth it IMHO
python/qemu/machine/machine.py def get_pid(self) -> Optional[int]: · line 302
Quoted 5 lines
python/qemu/machine/machine.py +2
-1if self._qemu_log_path is not None: - with open(self._qemu_log_path, "r") as iolog: + with open(self._qemu_log_path, "r", + encoding=encoding) as iolog: self._iolog = iolog.read()
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 :|
Re: [PATCH v3 1/1] python: Update for pylint 2.10
Quoted 41 lines · Daniel P. Berrangé
Quoted 10 lines · John Snow
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/#motivationUse
LC_CTYPEto determine an encoding to use for interpreting QEMU's terminal output. Note that Python states: "language code and encoding may be None if their values cannot be determined" -- use a platform default as a backup.Review
Quoted 7 lines
Signed-off-by John Snow <[email protected]>--- python/qemu/machine/machine.py | 9 ++++++++- python/setup.cfg | 1 + 2 files changed, 9 insertions(+), 1 deletion(-)(quoted diff) +0
-0
b/python/qemu/machine/machine.pyQuoted 12 lines
index
a7081b1845..51b6e79a13100644python/qemu/machine/machine.py +1
-0@@ -19,6 +19,7 @@ import errno from itertools import chain +import locale import logging import os import shutilpython/qemu/machine/machine.py def get_pid(self) -> Optional[int]: · line 295
Quoted 5 lines
python/qemu/machine/machine.py +2
-0@@ -290,8 +291,14 @@ def get_pid(self) -> Optional[int]: return self._subp.pid def _load_io_log(self) -> None: + # Assume that the output encoding of QEMU's terminal output + # is defined by our locale. If indeterminate, use a platformdefault.
python/qemu/machine/machine.py def get_pid(self) -> Optional[int]: · line 298
Quoted 3 lines
python/qemu/machine/machine.py +3
-0+ _, encoding = locale.getlocale() + if encoding is None: + encoding = locale.getpreferredencoding(do_setlocale=False)
Quoted 10 lines
Do we really need this getpreferredencoding ? IIUC, this is a sign that the application is buggy by not calling
locale.setlocale(locale.LC_ALL, '')during its
main()method, which I think we can just delegate to the code in question to fix. Missing setlocale will affect everything they run, so doing workarounds in only 1 place is not worth it IMHO
I genuinely don't know! (And, I try to keep the Python code free from assuming Linux as much as I can help it.)
Python's getlocale documentation states: "language code and encoding may be None if their values cannot be determined."
https://docs.python.org/3/library/locale.html#locale.getlocale
But it is quiet as to the circumstances under which this may happen. Browsing the cpython source code, (3.9ish):
```
def getlocale(category=LC_CTYPE):
localename = _setlocale(category)
if category == LC_ALL and ';' in localename:
raise TypeError('category LC_ALL is not supported')
return _parse_localename(localename)
```_setlocale is ultimately a call to (I think) _localemodule.c's PyLocale_setlocale(PyObject *self, PyObject *args) C function. It calls result = setlocale(category, locale) where the category is going to be LC_CTYPE, so this should be equivalent to locale(3) (LC_CTYPE, NULL).
locale(3) says that "The return value is NULL if the request cannot be honored."
Python parses that string according to _parse_localename, which in turn calls normalize(localename).
Normalization looks quite involved, but has a fallback of returning the string verbatim. If the normalized locale string is "C", we return the tuple (None, None)!
So I figured there was a non-zero chance that we'd see a value of None here.
Source code is in cpython/Lib/locale.py and cpython/Modules/_localemodule.c if you want to nose around yourself.
I also have no idea how this will all shake out on Windows, so I decided to add the fallback here just in case. (Does the Python package work on Windows? I don't know, but I avoid assuming it won't EVER run there... Certainly, I have an interest in having the QMP packages I am building work on all platforms.)
Thoughts?
Quoted 16 lines
if self._qemu_log_path is not None:
python/qemu/machine/machine.py def get_pid(self) -> Optional[int]: · line 302
Quoted 4 lines
python/qemu/machine/machine.py +2
-1- with open(self._qemu_log_path, "r") as iolog: + with open(self._qemu_log_path, "r", + encoding=encoding) as iolog: self._iolog = iolog.read()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 :|
Re: [PATCH v3 1/1] python: Update for pylint 2.10
Quoted 91 lines · John Snow
Quoted 41 lines · Daniel P. Berrangé
Quoted 10 lines · John Snow
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/#motivationUse
LC_CTYPEto determine an encoding to use for interpreting QEMU's terminal output. Note that Python states: "language code and encoding may be None if their values cannot be determined" -- use a platform default as a backup.Review
Quoted 7 lines
Signed-off-by John Snow <[email protected]>--- python/qemu/machine/machine.py | 9 ++++++++- python/setup.cfg | 1 + 2 files changed, 9 insertions(+), 1 deletion(-)(quoted diff) +0
-0
b/python/qemu/machine/machine.pyQuoted 12 lines
index
a7081b1845..51b6e79a13100644python/qemu/machine/machine.py +1
-0@@ -19,6 +19,7 @@ import errno from itertools import chain +import locale import logging import os import shutilpython/qemu/machine/machine.py def get_pid(self) -> Optional[int]: · line 295
Quoted 5 lines
python/qemu/machine/machine.py +2
-0@@ -290,8 +291,14 @@ def get_pid(self) -> Optional[int]: return self._subp.pid def _load_io_log(self) -> None: + # Assume that the output encoding of QEMU's terminal output + # is defined by our locale. If indeterminate, use a platformdefault.
python/qemu/machine/machine.py def get_pid(self) -> Optional[int]: · line 298
Quoted 3 lines
python/qemu/machine/machine.py +3
-0+ _, encoding = locale.getlocale() + if encoding is None: + encoding = locale.getpreferredencoding(do_setlocale=False)Quoted 10 lines
Do we really need this getpreferredencoding ? IIUC, this is a sign that the application is buggy by not calling
locale.setlocale(locale.LC_ALL, '')during its
main()method, which I think we can just delegate to the code in question to fix. Missing setlocale will affect everything they run, so doing workarounds in only 1 place is not worth it IMHOI genuinely don't know! (And, I try to keep the Python code free from assuming Linux as much as I can help it.)
Python's getlocale documentation states: "language code and encoding may be None if their values cannot be determined."
https://docs.python.org/3/library/locale.html#locale.getlocaleBut it is quiet as to the circumstances under which this may happen. Browsing the cpython source code, (3.9ish):
``` def getlocale(category=LC_CTYPE): localename = _setlocale(category) if category == LC_ALL and ';' in localename: raise TypeError('category LC_ALL is not supported') return _parse_localename(localename) ```
_setlocaleis ultimately a call to (I think) _localemodule.c'sPyLocale_setlocale(PyObject *self, PyObject *args)C function. It callsresult = setlocale(category, locale)where the category is going to beLC_CTYPE, so this should be equivalent to locale(3) (LC_CTYPE, NULL).locale(3) says that "The return value is NULL if the request cannot be honored."
Python parses that string according to
_parse_localename, which in turn calls normalize(localename).
Normalization looks quite involved, but has a fallback of returning the string verbatim. If the normalized locale string is "C", we return the tuple (None, None)!So I figured there was a non-zero chance that we'd see a value of
Nonehere.Source code is in
cpython/Lib/locale.pyandcpython/Modules/_localemodule.cif you want to nose around yourself.
Quoted 7 lines
I also have no idea how this will all shake out on Windows, so I decided to add the fallback here just in case. (Does the Python package work on Windows? I don't know, but I avoid assuming it won't EVER run there... Certainly, I have an interest in having the QMP packages I am building work on all platforms.)
Thoughts?
Well this machine.py is using UNIX domain sockets and FD passing, so Windows is out of the question.
I'd be inclined to just keep it simple unless someone reports a real bug with it.
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 :|
Re: [PATCH v3 1/1] python: Update for pylint 2.10
Quoted 105 lines · Daniel P. Berrangé
Quoted 36 lines · John Snow
Quoted 33 lines · Daniel P. Berrangé
Quoted 10 lines · John Snow
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/#motivationUse
LC_CTYPEto determine an encoding to use for interpreting QEMU's terminal output. Note that Python states: "language code and encoding may be None if their values cannot be determined" -- use a platform default as a backup.Review
Quoted 7 lines
Signed-off-by John Snow <[email protected]>--- python/qemu/machine/machine.py | 9 ++++++++- python/setup.cfg | 1 + 2 files changed, 9 insertions(+), 1 deletion(-)(quoted diff) +0
-0
b/python/qemu/machine/machine.pyQuoted 14 lines
index
a7081b1845..51b6e79a13100644python/qemu/machine/machine.py +1
-0@@ -19,6 +19,7 @@ import errno from itertools import chain +import locale import logging import os import shutil@@ -290,8 +291,14 @@ def get_pid(self) -> Optional[int]: return self._subp.pidQuoted 7 lines
Quoted 7 lines
python/qemu/machine/machine.py def get_pid(self) -> Optional[int]:
Quoted 3 lines
python/qemu/machine/machine.py +2
-0def _load_io_log(self) -> None: + # Assume that the output encoding of QEMU's terminal output + # is defined by our locale. If indeterminate, use a platformdefault.
Quoted 3 lines
python/qemu/machine/machine.py +3
-0+ _, encoding = locale.getlocale() + if encoding is None: + encoding =
locale.getpreferredencoding(do_setlocale=False)Quoted 11 lines
Quoted 11 lines
Do we really need this getpreferredencoding ? IIUC, this is a sign that the application is buggy by not calling
locale.setlocale(locale.LC_ALL, '')during its
main()method, which I think we can just delegate to the code in question to fix. Missing setlocale will affect everything they run, so doing workarounds in only 1 place is not worth it IMHOQuoted 4 lines
I genuinely don't know! (And, I try to keep the Python code free from assuming Linux as much as I can help it.)
Python's getlocale documentation states: "language code and encoding may
be
Quoted 13 lines
None if their values cannot be determined."
https://docs.python.org/3/library/locale.html#locale.getlocaleBut it is quiet as to the circumstances under which this may happen. Browsing the cpython source code, (3.9ish):
``` def getlocale(category=LC_CTYPE): localename = _setlocale(category) if category == LC_ALL and ';' in localename: raise TypeError('category LC_ALL is not supported') return _parse_localename(localename) ```Quoted 3 lines
_setlocaleis ultimately a call to (I think) _localemodule.c'sPyLocale_setlocale(PyObject *self, PyObject *args)C function. It callsresult = setlocale(category, locale)where the category isgoing
Quoted 1 line
to be
LC_CTYPE, so this should be equivalent to locale(3) (LC_CTYPE,NULL).
Quoted 10 lines
locale(3) says that "The return value is NULL if the request cannot be honored."
Python parses that string according to
_parse_localename, which in turn calls normalize(localename).
Normalization looks quite involved, but has a fallback of returning the string verbatim. If the normalized locale string is "C", we return the tuple (None, None)!Quoted 4 lines
So I figured there was a non-zero chance that we'd see a value of
Nonehere.Source code is in
cpython/Lib/locale.pyand
cpython/Modules/_localemodule.cQuoted 3 lines
if you want to nose around yourself.
I also have no idea how this will all shake out on Windows, so I decided
to
Quoted 3 lines
add the fallback here just in case. (Does the Python package work on Windows? I don't know, but I avoid assuming it won't EVER run there... Certainly, I have an interest in having the QMP packages I am building
work
Quoted 2 lines
on all platforms.)
Quoted 9 lines
Quoted 1 line
Thoughts?
Well this
machine.pyis using UNIX domain sockets and FD passing, so Windows is out of the question.I'd be inclined to just keep it simple unless someone reports a real bug with it.
What about the case where getlocale finds "C" and can't/won't return an encoding? Is that not a real circumstance?
--js
Re: [PATCH v3 1/1] python: Update for pylint 2.10
Quoted 118 lines · John Snow
Quoted 113 lines · Daniel P. Berrangé
Quoted 12 lines · John Snow
On Thu, Sep 16, 2021 at 8:59 AM Daniel P. Berrangé <[email protected]
Quoted 8 lines · wrote:
Quoted 7 lines · John Snow
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/#motivationUse
LC_CTYPEto determine an encoding to use for interpreting QEMU's terminal output. Note that Python states: "language code andencoding
Quoted 25 lines
Quoted 25 lines
Review
Quoted 10 lines
may be None if their values cannot be determined" -- use a platform default as a backup.
Signed-off-by John Snow <[email protected]>--- python/qemu/machine/machine.py | 9 ++++++++- python/setup.cfg | 1 + 2 files changed, 9 insertions(+), 1 deletion(-)(quoted diff) +0
-0
b/python/qemu/machine/machine.pyQuoted 14 lines
index
a7081b1845..51b6e79a13100644python/qemu/machine/machine.py +1
-0@@ -19,6 +19,7 @@ import errno from itertools import chain +import locale import logging import os import shutil@@ -290,8 +291,14 @@ def get_pid(self) -> Optional[int]: return self._subp.pidQuoted 3 lines
Quoted 3 lines
python/qemu/machine/machine.py def get_pid(self) -> Optional[int]:
Quoted 3 lines
python/qemu/machine/machine.py +2
-0def _load_io_log(self) -> None: + # Assume that the output encoding of QEMU's terminal output + # is defined by our locale. If indeterminate, use aplatform
Quoted 4 lines
Quoted 4 lines
default.
Quoted 3 lines
python/qemu/machine/machine.py +3
-0+ _, encoding = locale.getlocale() + if encoding is None: + encoding =
locale.getpreferredencoding(do_setlocale=False)Quoted 11 lines
Quoted 11 lines
Do we really need this getpreferredencoding ? IIUC, this is a sign that the application is buggy by not calling
locale.setlocale(locale.LC_ALL, '')during its
main()method, which I think we can just delegate to the code in question to fix. Missing setlocale will affect everything they run, so doing workarounds in only 1 place is not worth it IMHOQuoted 4 lines
I genuinely don't know! (And, I try to keep the Python code free from assuming Linux as much as I can help it.)
Python's getlocale documentation states: "language code and encoding
may be
Quoted 13 lines
None if their values cannot be determined."
https://docs.python.org/3/library/locale.html#locale.getlocaleBut it is quiet as to the circumstances under which this may happen. Browsing the cpython source code, (3.9ish):
``` def getlocale(category=LC_CTYPE): localename = _setlocale(category) if category == LC_ALL and ';' in localename: raise TypeError('category LC_ALL is not supported') return _parse_localename(localename) ```Quoted 3 lines
_setlocaleis ultimately a call to (I think) _localemodule.c'sPyLocale_setlocale(PyObject *self, PyObject *args)C function. It callsresult = setlocale(category, locale)where the category isgoing
Quoted 1 line
to be
LC_CTYPE, so this should be equivalent to locale(3) (LC_CTYPE,NULL).
Quoted 10 lines
locale(3) says that "The return value is NULL if the request cannot be honored."
Python parses that string according to
_parse_localename, which in turn calls normalize(localename).
Normalization looks quite involved, but has a fallback of returning the string verbatim. If the normalized locale string is "C", we return the tuple (None, None)!Quoted 4 lines
So I figured there was a non-zero chance that we'd see a value of
Nonehere.Source code is in
cpython/Lib/locale.pyand
cpython/Modules/_localemodule.cQuoted 3 lines
if you want to nose around yourself.
I also have no idea how this will all shake out on Windows, so I
decided to
Quoted 3 lines
add the fallback here just in case. (Does the Python package work on Windows? I don't know, but I avoid assuming it won't EVER run there... Certainly, I have an interest in having the QMP packages I am building
work
Quoted 3 lines
on all platforms.)
Thoughts?
Well this
machine.pyis using UNIX domain sockets and FD passing, so Windows is out of the question.
Quoted 9 lines
Quoted 4 lines
I'd be inclined to just keep it simple unless someone reports a real bug with it.
What about the case where getlocale finds "C" and can't/won't return an encoding? Is that not a real circumstance?
--js
So, a few things:
(1) machine.py can also use TCP, it doesn't have to use UNIX sockets.
(2) The FD passing is optional and already checks for the ability to do so before it is invoked
(3) jsnow@scv ~/s/q/python (python-package-pylint-210)> LC_ALL="C" python3 -c "import locale; print(locale.getlocale());"
(None, None)
(4) If you specify an encoding of "None", open() itself does exactly what I
wrote: getpreferredencoding(do_setlocale=False)
I'll drop the manual fallback -- but not because it wasn't actually necessary. It just so happens that it's handled for us.
--js