changeset: 92862:d5688a94a56c branch: 2.7 user: Benjamin Peterson date: Mon Oct 06 14:38:20 2014 -0400 files: Lib/test/test_socket.py Modules/socketmodule.c Modules/socketmodule.h description: make _socket.socket weakrefable (closes #22569) Patch from Alex Gaynor. diff -r 59fe0ff02c90 -r d5688a94a56c Lib/test/test_socket.py --- a/Lib/test/test_socket.py Mon Oct 06 18:01:02 2014 +0200 +++ b/Lib/test/test_socket.py Mon Oct 06 14:38:20 2014 -0400 @@ -12,9 +12,9 @@ import os import array import contextlib -from weakref import proxy import signal import math +import weakref try: import _socket except ImportError: @@ -264,7 +264,7 @@ def test_weakref(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - p = proxy(s) + p = weakref.proxy(s) self.assertEqual(p.fileno(), s.fileno()) s.close() s = None @@ -275,6 +275,14 @@ else: self.fail('Socket proxy still exists') + def test_weakref__sock(self): + s = socket.socket()._sock + w = weakref.ref(s) + self.assertIs(w(), s) + del s + test_support.gc_collect() + self.assertIsNone(w()) + def testSocketError(self): # Testing socket module exceptions def raise_error(*args, **kwargs): diff -r 59fe0ff02c90 -r d5688a94a56c Modules/socketmodule.c --- a/Modules/socketmodule.c Mon Oct 06 18:01:02 2014 +0200 +++ b/Modules/socketmodule.c Mon Oct 06 14:38:20 2014 -0400 @@ -3115,6 +3115,8 @@ { if (s->sock_fd != -1) (void) SOCKETCLOSE(s->sock_fd); + if (s->weakreflist != NULL) + PyObject_ClearWeakRefs((PyObject *)s); Py_TYPE(s)->tp_free((PyObject *)s); } @@ -3163,6 +3165,7 @@ ((PySocketSockObject *)new)->sock_fd = -1; ((PySocketSockObject *)new)->sock_timeout = -1.0; ((PySocketSockObject *)new)->errorhandler = &set_error; + ((PySocketSockObject *)new)->weakreflist = NULL; } return new; } @@ -3226,7 +3229,7 @@ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ + offsetof(PySocketSockObject, weakreflist), /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ sock_methods, /* tp_methods */ diff -r 59fe0ff02c90 -r d5688a94a56c Modules/socketmodule.h --- a/Modules/socketmodule.h Mon Oct 06 18:01:02 2014 +0200 +++ b/Modules/socketmodule.h Mon Oct 06 14:38:20 2014 -0400 @@ -132,6 +132,7 @@ sets a Python exception */ double sock_timeout; /* Operation timeout in seconds; 0.0 means non-blocking */ + PyObject *weakreflist; } PySocketSockObject; /* --- C API ----------------------------------------------------*/