Antonio Cuni avatar Antonio Cuni committed aad1226

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.

Comments (0)

Files changed (4)

pypy/interpreter/baseobjspace.py

         "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()
 

pypy/module/exceptions/__init__.py

 from pypy.interpreter.mixedmodule import MixedModule
 
 class Module(MixedModule):
+    applevel_name = '__exceptions__'
     appleveldefs = {}
     
     interpleveldefs = {

pypy/module/exceptions/interp_exceptions.py

 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),
         name,
         base.typedef,
         __doc__ = W_Exc.__doc__,
-        __module__ = 'exceptions',
+        __module__ = 'builtins',
         **kwargs
     )
     W_Exc.typedef.applevel_subclasses_base = realbase
     '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),
     '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),
     "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),
     __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),
     __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)
 )
 
     '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),
     '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),

pypy/module/exceptions/test/test_exc.py

         cls.space = gettestobjspace(usemodules=('exceptions',))
 
     def test_baseexc(self):
-        from exceptions import BaseException
-
         assert str(BaseException()) == ''
         assert repr(BaseException()) == 'BaseException()'
         assert BaseException().message == ''
         assert not hasattr(x, "message")
 
     def test_kwargs(self):
-        from exceptions import Exception
         class X(Exception):
             def __init__(self, x=3):
                 self.x = x
         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)
         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
         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
         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"
 
     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"
         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)
         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'
         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'
         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)
         assert isinstance(a, ValueError)
         assert not isinstance(a, KeyError)
 
-        from exceptions import UnicodeDecodeError, UnicodeEncodeError
         try:
             class B(UnicodeTranslateError, UnicodeEncodeError):
                 pass
         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)
         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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.