This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: subprocess.Popen.communicate: accept str for input parameter if universal_newlines is False
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, Ramchandra Apte, asvetlov, chris.jerdonek, martin.panter, r.david.murray, serhiy.storchaka
Priority: normal Keywords:

Created on 2012-08-14 15:47 by asvetlov, last changed 2022-04-11 14:57 by admin.

Messages (6)
msg168207 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-08-14 15:47
Currently Popen.communicate accept str is universal_newlines==True and bytes if universal_newlines==False.

I like to accept both str and bytes if universal_newlines is False and raise explicit exception for bytes if universal_newlines is True.

Looks like universal_newlines for bytes doesn't make sense, also we prefer to use str everywhere. Bytes should be used if it is really byte-stream or if unicode cannot be passed by some limitations of underlying OS etc.

There are couple dark corners:
1. What to do if stdin doesn't have `encoding` attribute? Convert to bytes using filesystemencoding? Raise explicit exception? Adding `encode` parameter for .communicate doesn't looks elegant.
2. How .communicate works on Windows? I see completely different implementation of this method for win32.
msg168211 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-08-14 16:35
> What to do if stdin doesn't have `encoding` attribute? Convert to bytes using filesystemencoding?

If choosing an encoding, it probably makes sense to default to the same as for when universal_newlines=True, namely locale.getpreferredencoding(False).

> Adding `encode` parameter for .communicate doesn't looks elegant.

Also see issue 6135 (to allow setting encoding).
msg168264 - (view) Author: Ramchandra Apte (Ramchandra Apte) * Date: 2012-08-15 07:19
Uhum.. Universal Newlines is being deprecated.
msg168265 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-15 07:36
No it isn't.  There's some issue about a 'U' parameter somewhere that is being deprecated, but that has nothing to do with this issue.
msg179426 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-01-09 09:29
See also issue16903.
msg268339 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-06-12 07:35
See also Issue 27273, proposing to use text string input as an indicator to turn on universal_newlines=True in the higher-level functions. I’m not excited by either proposal, but I find the other one a bit more palatable.

IMO allowing text string input to communicate(), but returning byte string outputs would be a bad idea. It would have been better to call the universal_newlines=... flag “text_mode”.
History
Date User Action Args
2022-04-11 14:57:34adminsetgithub: 59854
2016-06-12 07:35:03martin.pantersetnosy: + martin.panter
messages: + msg268339
2013-01-09 09:29:45serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg179426
2012-08-18 13:41:16Arfreversetnosy: + Arfrever
2012-08-15 07:36:56r.david.murraysetmessages: + msg168265
2012-08-15 07:19:22Ramchandra Aptesetnosy: + Ramchandra Apte
messages: + msg168264
2012-08-15 02:10:45eric.araujosetnosy: + r.david.murray
2012-08-14 16:35:39chris.jerdoneksetnosy: + chris.jerdonek
messages: + msg168211
2012-08-14 15:49:26asvetlovsetcomponents: + Library (Lib)
2012-08-14 15:47:00asvetlovcreate