[Python-checkins] cpython (merge 3.4 -> default): (Merge 3.4) Fix repr(_socket.socket) on Windows 64-bit: don't fail with
victor.stinner
python-checkins at python.org
Sat Jul 26 14:54:07 CEST 2014
http://hg.python.org/cpython/rev/68ab2fd81336
changeset: 91879:68ab2fd81336
parent: 91876:253c31930b32
parent: 91878:2a4a3d3c47a8
user: Victor Stinner <victor.stinner at gmail.com>
date: Sat Jul 26 14:37:57 2014 +0200
summary:
(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.
files:
Lib/test/test_socket.py | 18 ++++++++++++++++++
Misc/NEWS | 3 +++
Modules/socketmodule.c | 11 +++++++++--
3 files changed, 30 insertions(+), 2 deletions(-)
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
@@ -40,6 +40,11 @@
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."""
@@ -660,6 +665,19 @@
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)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -108,6 +108,9 @@
Library
-------
+- Fix repr(_socket.socket) on Windows 64-bit: don't fail with OverflowError
+ on closed socket. repr(socket.socket) already works fine.
+
- Issue #22033: Reprs of most Python implemened classes now contain actual
class name instead of hardcoded one.
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -3869,8 +3869,13 @@
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*/
@@ -3880,9 +3885,11 @@
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);
}
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list