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.

Title: subprocess.Popen.communicate takes bytes, not str
Type: Stage:
Components: Documentation Versions: Python 3.0
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: benjamin.peterson, georg.brandl, hop, humitos, kermode, zanella
Priority: normal Keywords: patch

Created on 2008-04-24 18:57 by kermode, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
subprocess_strEncode.diff zanella, 2008-06-29 22:57
subprocess_doc_string.diff hop, 2011-09-26 16:26 review
Messages (9)
msg65740 - (view) Author: Lenard Lindstrom (kermode) Date: 2008-04-24 18:57
subprocess.Popen.communicate is documented as taking a string as the 
input argument. Instead is accepts only a binary stream (bytes).

Python 3.0a4 (r30a4:62126, Apr  3 2008, 15:34:18) [MSC v.1500 32 bit 
(Intel)] on
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import Popen, PIPE
>>> p = Popen('', stdin=PIPE)
>>> p.communicate("dir\n")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python30\lib\", line 588, in communicate
  File "C:\Python30\lib\", line 844, in write
    raise TypeError("can't write str to binary stream")
TypeError: can't write str to binary stream
msg68510 - (view) Author: Manuel Kaufmann (humitos) * Date: 2008-06-21 16:31
What is wrong? Documentation or method?
msg68600 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-06-23 00:44
I say docs.
msg68974 - (view) Author: Rafael Zanella (zanella) Date: 2008-06-29 22:57
On the new method communicate() doesn't encode the string:

_communicate(self, input):
  if isinstance(input, str):
    input = input.encode()

I've attached a patch that adds the str.encode() call on communicate().
msg68975 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-06-29 23:02
Hmm. I think it's better to make people explicity encode their string.
msg69057 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-07-01 19:11
Fixed docs in r64619.
msg69058 - (view) Author: Rafael Zanella (zanella) Date: 2008-07-01 19:18
_communicate still encodes the string under the hood
msg69060 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-07-01 19:50
You're right, I fixed that too in r64621.
msg144541 - (view) Author: Christoph Schindler (hop) Date: 2011-09-26 16:26
The doc string refers to "string" instead of "byte string" as well.
Date User Action Args
2022-04-11 14:56:33adminsetgithub: 46935
2011-09-26 16:26:29hopsetfiles: + subprocess_doc_string.diff
nosy: + hop
messages: + msg144541

2008-07-01 19:50:18georg.brandlsetmessages: + msg69060
2008-07-01 19:18:09zanellasetmessages: + msg69058
2008-07-01 19:11:01georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg69057
2008-06-29 23:02:08benjamin.petersonsetmessages: + msg68975
2008-06-29 22:57:19zanellasetfiles: + subprocess_strEncode.diff
nosy: + zanella
messages: + msg68974
keywords: + patch
2008-06-23 00:44:50benjamin.petersonsetassignee: georg.brandl
messages: + msg68600
components: + Documentation, - Extension Modules
nosy: + georg.brandl, benjamin.peterson
2008-06-21 17:12:10adminsetmessages: + msg68510
2008-06-21 17:09:41loewissetmessages: - msg68510
2008-06-21 16:31:55humitossetnosy: + humitos
messages: + msg68510
2008-04-24 18:57:19kermodecreate