[pypy-svn] pypy interplevel-exception-classes: Implement _socket._error and _ssl.SSLError at interp_level.

amauryfa commits-noreply at bitbucket.org
Fri Feb 18 00:48:08 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: interplevel-exception-classes
Changeset: r42125:2b0d0b8bb55e
Date: 2011-02-18 00:48 +0100
http://bitbucket.org/pypy/pypy/changeset/2b0d0b8bb55e/

Log:	Implement _socket._error and _ssl.SSLError at interp_level. The
	app_* files are no longer needed,

	Flowgraphs of the the _ssl module don't need to build the _socket
	Module anymore. Now let's try a translation.

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
@@ -4,10 +4,12 @@
 from pypy.interpreter.gateway import ObjSpace, W_Root, NoneNotWrapped
 from pypy.interpreter.gateway import interp2app
 from pypy.rlib.rarithmetic import intmask
+from pypy.rlib.objectmodel import specialize
 from pypy.rlib import rsocket
 from pypy.rlib.rsocket import RSocket, AF_INET, SOCK_STREAM
 from pypy.rlib.rsocket import SocketError, SocketErrorWithErrno
 from pypy.interpreter.error import OperationError, operationerrfmt
+from pypy.interpreter.error import new_exception_class
 from pypy.interpreter import gateway
 
 class SignalChecker:
@@ -451,10 +453,27 @@
 # ____________________________________________________________
 # Error handling
 
+class SocketAPI:
+    def __init__(self, space):
+        self.w_error = new_exception_class(
+            space, "_socket.error", space.w_IOError)
+        self.w_herror = new_exception_class(
+            space, "_socket.herror", self.w_error)
+        self.w_gaierror = new_exception_class(
+            space, "_socket.gaierror", self.w_error)
+        self.w_timeout = new_exception_class(
+            space, "_socket.timeout", self.w_error)
+
+    @specialize.memo()
+    def get_exception(self, applevelerrcls):
+        return getattr(self, 'w_' + applevelerrcls)
+
+def get_error(space, name):
+    return space.fromcache(SocketAPI).get_exception(name)
+
 def converted_error(space, e):
     message = e.get_msg()
-    w_module = space.getbuiltinmodule('_socket')
-    w_exception_class = space.getattr(w_module, space.wrap(e.applevelerrcls))
+    w_exception_class = get_error(space, e.applevelerrcls)
     if isinstance(e, SocketErrorWithErrno):
         w_exception = space.call_function(w_exception_class, space.wrap(e.errno),
                                       space.wrap(message))

diff --git a/pypy/module/_ssl/app_ssl.py b/pypy/module/_ssl/app_ssl.py
deleted file mode 100644
--- a/pypy/module/_ssl/app_ssl.py
+++ /dev/null
@@ -1,7 +0,0 @@
-import _socket
-
-class SSLError(_socket.error):
-    pass
-
-__doc__ = """Implementation module for SSL socket operations. 
-See the socket module for documentation."""

diff --git a/pypy/module/_ssl/interp_ssl.py b/pypy/module/_ssl/interp_ssl.py
--- a/pypy/module/_ssl/interp_ssl.py
+++ b/pypy/module/_ssl/interp_ssl.py
@@ -1,5 +1,5 @@
 from pypy.rpython.lltypesystem import rffi, lltype
-from pypy.interpreter.error import OperationError
+from pypy.interpreter.error import OperationError, new_exception_class
 from pypy.interpreter.baseobjspace import W_Root, ObjSpace, Wrappable
 from pypy.interpreter.typedef import TypeDef
 from pypy.interpreter.gateway import interp2app
@@ -562,3 +562,11 @@
 sslwrap.unwrap_spec = [ObjSpace, W_Root, int, W_Root, W_Root,
                        int, int, W_Root, W_Root]
 
+class Cache:
+    def __init__(self, space):
+        w_socketerror = interp_socket.get_error(space, "error")
+        self.w_error = new_exception_class(
+            space, "_ssl.SSLError", w_socketerror)
+
+def get_error(space):
+    return space.fromcache(Cache).w_error

diff --git a/pypy/module/_socket/app_socket.py b/pypy/module/_socket/app_socket.py
deleted file mode 100644
--- a/pypy/module/_socket/app_socket.py
+++ /dev/null
@@ -1,15 +0,0 @@
-"""Implementation module for socket operations.
-
-See the socket module for documentation."""
-
-class error(IOError):
-    pass
-
-class herror(error):
-    pass
-
-class gaierror(error):
-    pass
-
-class timeout(error):
-    pass

diff --git a/pypy/module/_ssl/__init__.py b/pypy/module/_ssl/__init__.py
--- a/pypy/module/_ssl/__init__.py
+++ b/pypy/module/_ssl/__init__.py
@@ -1,13 +1,15 @@
 from pypy.interpreter.mixedmodule import MixedModule
 
 class Module(MixedModule):
+    """Implementation module for SSL socket operations.
+    See the socket module for documentation."""
+
     interpleveldefs = {
         'sslwrap': 'interp_ssl.sslwrap',
+        'SSLError': 'interp_ssl.get_error(space)',
     }
 
     appleveldefs = {
-        '__doc__': 'app_ssl.__doc__',
-        'SSLError': 'app_ssl.SSLError',
     }
 
     @classmethod

diff --git a/pypy/module/_socket/__init__.py b/pypy/module/_socket/__init__.py
--- a/pypy/module/_socket/__init__.py
+++ b/pypy/module/_socket/__init__.py
@@ -5,15 +5,15 @@
 class Module(MixedModule):
 
     appleveldefs = {
-        'error'      : 'app_socket.error',
-        'herror'     : 'app_socket.herror',
-        'gaierror'   : 'app_socket.gaierror',
-        'timeout'    : 'app_socket.timeout',
     }
 
     interpleveldefs = {
         'SocketType':  'interp_socket.W_RSocket',
         'socket'    :  'interp_socket.W_RSocket',
+        'error'     :  'interp_socket.get_error(space, "error")',
+        'herror'    :  'interp_socket.get_error(space, "herror")',
+        'gaierror'  :  'interp_socket.get_error(space, "gaierror")',
+        'timeout'   :  'interp_socket.get_error(space, "timeout")',
     }
 
     def startup(self, space):

diff --git a/pypy/module/_ssl/test/test_ssl.py b/pypy/module/_ssl/test/test_ssl.py
--- a/pypy/module/_ssl/test/test_ssl.py
+++ b/pypy/module/_ssl/test/test_ssl.py
@@ -11,8 +11,10 @@
         import _ssl
     
     def test_sslerror(self):
-        import _ssl
+        import _ssl, _socket
         assert issubclass(_ssl.SSLError, Exception)
+        assert issubclass(_ssl.SSLError, IOError)
+        assert issubclass(_ssl.SSLError, _socket.error)
 
     def test_constants(self):
         import _ssl


More information about the Pypy-commit mailing list