amitt001 / delegator.py

Subprocesses for Humans 2.0.
MIT License
1.7k stars 92 forks source link

Error getting subprocess error with non-blocking mode #54

Open jakul opened 6 years ago

jakul commented 6 years ago

Accessing command.err a second time causes an exception

In [1]: import delegator

In [2]: command = delegator.run('echo a', block=False)

In [3]: command.err
Out[3]: 'a\n'

In [4]: command.err
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-5e568e65e1e2> in <module>()
----> 1 command.err

~/.virtualenvs/service-mailer/lib/python3.6/site-packages/delegator.py in err(self)
    114             return self.__err
    115         else:
--> 116             return self._pexpect_out
    117
    118     @property

~/.virtualenvs/service-mailer/lib/python3.6/site-packages/delegator.py in _pexpect_out(self)
     82             result += self.subprocess.before
     83         if self.subprocess.after:
---> 84             result += self.subprocess.after
     85
     86         result += self.subprocess.read()

TypeError: must be str, not type

The cause is that self.subprocess.after becomes <class 'pexpect.exceptions.EOF'> after the first command.err

jakul commented 6 years ago

I've just checked the docs and found out that I shouldn't use command.err with non-blocking mode. Maybe the correct fix for this bug it so update the err function to not even try to read the error streams in non-blocking mode?

ParthS007 commented 5 years ago

Closing due to inactivity :+1:

timofurrer commented 5 years ago

I'm re-opening this because there is actually a PR for it.

@jakul I think #62 fixed your issue, too. Can you please double check?

jakul commented 5 years ago

@timofurrer The behaviour is better, but still not correct.

In [1]: import delegator

In [2]: command = delegator.run('echo a', block=False)

In [3]: command.err
Out[3]: 'a\n'

In [4]: command.err
Out[4]: 'a\n'

In [5]: command.err
Out[5]: ''

In [6]: command.err
Out[6]: ''

Now command.err has a different the first and second times I call it, compared to the 3rd, 4th etc.

timofurrer commented 5 years ago

It's any ways strange that you get an output at all for command.err. It seems to work fine when you try to get the stdout from command.out.

timofurrer commented 5 years ago

I've just commited https://github.com/kennethreitz/delegator.py/commit/ad67b14232cd54a36b60afc6b02a3e8a1e75cca2 with which command.err is cached properly, but doesn't actually return stderr but stdout (which was already the case before this change. I'll have a look at how to change that behavior, too)