Using Python codecs causes readline problems with sys.stdin?

I am writing a Python shell script (childscript.py) for a command line executable (childprogram). Another executable (parentprogram) spawns childscript.py and outputs it to childscript.py. childscript.py creates a child program with:

    retval = subprocess.Popen(RUNLINE, shell=False, stdout=None, stderr=None, stdin=subprocess.PIPE)

If childscript.py does a series of readings from sys.stdin directly using readline:

line = sys.stdin.readline()

I can get all the output from the parent program and pass it to the childprogram.

However, if I try to use the codec module by doing:

sys.stdin = codecs.open(sys.stdin.fileno(), encoding='iso-8859-1', mode='rb', buffering=0)

or execute:

sys.stdin = codecs.getreader('iso-8859-1')(sys.stdin.detach())

, . , , . , childscript.py , , .

- ? childscript.py iso-8859-1 .

EDIT:
, Python v3.x "open" . , "open" "codecs.open":

      sys.stdin = open(sys.stdin.fileno(), encoding='iso-8859-1', mode='r')

, - , open.codecs. script "open" .

- , -, .

+3
2

.

. - 4 . , , .

+1

:

import sys
import os

fd = sys.stdin.fileno()
text = ''
while 1:
    try:
        raw_data = os.read(fd, 1024)
        text += unicode(raw_data, 'iso-8859-1')
        # now do something with text
    except (EOFError, KeyboardInterrupt):
        break

, readline(), , ascii, .

, .

0

All Articles