classification
Title: subprocess.run throws exception when input and stdin are passed as kwargs
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: aecant, josh.r, remi.lapeyre, serhiy.storchaka
Priority: normal Keywords: patch, patch, patch

Created on 2018-10-03 18:50 by aecant, last changed 2019-02-01 22:50 by remi.lapeyre.

Files
File name Uploaded Description Edit
subprocess_run_bug.py aecant, 2018-10-03 18:50 Minimal working example
Pull Requests
URL Status Linked Edit
PR 11727 open remi.lapeyre, 2019-02-01 22:49
PR 11727 open remi.lapeyre, 2019-02-01 22:49
PR 11727 open remi.lapeyre, 2019-02-01 22:49
Messages (7)
msg327002 - (view) Author: Alessandro (aecant) * Date: 2018-10-03 18:50
If input and stdin parameters are passed as keyword arguments to subprocess.run, an exception is thrown even if input and stdin are both None.

The exception is ValueError: stdin and input arguments may not both be used.

I attach a minimal working example of the bug
msg327003 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-03 19:12
What is wrong with this?
msg327004 - (view) Author: Alessandro (aecant) * Date: 2018-10-03 19:22
subprocess.run('ls', input=b'', stdin=None) # this is ok

kwargs = {'input': b'', 'stdin': None}
subprocess.run('ls', **kwargs) # this throws exception

The two calls should have the same behaviour, but one throws exception and the other doesn't. I think the exception shouldn't be thrown, because stdin is None.
msg327008 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2018-10-03 19:49
I just tried:

    subprocess.run('ls', input=b'', stdin=None)

and I got the same ValueError as for passing using kwargs. Where did you get the idea subprocess.run('ls', input=b'', stdin=None) worked?
msg327009 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2018-10-03 19:52
The actual code receives input by name, but stdin is received in **kwargs. The test is just:

    if input is not None:
        if 'stdin' in kwargs:
            raise ValueError(...)
        kwargs['stdin'] = PIPE

Perhaps just change `if 'stdin' in kwargs:` to:

    if kwargs.get('stdin') is not None:

so it obeys the documented API (that says stdin defaults to None, and therefore passing stdin=None explicitly should be equivalent to not passing it at all)?
msg327011 - (view) Author: Alessandro (aecant) * Date: 2018-10-03 20:06
> and I got the same ValueError as for passing using kwargs. Where did you get the idea subprocess.run('ls', input=b'', stdin=None) worked?

Sorry, the example was wrong. Both calls have the same behaviour.

> so it obeys the documented API (that says stdin defaults to None, and therefore passing stdin=None explicitly should be equivalent to not passing it at all)?

The actual problem is this. The fix you propose works for me.
msg334728 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-02-01 22:50
I opened a PR with @josh.r proposed change.
History
Date User Action Args
2019-02-01 22:50:04remi.lapeyresetnosy: + remi.lapeyre
messages: + msg334728
2019-02-01 22:49:14remi.lapeyresetkeywords: + patch
stage: patch review
pull_requests: + pull_request11613
2019-02-01 22:49:07remi.lapeyresetkeywords: + patch
stage: (no value)
pull_requests: + pull_request11612
2019-02-01 22:49:00remi.lapeyresetkeywords: + patch
stage: (no value)
pull_requests: + pull_request11611
2018-10-03 20:06:56aecantsetmessages: + msg327011
2018-10-03 19:52:55josh.rsetmessages: + msg327009
2018-10-03 19:49:29josh.rsetnosy: + josh.r
messages: + msg327008
2018-10-03 19:22:57aecantsetmessages: + msg327004
2018-10-03 19:12:16serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg327003
2018-10-03 18:50:38aecantcreate