How to create a socket.socket() object from a socket fd?

Grant Edwards grant.b.edwards at gmail.com
Mon Jan 23 12:00:49 EST 2017


On 2017-01-23, Antoon Pardon <antoon.pardon at rece.vub.ac.be> wrote:
> Op 22-01-17 om 01:52 schreef Grant Edwards:
>> Newsgroups: gmane.comp.python.general
>> From: Grant Edwards <grant.b.edwards at gmail.com>
>> Subject: Re: How to create a socket.socket() object from a socket fd?
>> References: <o60naq$bbm$1 at blaine.gmane.org> <o60o2r$6cd$1 at blaine.gmane.org> <c7e3116f-6e9c-5343-6f82-c491fb917800 at python.org>
>> Followup-To: 
>>
>> <rant>
>>
>> I'm still baffled why the standard library fromfd() code dup()s the
>> descriptor.
>>
>> According to the comment in the CPython sources, the author of
>> fromfd() is guessing that the user wants to be able to close the
>> descriptor separately from the socket.
>>
>> If the user wanted the socket object to use a duplicate descriptor for
>> some reason, the caller should call os.dup() -- it's only _eight_
>> keystrokes.  Eight keystrokes that makes it obvious to anybody reading
>> the code that there are now two descriptors and you have to close both
>> the original descriptor and the socket.
>>
>> When you create a Python file object from a file descriptor using
>> os.fdopen(), does it dup the descriptor?  No.  Would a reasonable
>> person expect socket.fromfd() to duplicate the descriptor?  No.
>>
>> Should it?
>>
>> No.
>
> The standard response to issues like this is:
>
>    A foolish consistency is the hobgoblin of little minds

And wise consistency is the foundation of a good language design.

-- 
Grant Edwards               grant.b.edwards        Yow! I'm wet!  I'm wild!
                                  at               
                              gmail.com            




More information about the Python-list mailing list