0

Another question looked at how to pipe byte strings to subprocess.Popen's to stdin when using unicode_literals. How to fix an encoding migrating Python subprocess to unicode_literals?. The answer there fixed the problem and created a warning when collecting the subprocess' stdout strings.

Still using:

from future import unicode_literals

Initially, I got the same UnicodeDecodeError error when collecting stdout. So! Using my new knowledge, I updated the receiving code that services stdout in a separate thread:

for text in iter(popen.stdout.readline,''):
    text = text.decode(encoding)
    text = text.strip(' \r\n')

This fixed the error, but a new warning appeared:

test.py:456: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  for text in iter(popen.stdout.readline,''):

With a little searching, I found I can suppress the warning message like this:

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    for text in iter(popen.stdout.readline,''):
        text = text.decode('utf-8')
        text = text.strip(' \r\n')

QUESTION: Is there a way to resolve the mismatch or is suppressing the the best way to handle this?

Community
  • 1
  • 1
tahoar
  • 1,788
  • 3
  • 20
  • 36
  • `popen.stdout.readline` returns a byte-string, not a Unicode string; but you're comparing it with the Unicode string `''` -- that's clearly your bug. Use `b''` instead and it should work better (w/o suppressing useful warnings!-). – Alex Martelli Dec 31 '14 at 16:32
  • Thanks, Alex. If you convert your comment into an answer, I can accept it and close this question... Happy New Year everyone! – tahoar Jan 01 '15 at 00:31
  • done, the answer's in. – Alex Martelli Jan 01 '15 at 00:48

1 Answers1

1

popen.stdout.readline returns a byte-string, not a Unicode string; but you're comparing it with the Unicode string ''. Use b'' instead and it should work better (w/o suppressing useful warnings!-):

for text in iter(popen.stdout.readline, b''):
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395