[pypy-commit] pypy resource_warning: switch sockets to the register_finalizer approach

cfbolz pypy.commits at gmail.com
Fri Aug 5 05:17:41 EDT 2016


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: resource_warning
Changeset: r86026:63fff351448e
Date: 2016-08-05 11:17 +0200
http://bitbucket.org/pypy/pypy/changeset/63fff351448e/

Log:	switch sockets to the register_finalizer approach

	this is nice because it means that if resource_warnings are
	disabled, no overhead is imposed at all

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
@@ -159,16 +159,11 @@
         register_socket(space, sock)
         if self.space.sys.track_resources:
             self.w_tb = self.space.format_traceback()
+            self.register_finalizer(space)
 
-    def __del__(self):
+    def _finalize_(self):
         is_open = self.sock.fd >= 0
         if is_open and self.space.sys.track_resources:
-            self.enqueue_for_destruction(self.space, W_Socket.destructor,
-                                         '__del__ method of ')
-
-    def destructor(self):
-        assert isinstance(self, W_Socket)
-        if self.space.sys.track_resources:
             w_repr = self.space.repr(self)
             str_repr = self.space.str_w(w_repr)
             w_msg = self.space.wrap("WARNING: unclosed " + str_repr)
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
@@ -455,11 +455,11 @@
           File ".*", line .*, in fn
         """, msg)
         #
-        # check with track_resources enabled in the destructor BUT with a
-        # file which was created when track_resources was disabled
+        # track_resources is enabled after the construction of the socket. in
+        # this case, the socket is not registered for finalization at all, so
+        # we don't see a message
         msg = fn(False, True)
-        assert self.regex_search("WARNING: unclosed <socket object, .*>", msg)
-        assert "Created at" not in msg
+        assert msg == ''
 
 
     def test_socket_close_error(self):
diff --git a/pypy/objspace/fake/objspace.py b/pypy/objspace/fake/objspace.py
--- a/pypy/objspace/fake/objspace.py
+++ b/pypy/objspace/fake/objspace.py
@@ -428,4 +428,5 @@
 FakeObjSpace.sys.filesystemencoding = 'foobar'
 FakeObjSpace.sys.defaultencoding = 'ascii'
 FakeObjSpace.sys.dlopenflags = 123
+FakeObjSpace.sys.track_resources = False
 FakeObjSpace.builtin = FakeModule()


More information about the pypy-commit mailing list