Failed Regression Test: What socket.gethostname() is supposed to return?

Lie Ryan lie.1296 at
Sun Jul 26 00:30:30 CEST 2009

In my laptop, socket.gethostname() returned my username, and causing one
of python's "make test" regression test to error (test_socket):

ERROR: testSockName (test.test_socket.GeneralModuleTests)
Traceback (most recent call last):
line 456, in testSockName
    my_ip_addr = socket.gethostbyname(socket.gethostname())
gaierror: (-2, 'Name or service not known')


since on my system socket.gethostname() returns 'lieryan', and since
socket.gethostbyname('lieryan') does not resolve to anything; the test
becomes an error.

My system is Gentoo, but I think this also happened on Ubuntu (still on
this laptop). The trunk failed in similar manner.

Do I have a misconfigured system or is the test faulty?

For convenience, the relevant test code (taken from trunk):

def testSockName(self):
    # Testing getsockname()
    port = self._get_unused_port()
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(("", port))
    name = sock.getsockname()
    # XXX(nnorwitz): seems to indicate
    # it reasonable to get the host's addr in addition to
    # At least for eCos.  This is required for the S/390 to pass.
    my_ip_addr = socket.gethostbyname(socket.gethostname())
    self.assertTrue(name[0] in ("", my_ip_addr), '%s invalid' %
    self.assertEqual(name[1], port)

lieryan at lieryan ~/Desktop/pythontrunk/trunk $ ./python -m test.regrtest
Could not find '/home/lieryan/Desktop/pythontrunk/trunk/Lib/test' in
sys.path to remove it
test test_socket failed -- Traceback (most recent call last):
"/home/lieryan/Desktop/pythontrunk/trunk/Lib/test/", line
493, in testSockName
    my_ip_addr = socket.gethostbyname(socket.gethostname())
gaierror: [Errno -2] Name or service not known

1 test failed:

I tracked the code for socket.gethostname() and socket.gethostbyname()
and found that they are simply a wrapper for gethostname() from #import
<unistd.h> ( and gethostbyname()
from #import <netdb.h> ( A
simple test in C found that the C's equivalent to
gethostbyname(gethostname()) returns a null pointer (used to indicate
error, per documentation).

So, the question is: what is socket.gethostname() is supposed to return
that will be a valid argument for socket.gethostbyname()?

PS: I found an MSDN article by Microsoft stating that
gethostbyname(gethostname) is guaranteed to always succeed
(; is this
guarantee also true in linux?

More information about the Python-list mailing list