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

Lie Ryan lie.1296 at gmail.com
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):
  File
"/var/tmp/portage/dev-lang/python-2.5.4-r3/work/Python-2.5.4/Lib/test/test_socket.py",
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(("0.0.0.0", port))
    name = sock.getsockname()
    # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate
    # it reasonable to get the host's addr in addition to 0.0.0.0.
    # 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 ("0.0.0.0", my_ip_addr), '%s invalid' %
name[0])
    self.assertEqual(name[1], port)
=======================


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

1 test failed:
    test_socket

I tracked the code for socket.gethostname() and socket.gethostbyname()
and found that they are simply a wrapper for gethostname() from #import
<unistd.h> (http://linux.die.net/man/2/gethostname) and gethostbyname()
from #import <netdb.h> (http://linux.die.net/man/3/gethostbyname). 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
(http://msdn.microsoft.com/en-us/library/ms738527(VS.85).aspx); is this
guarantee also true in linux?




More information about the Python-list mailing list