[Jython-checkins] jython: Various minor socket improvements and bug fixes

jim.baker jython-checkins at python.org
Fri Sep 4 06:41:05 CEST 2015


https://hg.python.org/jython/rev/b3b82ef080a9
changeset:   7721:b3b82ef080a9
user:        Jim Baker <jim.baker at rackspace.com>
date:        Thu Sep 03 22:40:26 2015 -0600
summary:
  Various minor socket improvements and bug fixes

socket.socket is now a class and can be subclassed (fixes #2366),
protocol defaults to 0 (fixes #2374), exports SocketType (fixes
#2383); SSLSocket.sendto no longer directly raises errno.EPROTO, which
is missing on Windows, but instead conforms to CPython semantics of
ignoring destination address (fixes #2315). Improved stability of
test_socket. Upgraded Netty jars to 4.0.31.

files:
  Lib/_socket.py                           |   11 +-
  Lib/socket.py                            |    2 +-
  Lib/ssl.py                               |    4 +-
  Lib/test/test_socket.py                  |   66 ++++++++++-
  build.xml                                |   20 +-
  extlibs/netty-buffer-4.0.25.Final.jar    |  Bin 
  extlibs/netty-buffer-4.0.31.Final.jar    |  Bin 
  extlibs/netty-codec-4.0.25.Final.jar     |  Bin 
  extlibs/netty-codec-4.0.31.Final.jar     |  Bin 
  extlibs/netty-common-4.0.25.Final.jar    |  Bin 
  extlibs/netty-common-4.0.31.Final.jar    |  Bin 
  extlibs/netty-handler-4.0.25.Final.jar   |  Bin 
  extlibs/netty-handler-4.0.31.Final.jar   |  Bin 
  extlibs/netty-transport-4.0.25.Final.jar |  Bin 
  extlibs/netty-transport-4.0.31.Final.jar |  Bin 
  15 files changed, 79 insertions(+), 24 deletions(-)


diff --git a/Lib/_socket.py b/Lib/_socket.py
--- a/Lib/_socket.py
+++ b/Lib/_socket.py
@@ -702,11 +702,11 @@
 
 class _realsocket(object):
 
-    def __init__(self, family=None, type=None, proto=None):
+    def __init__(self, family=None, type=None, proto=0):
         # FIXME verify args are correct
         self.family = family
         self.type = type
-        if proto is None:
+        if not proto:
             if type == SOCK_STREAM:
                 proto = IPPROTO_TCP
             elif type == SOCK_DGRAM:
@@ -1309,8 +1309,7 @@
 
     __doc__ = _realsocket.__doc__
 
-
-    def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None):
+    def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=None, _sock=None):
         if _sock is None:
             _sock = _realsocket(family, type, proto)
         self._sock = _sock
@@ -1453,10 +1452,6 @@
 
 # EXPORTED constructors
 
-def socket(family=None, type=None, proto=None):
-    return _socketobject(family, type, proto)
-
-
 def select(rlist, wlist, xlist, timeout=None):
     for lst in (rlist, wlist, xlist):
         if not isinstance(lst, Iterable):
diff --git a/Lib/socket.py b/Lib/socket.py
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -1,7 +1,7 @@
 # dispatches to _socket for actual implementation
 
 from _socket import (
-    socket, error, herror, gaierror, timeout, has_ipv6,
+    socket, SocketType, error, herror, gaierror, timeout, has_ipv6,
 
     create_connection,
 
diff --git a/Lib/ssl.py b/Lib/ssl.py
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -189,7 +189,9 @@
         return self.sock.recv_into(buffer, nbytes, flags)
 
     def sendto(self, string, arg1, arg2=None):
-        raise socket_error(errno.EPROTO)
+        # as observed on CPython, sendto when wrapped ignores the
+        # destination address, thereby behaving just like send
+        return self.sock.send(string)
 
     def close(self):
         self.sock.close()
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -94,7 +94,7 @@
 
     Note, the server setup function cannot call any blocking
     functions that rely on the client thread during setup,
-    unless serverExplicityReady() is called just before
+    unless serverExplicitReady() is called just before
     the blocking call (such as in setting up a client/server
     connection and performing the accept() in setUp().
     """
@@ -656,6 +656,9 @@
         sock.close()
         self.assertRaises(socket.error, sock.send, "spam")
 
+    def testSocketTypeAvailable(self):
+        self.assertIs(socket.socket, socket.SocketType)
+
 class IPAddressTests(unittest.TestCase):
 
     def testValidIpV4Addresses(self):
@@ -1372,7 +1375,6 @@
             rfds, wfds, xfds = select.select([self.cli], [self.cli], [], 0.1)
             if rfds or wfds or xfds:
                 break
-        self.failUnless(self.cli in wfds)
         try:
             self.cli.send(MSG)
         except socket.error:
@@ -2549,13 +2551,67 @@
             try:
                 self.s.getpeername()
             except socket.error, se:
-                # FIXME Apparently Netty's doesn't set remoteAddress, even if connected, for datagram channels
-                # so we may have to shadow
+                # FIXME Apparently Netty doesn't set remoteAddress,
+                # even if connected, for datagram channels so we may
+                # have to shadow
                 self.fail("getpeername() on connected UDP socket should not have raised socket.error")
             self.failUnlessEqual(self.s.getpeername(), self._udp_peer.getsockname())
         finally:
             self._udp_peer.close()
 
+class ConfigurableClientSocketTest(SocketTCPTest, ThreadableTest):
+
+    # Too bad we are not using cooperative multiple inheritance -
+    # **super is super**, after all!  So this means we currently have
+    # a bit of code duplication with respect to other unit tests. May
+    # want to refactor these unit tests accordingly at some point.
+
+    def config_client(self):
+        raise NotImplementedError("subclassing unit tests must define")
+
+    def __init__(self, methodName='runTest'):
+        SocketTCPTest.__init__(self, methodName=methodName)
+        ThreadableTest.__init__(self)
+
+    def setUp(self):
+        SocketTCPTest.setUp(self)
+        # Indicate explicitly we're ready for the client thread to
+        # proceed and then perform the blocking call to accept
+        self.serverExplicitReady()
+        self.cli_conn, _ = self.serv.accept()
+
+    def clientSetUp(self):
+        self.cli = self.config_client()
+        self.cli.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+        self.cli.connect((self.HOST, self.PORT))
+        self.serv_conn = self.cli
+
+    def clientTearDown(self):
+        self.cli.close()
+        self.cli = None
+        ThreadableTest.clientTearDown(self)
+
+    def testRecv(self):
+        # Testing large receive over TCP
+        msg = self.cli_conn.recv(1024)
+        self.assertEqual(msg, MSG)
+
+    def _testRecv(self):
+        self.serv_conn.send(MSG)
+
+class ProtocolCanBeZeroTest(ConfigurableClientSocketTest):
+
+    def config_client(self):
+        return socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
+
+class SocketClassCanBeSubclassed(ConfigurableClientSocketTest):
+
+    def config_client(self):
+        class MySocket(socket.socket):
+            pass
+        return MySocket()
+
+
 def test_main():
     tests = [
         GeneralModuleTests,
@@ -2590,6 +2646,8 @@
         TestGetSockAndPeerNameTCPClient, 
         TestGetSockAndPeerNameTCPServer, 
         TestGetSockAndPeerNameUDP,
+        ProtocolCanBeZeroTest,
+        SocketClassCanBeSubclassed
     ]
 
     if hasattr(socket, "socketpair"):
diff --git a/build.xml b/build.xml
--- a/build.xml
+++ b/build.xml
@@ -178,11 +178,11 @@
             <pathelement path="${extlibs.dir}/jnr-posix-3.0.9.jar"/>
             <pathelement path="${extlibs.dir}/jnr-constants-0.8.6.jar"/>
             <pathelement path="${extlibs.dir}/jline-2.12.1.jar"/>
-            <pathelement path="${extlibs.dir}/netty-buffer-4.0.25.Final.jar"/>
-            <pathelement path="${extlibs.dir}/netty-codec-4.0.25.Final.jar"/>
-            <pathelement path="${extlibs.dir}/netty-common-4.0.25.Final.jar"/>
-            <pathelement path="${extlibs.dir}/netty-handler--4.0.25.Final.jar"/>
-            <pathelement path="${extlibs.dir}/netty-transport-4.0.25.Final.jar"/>
+            <pathelement path="${extlibs.dir}/netty-buffer-4.0.31.Final.jar"/>
+            <pathelement path="${extlibs.dir}/netty-codec-4.0.31.Final.jar"/>
+            <pathelement path="${extlibs.dir}/netty-common-4.0.31.Final.jar"/>
+            <pathelement path="${extlibs.dir}/netty-handler--4.0.31.Final.jar"/>
+            <pathelement path="${extlibs.dir}/netty-transport-4.0.31.Final.jar"/>
         </path>
 
         <available property="informix.present" classname="com.informix.jdbc.IfxDriver" classpath="${informix.jar}" />
@@ -589,15 +589,15 @@
             <rule pattern="com.google.**" result="org.python.google. at 1"/>
             <zipfileset src="extlibs/icu4j-54_1_1.jar"/>
             <rule pattern="com.ibm.icu.**" result="org.python.icu. at 1"/>
-            <zipfileset src="extlibs/netty-buffer-4.0.25.Final.jar" excludes="META-INF/**"/>
+            <zipfileset src="extlibs/netty-buffer-4.0.31.Final.jar" excludes="META-INF/**"/>
             <rule pattern="io.netty.**" result="org.python.netty. at 1"/>
-            <zipfileset src="extlibs/netty-codec-4.0.25.Final.jar" excludes="META-INF/**"/>
+            <zipfileset src="extlibs/netty-codec-4.0.31.Final.jar" excludes="META-INF/**"/>
             <rule pattern="io.netty.**" result="org.python.netty. at 1"/>
-            <zipfileset src="extlibs/netty-common-4.0.25.Final.jar" excludes="META-INF/**"/>
+            <zipfileset src="extlibs/netty-common-4.0.31.Final.jar" excludes="META-INF/**"/>
             <rule pattern="io.netty.**" result="org.python.netty. at 1"/>
-            <zipfileset src="extlibs/netty-handler-4.0.25.Final.jar" excludes="META-INF/**"/>
+            <zipfileset src="extlibs/netty-handler-4.0.31.Final.jar" excludes="META-INF/**"/>
             <rule pattern="io.netty.**" result="org.python.netty. at 1"/>
-            <zipfileset src="extlibs/netty-transport-4.0.25.Final.jar" excludes="META-INF/**"/>
+            <zipfileset src="extlibs/netty-transport-4.0.31.Final.jar" excludes="META-INF/**"/>
             <rule pattern="io.netty.**" result="org.python.netty. at 1"/>
             <zipfileset src="extlibs/jffi-arm-Linux.jar"/>
             <zipfileset src="extlibs/jffi-Darwin.jar"/>
diff --git a/extlibs/netty-buffer-4.0.25.Final.jar b/extlibs/netty-buffer-4.0.25.Final.jar
deleted file mode 100644
index de8fa8e44ed46356174579086a545f043d0d225c..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
GIT binary patch
[stripped]
diff --git a/extlibs/netty-buffer-4.0.31.Final.jar b/extlibs/netty-buffer-4.0.31.Final.jar
new file mode 100644
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..759696378f5a57652b4db83462043bc5bca8b542
GIT binary patch
[stripped]
diff --git a/extlibs/netty-codec-4.0.25.Final.jar b/extlibs/netty-codec-4.0.25.Final.jar
deleted file mode 100644
index f1a618c8f1c02025ad202d53a3d9894f703abf5d..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
GIT binary patch
[stripped]
diff --git a/extlibs/netty-codec-4.0.31.Final.jar b/extlibs/netty-codec-4.0.31.Final.jar
new file mode 100644
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..a943e6bf7361835342c8637c8db3588e87d70e19
GIT binary patch
[stripped]
diff --git a/extlibs/netty-common-4.0.25.Final.jar b/extlibs/netty-common-4.0.25.Final.jar
deleted file mode 100644
index f23daacc18d360fa782bf5bb355034da77408cf5..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
GIT binary patch
[stripped]
diff --git a/extlibs/netty-common-4.0.31.Final.jar b/extlibs/netty-common-4.0.31.Final.jar
new file mode 100644
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..ed507f2d323abfbd593442e32d9ecbd2ace149b4
GIT binary patch
[stripped]
diff --git a/extlibs/netty-handler-4.0.25.Final.jar b/extlibs/netty-handler-4.0.25.Final.jar
deleted file mode 100644
index b1c61f2ce65544cc6b9643c9cbe931882f0450b2..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
GIT binary patch
[stripped]
diff --git a/extlibs/netty-handler-4.0.31.Final.jar b/extlibs/netty-handler-4.0.31.Final.jar
new file mode 100644
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..1a002fb26d8b46e7e132eabf952ba127d9ac2bb3
GIT binary patch
[stripped]
diff --git a/extlibs/netty-transport-4.0.25.Final.jar b/extlibs/netty-transport-4.0.25.Final.jar
deleted file mode 100644
index 2edc97363251cb88ed2eae2d44bea1a4e43602fb..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
GIT binary patch
[stripped]
diff --git a/extlibs/netty-transport-4.0.31.Final.jar b/extlibs/netty-transport-4.0.31.Final.jar
new file mode 100644
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..87881ca0beac4e5c5aecb099acffd4d673922d55
GIT binary patch
[stripped]

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list