From: <pj...@us...> - 2008-10-14 23:08:57
|
Revision: 5393 http://jython.svn.sourceforge.net/jython/?rev=5393&view=rev Author: pjenvey Date: 2008-10-14 23:08:45 +0000 (Tue, 14 Oct 2008) Log Message: ----------- o fix the socket hostname functions returning unicode hostnames instead of strs. was causing test_wsgiref to fail on environments where socket.getfqdn('localhost') != 'localhost' o fix getaddrinfo default family and handling of bad familys Modified Paths: -------------- trunk/jython/Lib/socket.py trunk/jython/Lib/test/test_socket.py Modified: trunk/jython/Lib/socket.py =================================================================== --- trunk/jython/Lib/socket.py 2008-10-14 19:28:28 UTC (rev 5392) +++ trunk/jython/Lib/socket.py 2008-10-14 23:08:45 UTC (rev 5393) @@ -81,6 +81,7 @@ import org.python.core.io.DatagramSocketIO import org.python.core.io.ServerSocketIO import org.python.core.io.SocketIO +from org.python.core.util.StringUtil import asPyString class error(Exception): pass class herror(error): pass @@ -464,8 +465,8 @@ names = [] addrs = [] for addr in addresses: - names.append(addr.getHostName()) - addrs.append(addr.getHostAddress()) + names.append(asPyString(addr.getHostName())) + addrs.append(asPyString(addr.getHostAddress())) return (names, addrs) def getfqdn(name=None): @@ -487,13 +488,13 @@ def gethostname(): try: - return java.net.InetAddress.getLocalHost().getHostName() + return asPyString(java.net.InetAddress.getLocalHost().getHostName()) except java.lang.Exception, jlx: raise _map_exception(jlx) def gethostbyname(name): try: - return java.net.InetAddress.getByName(name).getHostAddress() + return asPyString(java.net.InetAddress.getByName(name).getHostAddress()) except java.lang.Exception, jlx: raise _map_exception(jlx) @@ -523,10 +524,10 @@ else: return _udpsocket() -def getaddrinfo(host, port, family=None, socktype=None, proto=0, flags=None): +def getaddrinfo(host, port, family=AF_INET, socktype=None, proto=0, flags=None): try: if not family in [AF_INET, AF_INET6, AF_UNSPEC]: - raise NotSupportedError() + raise gaierror(errno.EIO, 'ai_family not supported') filter_fns = [] filter_fns.append({ AF_INET: lambda x: isinstance(x, java.net.Inet4Address), @@ -541,7 +542,9 @@ if len([f for f in filter_fns if f(a)]): family = {java.net.Inet4Address: AF_INET, java.net.Inet6Address: AF_INET6}[a.getClass()] # TODO: Include flowinfo and scopeid in a 4-tuple for IPv6 addresses - results.append( (family, socktype, proto, a.getCanonicalHostName(), (a.getHostAddress(), port)) ) + canonname = asPyString(a.getCanonicalHostName()) + sockname = asPyString(a.getHostAddress()) + results.append((family, socktype, proto, canonname, (sockname, port))) return results except java.lang.Exception, jlx: raise _map_exception(jlx) Modified: trunk/jython/Lib/test/test_socket.py =================================================================== --- trunk/jython/Lib/test/test_socket.py 2008-10-14 19:28:28 UTC (rev 5392) +++ trunk/jython/Lib/test/test_socket.py 2008-10-14 23:08:45 UTC (rev 5393) @@ -274,8 +274,10 @@ def testHostnameRes(self): # Testing hostname resolution mechanisms hostname = socket.gethostname() + self.assert_(isinstance(hostname, str)) try: ip = socket.gethostbyname(hostname) + self.assert_(isinstance(ip, str)) except socket.error: # Probably name lookup wasn't set up right; skip this test self.fail("Probably name lookup wasn't set up right; skip testHostnameRes.gethostbyname") @@ -283,15 +285,35 @@ self.assert_(ip.find('.') >= 0, "Error resolving host to ip.") try: hname, aliases, ipaddrs = socket.gethostbyaddr(ip) + self.assert_(isinstance(hname, str)) + for hosts in aliases, ipaddrs: + self.assert_(all(isinstance(host, str) for host in hosts)) except socket.error: # Probably a similar problem as above; skip this test self.fail("Probably name lookup wasn't set up right; skip testHostnameRes.gethostbyaddr") return all_host_names = [hostname, hname] + aliases fqhn = socket.getfqdn() + self.assert_(isinstance(fqhn, str)) if not fqhn in all_host_names: self.fail("Error testing host resolution mechanisms.") + def testGetAddrInfo(self): + try: + socket.getaddrinfo(HOST, PORT, 9999) + except socket.gaierror, gaix: + self.failUnlessEqual(gaix[0], errno.EIO) + except Exception, x: + self.fail("getaddrinfo with bad family raised wrong exception: %s" % x) + else: + self.fail("getaddrinfo with bad family should have raised exception") + + addrinfos = socket.getaddrinfo(HOST, PORT) + for addrinfo in addrinfos: + family, socktype, proto, canonname, sockaddr = addrinfo + self.assert_(isinstance(canonname, str)) + self.assert_(isinstance(sockaddr[0], str)) + def testRefCountGetNameInfo(self): # Testing reference count for getnameinfo import sys This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |