This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Unix popen does not return exit status
Type: Stage:
Components: Library (Lib) Versions: Python 2.2
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: twouters Nosy List: barry-scott, facundobatista, orenti, twouters
Priority: normal Keywords:

Created on 2002-09-12 22:03 by barry-scott, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
popen2.py barry-scott, 2002-09-12 22:03 popen2.py
Messages (8)
msg12386 - (view) Author: Barry Alan Scott (barry-scott) * Date: 2002-09-12 22:03
The documentation for os.popen[234] states that the close
will 
return the rc of the process. This works well on
windows, but the 
unix code does not work, None is
returned.

The 
attached version of popen2.py fixes the problem
by wrapping 
the fromchild fd with an object that
implements the 
documented close() behaviour.

I have also extended the 
test() to cover testing the
rc and run on FreeBSD against 
2.2.1
msg12387 - (view) Author: Thomas Wouters (twouters) * (Python committer) Date: 2002-09-13 09:03
Logged In: YES 
user_id=34209

Assigned to me because I'm last in the 'assigned to' select box.

 - Points off because you didn't upload a patch, you
uploaded a new version.   I think I diff'ed against the
right version, but a patch is much better.

 - I'm not happy with the selective copying of attributes to
the wrapper object. For example, you aren't copying
xreadlines -- and what else ? Copying all 'public'
attributes (those not starting with _) seems a much safer bet.

 - The test is wrong; you mean 'exit 6', not 'return 6';
return might work in some shells, but it isn't the correct
way and it doesn't work in bash, at least. I'm also not sure
how portable this is; howmany platforms use the popen2 module ?

 - Why do you a result of convert -1 to None ? -1 seems a
valid return value to me, especially if it's documented as
meaning "process has not finished yet" :)

 - I'm not happy with this solution in general. The problem
lies in Python's handling of the (f)close function (and its
return value) on the fileobject.. os.pipe() returns a fd,
which is turned into a fileobject by os.fdopen -- which
passes 'fclose' as the close method, which always returns 0.
I'd much rather go for a solution where fdopen (or a new
function) handles pipes specially, passing in a fake-fclose
that really does 'close' on the fd (the infrastructure is
all there) or the Popen* classes should just provide a
file-wrapper (or subclass?) altogether, and have the close
method call close on the fd.

But that's just me.
msg12388 - (view) Author: Thomas Wouters (twouters) * (Python committer) Date: 2002-09-13 09:12
Logged In: YES 
user_id=34209

Hm, for some reason I thought the C 'close()' function (and
os.close()) did return the status. They don't, it should be
'pclose()'. os.popen() passes this as the 'close' function
to the file constructor, so the rest of the statements still
stand :)
msg12389 - (view) Author: Oren Tirosh (orenti) Date: 2002-09-16 07:41
Logged In: YES 
user_id=562624

I think I have a cleaner solution than a wrapper object. Now 
that we can inherit from built-in types it should be possible to 
make a subclass of file and override the close method.

There's a small problem because file.__new__ returns an 
uninitialized file object and there is no way to initialize it in 
Python, only from C. I noticed this a while ago but now that I 
see an actual use for subclassing file objects I think it's time to 
fix it.

Patch coming soon.

msg12390 - (view) Author: Oren Tirosh (orenti) Date: 2002-09-16 12:29
Logged In: YES 
user_id=562624

It seems that Taral has beat me to it. 

http://python.org/sf/608182 "Enhanced file constructor"

This patch would make it possible to implement popen as a 
subclass of file.

msg12391 - (view) Author: Barry Alan Scott (barry-scott) * Date: 2002-09-16 22:15
Logged In: YES 
user_id=28665

I don't care how this is fixed. I just want a fix.

It would be nice if any fix 
can make it into a 2.2.x maint release rather then wait for 2.3. If you also 
think this way then exploiting 2.3 features is not 
reasonable.

Which version should I diff against to make a 
patch?

On the specific points: Why not return -1?
The -1 is  the 
value set that means no status available.
(See sts variable - not sure 
how this works as a class variable!)
This will be the case if you close 
the fd before the
process exits. Windows version seems to return 
None
in cases where the exit code is not available. But I've not
read 
the code to find out details.

A suppose a __get__() would allow 
forwarding of gets to all of the fd's properties. I may be worth copying the 
functions from the fd to the wrapper for speed.

Note: fd.close() 
returns None not zero.

I was hoping whoever designed the API 
could comment on how they though it should work.
msg12392 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2004-12-26 14:46
Logged In: YES 
user_id=752496

Please, could you verify if this problem persists in Python 2.3.4
or 2.4?

If yes, in which version? Can you provide a test case?

If the problem is solved, from which version?

Note that if you fail to answer in one month, I'll close this bug
as "Won't fix".

Thank you! 

.    Facundo
msg12393 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2005-03-22 21:23
Logged In: YES 
user_id=752496

Deprecated. Reopen only if still happens in 2.3 or newer. 

.    Facundo
History
Date User Action Args
2022-04-10 16:05:40adminsetgithub: 37174
2002-09-12 22:03:35barry-scottcreate