efficient vim config
[dotfiles/.git] / .local / lib / python2.7 / site-packages / trollius / py33_exceptions.py
1 __all__ = ['BlockingIOError', 'BrokenPipeError', 'ChildProcessError',
2            'ConnectionRefusedError', 'ConnectionResetError',
3            'InterruptedError', 'ConnectionAbortedError', 'PermissionError',
4            'FileNotFoundError', 'ProcessLookupError',
5            ]
6
7 import errno
8 import select
9 import socket
10 import sys
11 try:
12     import ssl
13 except ImportError:
14     ssl = None
15
16 from .compat import PY33
17
18 if PY33:
19     import builtins
20     BlockingIOError = builtins.BlockingIOError
21     BrokenPipeError = builtins.BrokenPipeError
22     ChildProcessError = builtins.ChildProcessError
23     ConnectionRefusedError = builtins.ConnectionRefusedError
24     ConnectionResetError = builtins.ConnectionResetError
25     InterruptedError = builtins.InterruptedError
26     ConnectionAbortedError = builtins.ConnectionAbortedError
27     PermissionError = builtins.PermissionError
28     FileNotFoundError = builtins.FileNotFoundError
29     ProcessLookupError = builtins.ProcessLookupError
30
31 else:
32     # Python < 3.3
33     class BlockingIOError(OSError):
34         pass
35
36     class BrokenPipeError(OSError):
37         pass
38
39     class ChildProcessError(OSError):
40         pass
41
42     class ConnectionRefusedError(OSError):
43         pass
44
45     class InterruptedError(OSError):
46         pass
47
48     class ConnectionResetError(OSError):
49         pass
50
51     class ConnectionAbortedError(OSError):
52         pass
53
54     class PermissionError(OSError):
55         pass
56
57     class FileNotFoundError(OSError):
58         pass
59
60     class ProcessLookupError(OSError):
61         pass
62
63
64 _MAP_ERRNO = {
65     errno.EACCES: PermissionError,
66     errno.EAGAIN: BlockingIOError,
67     errno.EALREADY: BlockingIOError,
68     errno.ECHILD: ChildProcessError,
69     errno.ECONNABORTED: ConnectionAbortedError,
70     errno.ECONNREFUSED: ConnectionRefusedError,
71     errno.ECONNRESET: ConnectionResetError,
72     errno.EINPROGRESS: BlockingIOError,
73     errno.EINTR: InterruptedError,
74     errno.ENOENT: FileNotFoundError,
75     errno.EPERM: PermissionError,
76     errno.EPIPE: BrokenPipeError,
77     errno.ESHUTDOWN: BrokenPipeError,
78     errno.EWOULDBLOCK: BlockingIOError,
79     errno.ESRCH: ProcessLookupError,
80 }
81
82 if sys.platform == 'win32':
83     from trollius import _overlapped
84     _MAP_ERRNO.update({
85         _overlapped.ERROR_CONNECTION_REFUSED: ConnectionRefusedError,
86         _overlapped.ERROR_CONNECTION_ABORTED: ConnectionAbortedError,
87         _overlapped.ERROR_NETNAME_DELETED: ConnectionResetError,
88     })
89
90
91 def get_error_class(key, default):
92     return _MAP_ERRNO.get(key, default)
93
94
95 if sys.version_info >= (3,):
96     def reraise(tp, value, tb=None):
97         if value.__traceback__ is not tb:
98             raise value.with_traceback(tb)
99         raise value
100 else:
101     exec("""def reraise(tp, value, tb=None):
102     raise tp, value, tb
103 """)
104
105
106 def _wrap_error(exc, mapping, key):
107     if key not in mapping:
108         return
109     new_err_cls = mapping[key]
110     new_err = new_err_cls(*exc.args)
111
112     # raise a new exception with the original traceback
113     if hasattr(exc, '__traceback__'):
114         traceback = exc.__traceback__
115     else:
116         traceback = sys.exc_info()[2]
117     reraise(new_err_cls, new_err, traceback)
118
119
120 if not PY33:
121     def wrap_error(func, *args, **kw):
122         """
123         Wrap socket.error, IOError, OSError, select.error to raise new specialized
124         exceptions of Python 3.3 like InterruptedError (PEP 3151).
125         """
126         try:
127             return func(*args, **kw)
128         except (socket.error, IOError, OSError) as exc:
129             if ssl is not None and isinstance(exc, ssl.SSLError):
130                 raise
131             if hasattr(exc, 'winerror'):
132                 _wrap_error(exc, _MAP_ERRNO, exc.winerror)
133                 # _MAP_ERRNO does not contain all Windows errors.
134                 # For some errors like "file not found", exc.errno should
135                 # be used (ex: ENOENT).
136             _wrap_error(exc, _MAP_ERRNO, exc.errno)
137             raise
138         except select.error as exc:
139             if exc.args:
140                 _wrap_error(exc, _MAP_ERRNO, exc.args[0])
141             raise
142 else:
143     def wrap_error(func, *args, **kw):
144         return func(*args, **kw)