Eduardo Habkost <ehabk...@redhat.com> writes: > On Mon, Jun 18, 2018 at 07:25:14AM +0200, Markus Armbruster wrote: >> Matthias Maier <tam...@43-1.org> writes: >> >> > This is a different approach to fix the locale dependent encode/decode >> > problem in common.py utilizing the binary read/write mode [1,2], and (if >> > a python 3 interpreter is used) with explicit decode/encode arguments >> > [3]. >> >> Why can't we simply pass encoding='utf-8' to open()? > > This wouldn't work in Python 2.7 (where the `open()` builtin > doesn't support the `encoding` parameter). > > io.open(..., encoding='utf-8') should work, though.
This falls apart because then f.read() returns objects of type 'unicode' in Python 2, breaking isinstance(..., str) predicates in several places. What I asked for is something else: wrap the version conditional around open() instead of around the conversion from bytes to str. Coding up the (trivial) patch is easier than explaining it in more detail than "pass encoding='utf-8' to open()", so I did just that.