[pypy-commit] pypy py3k: the 'exceptions' module is gone in Python3, and exceptions are now directly in

antocuni noreply at buildbot.pypy.org
Fri Jul 13 00:50:15 CEST 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k
Changeset: r56047:aad1226fa707
Date: 2012-07-13 00:36 +0200
http://bitbucket.org/pypy/pypy/changeset/aad1226fa707/

Log:	the 'exceptions' module is gone in Python3, and exceptions are now
	directly in builtins. However, in PyPy we cannot simply move them
	to builtins, because they are needed in the early bootstrap of the
	space, before builtins is initialized. So, we keep them in a
	separate module (renamed to '__exceptions__' because it's an
	internal implementation detail only) but we pretend that its
	__module__ is 'builtins'.

	This approach has the extra bonus that it minimizes the divergence
	from default.

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -503,7 +503,7 @@
         "NOT_RPYTHON: only for initializing the space."
 
         from pypy.module.exceptions import Module
-        w_name = self.wrap('exceptions')
+        w_name = self.wrap('__exceptions__')
         self.exceptions_module = Module(self, w_name)
         self.exceptions_module.install()
 
diff --git a/pypy/module/exceptions/__init__.py b/pypy/module/exceptions/__init__.py
--- a/pypy/module/exceptions/__init__.py
+++ b/pypy/module/exceptions/__init__.py
@@ -2,6 +2,7 @@
 from pypy.interpreter.mixedmodule import MixedModule
 
 class Module(MixedModule):
+    applevel_name = '__exceptions__'
     appleveldefs = {}
     
     interpleveldefs = {
diff --git a/pypy/module/exceptions/interp_exceptions.py b/pypy/module/exceptions/interp_exceptions.py
--- a/pypy/module/exceptions/interp_exceptions.py
+++ b/pypy/module/exceptions/interp_exceptions.py
@@ -246,7 +246,7 @@
 W_BaseException.typedef = TypeDef(
     'BaseException',
     __doc__ = W_BaseException.__doc__,
-    __module__ = 'exceptions',
+    __module__ = 'builtins',
     __new__ = _new(W_BaseException),
     __init__ = interp2app(W_BaseException.descr_init),
     __str__ = interp2app(W_BaseException.descr_str),
@@ -291,7 +291,7 @@
         name,
         base.typedef,
         __doc__ = W_Exc.__doc__,
-        __module__ = 'exceptions',
+        __module__ = 'builtins',
         **kwargs
     )
     W_Exc.typedef.applevel_subclasses_base = realbase
@@ -359,7 +359,7 @@
     'UnicodeTranslateError',
     W_UnicodeError.typedef,
     __doc__ = W_UnicodeTranslateError.__doc__,
-    __module__ = 'exceptions',
+    __module__ = 'builtins',
     __new__ = _new(W_UnicodeTranslateError),
     __init__ = interp2app(W_UnicodeTranslateError.descr_init),
     __str__ = interp2app(W_UnicodeTranslateError.descr_str),
@@ -443,7 +443,7 @@
     'EnvironmentError',
     W_StandardError.typedef,
     __doc__ = W_EnvironmentError.__doc__,
-    __module__ = 'exceptions',
+    __module__ = 'builtins',
     __new__ = _new(W_EnvironmentError),
     __reduce__ = interp2app(W_EnvironmentError.descr_reduce),
     __init__ = interp2app(W_EnvironmentError.descr_init),
@@ -497,7 +497,7 @@
     "WindowsError",
     W_OSError.typedef,
     __doc__  = W_WindowsError.__doc__,
-    __module__ = 'exceptions',
+    __module__ = 'builtins',
     __new__  = _new(W_WindowsError),
     __init__ = interp2app(W_WindowsError.descr_init),
     __str__  = interp2app(W_WindowsError.descr_str),
@@ -608,7 +608,7 @@
     __str__ = interp2app(W_SyntaxError.descr_str),
     __repr__ = interp2app(W_SyntaxError.descr_repr),
     __doc__ = W_SyntaxError.__doc__,
-    __module__ = 'exceptions',
+    __module__ = 'builtins',
     msg      = readwrite_attrproperty_w('w_msg', W_SyntaxError),
     filename = readwrite_attrproperty_w('w_filename', W_SyntaxError),
     lineno   = readwrite_attrproperty_w('w_lineno', W_SyntaxError),
@@ -642,7 +642,7 @@
     __new__ = _new(W_SystemExit),
     __init__ = interp2app(W_SystemExit.descr_init),
     __doc__ = W_SystemExit.__doc__,
-    __module__ = 'exceptions',
+    __module__ = 'builtins',
     code    = readwrite_attrproperty_w('w_code', W_SystemExit)
 )
 
@@ -705,7 +705,7 @@
     'UnicodeDecodeError',
     W_UnicodeError.typedef,
     __doc__ = W_UnicodeDecodeError.__doc__,
-    __module__ = 'exceptions',
+    __module__ = 'builtins',
     __new__ = _new(W_UnicodeDecodeError),
     __init__ = interp2app(W_UnicodeDecodeError.descr_init),
     __str__ = interp2app(W_UnicodeDecodeError.descr_str),
@@ -800,7 +800,7 @@
     'UnicodeEncodeError',
     W_UnicodeError.typedef,
     __doc__ = W_UnicodeEncodeError.__doc__,
-    __module__ = 'exceptions',
+    __module__ = 'builtins',
     __new__ = _new(W_UnicodeEncodeError),
     __init__ = interp2app(W_UnicodeEncodeError.descr_init),
     __str__ = interp2app(W_UnicodeEncodeError.descr_str),
diff --git a/pypy/module/exceptions/test/test_exc.py b/pypy/module/exceptions/test/test_exc.py
--- a/pypy/module/exceptions/test/test_exc.py
+++ b/pypy/module/exceptions/test/test_exc.py
@@ -6,8 +6,6 @@
         cls.space = gettestobjspace(usemodules=('exceptions',))
 
     def test_baseexc(self):
-        from exceptions import BaseException
-
         assert str(BaseException()) == ''
         assert repr(BaseException()) == 'BaseException()'
         assert BaseException().message == ''
@@ -35,7 +33,6 @@
         assert not hasattr(x, "message")
 
     def test_kwargs(self):
-        from exceptions import Exception
         class X(Exception):
             def __init__(self, x=3):
                 self.x = x
@@ -44,8 +41,6 @@
         assert x.x == 8
 
     def test_exc(self):
-        from exceptions import Exception, BaseException
-
         assert issubclass(Exception, BaseException)
         assert isinstance(Exception(), Exception)
         assert isinstance(Exception(), BaseException)
@@ -55,8 +50,6 @@
         assert str(IOError(1, 2)) == "[Errno 1] 2"
 
     def test_custom_class(self):
-        from exceptions import Exception, BaseException, LookupError
-
         class MyException(Exception):
             def __init__(self, x):
                 self.x = x
@@ -70,7 +63,6 @@
         assert str(MyException("x")) == "x"
 
     def test_unicode_translate_error(self):
-        from exceptions import UnicodeTranslateError
         ut = UnicodeTranslateError("x", 1, 5, "bah")
         assert ut.object == 'x'
         assert ut.start == 1
@@ -88,12 +80,9 @@
         assert ut.object == []
 
     def test_key_error(self):
-        from exceptions import KeyError
-
         assert str(KeyError('s')) == "'s'"
 
     def test_environment_error(self):
-        from exceptions import EnvironmentError
         ee = EnvironmentError(3, "x", "y")
         assert str(ee) == "[Errno 3] x: 'y'"
         assert str(EnvironmentError(3, "x")) == "[Errno 3] x"
@@ -104,8 +93,8 @@
 
     def test_windows_error(self):
         try:
-            from exceptions import WindowsError
-        except ImportError:
+            WindowsError
+        except NameError:
             skip('WindowsError not present')
         ee = WindowsError(3, "x", "y")
         assert str(ee) == "[Error 3] x: y"
@@ -115,7 +104,6 @@
         assert str(WindowsError(3, "x")) == "[Error 3] x"
 
     def test_syntax_error(self):
-        from exceptions import SyntaxError
         s = SyntaxError()
         assert s.msg is None
         s = SyntaxError(3)
@@ -128,13 +116,11 @@
         assert str(SyntaxError("msg", ("file.py", 2, 3, 4))) == "msg (file.py, line 2)"
 
     def test_system_exit(self):
-        from exceptions import SystemExit
         assert SystemExit().code is None
         assert SystemExit("x").code == "x"
         assert SystemExit(1, 2).code == (1, 2)
 
     def test_unicode_decode_error(self):
-        from exceptions import UnicodeDecodeError
         ud = UnicodeDecodeError("x", b"y", 1, 5, "bah")
         assert ud.encoding == 'x'
         assert ud.object == b'y'
@@ -150,7 +136,6 @@
         assert str(ud) == "'x' codec can't decode byte 0x39 in position 1: bah"
 
     def test_unicode_encode_error(self):
-        from exceptions import UnicodeEncodeError
         ue = UnicodeEncodeError("x", "y", 1, 5, "bah")
         assert ue.encoding == 'x'
         assert ue.object == 'y'
@@ -169,7 +154,6 @@
         raises(TypeError, UnicodeEncodeError, "x", b"y", 1, 5, "bah")
 
     def test_multiple_inheritance(self):
-        from exceptions import LookupError, ValueError, Exception, IOError
         class A(LookupError, ValueError):
             pass
         assert issubclass(A, A)
@@ -184,7 +168,6 @@
         assert isinstance(a, ValueError)
         assert not isinstance(a, KeyError)
 
-        from exceptions import UnicodeDecodeError, UnicodeEncodeError
         try:
             class B(UnicodeTranslateError, UnicodeEncodeError):
                 pass
@@ -205,16 +188,14 @@
         assert not isinstance(c, KeyError)
 
     def test_doc_and_module(self):
-        import exceptions
-        for name, e in exceptions.__dict__.items():
-            if isinstance(e, type) and issubclass(e, exceptions.BaseException):
+        import builtins
+        for name, e in builtins.__dict__.items():
+            if isinstance(e, type) and issubclass(e, BaseException):
                 assert e.__doc__, e
-                assert e.__module__ == 'exceptions', e
+                assert e.__module__ == 'builtins', e
         assert 'run-time' in RuntimeError.__doc__
 
     def test_reduce(self):
-        from exceptions import LookupError, EnvironmentError
-
         le = LookupError(1, 2, "a")
         assert le.__reduce__() == (LookupError, (1, 2, "a"))
         le.xyz = (1, 2)
@@ -223,8 +204,6 @@
         assert ee.__reduce__() == (EnvironmentError, (1, 2, "a"))
 
     def test_setstate(self):
-        from exceptions import FutureWarning
-
         fw = FutureWarning()
         fw.__setstate__({"xyz": (1, 2)})
         assert fw.xyz == (1, 2)


More information about the pypy-commit mailing list