Bug 1626934 Comment 0 Edit History

Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.

STR:
```python
In [1]: from mozprocess import ProcessHandler 
In [2]: proc = ProcessHandler(["echo", "foo"])In [3]: proc.run()                                                                                                                                                                                                                                                          
Exception in thread ProcessReader:

Traceback (most recent call last):
In [4]:   File "/home/ahal/.pyenv/versions/3.8.2/lib/python3.8/threading.py", line 932, in _bootstrap_inner                                                                                                                                                                 
    self.run()
  File "/home/ahal/.pyenv/versions/3.8.2/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ahal/dev/mozilla-central/testing/mozbase/mozprocess/mozprocess/processhandler.py", line 1099, in _read
    callback(line.rstrip())
  File "/home/ahal/dev/mozilla-central/testing/mozbase/mozprocess/mozprocess/processhandler.py", line 1009, in __call__
    e(*args, **kwargs)
  File "/home/ahal/dev/mozilla-central/testing/mozbase/mozprocess/mozprocess/processhandler.py", line 1150, in __call__
    self.stream.write(line + '\n'.encode('utf8'))
TypeError: write() argument must be str, not bytes
```

This is happening because we set up a `sys.stdout` stream by default here:
https://searchfox.org/mozilla-central/source/testing/mozbase/mozprocess/mozprocess/processhandler.py#1205

But on Python 3, `sys.stdout` requires text not bytes (whereas, the default subprocess output remains bytes).

In general, I think it's reasonable to fail if a user requests bytes from the subprocess and then tries to set up a stream that needs text (or vice versa). But we at least need to fix our default case there.

I think we need something like:
```
if PY3 and not (kwargs.get("universal_newlines") or kwargs.get("text")):
    use sys.stdout.buffer as the stream (unsure if this has other negative consequences)
else:
    use sys.stdout as the stream
```
STR:
```python
In [1]: from mozprocess import ProcessHandler 
In [2]: proc = ProcessHandler(["echo", "foo"])
In [3]: proc.run()

Exception in thread ProcessReader:

Traceback (most recent call last):
In [4]:   File "/home/ahal/.pyenv/versions/3.8.2/lib/python3.8/threading.py", line 932, in _bootstrap_inner                                                                                                                                                                 
    self.run()
  File "/home/ahal/.pyenv/versions/3.8.2/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ahal/dev/mozilla-central/testing/mozbase/mozprocess/mozprocess/processhandler.py", line 1099, in _read
    callback(line.rstrip())
  File "/home/ahal/dev/mozilla-central/testing/mozbase/mozprocess/mozprocess/processhandler.py", line 1009, in __call__
    e(*args, **kwargs)
  File "/home/ahal/dev/mozilla-central/testing/mozbase/mozprocess/mozprocess/processhandler.py", line 1150, in __call__
    self.stream.write(line + '\n'.encode('utf8'))
TypeError: write() argument must be str, not bytes
```

This is happening because we set up a `sys.stdout` stream by default here:
https://searchfox.org/mozilla-central/source/testing/mozbase/mozprocess/mozprocess/processhandler.py#1205

But on Python 3, `sys.stdout` requires text not bytes (whereas, the default subprocess output remains bytes).

In general, I think it's reasonable to fail if a user requests bytes from the subprocess and then tries to set up a stream that needs text (or vice versa). But we at least need to fix our default case there.

I think we need something like:
```
if PY3 and not (kwargs.get("universal_newlines") or kwargs.get("text")):
    use sys.stdout.buffer as the stream (unsure if this has other negative consequences)
else:
    use sys.stdout as the stream
```
STR:
```python
In [1]: from mozprocess import ProcessHandler 
In [2]: proc = ProcessHandler(["echo", "foo"])
In [3]: proc.run()

Exception in thread ProcessReader:

Traceback (most recent call last):
File "/home/ahal/.pyenv/versions/3.8.2/lib/python3.8/threading.py", line 932, in _bootstrap_inner                                                                                                                                                                 
    self.run()
  File "/home/ahal/.pyenv/versions/3.8.2/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ahal/dev/mozilla-central/testing/mozbase/mozprocess/mozprocess/processhandler.py", line 1099, in _read
    callback(line.rstrip())
  File "/home/ahal/dev/mozilla-central/testing/mozbase/mozprocess/mozprocess/processhandler.py", line 1009, in __call__
    e(*args, **kwargs)
  File "/home/ahal/dev/mozilla-central/testing/mozbase/mozprocess/mozprocess/processhandler.py", line 1150, in __call__
    self.stream.write(line + '\n'.encode('utf8'))
TypeError: write() argument must be str, not bytes
```

This is happening because we set up a `sys.stdout` stream by default here:
https://searchfox.org/mozilla-central/source/testing/mozbase/mozprocess/mozprocess/processhandler.py#1205

But on Python 3, `sys.stdout` requires text not bytes (whereas, the default subprocess output remains bytes).

In general, I think it's reasonable to fail if a user requests bytes from the subprocess and then tries to set up a stream that needs text (or vice versa). But we at least need to fix our default case there.

I think we need something like:
```
if PY3 and not (kwargs.get("universal_newlines") or kwargs.get("text")):
    use sys.stdout.buffer as the stream (unsure if this has other negative consequences)
else:
    use sys.stdout as the stream
```

Back to Bug 1626934 Comment 0