[Jython-checkins] jython: Enable IP address objects to be used as tuples.

jim.baker jython-checkins at python.org
Wed Jun 18 19:22:22 CEST 2014


http://hg.python.org/jython/rev/c57ccfce5106
changeset:   7302:c57ccfce5106
user:        Santoso Wijaya <santoso.wijaya at gmail.com>
date:        Wed Jun 18 11:22:48 2014 -0600
summary:
  Enable IP address objects to be used as tuples.

Fixes http://bugs.jython.org/issue2155
Merged from https://bitbucket.org/jython/jython/pull-request/43/fix-issue-2155-make-tuple-like-object/

files:
  Lib/_socket.py          |  58 ++++++----------------------
  Lib/test/test_socket.py |  11 +++++
  2 files changed, 24 insertions(+), 45 deletions(-)


diff --git a/Lib/_socket.py b/Lib/_socket.py
--- a/Lib/_socket.py
+++ b/Lib/_socket.py
@@ -1388,60 +1388,28 @@
 
 
 # Define data structures to support IPV4 and IPV6.
-# FIXME are these ip address classes required by CPython API? Must they be old-style classes?
 
-class _ip_address_t: pass
+class _ip_address_t(tuple):
+    pass
+
 
 class _ipv4_address_t(_ip_address_t):
 
-    def __init__(self, sockaddr, port, jaddress):
-        self.sockaddr = sockaddr
-        self.port     = port
-        self.jaddress = jaddress
+    jaddress = None
 
-    def __getitem__(self, index):
-        if   0 == index:
-            return self.sockaddr
-        elif 1 == index:
-            return self.port
-        else:
-            raise IndexError()
-
-    def __len__(self):
-        return 2
-
-    def __str__(self):
-        return "('%s', %d)" % (self.sockaddr, self.port)
-
-    __repr__ = __str__
+    def __new__(cls, sockaddr, port, jaddress):
+        ntup = tuple.__new__(cls, (sockaddr, port))
+        ntup.jaddress = jaddress
+        return ntup
 
 class _ipv6_address_t(_ip_address_t):
 
-    def __init__(self, sockaddr, port, jaddress):
-        self.sockaddr = sockaddr
-        self.port     = port
-        self.jaddress = jaddress
+    jaddress = None
 
-    def __getitem__(self, index):
-        if   0 == index:
-            return self.sockaddr
-        elif 1 == index:
-            return self.port
-        elif 2 == index:
-            return 0
-        elif 3 == index:
-            return self.jaddress.scopeId
-        else:
-            raise IndexError()
-
-    def __len__(self):
-        return 4
-
-    def __str__(self):
-        return "('%s', %d, 0, %d)" % (self.sockaddr, self.port, self.jaddress.scopeId)
-
-    __repr__ = __str__
-
+    def __new__(cls, sockaddr, port, jaddress):
+        ntup = tuple.__new__(cls, (sockaddr, port, 0, jaddress.scopeId))
+        ntup.jaddress = jaddress
+        return ntup
 
 
 def _get_jsockaddr(address_object, family, sock_type, proto, flags):
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
@@ -1802,6 +1802,17 @@
             self.assert_(isinstance(canonname, str))
             self.assert_(isinstance(sockaddr[0], str))
 
+    def testSockAddrAsTuple(self):
+        family, socktype, proto, canonname, sockaddr = socket.getaddrinfo(HOST, PORT, socket.AF_INET, socket.SOCK_STREAM)[0]
+        self.assertEqual(len(sockaddr), 2)
+        self.assertEqual(sockaddr[-1], PORT)
+        self.assertEqual(sockaddr[:2], ('127.0.0.1', PORT))
+
+        family, socktype, proto, canonname, sockaddr = socket.getaddrinfo('::1', PORT, socket.AF_INET6, socket.SOCK_STREAM)[0]
+        self.assertEqual(len(sockaddr), 4)
+        self.assertEqual(sockaddr[-3], PORT)
+        #self.assertEqual(sockaddr[:2], ('::1', PORT))      # FIXME: Got '0:0:...:1' instead!
+
     def testAI_PASSIVE(self):
         # Disabling this test for now; it's expectations are not portable.
         # Expected results are too dependent on system config to be made portable between systems.

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


More information about the Jython-checkins mailing list