Skip to content

Commit b54219b

Browse files
committed
Runtime: Implemented errno module
For now it only has Linux specific errno values
1 parent 9733c52 commit b54219b

4 files changed

Lines changed: 180 additions & 0 deletions

File tree

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ set(RUNTIME_SOURCE_FILES
115115
runtime/modules/collections/module.cpp
116116
runtime/modules/collections/Deque.cpp
117117
runtime/modules/collections/DefaultDict.cpp
118+
runtime/modules/errno/module.cpp
118119
runtime/modules/itertools/module.cpp
119120
runtime/modules/itertools/Chain.cpp
120121
runtime/modules/itertools/ISlice.cpp

src/runtime/modules/Modules.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace py {
44

55
PyModule *builtins_module(Interpreter &interpreter);
66
PyModule *collections_module();
7+
PyModule *errno_module();
78
PyModule *imp_module();
89
PyModule *io_module();
910
PyModule *marshal_module();

src/runtime/modules/config.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static constexpr std::array builtin_modules{
1717
std::tuple<std::string_view, PyModule *(*)()>{ "itertools", itertools_module },
1818
std::tuple<std::string_view, PyModule *(*)()>{ "_collections", collections_module },
1919
std::tuple<std::string_view, PyModule *(*)()>{ "time", time_module },
20+
std::tuple<std::string_view, PyModule *(*)()>{ "errno", errno_module },
2021
};
2122

2223
inline bool is_builtin(std::string_view name)
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#include "../Modules.hpp"
2+
#include "runtime/PyDict.hpp"
3+
#include <array>
4+
#include <cerrno>
5+
6+
namespace py {
7+
8+
namespace {
9+
static constexpr std::string_view kDoc =
10+
R"(This module makes available standard errno system symbols.
11+
12+
The value of each symbol is the corresponding integer value,
13+
e.g., on most systems, errno.ENOENT equals the integer 2.
14+
15+
The dictionary errno.errorcode maps numeric codes to symbol names,
16+
e.g., errno.errorcode[2] could be the string 'ENOENT'.
17+
18+
Symbols that are not relevant to the underlying system are not defined.
19+
20+
To map error codes to error messages, use the function os.strerror(),
21+
e.g. os.strerror(2) could return 'No such file or directory'.)";
22+
}
23+
24+
// TODO: these errno values are Linux specific
25+
static constexpr std::array kErrnoValues{
26+
std::tuple{ EPERM, "EPERM" },
27+
std::tuple{ ENOENT, "ENOENT" },
28+
std::tuple{ ESRCH, "ESRCH" },
29+
std::tuple{ EINTR, "EINTR" },
30+
std::tuple{ EIO, "EIO" },
31+
std::tuple{ ENXIO, "ENXIO" },
32+
std::tuple{ E2BIG, "E2BIG" },
33+
std::tuple{ ENOEXEC, "ENOEXEC" },
34+
std::tuple{ EBADF, "EBADF" },
35+
std::tuple{ ECHILD, "ECHILD" },
36+
std::tuple{ EAGAIN, "EAGAIN" },
37+
std::tuple{ ENOMEM, "ENOMEM" },
38+
std::tuple{ EACCES, "EACCES" },
39+
std::tuple{ EFAULT, "EFAULT" },
40+
std::tuple{ EBUSY, "EBUSY" },
41+
std::tuple{ EEXIST, "EEXIST" },
42+
std::tuple{ EXDEV, "EXDEV" },
43+
std::tuple{ ENODEV, "ENODEV" },
44+
std::tuple{ ENOTDIR, "ENOTDIR" },
45+
std::tuple{ EISDIR, "EISDIR" },
46+
std::tuple{ EINVAL, "EINVAL" },
47+
std::tuple{ ENFILE, "ENFILE" },
48+
std::tuple{ EMFILE, "EMFILE" },
49+
std::tuple{ ENOTTY, "ENOTTY" },
50+
std::tuple{ ETXTBSY, "ETXTBSY" },
51+
std::tuple{ EFBIG, "EFBIG" },
52+
std::tuple{ ENOSPC, "ENOSPC" },
53+
std::tuple{ ESPIPE, "ESPIPE" },
54+
std::tuple{ EROFS, "EROFS" },
55+
std::tuple{ EMLINK, "EMLINK" },
56+
std::tuple{ EPIPE, "EPIPE" },
57+
std::tuple{ EDOM, "EDOM" },
58+
std::tuple{ ERANGE, "ERANGE" },
59+
std::tuple{ EDEADLK, "EDEADLK" },
60+
std::tuple{ ENAMETOOLONG, "ENAMETOOLONG" },
61+
std::tuple{ ENOLCK, "ENOLCK" },
62+
std::tuple{ ENOSYS, "ENOSYS" },
63+
std::tuple{ ENOTEMPTY, "ENOTEMPTY" },
64+
std::tuple{ ELOOP, "ELOOP" },
65+
std::tuple{ EWOULDBLOCK, "EWOULDBLOCK" },
66+
std::tuple{ ENOMSG, "ENOMSG" },
67+
std::tuple{ EIDRM, "EIDRM" },
68+
std::tuple{ ENOSTR, "ENOSTR" },
69+
std::tuple{ ENODATA, "ENODATA" },
70+
std::tuple{ ETIME, "ETIME" },
71+
std::tuple{ ENOSR, "ENOSR" },
72+
std::tuple{ EREMOTE, "EREMOTE" },
73+
std::tuple{ ENOLINK, "ENOLINK" },
74+
std::tuple{ EPROTO, "EPROTO" },
75+
std::tuple{ EBADMSG, "EBADMSG" },
76+
std::tuple{ EOVERFLOW, "EOVERFLOW" },
77+
std::tuple{ EILSEQ, "EILSEQ" },
78+
std::tuple{ EUSERS, "EUSERS" },
79+
std::tuple{ ENOTSOCK, "ENOTSOCK" },
80+
std::tuple{ EDESTADDRREQ, "EDESTADDRREQ" },
81+
std::tuple{ EMSGSIZE, "EMSGSIZE" },
82+
std::tuple{ EPROTOTYPE, "EPROTOTYPE" },
83+
std::tuple{ ENOPROTOOPT, "ENOPROTOOPT" },
84+
std::tuple{ EPROTONOSUPPORT, "EPROTONOSUPPORT" },
85+
std::tuple{ ESOCKTNOSUPPORT, "ESOCKTNOSUPPORT" },
86+
std::tuple{ ENOTSUP, "ENOTSUP" },
87+
std::tuple{ EOPNOTSUPP, "EOPNOTSUPP" },
88+
std::tuple{ EPFNOSUPPORT, "EPFNOSUPPORT" },
89+
std::tuple{ EAFNOSUPPORT, "EAFNOSUPPORT" },
90+
std::tuple{ EADDRINUSE, "EADDRINUSE" },
91+
std::tuple{ EADDRNOTAVAIL, "EADDRNOTAVAIL" },
92+
std::tuple{ ENETDOWN, "ENETDOWN" },
93+
std::tuple{ ENETUNREACH, "ENETUNREACH" },
94+
std::tuple{ ENETRESET, "ENETRESET" },
95+
std::tuple{ ECONNABORTED, "ECONNABORTED" },
96+
std::tuple{ ECONNRESET, "ECONNRESET" },
97+
std::tuple{ ENOBUFS, "ENOBUFS" },
98+
std::tuple{ EISCONN, "EISCONN" },
99+
std::tuple{ ENOTCONN, "ENOTCONN" },
100+
std::tuple{ ESHUTDOWN, "ESHUTDOWN" },
101+
std::tuple{ ETOOMANYREFS, "ETOOMANYREFS" },
102+
std::tuple{ ETIMEDOUT, "ETIMEDOUT" },
103+
std::tuple{ ECONNREFUSED, "ECONNREFUSED" },
104+
std::tuple{ EHOSTDOWN, "EHOSTDOWN" },
105+
std::tuple{ EHOSTUNREACH, "EHOSTUNREACH" },
106+
std::tuple{ EALREADY, "EALREADY" },
107+
std::tuple{ EINPROGRESS, "EINPROGRESS" },
108+
std::tuple{ ESTALE, "ESTALE" },
109+
std::tuple{ EDQUOT, "EDQUOT" },
110+
std::tuple{ ECANCELED, "ECANCELED" },
111+
std::tuple{ ENOTRECOVERABLE, "ENOTRECOVERABLE" },
112+
std::tuple{ EOWNERDEAD, "EOWNERDEAD" },
113+
std::tuple{ EDEADLOCK, "EDEADLOCK" },
114+
std::tuple{ ECHRNG, "ECHRNG" },
115+
std::tuple{ EL2NSYNC, "EL2NSYNC" },
116+
std::tuple{ EL3HLT, "EL3HLT" },
117+
std::tuple{ EL3RST, "EL3RST" },
118+
std::tuple{ ELNRNG, "ELNRNG" },
119+
std::tuple{ EUNATCH, "EUNATCH" },
120+
std::tuple{ ENOCSI, "ENOCSI" },
121+
std::tuple{ EL2HLT, "EL2HLT" },
122+
std::tuple{ EBADE, "EBADE" },
123+
std::tuple{ EBADR, "EBADR" },
124+
std::tuple{ EXFULL, "EXFULL" },
125+
std::tuple{ ENOANO, "ENOANO" },
126+
std::tuple{ EBADRQC, "EBADRQC" },
127+
std::tuple{ EBADSLT, "EBADSLT" },
128+
std::tuple{ EBFONT, "EBFONT" },
129+
std::tuple{ ENONET, "ENONET" },
130+
std::tuple{ ENOPKG, "ENOPKG" },
131+
std::tuple{ EADV, "EADV" },
132+
std::tuple{ ESRMNT, "ESRMNT" },
133+
std::tuple{ ECOMM, "ECOMM" },
134+
std::tuple{ EDOTDOT, "EDOTDOT" },
135+
std::tuple{ ENOTUNIQ, "ENOTUNIQ" },
136+
std::tuple{ EBADFD, "EBADFD" },
137+
std::tuple{ EREMCHG, "EREMCHG" },
138+
std::tuple{ ELIBACC, "ELIBACC" },
139+
std::tuple{ ELIBBAD, "ELIBBAD" },
140+
std::tuple{ ELIBSCN, "ELIBSCN" },
141+
std::tuple{ ELIBMAX, "ELIBMAX" },
142+
std::tuple{ ELIBEXEC, "ELIBEXEC" },
143+
std::tuple{ ERESTART, "ERESTART" },
144+
std::tuple{ ESTRPIPE, "ESTRPIPE" },
145+
std::tuple{ EUCLEAN, "EUCLEAN" },
146+
std::tuple{ ENOTNAM, "ENOTNAM" },
147+
std::tuple{ ENAVAIL, "ENAVAIL" },
148+
std::tuple{ EISNAM, "EISNAM" },
149+
std::tuple{ EREMOTEIO, "EREMOTEIO" },
150+
std::tuple{ EKEYEXPIRED, "EKEYEXPIRED" },
151+
std::tuple{ EKEYREJECTED, "EKEYREJECTED" },
152+
std::tuple{ EKEYREVOKED, "EKEYREVOKED" },
153+
std::tuple{ EMEDIUMTYPE, "EMEDIUMTYPE" },
154+
std::tuple{ ENOKEY, "ENOKEY" },
155+
std::tuple{ ENOMEDIUM, "ENOMEDIUM" },
156+
std::tuple{ ERFKILL, "ERFKILL" },
157+
};
158+
159+
PyModule *errno_module()
160+
{
161+
auto symbols = PyDict::create().unwrap();
162+
auto name = PyString::create("errno").unwrap();
163+
auto doc = PyString::create(std::string{ kDoc }).unwrap();
164+
165+
auto *module = PyModule::create(symbols, name, doc).unwrap();
166+
167+
PyDict::MapType errorcode;
168+
for (auto [errno_value, errno_name] : kErrnoValues) {
169+
module->add_symbol(
170+
PyString::create(std::string{ errno_name }).unwrap(), Number{ errno_value });
171+
errorcode[Number{ errno_value }] = String{ errno_name };
172+
}
173+
module->add_symbol(PyString::create("errorcode").unwrap(), PyDict::create(errorcode).unwrap());
174+
175+
return module;
176+
}
177+
}// namespace py

0 commit comments

Comments
 (0)