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: os.popen with os.close gives error message
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, dtrosset, ronaldoussoren
Priority: low Keywords:

Created on 2006-10-10 07:45 by dtrosset, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg61018 - (view) Author: dtrosset (dtrosset) Date: 2006-10-10 07:45
Given the following code:

  import os
  child_stdin = os.popen("cat -", "w")
  old_stdout = os.dup(1)
  os.close(child_stdin.fileno())
  print "foo"
  os.dup2(old_stdout, 1)
  os.close(old_stdout)


I got these different results depending on the version
of python I am using. 


$ python2.4 -V 
Python 2.4.4c0
$ python2.4 test.py 
foo
close failed: [Errno 9] Bad file descriptor

$ python2.3 -V 
Python 2.3.5
$ python2.3 test/new/test.py 
foo

My .02$ guess is that underlying file descriptor of
child_stdin being closed, when trying to delete this
object, it tries again to close it.
msg61019 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2006-11-08 07:03
Logged In: YES 
user_id=580910

IMHO this is "don't do that then" territory. You're poking around in the inside of file objects, you have to be careful if you do that.

BTW. What are you trying to accomplish? If you set sys.stdout to child_stdin (e.g. "import sys; sys.stdout = child_stdin"), print will write to the 
pipe. If you really want to be sure that the C-level variable stdout writes to the pipe: os.dup2(child_stdout.fileno(), 1). You can then close 
child_stdout, but still have to do the 'os.dup(1)' part if you want to restore the real stdout later on.
msg84528 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-03-30 07:21
I'll close this based on Ronald's analysis unless opposition is voiced.
History
Date User Action Args
2022-04-11 14:56:20adminsetgithub: 44108
2009-05-12 17:05:12ajaksu2setstatus: pending -> closed
resolution: not a bug
stage: test needed -> resolved
2009-03-30 07:21:15ajaksu2setstatus: open -> pending
priority: normal -> low
type: behavior

versions: + Python 2.6, - Python 2.4
nosy: + ajaksu2

messages: + msg84528
stage: test needed
2006-10-10 07:45:08dtrossetcreate