[ python-Bugs-1755388 ] Problem with socket.gethostbyaddr() and KeyboardInterrupt

SourceForge.net noreply at sourceforge.net
Thu Jul 19 23:48:52 CEST 2007


Bugs item #1755388, was opened at 2007-07-17 07:07
Message generated for change (Comment added) made by gagenellina
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1755388&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Extension Modules
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: STINNER Victor (haypo)
Assigned to: Nobody/Anonymous (nobody)
Summary: Problem with socket.gethostbyaddr() and KeyboardInterrupt

Initial Comment:
Hi,

I wrote a small function to translate an IP to a hostname (eg. 212.27.33.225 => "linuxfr.org").

---------------------------------------------
from socket import gethostbyaddr, herror as socket_host_error

def ip2name(addr):
    try:
        if addr in ip2name.cache:
            return ip2name.cache[addr]
        name = gethostbyaddr(addr)[0]
    except (socket_host_error, KeyboardInterrupt, ValueError):
        name = addr
    ip2name.cache[addr] = name
    return name
ip2name.cache = {}
---------------------------------------------

Problem: sometimes gethostbyaddr() takes 5 seconds to answer "unknown host" so I hit CTRL+c to interrupt hit. But the "try/except" block doesn't catch the interruption. I found a workaround: use double try/except (try: gethostbyaddr() except KeyboardInterrupt: raise).

Last version of my function
---------------------------------------------
def ip2name(addr):
    if not ip2name.resolve:
        return addr
    try:
        if addr in ip2name.cache:
            return ip2name.cache[addr]
        # FIXME: Workaround Python bug
        # Need double try/except to catch the bug
        try:
            name = gethostbyaddr(addr)[0]
        except KeyboardInterrupt:
            raise
    except (socket_host_error, ValueError):
        name = addr
    except (socket_host_error, KeyboardInterrupt, ValueError):
        ip2name.resolve = False
        name = addr
    ip2name.cache[addr] = name
    return name
ip2name.cache = {}
ip2name.resolve = True
---------------------------------------------

I'm using Python 2.5.1 on Ubuntu Feisty (Linux kernel 2.6.20).

To reproduce my error, try "10.0.0.1" or "192.168.0.1" (or any other IP).

----------------------------------------------------------------------

Comment By: Gabriel Genellina (gagenellina)
Date: 2007-07-19 18:48

Message:
Logged In: YES 
user_id=479790
Originator: NO

I don't see any Attach button (?), code showing the bug is at
<http://pastebin.com/f7e02983d>


----------------------------------------------------------------------

Comment By: Gabriel Genellina (gagenellina)
Date: 2007-07-19 18:37

Message:
Logged In: YES 
user_id=479790
Originator: NO

I can reproduce the bug on Windows XP SP2, a simpler program is attached.

First run, I did not press Ctrl-C:

c:\temp>python bug1.py
enter test, gethostbyname('e.p.l.w.d.com')
Yo can press Ctrl-C
inner try
<type 'type'> <class 'socket.gaierror'> False
outer try
<type 'type'> <class 'socket.gaierror'> False
exit test

The inner try/except catches the exception as expected.

Second run, I *did* press Ctrl-C:

c:\temp>python bug1.py
enter test, gethostbyname('s.g.s.w.s.com')
Yo can press Ctrl-C
outer try
<type 'type'> <type 'exceptions.KeyboardInterrupt'> True
exit test

The inner try/except didn't catch the exception, and it propagates to the
outer one. I don't understand why; even if the gethostbyname call is
blocking, and Ctrl-C is not detected until it returns, there IS an
exception raised (either KeyboardInterrupt or socket.gaierror) in the inner
block that should be handled by the inner except clause.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1755388&group_id=5470


More information about the Python-bugs-list mailing list