Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r61483:bcbd82aac4ba
Date: 2013-02-19 14:49 -0800
http://bitbucket.org/pypy/pypy/changeset/bcbd82aac4ba/
Log: add a dealloc warning to sockets
diff --git a/pypy/module/_socket/interp_socket.py
b/pypy/module/_socket/interp_socket.py
--- a/pypy/module/_socket/interp_socket.py
+++ b/pypy/module/_socket/interp_socket.py
@@ -136,6 +136,10 @@
class W_RSocket(Wrappable, RSocket):
+
+ # for _dealloc_warn
+ space = None
+
def descr_new(space, w_subtype, __args__):
sock = space.allocate_instance(W_RSocket, w_subtype)
return space.wrap(sock)
@@ -150,9 +154,23 @@
fd=space.c_filedescriptor_w(w_fileno))
else:
W_RSocket.__init__(self, family, type, proto)
+ self.space = space
except SocketError, e:
raise converted_error(space, e)
+ def _dealloc_warn(self):
+ space = self.space
+ if not space:
+ return
+ try:
+ msg = (u"unclosed %s" %
+ space.unicode_w(space.repr(space.wrap(self))))
+ space.warn(space.wrap(msg), space.w_ResourceWarning)
+ except OperationError as e:
+ # Spurious errors can appear at shutdown
+ if e.match(space, space.w_Warning):
+ e.write_unraisable(space, '', space.wrap(self))
+
def _accept_w(self, space):
"""_accept() -> (socket object, address info)
diff --git a/pypy/module/_socket/test/test_sock_app.py
b/pypy/module/_socket/test/test_sock_app.py
--- a/pypy/module/_socket/test/test_sock_app.py
+++ b/pypy/module/_socket/test/test_sock_app.py
@@ -554,6 +554,20 @@
socket.socket.__init__(self, family=socket.AF_INET6)
assert Socket_IPV6().family == socket.AF_INET6
+ def test_dealloc_warn(self):
+ import _socket
+ import gc
+ import warnings
+
+ s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
+ r = repr(s)
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ s = None
+ gc.collect()
+ assert len(w) == 1
+ assert r in str(w[0])
+
class AppTestSocketTCP:
def setup_class(cls):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit