[Tutor] Using popen

Sheila King sheila@thinkspot.net
Tue, 2 Apr 2002 23:56:21 -0800


On Tue, 02 Apr 2002 22:53:07 -0800 (PST), Sean 'Shaleh' Perry wrote:

> > Python 2.2 (#1, Feb 21 2002, 02:25:03)
> > [GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
> > Type "help", "copyright", "credits" or "license" for more
> > information.
> > > > > import os p = os.popen('vauthenticate secret_test', 'w', 0)
> > > > > p.write('secret')
> > > > > p.close()
> > 256
> >
> > Now, I don't really understand these pipes and popen as well as
> > I'd like, so I may well be doing something really stupid here.
> > But, I certainly did not expect to get an exit code of 256 with
> > this particular ACCOUNTNAME/password pair, as it returns an exit
> > code of zero at the command line (and I know these are correct
> > values for one of my POP accounts...).
> >
> > Advice...? Please? I'd be most grateful.

> the shell is nice and returns the real exit code to you with $?.
> I copied your code into a python script and wrote the following sh
> script:
>
> #!/bin/sh
> read foo
> exit 2
>
> I added:
>
> ret = p.close()
> print "Return value:", ret
>
> to your script.  Here is the output:
>
> $ python retval.py Return value 512
>
> hmm, let's try exit 1.
>
>
> $ python retval.py Return value 256
>
> ok, let's try exit 0.
>
>
> $ python retval.py Return value None
>
> ok, let's try exit 10.
>
>
> $ python retval.py Return value 2560
>
> and one more, exit 4.
>
>
> $ python retval.py Return value 1024
>
> see the pattern?  the return value is ret / 256.  If the return
> value was zero then ret == None.

That's helpful, Sean, yes. However, I still don't understand even in 
this case, why I got a return of 256. I should have gotten None. At 
least, that command in the shell returns an exit code of zero with 
the data I tested. Shouldn't it return the same result in Python with 
popen? (i.e. in case of exitcode = 0, popen.close() should return 
None.)

Still confused...

Sheila King
http://www.thinkspot.net/sheila/