[pypy-commit] pypy py3k: Allow sockets subclasses to override __init__ only.

amauryfa noreply at buildbot.pypy.org
Sun Dec 4 20:59:16 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r50147:138070ed1361
Date: 2011-12-03 22:44 +0100
http://bitbucket.org/pypy/pypy/changeset/138070ed1361/

Log:	Allow sockets subclasses to override __init__ only. ssl.py relies on
	this.

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
@@ -16,6 +16,22 @@
         self.space.getexecutioncontext().checksignals()
 
 class W_RSocket(Wrappable, RSocket):
+    def descr_new(space, w_subtype, __args__):
+        sock = space.allocate_instance(W_RSocket, w_subtype)
+        return space.wrap(sock)
+
+    @unwrap_spec(family=int, type=int, proto=int)
+    def descr_init(self, space, family=AF_INET, type=SOCK_STREAM, proto=0,
+                   w_fileno=None):
+        try:
+            if not space.is_w(w_fileno, space.w_None):
+                W_RSocket.__init__(self, family, type, proto,
+                                   fd=space.c_filedescriptor_w(w_fileno))
+            else:
+                W_RSocket.__init__(self, family, type, proto)
+        except SocketError, e:
+            raise converted_error(space, e)
+
     def _accept_w(self, space):
         """_accept() -> (socket object, address info)
 
@@ -409,21 +425,6 @@
     return os.fdopen(newfd, mode, buffersize)
 ''', filename =__file__).interphook('makefile')
 
- at unwrap_spec(family=int, type=int, proto=int)
-def newsocket(space, w_subtype, family=AF_INET,
-              type=SOCK_STREAM, proto=0, w_fileno=NoneNotWrapped):
-    sock = space.allocate_instance(W_RSocket, w_subtype)
-    try:
-        if w_fileno:
-            W_RSocket.__init__(sock, family, type, proto,
-                               fd=space.c_filedescriptor_w(w_fileno))
-        else:
-            W_RSocket.__init__(sock, family, type, proto)
-    except SocketError, e:
-        raise converted_error(space, e)
-    return space.wrap(sock)
-descr_socket_new = interp2app(newsocket)
-
 # ____________________________________________________________
 # Error handling
 
@@ -518,7 +519,8 @@
 shutdown(how) -- shut down traffic in one or both directions
 
  [*] not available on all platforms!""",
-    __new__ = descr_socket_new,
+    __new__ = interp2app(W_RSocket.descr_new.im_func),
+    __init__ = interp2app(W_RSocket.descr_init),
     type = interp_attrproperty('type', W_RSocket),
     proto = interp_attrproperty('proto', W_RSocket),
     family = interp_attrproperty('family', W_RSocket),
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
@@ -550,6 +550,14 @@
         clientsock.close()
         s.close()
 
+    def test_subclass(self):
+        # Socket is not created in __new__, but in __init__.
+        import socket
+        class Socket_IPV6(socket.socket):
+            def __init__(self):
+                socket.socket.__init__(self, family=socket.AF_INET6)
+        assert Socket_IPV6().family == socket.AF_INET6
+
 
 class AppTestSocketTCP:
     def setup_class(cls):


More information about the pypy-commit mailing list