On 2019-06-03, José Abílio Matos wrote: > On Thursday, 28 February 2019 22.03.48 WEST Guenter Milde wrote: >> >> diff --git a/lib/scripts/convertDefault.py >> >> b/lib/scripts/convertDefault.py >> >> index 8678965013..c7db4f5499 100644 >> >> --- a/lib/scripts/convertDefault.py >> >> +++ b/lib/scripts/convertDefault.py >> >> @@ -35,7 +35,10 @@ if fout.close() != None: >> >> output = fout.readline() >> >> fout.close() >> >> if not PY2: >> >> - output = output.decode() >> >> + # ensure we have a (unicode) string object in Python3 >> >> + # FIXME: not required for version >= 3.5 >> >> + # check whether this is required with any supported 3.x >> >> version! + output = str(output)
> Hi Günter, > today I tried to find why the fix is required for python >= 3.5 but > after an exhaustive look into python documentation I found no > reference. > Do you remember what is the reason for that? There was a failure due to calling "output.decode()". The commit message says: At least since Python 3.5, `output` is already a (unicode) string and does not have a "decode" method. In Python 2, after fout = os.popen('magick -version 2>&1') output = fout.readline() `output` is an ordinary "bytes" string. A "bytes" string can be converted to a "unicode" string with: a) unicode(output) # Python 2 rsp. str(output) # Python 3 b) output.decode() c) <implicit when used in a context requiring a 'unicode' object> * The .decode() method has the advantage that you can specify the encoding (not required in this case) but fails if the object is already of type "unicode" (Python 2) rsp. "str" (Python 3). * Method a) fails in Python 3, because 'unicode' is not defined (it is now called "str"). * Method c) fails in Python 3 (there is no implicit conversion from "bytes" to "str"). This is probably the reason for the pre-fix code: if not PY2: output = output.decode() The post-fix code if not PY2: output = str(output) is failsafe (it does not fail if `output` is already a (unicode) "str" object). It could be replaced with the one-liner output = str(output) because this does not fail with Python 2 either. OTOH, the "post-fix" code has no effect with Python 2 `output` is a "bytes" string, before and after Python 3.5 and above `output` is a "unicode" string, before and after. It *may* be required with Python 3.1, 3.2, 3.3, or 3.4. Unfortunately, I don't know whether `output` is of type "bytes" or "str" with this versions. If we can establish, that `output` is of type "str" in all Python versions that are supported by LyX, we can remove the complete sequence: - if not PY2: - # Ensure we have a (unicode) string object in Python3 - # (not required for version >= 3.5). - # FIXME: Check whether this is required with any supported 3.x version! - output = str(output) Günter