python - Subprocess.Popen behaves differently in interpreter, executable scripts -


let's have following:

command = shlex.split("mcf -o -q -e -w %s %s" % (solfile, netfile)) task = subprocess.popen(command, stdout=subprocess.pipe, stderr=subprocess.pipe) stdout, stderr = task.communicate() print "stdout: %s" % stdout #debugging print "stderr: %s" % stderr #debugging if stderr:     sys.exit("mcf crashed on %s" % netfile) 

it's not necessary know mcf is, except it's c program overflow if it's not given satisfiable netfile. (why can't ensure netfiles satisfiable? well, because easiest way check feed mcf , see if overflows...)

anyway, when run in executable script, task.communicate() doesn't seem store in stdout , stderr. (to precise, stdout == stderr == ''.) instead, stderr stream mcf seems "leaking" terminal rather getting captured subprocess pipe. here's sample output illustrate:

netfile: facility3cat_nat5000_wholesaler_capacitation_test_.net solfile: facility3cat_nat5000_wholesaler_capacitation_test_.sol *** buffer overflow detected ***: mcf terminated ======= backtrace: ========= ... ...[fifty lines of linda blair-esque output]... ... stdout: none stderr:  ...[program continues, since stderr did not evaluate true]... 

this fails when running script command line. when step through line line in interpreter, stdout , stderr correctly assigned:

>>> task = subprocess.popen(command, stdout=subprocess.pipe, stderr=subprocess.pipe) >>> stdout, stderr = task.communicate() >>> stderr '*** buffer overflow detected ***: mcf terminated\n======= backtrace: =========\n' ...[more headspinning , vomit]... 

could me understand why works in interpreter, not when executed? in advance!

i wrote little test script test subprocess module with.

#!/bin/bash  echo echo stderr 1>&2 echo echo stdout 

then wrote small python script calls it:

#!/usr/bin/python  import subprocess  command = ('./joe.sh',) task = subprocess.popen(command, stdout=subprocess.pipe,                                  stderr=subprocess.pipe) stdout, stderr = task.communicate() print 'stdout == %r\nstderr == %r' % (stdout, stderr) 

the output of running looks this:

$ python joe.py  stdout == 'echo stdout\n' stderr == 'echo stderr\n' 

the output of running same sequence in ipython same.

so subprocess module behaving in manner expect, , not how it's behaving in question. think other subprocess module must @ fault here because you're doing works me.

i'm running python 2.7, possibility maybe there kind of weird bug in older versions of subprocess module.


Comments

Popular posts from this blog

c# - IMAP GMAIL getting folder list problem -

CUDA shared memory -