os.popen varies return type depending on exit status?

Donn Cave donn at u.washington.edu
Fri Mar 5 14:46:29 EST 2004


In article <99dce321.0403050330.572a68da at posting.google.com>,
 dw-google.com at botanicus.net (David M. Wilson) wrote:

> Hi there, on Linux:
> 
>    >>> [ type(popen(cmd).close()) for cmd in ( 'true', 'foo' ) ]
>    sh: line 1: foo: command not found
>    [<type 'NoneType'>, <type 'int'>]
> 
> 
> This feels inconsistent to me, and it just bit me because I was
> expecting a 0 exit status on success, not None. Does close() return
> this for historic reasons?

The close function wraps either fclose(3) or pclose(3),
and I don't believe it actually knows which.  They return
the same 0 value on success, so in this case you see fclose
behavior.  Maybe that is for historic reasons, since in
principle I don't think it would hurt anything for fclose
to return 0.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list