]> granicus.if.org Git - python/commitdiff
Fix repr(_socket.socket) on Windows 64-bit: don't fail with OverflowError
authorVictor Stinner <victor.stinner@gmail.com>
Sat, 26 Jul 2014 12:36:55 +0000 (14:36 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Sat, 26 Jul 2014 12:36:55 +0000 (14:36 +0200)
on closed socket. repr(socket.socket) already works fine.

Lib/test/test_socket.py
Misc/NEWS
Modules/socketmodule.c

index 8b37b21417965c83a5a37abcc635fb4fe07b387a..28bf8f5cc1cfa657239211d56f88662499c0b0ae 100644 (file)
@@ -38,6 +38,11 @@ try:
 except ImportError:
     thread = None
     threading = None
+try:
+    import _socket
+except ImportError:
+    _socket = None
+
 
 def _have_socket_can():
     """Check whether CAN sockets are supported on this host."""
@@ -658,6 +663,19 @@ class GeneralModuleTests(unittest.TestCase):
         self.assertIn('[closed]', repr(s))
         self.assertNotIn('laddr', repr(s))
 
+    @unittest.skipUnless(_socket is not None, 'need _socket module')
+    def test_csocket_repr(self):
+        s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
+        try:
+            expected = ('<socket object, fd=%s, family=%s, type=%s, proto=%s>'
+                        % (s.fileno(), s.family, s.type, s.proto))
+            self.assertEqual(repr(s), expected)
+        finally:
+            s.close()
+        expected = ('<socket object, fd=-1, family=%s, type=%s, proto=%s>'
+                    % (s.family, s.type, s.proto))
+        self.assertEqual(repr(s), expected)
+
     def test_weakref(self):
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         p = proxy(s)
index d580e634023377f989e74c2777ab42076959eb9a..9f7ca1dfcbb1b3544911724b27623d422b3e1035 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@ Core and Builtins
 Library
 -------
 
+- Fix repr(_socket.socket) on Windows 64-bit: don't fail with OverflowError
+  on closed socket. repr(socket.socket) already works fine.
+
 - Issue #16133: The asynchat.async_chat.handle_read() method now ignores
   BlockingIOError exceptions.
 
index 7c08f8faa3a9c2958239ecfa981e13060523a406..b37e7935ffa72e7a650831b178f99d1855c6c8f4 100644 (file)
@@ -3868,8 +3868,13 @@ sock_dealloc(PySocketSockObject *s)
 static PyObject *
 sock_repr(PySocketSockObject *s)
 {
+    long sock_fd;
+    /* On Windows, this test is needed because SOCKET_T is unsigned */
+    if (s->sock_fd == INVALID_SOCKET) {
+        sock_fd = -1;
+    }
 #if SIZEOF_SOCKET_T > SIZEOF_LONG
-    if (s->sock_fd > LONG_MAX) {
+    else if (s->sock_fd > LONG_MAX) {
         /* this can occur on Win64, and actually there is a special
            ugly printf formatter for decimal pointer length integer
            printing, only bother if necessary*/
@@ -3879,9 +3884,11 @@ sock_repr(PySocketSockObject *s)
         return NULL;
     }
 #endif
+    else
+        sock_fd = (long)s->sock_fd;
     return PyUnicode_FromFormat(
         "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
-        (long)s->sock_fd, s->sock_family,
+        sock_fd, s->sock_family,
         s->sock_type,
         s->sock_proto);
 }