[pypy-svn] pypy interplevel-exception-classes: Try a slightly different interface, class name can be "module.name"

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


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

Log:	Try a slightly different interface, class name can be "module.name"

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
@@ -76,7 +76,7 @@
                                     ["test.py"])
 
 def test_new_exception(space):
-    w_error = new_exception_class(space, 'error', module="_socket")
+    w_error = new_exception_class(space, '_socket.error')
     assert w_error.getname(space) == 'error'
     assert space.str_w(space.repr(w_error)) == "<class '_socket.error'>"
     operr = OperationError(w_error, space.wrap("message"))

diff --git a/pypy/interpreter/error.py b/pypy/interpreter/error.py
--- a/pypy/interpreter/error.py
+++ b/pypy/interpreter/error.py
@@ -409,14 +409,17 @@
     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):
+def new_exception_class(space, name, w_bases=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 '.' in name:
+        module, name = name.rsplit('.', 1)
+    else:
+        module = None
     if w_bases is None:
         w_bases = space.newtuple([space.w_Exception])
     elif not space.isinstance_w(w_bases, space.w_tuple):
@@ -425,6 +428,6 @@
         w_dict = space.newdict()
     w_exc = space.call_function(
         space.w_type, space.wrap(name), w_bases, w_dict)
-    if module is not None:
+    if module:
         space.setattr(w_exc, space.wrap("__module__"), space.wrap(module))
     return w_exc


More information about the Pypy-commit mailing list