delegator.py icon indicating copy to clipboard operation
delegator.py copied to clipboard

Error getting subprocess error with non-blocking mode

Open jakul opened this issue 7 years ago • 6 comments
trafficstars

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 avatar Apr 27 '18 10:04 jakul

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?

jakul avatar Apr 27 '18 10:04 jakul

Closing due to inactivity :+1:

ParthS007 avatar Feb 14 '19 13:02 ParthS007

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?

timofurrer avatar Feb 14 '19 17:02 timofurrer

@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.

jakul avatar Feb 14 '19 17:02 jakul

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 avatar Feb 14 '19 17:02 timofurrer

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)

timofurrer avatar Feb 14 '19 18:02 timofurrer