mwilliamson / spur.py

Run commands and manipulate files locally or over SSH using the same interface
BSD 2-Clause "Simplified" License
267 stars 37 forks source link

sys.stdout encoding error #40

Closed o3bvv closed 8 years ago

o3bvv commented 8 years ago

Hi,

if you use stdout=sys.stdout while running command using Python 3, you will get such error:

  File "/home/alex/.virtualenvs/XXX/lib/python3.4/site-packages/spur/io.py", line 82, in _capture_output
    self._file_out.write(output)
TypeError: must be str, not bytes

To fix this, you need to decode child's output at io._ContinuousReader._capture_output():82:

                if self._file_out is not None:
                    self._file_out.write(output.decode('utf-8'))

Thanks.

mwilliamson commented 8 years ago

Thanks for the report. This is intended behaviour: by default, stdout is treated as a raw byte stream. The encoding argument claims to change the captured output:

encoding -- if set, this is used to decode any output. By default, any output is treated as raw bytes. If set, the raw bytes are decoded before writing to the passed stdout and stderr arguments (if set) and before setting the output attributes on the result.

However, from a quick look at the source code, I suspect I erred and this is only happening to the output attributes on the result. The code you've suggested doesn't work since it'll fail when the output is the first byte of a multi-byte character.

mwilliamson commented 8 years ago

0.3.17 should now behave as the documentation describes with respect to the encoding argument.

o3bvv commented 8 years ago

The code I've suggested was not to fix the error, but just to point where it is happening.

Nevertheless, thanks for your answer!