forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
146 lines (128 loc) · 4.5 KB
/
error.rs
File metadata and controls
146 lines (128 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// SSL exception types shared between ssl (rustls) and openssl backends
pub(crate) use ssl_error::*;
#[pymodule(sub)]
pub(crate) mod ssl_error {
use crate::vm::{
Py, PyPayload, PyRef, PyResult, VirtualMachine,
builtins::{PyBaseException, PyOSError, PyStrRef},
types::Constructor,
};
// Error type constants - exposed as pyattr and available for internal use
#[pyattr]
pub(crate) const SSL_ERROR_NONE: i32 = 0;
#[pyattr]
pub(crate) const SSL_ERROR_SSL: i32 = 1;
#[pyattr]
pub(crate) const SSL_ERROR_WANT_READ: i32 = 2;
#[pyattr]
pub(crate) const SSL_ERROR_WANT_WRITE: i32 = 3;
#[pyattr]
pub(crate) const SSL_ERROR_WANT_X509_LOOKUP: i32 = 4;
#[pyattr]
pub(crate) const SSL_ERROR_SYSCALL: i32 = 5;
#[pyattr]
pub(crate) const SSL_ERROR_ZERO_RETURN: i32 = 6;
#[pyattr]
pub(crate) const SSL_ERROR_WANT_CONNECT: i32 = 7;
#[pyattr]
pub(crate) const SSL_ERROR_EOF: i32 = 8;
#[pyattr]
pub(crate) const SSL_ERROR_INVALID_ERROR_CODE: i32 = 10;
#[pyattr]
#[pyexception(name = "SSLError", base = PyOSError)]
#[derive(Debug)]
#[repr(transparent)]
pub struct PySSLError(PyOSError);
#[pyexception]
impl PySSLError {
// Returns strerror attribute if available, otherwise str(args)
#[pymethod]
fn __str__(exc: &Py<PyBaseException>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
use crate::vm::AsObject;
// Try to get strerror attribute first (OSError compatibility)
if let Ok(strerror) = exc.as_object().get_attr("strerror", vm)
&& !vm.is_none(&strerror)
{
return strerror.str(vm);
}
// Otherwise return str(args)
let args = exc.args();
if args.len() == 1 {
args.as_slice()[0].str(vm)
} else {
args.as_object().str(vm)
}
}
}
#[pyattr]
#[pyexception(name = "SSLZeroReturnError", base = PySSLError)]
#[derive(Debug)]
#[repr(transparent)]
pub struct PySSLZeroReturnError(PySSLError);
#[pyexception]
impl PySSLZeroReturnError {}
#[pyattr]
#[pyexception(name = "SSLWantReadError", base = PySSLError, impl)]
#[derive(Debug)]
#[repr(transparent)]
pub struct PySSLWantReadError(PySSLError);
#[pyattr]
#[pyexception(name = "SSLWantWriteError", base = PySSLError, impl)]
#[derive(Debug)]
#[repr(transparent)]
pub struct PySSLWantWriteError(PySSLError);
#[pyattr]
#[pyexception(name = "SSLSyscallError", base = PySSLError, impl)]
#[derive(Debug)]
#[repr(transparent)]
pub struct PySSLSyscallError(PySSLError);
#[pyattr]
#[pyexception(name = "SSLEOFError", base = PySSLError, impl)]
#[derive(Debug)]
#[repr(transparent)]
pub struct PySSLEOFError(PySSLError);
#[pyattr]
#[pyexception(name = "SSLCertVerificationError", base = PySSLError, impl)]
#[derive(Debug)]
#[repr(transparent)]
pub struct PySSLCertVerificationError(PySSLError);
// Helper functions to create SSL exceptions with proper errno attribute
pub fn create_ssl_want_read_error(vm: &VirtualMachine) -> PyRef<PyOSError> {
vm.new_os_subtype_error(
PySSLWantReadError::class(&vm.ctx).to_owned(),
Some(SSL_ERROR_WANT_READ),
"The operation did not complete (read)",
)
}
pub fn create_ssl_want_write_error(vm: &VirtualMachine) -> PyRef<PyOSError> {
vm.new_os_subtype_error(
PySSLWantWriteError::class(&vm.ctx).to_owned(),
Some(SSL_ERROR_WANT_WRITE),
"The operation did not complete (write)",
)
}
pub fn create_ssl_eof_error(vm: &VirtualMachine) -> PyRef<PyOSError> {
vm.new_os_subtype_error(
PySSLEOFError::class(&vm.ctx).to_owned(),
Some(SSL_ERROR_EOF),
"EOF occurred in violation of protocol",
)
}
pub fn create_ssl_zero_return_error(vm: &VirtualMachine) -> PyRef<PyOSError> {
vm.new_os_subtype_error(
PySSLZeroReturnError::class(&vm.ctx).to_owned(),
Some(SSL_ERROR_ZERO_RETURN),
"TLS/SSL connection has been closed (EOF)",
)
}
pub fn create_ssl_syscall_error(
vm: &VirtualMachine,
msg: impl Into<String>,
) -> PyRef<PyOSError> {
vm.new_os_subtype_error(
PySSLSyscallError::class(&vm.ctx).to_owned(),
Some(SSL_ERROR_SYSCALL),
msg.into(),
)
}
}