[pypy-svn] pypy interplevel-exception-classes: New function: error.new_exception_class(space, name, w_base)

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


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: interplevel-exception-classes
Changeset: r42123:c9d190210332
Date: 2011-02-17 23:15 +0100
http://bitbucket.org/pypy/pypy/changeset/c9d190210332/

Log:	New function: error.new_exception_class(space, name, w_base)

diff --git a/pypy/interpreter/test/test_error.py b/pypy/interpreter/test/test_error.py
--- a/pypy/interpreter/test/test_error.py
+++ b/pypy/interpreter/test/test_error.py
@@ -1,7 +1,7 @@
 import py, os, errno
 from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.interpreter.error import decompose_valuefmt, get_operrcls2
-from pypy.interpreter.error import wrap_oserror
+from pypy.interpreter.error import wrap_oserror, new_exception_class
 
 
 def test_decompose_valuefmt():
@@ -74,3 +74,26 @@
     assert e.get_w_value(space) == ([SystemError], [errno.EBADF],
                                     [os.strerror(errno.EBADF)],
                                     ["test.py"])
+
+def test_new_exception(space):
+    w_error = new_exception_class(space, 'error', module="_socket")
+    assert w_error.getname(space) == 'error'
+    assert space.str_w(space.repr(w_error)) == "<class '_socket.error'>"
+    operr = OperationError(w_error, space.wrap("message"))
+    assert operr.match(space, w_error)
+    assert operr.match(space, space.w_Exception)
+
+    # subclass of ValueError
+    w_error = new_exception_class(space, 'error', space.w_ValueError)
+    operr = OperationError(w_error, space.wrap("message"))
+    assert operr.match(space, w_error)
+    assert operr.match(space, space.w_ValueError)
+
+    # subclass of (ValueError, TypeError)
+    w_bases = space.newtuple([space.w_ValueError, space.w_TypeError])
+    w_error = new_exception_class(space, 'error', w_bases)
+    operr = OperationError(w_error, space.wrap("message"))
+    assert operr.match(space, w_error)
+    assert operr.match(space, space.w_ValueError)
+    assert operr.match(space, space.w_TypeError)
+

diff --git a/pypy/interpreter/error.py b/pypy/interpreter/error.py
--- a/pypy/interpreter/error.py
+++ b/pypy/interpreter/error.py
@@ -408,3 +408,23 @@
     msg = os.strerror(errno)
     w_error = space.call_function(w_type, space.wrap(errno), space.wrap(msg))
     return OperationError(w_type, w_error)
+
+def new_exception_class(space, name, w_bases=None, module=None, w_dict=None):
+    """Create a new exception type.
+    @param name: the name of the type.
+    @param w_bases: Either an exception type, or a wrapped tuple of
+                    exception types.  default is space.w_Exception.
+    @param module: optional module name.
+    @param w_dict: an optional dictionary to populate the class __dict__.
+    """
+    if w_bases is None:
+        w_bases = space.newtuple([space.w_Exception])
+    elif not space.isinstance_w(w_bases, space.w_tuple):
+        w_bases = space.newtuple([w_bases])
+    if w_dict is None:
+        w_dict = space.newdict()
+    w_exc = space.call_function(
+        space.w_type, space.wrap(name), w_bases, w_dict)
+    if module is not None:
+        space.setattr(w_exc, space.wrap("__module__"), space.wrap(module))
+    return w_exc


More information about the Pypy-commit mailing list