os.popen varies return type depending on exit status?

Discussion in 'Python' started by David M. Wilson, Mar 5, 2004.

  1. 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?


    David.
     
    David M. Wilson, Mar 5, 2004
    #1
    1. Advertisements

  2. David M. Wilson

    Donn Cave Guest

    In article <>,
    (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 Cave, Mar 5, 2004
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Evans
    Replies:
    2
    Views:
    1,214
    Jeff Evans
    Dec 5, 2004
  2. Kevin Frey
    Replies:
    0
    Views:
    531
    Kevin Frey
    Nov 19, 2006
  3. QQ
    Replies:
    5
    Views:
    708
    Jonathan Adams
    May 10, 2005
  4. jacob navia
    Replies:
    3
    Views:
    788
    Nick Keighley
    Feb 24, 2010
  5. Keith Thompson
    Replies:
    10
    Views:
    928
    Tim Rentsch
    Mar 3, 2010
  6. Garance A Drosihn

    Exit status on cmd executed via popen()

    Garance A Drosihn, Nov 24, 2003, in forum: Ruby
    Replies:
    3
    Views:
    1,454
    Gennady
    Nov 25, 2003
  7. Claudio Jeker

    IO#popen and exit status

    Claudio Jeker, Mar 20, 2006, in forum: Ruby
    Replies:
    7
    Views:
    417
    Tanaka Akira
    Mar 28, 2006
  8. kaleem
    Replies:
    8
    Views:
    1,221
    Tad McClellan
    Dec 14, 2006
Loading...