Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95847
| Date | 2015-09-02 08:01 +1000 |
|---|---|
| From | Cameron Simpson <cs@zip.com.au> |
| Subject | Re: Low level file descriptors and high-level Python files |
| References | <1441122964.2424706.371812753.75CE734E@webmail.messagingengine.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.57.1441144917.23514.python-list@python.org> (permalink) |
On 01Sep2015 11:56, random832@fastmail.us <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 <cs@zip.com.au>
This is not a bug. It's just the way it works, and makes perfect sense.
- Tom Christiansen <tchrist@jhereg.perl.com>
I like that line. I hope my boss falls for it.
- Chaim Frenkel <chaimf@cris.com>
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Low level file descriptors and high-level Python files Cameron Simpson <cs@zip.com.au> - 2015-09-02 08:01 +1000
csiph-web