Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Python codecs causes readline problems with sys.stdin?
    primarykey
    data
    text
    <p>I am writing a Python wrapper script (childscript.py) for a command line executable (childprogram). Another executable, (parentprogram) spawns childscript.py and pipes output into childscript.py. childscript.py spawns childprogram with:</p> <pre><code> retval = subprocess.Popen(RUNLINE, shell=False, stdout=None, stderr=None, stdin=subprocess.PIPE) </code></pre> <p>If childscript.py does a series of reads from sys.stdin straight up using readline:</p> <pre><code>line = sys.stdin.readline() </code></pre> <p>I am able to get all the output from parentprogram and feed it into childprogram.</p> <p>However, if I try to use the codecs module by doing:</p> <pre><code>sys.stdin = codecs.open(sys.stdin.fileno(), encoding='iso-8859-1', mode='rb', buffering=0) </code></pre> <p>or do a:</p> <pre><code>sys.stdin = codecs.getreader('iso-8859-1')(sys.stdin.detach()) </code></pre> <p>and attempt to do the read, the read does not get all the output from parentprogram. If I force additional output from parentprogram, the missing bits come out along with part of the additional output that I pushed in. It looks like childscript.py is not reading everything that it is being provided to it when I use the codecs module.</p> <p>Am I doing something totally wrong? Without the codecs, childscript.py triggers an exception when presented with iso-8859-1 encoded stuff from parentprogram.</p> <p>EDIT:<br> I discovered that Python v3.x "open" can take the encoding option as well. I changed the line to use "open" instead of "codecs.open":</p> <pre><code> sys.stdin = open(sys.stdin.fileno(), encoding='iso-8859-1', mode='r') </code></pre> <p>and it works as expected, without any of the problems that open.codecs produces. I've switched my script to use "open" instead.</p> <p>If anybody can explain why the codecs module behaves differently, I'd appreciate it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload