Fwd: Converting a script to Python 3 - having trouble.

Russell Jackson rusty at rcjacksonconsulting.com
Tue Sep 15 21:39:50 EDT 2009


I received a reply from the help group that suggested I added a call to
flush in there, and that fixed it. The working code looks like this:
def setpassword(user):
    password = "passworD\n"
    try:
        cmd = ' passwd {0}'.format(user)
        pipe = Popen(p4 + cmd, shell=True, stdin=PIPE, stdout=PIPE,
stderr=PIPE, universal_newlines=True)
        stderr = pipe.stdin.write(password)
        pipe.stdin.flush()
        time.sleep(2)
        stderr = pipe.stdin.write(password)
        pipe.stdin.flush()
        pipe.stdin.close()
        if pipe.wait() != 0:
            log("ERROR", "Password reset failed.\n{0}{1} generated the
following error: {2}".format(p4, cmd, stderr))
    except OSError as err:
        log("ERROR", "Execution failed: {0}".format(err))

Thanks,
Rusty


On Tue, Sep 15, 2009 at 6:37 PM, Chris Rebert <clp2 at rebertia.com> wrote:

> On Tue, Sep 15, 2009 at 4:58 PM, Russell Jackson
> <rusty at rcjacksonconsulting.com> wrote:
> > I just get an errorlevel from the executable when I read stdout, but I
> can't
> > tell what is going on because, of course, I can't tell what Popen is
> > actually doing. I never see the prompt from the executable that I would
> > expect to see when I read stdout.
> > I originally had the function like this:
> > def setpassword(user):
> >     password = "passworD\n"
> >     try:
> >         cmd = ' passwd {0}'.format(user)
> >         pipe = Popen(p4 + cmd, shell=True, stdin=PIPE, stdout=PIPE,
> > stderr=PIPE, universal_newlines=True)
> >         stdout = pipe.stdout.readline()
> >         stderr = pipe.stdin.write(password)
> >         time.sleep(1)
> >         stdout = pipe.stdout.readline()
> >         stderr = pipe.stdin.write(password)
> >         if pipe.stdin.close != 0:
> >             log("ERROR", "Password reset failed.\n{0}{1} generated the
> > following error: {2}".format(p4, cmd, stderr))
> >     except OSError as err:
> >         log("ERROR", "Execution failed: {0}".format(err))
> > but, the script just hung when I did that. I think it was stuck on the
> > readline, and never got anything from the process.
> > I didn't think that the if statement was incorrect based on examples I
> saw
> > in the docs,
>
> I'm unable to locate any example in the subprocess docs using a
> similar "if". The closest is this code snippet:
>
> rc = pipe.close()
> if  rc != None and rc % 256:
>    print "There were some errors"
>
> ...but that's used as an example of code *using os.popen()* in the
> context of how to translate it to use subprocess.Popen().
>
> > and the fact that it didn't complain about that part,
>
> Well, you're just comparing the .close method of a file object for
> equality with 0, which is valid, meaningful, and well-defined (hence,
> no error), but not useful or relevant in this context.
>
> >but I'll try the close():
>
> I agree with Rhodri that that "if" statement is definitely bunk.
> Rereading the docs, I think what was intended is:
>
> pipe.stdin.close()
> if pipe.wait() != 0:
>    log(...)
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>



-- 
Rusty

775-636-7402 Office
775-851-1982 Fax
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090915/614a14e0/attachment.html>


More information about the Python-list mailing list