Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'bug.': 0.07; 'subject:file': 0.07; 'tom': 0.07; 'closed,': 0.09; 'descriptor': 0.09; 'falls': 0.09; 'oserror': 0.09; 'subject:files': 0.09; 'underlying': 0.09; 'files.': 0.13; '.close': 0.16; '>on': 0.16; 'fp.close()': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'simpson': 0.16; 'stuff,': 0.16; 'wrote:': 0.16; 'library': 0.20; 'sep': 0.22; 'cheers,': 0.22; 'passing': 0.23; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'equivalent': 0.27; 'not.': 0.27; 'object,': 0.27; 'function': 0.28; 'i/o': 0.29; 'inspect': 0.29; 'raise': 0.29; 'code': 0.30; 'supposed': 0.31; 'probably': 0.31; "d'aprano": 0.33; 'received:com.au': 0.33; 'steven': 0.33; 'open': 0.33; 'tue,': 0.34; 'file': 0.34; 'this?': 0.34; 'could': 0.35; 'level': 0.35; 'but': 0.36; 'there': 0.36; 'mode': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; "won't": 0.38; 'whatever': 0.39; 'enough': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'still': 0.40; 'some': 0.40; 'high': 0.60; 'close': 0.61; 'hope': 0.61; 'side': 0.62; 'further': 0.62; 'boss': 0.66; 'cameron': 0.66; 'effects.': 0.91; 'imagine': 0.96 Date: Wed, 2 Sep 2015 08:01:42 +1000 From: Cameron Simpson To: python-list@python.org Subject: Re: Low level file descriptors and high-level Python files MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <1441122964.2424706.371812753.75CE734E@webmail.messagingengine.com> User-Agent: Mutt/1.5.23 (2014-03-12) References: <1441122964.2424706.371812753.75CE734E@webmail.messagingengine.com> X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=Xd90t9N5 c=1 sm=1 tr=0 a=7VRakydLWw4CnqwyGz1TSg==:117 a=7VRakydLWw4CnqwyGz1TSg==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=vrnE16BAAAAA:8 a=kj9zAlcOel0A:10 a=ff-B7xzCdYMA:10 a=0Wkxbn5hAAAA:8 a=ixx56aDUAAAA:8 a=gGScE0-FqMDO5m2H9P8A:9 a=CjuIK1q_8ugA:10 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441144917 news.xs4all.nl 23775 [2001:888:2000:d::a6]:46443 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5001 X-Received-Body-CRC: 2086609518 Xref: csiph.com comp.lang.python:95847 On 01Sep2015 11:56, random832@fastmail.us wrote: >On Tue, Sep 1, 2015, at 10:57, Steven D'Aprano wrote: >> Q3: I could probably answer Q2 myself if I knew how to check whether a >> fd >> was open or not. With a file object, I can inspect file_obj.closed and it >> will tell me whether the file is open or not. Is there an equivalent for >> file descriptors? > >Well, if you try to call os.close, or any other operation for that >matter, it will raise an OSError with errno=EBADF. os.fstat might be safer. It won't have side effects. As additional remarks: Underwhat circumstances would you imagine probing an fd like this? For what purpose? It feels like a code smell for know having enough situational awareness, and then you're into guesswork world. One circumstance where you might use fdopen and _not_ want .close to close the underlying service is when you're handed a file descriptor over which you're supposed to perform some I/O, and the I/O library functions use high level files. In that case you might want code like this: fd2 = os.dup(fd) fp = open(fd2, 'a+b') # or whatever mode ... do stuff, perhaps passing fp to a library function ... fp.close() fd2 is not closed, but fd is still open for further use. Cheers, Cameron Simpson This is not a bug. It's just the way it works, and makes perfect sense. - Tom Christiansen I like that line. I hope my boss falls for it. - Chaim Frenkel