os.popen varies return type depending on exit status?

  • Thread starter David M. Wilson
  • Start date
D

David M. Wilson

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.
 
D

Donn Cave

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, (e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top