xref: /freebsd/contrib/lib9p/pytest/p9err.py (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1#! /usr/bin/env python
2
3"""
4Error number definitions for 9P2000, .u, and .L.
5
6Note that there is no native-to-9P2000 (plain) translation
7table since 9P2000 takes error *strings* rather than error
8*numbers*.
9"""
10
11import errno as _errno
12import lerrno as _lerrno
13import os as _os
14
15_native_to_dotu = {
16    # These are in the "standard" range(1, errno.ERANGE)
17    # but do not map to themselves, so map them here first.
18    _errno.ENOTEMPTY: _errno.EPERM,
19    _errno.EDQUOT: _errno.EPERM,
20    _errno.ENOSYS: _errno.EPERM,
21}
22
23_native_to_dotl = {}
24
25# Add standard errno's.
26for _i in range(1, _errno.ERANGE):
27    _native_to_dotu.setdefault(_i, _i)
28    _native_to_dotl[_i] = _i
29
30# Add linux errno's.  Note that Linux EAGAIN at #11 overrides BSD EDEADLK,
31# but Linux has EDEADLK at #35 which overrides BSD EAGAIN, so it all
32# works out.
33#
34# We just list every BSD error name here, since the hasattr()s do
35# the real work.
36for _i in (
37    'EDEADLK',
38    'EAGAIN',
39    'EINPROGRESS',
40    'EALREADY',
41    'ENOTSOCK',
42    'EDESTADDRREQ',
43    'EMSGSIZE',
44    'EPROTOTYPE',
45    'ENOPROTOOPT',
46    'EPROTONOSUPPORT',
47    'ESOCKTNOSUPPORT',
48    'EOPNOTSUPP',
49    'EPFNOSUPPORT',
50    'EAFNOSUPPORT',
51    'EADDRINUSE',
52    'EADDRNOTAVAIL',
53    'ENETDOWN',
54    'ENETUNREACH',
55    'ENETRESET',
56    'ECONNABORTED',
57    'ECONNRESET',
58    'ENOBUFS',
59    'EISCONN',
60    'ENOTCONN',
61    'ESHUTDOWN',
62    'ETOOMANYREFS',
63    'ETIMEDOUT',
64    'ECONNREFUSED',
65    'ELOOP',
66    'ENAMETOOLONG',
67    'EHOSTDOWN',
68    'EHOSTUNREACH',
69    'ENOTEMPTY',
70    'EPROCLIM',
71    'EUSERS',
72    'EDQUOT',
73    'ESTALE',
74    'EREMOTE',
75    'EBADRPC',
76    'ERPCMISMATCH',
77    'EPROGUNAVAIL',
78    'EPROGMISMATCH',
79    'EPROCUNAVAIL',
80    'ENOLCK',
81    'ENOSYS',
82    'EFTYPE',
83    'EAUTH',
84    'ENEEDAUTH',
85    'EIDRM',
86    'ENOMSG',
87    'EOVERFLOW',
88    'ECANCELED',
89    'EILSEQ',
90    'EDOOFUS',
91    'EBADMSG',
92    'EMULTIHOP',
93    'ENOLINK',
94    'EPROTO',
95    'ENOTCAPABLE',
96    'ECAPMODE',
97    'ENOTRECOVERABLE',
98    'EOWNERDEAD',
99):
100    if hasattr(_errno, _i) and hasattr(_lerrno, _i):
101        _native_to_dotl[getattr(_errno, _i)] = getattr(_lerrno, _i)
102del _i
103
104def to_dotu(errnum):
105    """
106    Translate native errno to 9P2000.u errno.
107
108    >>> import errno
109    >>> to_dotu(errno.EIO)
110    5
111    >>> to_dotu(errno.EDQUOT)
112    1
113    >>> to_dotu(errno.ELOOP)
114    5
115
116    There is a corresponding dotu_strerror() (which is really
117    just os.strerror):
118
119    >>> dotu_strerror(5)
120    'Input/output error'
121
122    """
123    return _native_to_dotu.get(errnum, _errno.EIO) # default to EIO
124
125def to_dotl(errnum):
126    """
127    Translate native errno to 9P2000.L errno.
128
129    >>> import errno
130    >>> to_dotl(errno.ELOOP)
131    40
132
133    There is a corresponding dotl_strerror():
134
135    >>> dotl_strerror(40)
136    'Too many levels of symbolic links'
137    """
138    return _native_to_dotl.get(errnum, _lerrno.ENOTRECOVERABLE)
139
140dotu_strerror = _os.strerror
141
142dotl_strerror = _lerrno.strerror
143
144if __name__ == '__main__':
145    import doctest
146    doctest.testmod()
147