1use crate::{builtins::PyModule, PyRef, VirtualMachine};
2
3#[pymodule]
4mod errno {}
5
6pub fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
7 let module = errno::make_module(vm);
8 let errorcode = vm.ctx.new_dict();
9 extend_module!(vm, &module, {
10 "errorcode" => errorcode.clone(),
11 });
12 for (name, code) in ERROR_CODES {
13 let name = vm.ctx.intern_str(*name);
14 let code = vm.new_pyobj(*code);
15 errorcode
16 .set_item(&*code, name.to_owned().into(), vm)
17 .unwrap();
18 module.set_attr(name, code, vm).unwrap();
19 }
20 module
21}
22
23#[cfg(any(unix, windows, target_os = "wasi"))]
24pub mod errors {
25 pub use libc::*;
26 #[cfg(windows)]
27 pub use windows_sys::Win32::{
28 Foundation::*,
29 Networking::WinSock::{
30 WSABASEERR, WSADESCRIPTION_LEN, WSAEACCES, WSAEADDRINUSE, WSAEADDRNOTAVAIL,
31 WSAEAFNOSUPPORT, WSAEALREADY, WSAEBADF, WSAECANCELLED, WSAECONNABORTED,
32 WSAECONNREFUSED, WSAECONNRESET, WSAEDESTADDRREQ, WSAEDISCON, WSAEDQUOT, WSAEFAULT,
33 WSAEHOSTDOWN, WSAEHOSTUNREACH, WSAEINPROGRESS, WSAEINTR, WSAEINVAL,
34 WSAEINVALIDPROCTABLE, WSAEINVALIDPROVIDER, WSAEISCONN, WSAELOOP, WSAEMFILE,
35 WSAEMSGSIZE, WSAENAMETOOLONG, WSAENETDOWN, WSAENETRESET, WSAENETUNREACH, WSAENOBUFS,
36 WSAENOMORE, WSAENOPROTOOPT, WSAENOTCONN, WSAENOTEMPTY, WSAENOTSOCK, WSAEOPNOTSUPP,
37 WSAEPFNOSUPPORT, WSAEPROCLIM, WSAEPROTONOSUPPORT, WSAEPROTOTYPE,
38 WSAEPROVIDERFAILEDINIT, WSAEREFUSED, WSAEREMOTE, WSAESHUTDOWN, WSAESOCKTNOSUPPORT,
39 WSAESTALE, WSAETIMEDOUT, WSAETOOMANYREFS, WSAEUSERS, WSAEWOULDBLOCK, WSAHOST_NOT_FOUND,
40 WSAID_ACCEPTEX, WSAID_CONNECTEX, WSAID_DISCONNECTEX, WSAID_GETACCEPTEXSOCKADDRS,
41 WSAID_TRANSMITFILE, WSAID_TRANSMITPACKETS, WSAID_WSAPOLL, WSAID_WSARECVMSG,
42 WSANOTINITIALISED, WSANO_DATA, WSANO_RECOVERY, WSAPROTOCOL_LEN, WSASERVICE_NOT_FOUND,
43 WSASYSCALLFAILURE, WSASYSNOTREADY, WSASYS_STATUS_LEN, WSATRY_AGAIN, WSATYPE_NOT_FOUND,
44 WSAVERNOTSUPPORTED,
45 },
46 };
47 #[cfg(windows)]
48 macro_rules! reexport_wsa {
49 ($($errname:ident),*$(,)?) => {
50 paste::paste! {
51 $(pub const $errname: i32 = windows_sys::Win32::Networking::WinSock:: [<WSA $errname>] as i32;)*
52 }
53 }
54 }
55 #[cfg(windows)]
56 reexport_wsa! {
57 EADDRINUSE, EADDRNOTAVAIL, EAFNOSUPPORT, EALREADY, ECONNABORTED, ECONNREFUSED, ECONNRESET,
58 EDESTADDRREQ, EDQUOT, EHOSTDOWN, EHOSTUNREACH, EINPROGRESS, EISCONN, ELOOP, EMSGSIZE,
59 ENETDOWN, ENETRESET, ENETUNREACH, ENOBUFS, ENOPROTOOPT, ENOTCONN, ENOTSOCK, EOPNOTSUPP,
60 EPFNOSUPPORT, EPROTONOSUPPORT, EPROTOTYPE, EREMOTE, ESHUTDOWN, ESOCKTNOSUPPORT, ESTALE,
61 ETIMEDOUT, ETOOMANYREFS, EUSERS, EWOULDBLOCK,
62 }
64 #[cfg(windows)]
65 pub const WSAHOS: i32 = WSAHOST_NOT_FOUND;
66}
67
68#[cfg(any(unix, windows, target_os = "wasi"))]
69macro_rules! e {
70 ($name:ident) => {
71 (stringify!($name), errors::$name as _)
72 };
73 (cfg($($cfg:tt)*), $name:ident) => {
74 #[cfg($($cfg)*)]
75 (stringify!($name), errors::$name as _)
76 };
77}
78
79#[cfg(any(unix, windows, target_os = "wasi"))]
80const ERROR_CODES: &[(&str, i32)] = &[
81 e!(ENODEV),
82 e!(
83 cfg(any(
84 target_os = "android",
85 target_os = "fuchsia",
86 target_os = "linux",
87 target_os = "redox"
88 )),
89 ENOCSI
90 ),
91 e!(EHOSTUNREACH),
92 e!(cfg(not(windows)), ENOMSG),
93 e!(
94 cfg(any(
95 target_os = "android",
96 target_os = "fuchsia",
97 target_os = "linux",
98 target_os = "redox"
99 )),
100 EUCLEAN
101 ),
102 e!(
103 cfg(any(
104 target_os = "android",
105 target_os = "fuchsia",
106 target_os = "linux",
107 target_os = "redox"
108 )),
109 EL2NSYNC
110 ),
111 e!(
112 cfg(any(
113 target_os = "android",
114 target_os = "fuchsia",
115 target_os = "linux",
116 target_os = "redox"
117 )),
118 EL2HLT
119 ),
120 e!(
121 cfg(not(any(
122 target_os = "openbsd",
123 target_os = "freebsd",
124 target_os = "wasi",
125 windows
126 ))),
127 ENODATA
128 ),
129 e!(cfg(not(any(windows, target_os = "wasi"))), ENOTBLK),
130 e!(ENOSYS),
131 e!(EPIPE),
132 e!(EINVAL),
133 e!(cfg(not(windows)), EOVERFLOW),
134 e!(
135 cfg(any(
136 target_os = "android",
137 target_os = "fuchsia",
138 target_os = "linux",
139 target_os = "redox"
140 )),
141 EADV
142 ),
143 e!(EINTR),
144 e!(cfg(not(target_os = "wasi")), EUSERS),
145 e!(ENOTEMPTY),
146 e!(ENOBUFS),
147 e!(cfg(not(windows)), EPROTO),
148 e!(cfg(not(target_os = "wasi")), EREMOTE),
149 e!(
150 cfg(any(
151 target_os = "android",
152 target_os = "fuchsia",
153 target_os = "linux",
154 target_os = "redox"
155 )),
156 ENAVAIL
157 ),
158 e!(ECHILD),
159 e!(ELOOP),
160 e!(EXDEV),
161 e!(E2BIG),
162 e!(ESRCH),
163 e!(EMSGSIZE),
164 e!(EAFNOSUPPORT),
165 e!(
166 cfg(any(
167 target_os = "android",
168 target_os = "fuchsia",
169 target_os = "linux",
170 target_os = "redox"
171 )),
172 EBADR
173 ),
174 e!(cfg(not(target_os = "wasi")), EHOSTDOWN),
175 e!(cfg(not(target_os = "wasi")), EPFNOSUPPORT),
176 e!(ENOPROTOOPT),
177 e!(EBUSY),
178 e!(EWOULDBLOCK),
179 e!(
180 cfg(any(
181 target_os = "android",
182 target_os = "fuchsia",
183 target_os = "linux",
184 target_os = "redox"
185 )),
186 EBADFD
187 ),
188 e!(
189 cfg(any(
190 target_os = "android",
191 target_os = "fuchsia",
192 target_os = "linux",
193 target_os = "redox"
194 )),
195 EDOTDOT
196 ),
197 e!(EISCONN),
198 e!(
199 cfg(any(
200 target_os = "android",
201 target_os = "fuchsia",
202 target_os = "linux",
203 target_os = "redox"
204 )),
205 ENOANO
206 ),
207 e!(cfg(not(target_os = "wasi")), ESHUTDOWN),
208 e!(
209 cfg(any(
210 target_os = "android",
211 target_os = "fuchsia",
212 target_os = "linux",
213 target_os = "redox"
214 )),
215 ECHRNG
216 ),
217 e!(
218 cfg(any(
219 target_os = "android",
220 target_os = "fuchsia",
221 target_os = "linux",
222 target_os = "redox"
223 )),
224 ELIBBAD
225 ),
226 e!(
227 cfg(any(
228 target_os = "android",
229 target_os = "fuchsia",
230 target_os = "linux",
231 target_os = "redox"
232 )),
233 ENONET
234 ),
235 e!(
236 cfg(any(
237 target_os = "android",
238 target_os = "fuchsia",
239 target_os = "linux",
240 target_os = "redox"
241 )),
242 EBADE
243 ),
244 e!(EBADF),
245 e!(cfg(not(any(target_os = "openbsd", windows))), EMULTIHOP),
246 e!(EIO),
247 e!(
248 cfg(any(
249 target_os = "android",
250 target_os = "fuchsia",
251 target_os = "linux",
252 target_os = "redox"
253 )),
254 EUNATCH
255 ),
256 e!(EPROTOTYPE),
257 e!(ENOSPC),
258 e!(ENOEXEC),
259 e!(EALREADY),
260 e!(ENETDOWN),
261 e!(
262 cfg(any(
263 target_os = "android",
264 target_os = "fuchsia",
265 target_os = "linux",
266 target_os = "redox"
267 )),
268 ENOTNAM
269 ),
270 e!(EACCES),
271 e!(
272 cfg(any(
273 target_os = "android",
274 target_os = "fuchsia",
275 target_os = "linux",
276 target_os = "redox"
277 )),
278 ELNRNG
279 ),
280 e!(EILSEQ),
281 e!(ENOTDIR),
282 e!(
283 cfg(any(
284 target_os = "android",
285 target_os = "fuchsia",
286 target_os = "linux",
287 target_os = "redox"
288 )),
289 ENOTUNIQ
290 ),
291 e!(EPERM),
292 e!(EDOM),
293 e!(
294 cfg(any(
295 target_os = "android",
296 target_os = "fuchsia",
297 target_os = "linux",
298 target_os = "redox"
299 )),
300 EXFULL
301 ),
302 e!(ECONNREFUSED),
303 e!(EISDIR),
304 e!(EPROTONOSUPPORT),
305 e!(EROFS),
306 e!(EADDRNOTAVAIL),
307 e!(cfg(not(windows)), EIDRM),
308 e!(
309 cfg(any(
310 target_os = "android",
311 target_os = "fuchsia",
312 target_os = "linux",
313 target_os = "redox"
314 )),
315 ECOMM
316 ),
317 e!(
318 cfg(any(
319 target_os = "android",
320 target_os = "fuchsia",
321 target_os = "linux",
322 target_os = "redox"
323 )),
324 ESRMNT
325 ),
326 e!(
327 cfg(any(
328 target_os = "android",
329 target_os = "fuchsia",
330 target_os = "linux",
331 target_os = "redox"
332 )),
333 EREMOTEIO
334 ),
335 e!(
336 cfg(any(
337 target_os = "android",
338 target_os = "fuchsia",
339 target_os = "linux",
340 target_os = "redox"
341 )),
342 EL3RST
343 ),
344 e!(cfg(not(windows)), EBADMSG),
345 e!(ENFILE),
346 e!(
347 cfg(any(
348 target_os = "android",
349 target_os = "fuchsia",
350 target_os = "linux",
351 target_os = "redox"
352 )),
353 ELIBMAX
354 ),
355 e!(ESPIPE),
356 e!(cfg(not(any(target_os = "openbsd", windows))), ENOLINK),
357 e!(ENETRESET),
358 e!(ETIMEDOUT),
359 e!(ENOENT),
360 e!(EEXIST),
361 e!(EDQUOT),
362 e!(
363 cfg(not(any(
364 target_os = "openbsd",
365 target_os = "freebsd",
366 target_os = "wasi",
367 windows
368 ))),
369 ENOSTR
370 ),
371 e!(
372 cfg(any(
373 target_os = "android",
374 target_os = "fuchsia",
375 target_os = "linux",
376 target_os = "redox"
377 )),
378 EBADSLT
379 ),
380 e!(
381 cfg(any(
382 target_os = "android",
383 target_os = "fuchsia",
384 target_os = "linux",
385 target_os = "redox"
386 )),
387 EBADRQC
388 ),
389 e!(
390 cfg(any(
391 target_os = "android",
392 target_os = "fuchsia",
393 target_os = "linux",
394 target_os = "redox"
395 )),
396 ELIBACC
397 ),
398 e!(EFAULT),
399 e!(EFBIG),
400 e!(EDEADLK),
401 e!(ENOTCONN),
402 e!(EDESTADDRREQ),
403 e!(
404 cfg(any(
405 target_os = "android",
406 target_os = "fuchsia",
407 target_os = "linux",
408 target_os = "redox"
409 )),
410 ELIBSCN
411 ),
412 e!(ENOLCK),
413 e!(
414 cfg(any(
415 target_os = "android",
416 target_os = "fuchsia",
417 target_os = "linux",
418 target_os = "redox"
419 )),
420 EISNAM
421 ),
422 e!(ECONNABORTED),
423 e!(ENETUNREACH),
424 e!(ESTALE),
425 e!(
426 cfg(not(any(
427 target_os = "openbsd",
428 target_os = "freebsd",
429 target_os = "wasi",
430 windows
431 ))),
432 ENOSR
433 ),
434 e!(ENOMEM),
435 e!(ENOTSOCK),
436 e!(
437 cfg(any(
438 target_os = "android",
439 target_os = "fuchsia",
440 target_os = "linux",
441 target_os = "redox"
442 )),
443 ESTRPIPE
444 ),
445 e!(EMLINK),
446 e!(ERANGE),
447 e!(
448 cfg(any(
449 target_os = "android",
450 target_os = "fuchsia",
451 target_os = "linux",
452 target_os = "redox"
453 )),
454 ELIBEXEC
455 ),
456 e!(
457 cfg(any(
458 target_os = "android",
459 target_os = "fuchsia",
460 target_os = "linux",
461 target_os = "redox"
462 )),
463 EL3HLT
464 ),
465 e!(ECONNRESET),
466 e!(EADDRINUSE),
467 e!(EOPNOTSUPP),
468 e!(
469 cfg(any(
470 target_os = "android",
471 target_os = "fuchsia",
472 target_os = "linux",
473 target_os = "redox"
474 )),
475 EREMCHG
476 ),
477 e!(EAGAIN),
478 e!(ENAMETOOLONG),
479 e!(ENOTTY),
480 e!(
481 cfg(any(
482 target_os = "android",
483 target_os = "fuchsia",
484 target_os = "linux",
485 target_os = "redox"
486 )),
487 ERESTART
488 ),
489 e!(cfg(not(target_os = "wasi")), ESOCKTNOSUPPORT),
490 e!(
491 cfg(not(any(
492 target_os = "openbsd",
493 target_os = "freebsd",
494 target_os = "wasi",
495 windows
496 ))),
497 ETIME
498 ),
499 e!(
500 cfg(any(
501 target_os = "android",
502 target_os = "fuchsia",
503 target_os = "linux",
504 target_os = "redox"
505 )),
506 EBFONT
507 ),
508 e!(
509 cfg(any(
510 target_os = "fuchsia",
511 target_os = "linux",
512 target_os = "redox",
513 windows
514 )),
515 EDEADLOCK
516 ),
517 e!(cfg(not(target_os = "wasi")), ETOOMANYREFS),
518 e!(EMFILE),
519 e!(cfg(not(windows)), ETXTBSY),
520 e!(EINPROGRESS),
521 e!(ENXIO),
522 e!(
523 cfg(any(
524 target_os = "android",
525 target_os = "fuchsia",
526 target_os = "linux",
527 target_os = "redox"
528 )),
529 ENOPKG
530 ),
531 e!(cfg(windows), WSAEHOSTDOWN),
533 e!(cfg(windows), WSAENETDOWN),
534 e!(cfg(windows), WSAENOTSOCK),
535 e!(cfg(windows), WSAEHOSTUNREACH),
536 e!(cfg(windows), WSAELOOP),
537 e!(cfg(windows), WSAEMFILE),
538 e!(cfg(windows), WSAESTALE),
539 e!(cfg(windows), WSAVERNOTSUPPORTED),
540 e!(cfg(windows), WSAENETUNREACH),
541 e!(cfg(windows), WSAEPROCLIM),
542 e!(cfg(windows), WSAEFAULT),
543 e!(cfg(windows), WSANOTINITIALISED),
544 e!(cfg(windows), WSAEUSERS),
545 e!(cfg(windows), WSAENOPROTOOPT),
547 e!(cfg(windows), WSAECONNABORTED),
548 e!(cfg(windows), WSAENAMETOOLONG),
549 e!(cfg(windows), WSAENOTEMPTY),
550 e!(cfg(windows), WSAESHUTDOWN),
551 e!(cfg(windows), WSAEAFNOSUPPORT),
552 e!(cfg(windows), WSAETOOMANYREFS),
553 e!(cfg(windows), WSAEACCES),
554 e!(cfg(windows), WSABASEERR),
556 e!(cfg(windows), WSAEMSGSIZE),
558 e!(cfg(windows), WSAEBADF),
559 e!(cfg(windows), WSAECONNRESET),
560 e!(cfg(windows), WSAETIMEDOUT),
562 e!(cfg(windows), WSAENOBUFS),
563 e!(cfg(windows), WSAEDISCON),
564 e!(cfg(windows), WSAEINTR),
565 e!(cfg(windows), WSAEPROTOTYPE),
566 e!(cfg(windows), WSAHOS),
567 e!(cfg(windows), WSAEADDRINUSE),
568 e!(cfg(windows), WSAEADDRNOTAVAIL),
569 e!(cfg(windows), WSAEALREADY),
570 e!(cfg(windows), WSAEPROTONOSUPPORT),
571 e!(cfg(windows), WSASYSNOTREADY),
572 e!(cfg(windows), WSAEWOULDBLOCK),
573 e!(cfg(windows), WSAEPFNOSUPPORT),
574 e!(cfg(windows), WSAEOPNOTSUPP),
575 e!(cfg(windows), WSAEISCONN),
576 e!(cfg(windows), WSAEDQUOT),
577 e!(cfg(windows), WSAENOTCONN),
578 e!(cfg(windows), WSAEREMOTE),
579 e!(cfg(windows), WSAEINVAL),
580 e!(cfg(windows), WSAEINPROGRESS),
581 e!(cfg(windows), WSAESOCKTNOSUPPORT),
583 e!(cfg(windows), WSAEDESTADDRREQ),
587 e!(cfg(windows), WSAECONNREFUSED),
588 e!(cfg(windows), WSAENETRESET),
589 e!(
591 cfg(any(
592 target_os = "android",
593 target_os = "dragonfly",
594 target_os = "fuchsia",
595 target_os = "linux",
596 target_os = "openbsd",
597 target_os = "redox"
598 )),
599 ENOMEDIUM
600 ),
601 e!(
602 cfg(any(
603 target_os = "android",
604 target_os = "fuchsia",
605 target_os = "linux",
606 target_os = "openbsd",
607 target_os = "redox"
608 )),
609 EMEDIUMTYPE
610 ),
611 e!(ECANCELED),
612 e!(
613 cfg(any(
614 target_os = "android",
615 target_os = "fuchsia",
616 target_os = "linux",
617 target_os = "redox"
618 )),
619 ENOKEY
620 ),
621 e!(
622 cfg(any(
623 target_os = "android",
624 target_os = "fuchsia",
625 target_os = "linux",
626 target_os = "redox"
627 )),
628 EKEYEXPIRED
629 ),
630 e!(
631 cfg(any(
632 target_os = "android",
633 target_os = "fuchsia",
634 target_os = "linux",
635 target_os = "redox"
636 )),
637 EKEYREVOKED
638 ),
639 e!(
640 cfg(any(
641 target_os = "android",
642 target_os = "fuchsia",
643 target_os = "linux",
644 target_os = "redox"
645 )),
646 EKEYREJECTED
647 ),
648 e!(cfg(not(any(windows, target_os = "netbsd"))), EOWNERDEAD),
649 e!(
650 cfg(not(any(windows, target_os = "netbsd"))),
651 ENOTRECOVERABLE
652 ),
653 e!(
654 cfg(any(target_os = "fuchsia", target_os = "linux")),
655 ERFKILL
656 ),
657 e!(cfg(not(target_os = "redox")), ENOTSUP),
659 e!(
660 cfg(any(target_os = "illumos", target_os = "solaris")),
661 ELOCKUNMAPPED
662 ),
663 e!(
664 cfg(any(target_os = "illumos", target_os = "solaris")),
665 ENOTACTIVE
666 ),
667 e!(
669 cfg(any(
670 target_os = "dragonfly",
671 target_os = "freebsd",
672 target_os = "netbsd",
673 target_os = "openbsd",
674 target_vendor = "apple"
675 )),
676 EAUTH
677 ),
678 e!(cfg(target_vendor = "apple"), EBADARCH),
679 e!(cfg(target_vendor = "apple"), EBADEXEC),
680 e!(cfg(target_vendor = "apple"), EBADMACHO),
681 e!(
682 cfg(any(
683 target_os = "dragonfly",
684 target_os = "freebsd",
685 target_os = "netbsd",
686 target_os = "openbsd",
687 target_vendor = "apple"
688 )),
689 EBADRPC
690 ),
691 e!(cfg(target_vendor = "apple"), EDEVERR),
692 e!(
693 cfg(any(
694 target_os = "dragonfly",
695 target_os = "freebsd",
696 target_os = "netbsd",
697 target_os = "openbsd",
698 target_vendor = "apple"
699 )),
700 EFTYPE
701 ),
702 e!(
703 cfg(any(
704 target_os = "dragonfly",
705 target_os = "freebsd",
706 target_os = "netbsd",
707 target_os = "openbsd",
708 target_vendor = "apple"
709 )),
710 ENEEDAUTH
711 ),
712 e!(
713 cfg(any(
714 target_os = "dragonfly",
715 target_os = "freebsd",
716 target_os = "netbsd",
717 target_os = "openbsd",
718 target_vendor = "apple"
719 )),
720 ENOATTR
721 ),
722 e!(cfg(target_vendor = "apple"), ENOPOLICY),
723 e!(
724 cfg(any(
725 target_os = "dragonfly",
726 target_os = "freebsd",
727 target_os = "netbsd",
728 target_os = "openbsd",
729 target_vendor = "apple"
730 )),
731 EPROCLIM
732 ),
733 e!(
734 cfg(any(
735 target_os = "dragonfly",
736 target_os = "freebsd",
737 target_os = "netbsd",
738 target_os = "openbsd",
739 target_vendor = "apple"
740 )),
741 EPROCUNAVAIL
742 ),
743 e!(
744 cfg(any(
745 target_os = "dragonfly",
746 target_os = "freebsd",
747 target_os = "netbsd",
748 target_os = "openbsd",
749 target_vendor = "apple"
750 )),
751 EPROGMISMATCH
752 ),
753 e!(
754 cfg(any(
755 target_os = "dragonfly",
756 target_os = "freebsd",
757 target_os = "netbsd",
758 target_os = "openbsd",
759 target_vendor = "apple"
760 )),
761 EPROGUNAVAIL
762 ),
763 e!(cfg(target_vendor = "apple"), EPWROFF),
764 e!(
765 cfg(any(
766 target_os = "dragonfly",
767 target_os = "freebsd",
768 target_os = "netbsd",
769 target_os = "openbsd",
770 target_vendor = "apple"
771 )),
772 ERPCMISMATCH
773 ),
774 e!(cfg(target_vendor = "apple"), ESHLIBVERS),
775];
776
777#[cfg(not(any(unix, windows, target_os = "wasi")))]
778const ERROR_CODES: &[(&str, i32)] = &[];