From: <am...@us...> - 2009-01-29 20:51:24
|
Revision: 6002 http://jython.svn.sourceforge.net/jython/?rev=6002&view=rev Author: amak Date: 2009-01-29 20:51:21 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Back-porting changes relating to UDP broadcast. Also back-porting some variable scoping changes to the test_socket module. Modified Paths: -------------- branches/Release_2_2maint/jython/Lib/socket.py branches/Release_2_2maint/jython/Lib/test/test_socket.py Modified: branches/Release_2_2maint/jython/Lib/socket.py =================================================================== --- branches/Release_2_2maint/jython/Lib/socket.py 2009-01-29 17:59:04 UTC (rev 6001) +++ branches/Release_2_2maint/jython/Lib/socket.py 2009-01-29 20:51:21 UTC (rev 6002) @@ -177,6 +177,9 @@ TCP_NODELAY = 256 +INADDR_ANY = "0.0.0.0" +INADDR_BROADCAST = "255.255.255.255" + # Options with negative constants are not supported # They are being added here so that code that refers to them # will not break with an AttributeError @@ -712,7 +715,7 @@ def _get_jsocket(self): return self.sock_impl.jsocket -def _unpack_address_tuple(address_tuple, for_tx=False): +def _unpack_address_tuple(address_tuple): # TODO: Upgrade to support the 4-tuples used for IPv6 addresses # which include flowinfo and scope_id. # To be upgraded in synch with getaddrinfo @@ -722,11 +725,6 @@ or type(address_tuple[1]) is not type(0): raise TypeError(error_message) hostname = address_tuple[0].strip() - if hostname == "<broadcast>": - if for_tx: - hostname = "255.255.255.255" - else: - hostname = "0.0.0.0" return hostname, address_tuple[1] class _tcpsocket(_nonblocking_api_mixin): @@ -902,6 +900,8 @@ try: assert not self.sock_impl host, port = _unpack_address_tuple(addr) + if host == "": + host = INADDR_ANY host_address = java.net.InetAddress.getByName(host) self.sock_impl = _datagram_socket_impl(port, host_address, self.pending_options[SO_REUSEADDR]) self._config() @@ -941,7 +941,9 @@ if not self.sock_impl: self.sock_impl = _datagram_socket_impl() self._config() - host, port = _unpack_address_tuple(addr, True) + host, port = _unpack_address_tuple(addr) + if host == "<broadcast>": + host = INADDR_BROADCAST byte_array = java.lang.String(data).getBytes('iso-8859-1') result = self.sock_impl.sendto(byte_array, host, port, flags) return result Modified: branches/Release_2_2maint/jython/Lib/test/test_socket.py =================================================================== --- branches/Release_2_2maint/jython/Lib/test/test_socket.py 2009-01-29 17:59:04 UTC (rev 6001) +++ branches/Release_2_2maint/jython/Lib/test/test_socket.py 2009-01-29 20:51:21 UTC (rev 6002) @@ -28,10 +28,13 @@ class SocketTCPTest(unittest.TestCase): + HOST = HOST + PORT = PORT + def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.serv.bind((HOST, PORT)) + self.serv.bind((self.HOST, self.PORT)) self.serv.listen(1) def tearDown(self): @@ -40,10 +43,13 @@ class SocketUDPTest(unittest.TestCase): + HOST = HOST + PORT = PORT + def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.serv.bind((HOST, PORT)) + self.serv.bind((self.HOST, self.PORT)) def tearDown(self): self.serv.close() @@ -196,7 +202,7 @@ def clientSetUp(self): ThreadedTCPSocketTest.clientSetUp(self) - self.cli.connect((HOST, PORT)) + self.cli.connect((self.HOST, self.PORT)) self.serv_conn = self.cli def clientTearDown(self): @@ -540,7 +546,7 @@ # First listen on a server socket, so that the connection won't be refused. server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.bind( (HOST, PORT) ) - server_sock.listen() + server_sock.listen(50) # Now do the tests sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._testSetAndGetOption(sock, option, values) @@ -561,7 +567,7 @@ self._testSetAndGetOption(sock, option, values) # now bind and listen on the socket i.e. cause the implementation socket to be created sock.bind( (HOST, PORT) ) - sock.listen() + sock.listen(50) self.failUnlessEqual(sock.getsockopt(socket.SOL_SOCKET, option), values[-1], \ "Option value '%s'='%s' did not propagate to implementation socket" % (option, values[-1])) self._testSetAndGetOption(sock, option, values) @@ -825,7 +831,7 @@ self.assertEqual(msg, MSG) def _testSendtoAndRecv(self): - self.cli.sendto(MSG, 0, (HOST, PORT)) + self.cli.sendto(MSG, 0, (self.HOST, self.PORT)) def testSendtoAndRecvTimeoutMode(self): # Need to test again in timeout mode, which follows @@ -836,7 +842,7 @@ def _testSendtoAndRecvTimeoutMode(self): self.cli.settimeout(10) - self.cli.sendto(MSG, 0, (HOST, PORT)) + self.cli.sendto(MSG, 0, (self.HOST, self.PORT)) def testRecvFrom(self): # Testing recvfrom() over UDP @@ -844,7 +850,7 @@ self.assertEqual(msg, MSG) def _testRecvFrom(self): - self.cli.sendto(MSG, 0, (HOST, PORT)) + self.cli.sendto(MSG, 0, (self.HOST, self.PORT)) def testRecvFromTimeoutMode(self): # Need to test again in timeout mode, which follows @@ -855,7 +861,7 @@ def _testRecvFromTimeoutMode(self): self.cli.settimeout(10) - self.cli.sendto(MSG, 0, (HOST, PORT)) + self.cli.sendto(MSG, 0, (self.HOST, self.PORT)) def testSendtoEightBitSafe(self): # This test is necessary because java only supports signed bytes @@ -863,7 +869,7 @@ self.assertEqual(msg, EIGHT_BIT_MSG) def _testSendtoEightBitSafe(self): - self.cli.sendto(EIGHT_BIT_MSG, 0, (HOST, PORT)) + self.cli.sendto(EIGHT_BIT_MSG, 0, (self.HOST, self.PORT)) def testSendtoEightBitSafeTimeoutMode(self): # Need to test again in timeout mode, which follows @@ -874,7 +880,7 @@ def _testSendtoEightBitSafeTimeoutMode(self): self.cli.settimeout(10) - self.cli.sendto(EIGHT_BIT_MSG, 0, (HOST, PORT)) + self.cli.sendto(EIGHT_BIT_MSG, 0, (self.HOST, self.PORT)) class UDPBroadcastTest(ThreadedUDPSocketTest): @@ -883,13 +889,13 @@ self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) def testBroadcast(self): - self.serv.bind( ("<broadcast>", PORT) ) + self.serv.bind( ("", self.PORT) ) msg = self.serv.recv(len(EIGHT_BIT_MSG)) self.assertEqual(msg, EIGHT_BIT_MSG) def _testBroadcast(self): self.cli.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) - self.cli.sendto(EIGHT_BIT_MSG, ("<broadcast>", PORT) ) + self.cli.sendto(EIGHT_BIT_MSG, ("<broadcast>", self.PORT) ) class BasicSocketPairTest(SocketPairTest): @@ -956,7 +962,7 @@ def _testAcceptConnection(self): # Make a connection to the server - self.cli.connect((HOST, PORT)) + self.cli.connect((self.HOST, self.PORT)) # # AMAK: 20070311 @@ -971,7 +977,7 @@ def _testBlockingConnect(self): # Testing blocking connect self.cli.settimeout(10) - self.cli.connect((HOST, PORT)) + self.cli.connect((self.HOST, self.PORT)) def testNonBlockingConnect(self): # Testing non-blocking connect @@ -980,7 +986,7 @@ def _testNonBlockingConnect(self): # Testing non-blocking connect self.cli.setblocking(0) - result = self.cli.connect_ex((HOST, PORT)) + result = self.cli.connect_ex((self.HOST, self.PORT)) rfds, wfds, xfds = select.select([], [self.cli], []) self.failUnless(self.cli in wfds) try: @@ -999,13 +1005,13 @@ def _testConnectWithLocalBind(self): # Testing blocking connect with local bind - cli_port = PORT - 1 + cli_port = self.PORT - 1 while True: # Keep trying until a local port is available self.cli.settimeout(1) - self.cli.bind( (HOST, cli_port) ) + self.cli.bind( (self.HOST, cli_port) ) try: - self.cli.connect((HOST, PORT)) + self.cli.connect((self.HOST, self.PORT)) break except socket.error, se: # cli_port is in use (maybe in TIME_WAIT state from a @@ -1033,7 +1039,7 @@ self.fail("Non-blocking socket with data should been in read list.") def _testRecvData(self): - self.cli.connect((HOST, PORT)) + self.cli.connect((self.HOST, self.PORT)) self.cli.send(MSG) def testRecvNoData(self): @@ -1048,7 +1054,7 @@ self.fail("Non-blocking recv of no data should have raised socket.error.") def _testRecvNoData(self): - self.cli.connect((HOST, PORT)) + self.cli.connect((self.HOST, self.PORT)) time.sleep(0.1) class NonBlockingUDPTests(ThreadedUDPSocketTest): pass @@ -1096,7 +1102,7 @@ self.cli_file = self.cli.makefile('wb') self.cli_file.close() try: - self.cli.sendto(MSG, 0, (HOST, PORT)) + self.cli.sendto(MSG, 0, (self.HOST, self.PORT)) except Exception, x: self.fail("Closing file wrapper appears to have closed underlying socket: %s" % str(x)) @@ -1108,7 +1114,7 @@ def _testCloseSocketDoesNotCloseFile(self): try: - self.cli.sendto(MSG, 0, (HOST, PORT)) + self.cli.sendto(MSG, 0, (self.HOST, self.PORT)) except Exception, x: self.fail("Closing file wrapper appears to have closed underlying socket: %s" % str(x)) @@ -1496,12 +1502,12 @@ def testBindException(self): # First bind to the target port self.s.bind( (HOST, PORT) ) - self.s.listen() + self.s.listen(50) try: # And then try to bind again t = socket.socket(socket.AF_INET, socket.SOCK_STREAM) t.bind( (HOST, PORT) ) - t.listen() + t.listen(50) except socket.error, se: self.failUnlessEqual(se[0], errno.EADDRINUSE) except Exception, x: @@ -1571,7 +1577,7 @@ self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def testShutdownIOOnListener(self): - self.socket.listen() # socket is now a server socket + self.socket.listen(50) # socket is now a server socket try: self.socket.shutdown(socket.SHUT_RDWR) except Exception, x: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |