11

I'm calling pipe.communicate from Python's subprocess module from Python 2.6. I get the following error from this code:

from subprocess import Popen

pipe = Popen(cwd)

pipe.communicate( data )

For an arbitrary cwd, and where data that contains unicode (specifically 0xE9):

Exec. exception: 'ascii' codec can't encode character u'\xe9' in position 507: ordinal not in range(128)
Traceback (most recent call last):  

... stdout, stderr = pipe.communicate( data )

  File
"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py",
line 671, in communicate
    return self._communicate(input)

  File
"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py",
line 1177, in _communicate
    bytes_written = os.write(self.stdin.fileno(), chunk)

This is happening, I presume, because pipe.communicate() is expecting ASCII encoded string, but data is unicode.

Is this the problem I'm encountering, and i sthere a way to pass unicode to pipe.communicate()?

Thank you for reading!

Brian

bignose
  • 30,281
  • 14
  • 77
  • 110
Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343
  • What do you mean by "contains Unicode"? Specifically, 0xE9 is not, by itself, a valid Unicode code point in any common encoding. – tripleee Oct 06 '15 at 08:30

1 Answers1

14

I may have solved this by changing:

pipe.communicate( data )

to

pipe.communicate( data.encode('utf8') )

Though I stand to be corrected!

Brian

Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343
  • 2
    That's right. Pipes (and files, sockets, etc.) transmit bytes, not Unicode (which is "characters"). You have to encode Unicode to transmit it. – Mark Tolonen Jun 15 '10 at 01:02
  • Thanks! This fixed an issue I was having with Django - one of my forms is passed to an external processor via subprocess, and Unicode submissions kept failing until this was changed. – CoderMD666 Mar 05 '12 at 00:47