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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top