Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 011428e

Browse files
committed
(Merge 3.4) Fix repr(_socket.socket) on Windows 64-bit: don't fail with
OverflowError on closed socket. repr(socket.socket) already works fine.
2 parents af52903 + e254e53 commit 011428e

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

Lib/test/test_socket.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
except ImportError:
4141
thread = None
4242
threading = None
43+
try:
44+
import _socket
45+
except ImportError:
46+
_socket = None
47+
4348

4449
def _have_socket_can():
4550
"""Check whether CAN sockets are supported on this host."""
@@ -660,6 +665,19 @@ def test_repr(self):
660665
self.assertIn('[closed]', repr(s))
661666
self.assertNotIn('laddr', repr(s))
662667

668+
@unittest.skipUnless(_socket is not None, 'need _socket module')
669+
def test_csocket_repr(self):
670+
s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
671+
try:
672+
expected = ('<socket object, fd=%s, family=%s, type=%s, proto=%s>'
673+
% (s.fileno(), s.family, s.type, s.proto))
674+
self.assertEqual(repr(s), expected)
675+
finally:
676+
s.close()
677+
expected = ('<socket object, fd=-1, family=%s, type=%s, proto=%s>'
678+
% (s.family, s.type, s.proto))
679+
self.assertEqual(repr(s), expected)
680+
663681
def test_weakref(self):
664682
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
665683
p = proxy(s)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ Core and Builtins
108108
Library
109109
-------
110110

111+
- Fix repr(_socket.socket) on Windows 64-bit: don't fail with OverflowError
112+
on closed socket. repr(socket.socket) already works fine.
113+
111114
- Issue #22033: Reprs of most Python implemened classes now contain actual
112115
class name instead of hardcoded one.
113116

Modules/socketmodule.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3869,8 +3869,13 @@ sock_dealloc(PySocketSockObject *s)
38693869
static PyObject *
38703870
sock_repr(PySocketSockObject *s)
38713871
{
3872+
long sock_fd;
3873+
/* On Windows, this test is needed because SOCKET_T is unsigned */
3874+
if (s->sock_fd == INVALID_SOCKET) {
3875+
sock_fd = -1;
3876+
}
38723877
#if SIZEOF_SOCKET_T > SIZEOF_LONG
3873-
if (s->sock_fd > LONG_MAX) {
3878+
else if (s->sock_fd > LONG_MAX) {
38743879
/* this can occur on Win64, and actually there is a special
38753880
ugly printf formatter for decimal pointer length integer
38763881
printing, only bother if necessary*/
@@ -3880,9 +3885,11 @@ sock_repr(PySocketSockObject *s)
38803885
return NULL;
38813886
}
38823887
#endif
3888+
else
3889+
sock_fd = (long)s->sock_fd;
38833890
return PyUnicode_FromFormat(
38843891
"<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
3885-
(long)s->sock_fd, s->sock_family,
3892+
sock_fd, s->sock_family,
38863893
s->sock_type,
38873894
s->sock_proto);
38883895
}

0 commit comments

Comments
 (0)