Skip to content

Commit 07f4427

Browse files
committed
Share ssl error codes
1 parent 98b966a commit 07f4427

File tree

3 files changed

+23
-42
lines changed

3 files changed

+23
-42
lines changed

crates/stdlib/src/openssl.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,6 @@ mod _ssl {
248248
#[pyattr]
249249
const VERIFY_DEFAULT: u32 = 0;
250250
#[pyattr]
251-
const SSL_ERROR_EOF: u32 = 8; // custom for python
252-
#[pyattr]
253251
const HAS_SNI: bool = true;
254252
#[pyattr]
255253
const HAS_ECDH: bool = true;
@@ -3391,15 +3389,8 @@ mod _ssl {
33913389
Some(io_err) => return io_err.to_pyexception(vm),
33923390
// When no I/O error and OpenSSL error queue is empty,
33933391
// this is an EOF in violation of protocol -> SSLEOFError
3394-
// Need to set args[0] = SSL_ERROR_EOF for suppress_ragged_eofs check
33953392
None => {
3396-
return vm
3397-
.new_os_subtype_error(
3398-
PySSLEOFError::class(&vm.ctx).to_owned(),
3399-
Some(SSL_ERROR_EOF as i32),
3400-
"EOF occurred in violation of protocol",
3401-
)
3402-
.upcast();
3393+
return create_ssl_eof_error(vm).upcast();
34033394
}
34043395
},
34053396
ssl::ErrorCode::SSL => {
@@ -3412,13 +3403,7 @@ mod _ssl {
34123403
let reason = sys::ERR_GET_REASON(err_code);
34133404
let lib = sys::ERR_GET_LIB(err_code);
34143405
if lib == ERR_LIB_SSL && reason == SSL_R_UNEXPECTED_EOF_WHILE_READING {
3415-
return vm
3416-
.new_os_subtype_error(
3417-
PySSLEOFError::class(&vm.ctx).to_owned(),
3418-
Some(SSL_ERROR_EOF as i32),
3419-
"EOF occurred in violation of protocol",
3420-
)
3421-
.upcast();
3406+
return create_ssl_eof_error(vm).upcast();
34223407
}
34233408
}
34243409
return convert_openssl_error(vm, ssl_err.clone());

crates/stdlib/src/ssl.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -207,28 +207,6 @@ mod _ssl {
207207
#[pyattr]
208208
const OP_ALL: i32 = 0x00000BFB; // Combined "safe" options (reduced for i32, excluding OP_LEGACY_SERVER_CONNECT for OpenSSL 3.0.0+ compatibility)
209209

210-
// Error types
211-
#[pyattr]
212-
const SSL_ERROR_NONE: i32 = 0;
213-
#[pyattr]
214-
const SSL_ERROR_SSL: i32 = 1;
215-
#[pyattr]
216-
const SSL_ERROR_WANT_READ: i32 = 2;
217-
#[pyattr]
218-
const SSL_ERROR_WANT_WRITE: i32 = 3;
219-
#[pyattr]
220-
const SSL_ERROR_WANT_X509_LOOKUP: i32 = 4;
221-
#[pyattr]
222-
const SSL_ERROR_SYSCALL: i32 = 5;
223-
#[pyattr]
224-
const SSL_ERROR_ZERO_RETURN: i32 = 6;
225-
#[pyattr]
226-
const SSL_ERROR_WANT_CONNECT: i32 = 7;
227-
#[pyattr]
228-
const SSL_ERROR_EOF: i32 = 8;
229-
#[pyattr]
230-
const SSL_ERROR_INVALID_ERROR_CODE: i32 = 10;
231-
232210
// Alert types (matching _TLSAlertType enum)
233211
#[pyattr]
234212
const ALERT_DESCRIPTION_CLOSE_NOTIFY: i32 = 0;

crates/stdlib/src/ssl/error.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,27 @@ pub(crate) mod ssl_error {
1010
types::Constructor,
1111
};
1212

13-
// Error type constants (needed for create_ssl_want_read_error etc.)
13+
// Error type constants - exposed as pyattr and available for internal use
14+
#[pyattr]
15+
pub(crate) const SSL_ERROR_NONE: i32 = 0;
16+
#[pyattr]
17+
pub(crate) const SSL_ERROR_SSL: i32 = 1;
18+
#[pyattr]
1419
pub(crate) const SSL_ERROR_WANT_READ: i32 = 2;
20+
#[pyattr]
1521
pub(crate) const SSL_ERROR_WANT_WRITE: i32 = 3;
22+
#[pyattr]
23+
pub(crate) const SSL_ERROR_WANT_X509_LOOKUP: i32 = 4;
24+
#[pyattr]
25+
pub(crate) const SSL_ERROR_SYSCALL: i32 = 5;
26+
#[pyattr]
27+
pub(crate) const SSL_ERROR_ZERO_RETURN: i32 = 6;
28+
#[pyattr]
29+
pub(crate) const SSL_ERROR_WANT_CONNECT: i32 = 7;
30+
#[pyattr]
31+
pub(crate) const SSL_ERROR_EOF: i32 = 8;
32+
#[pyattr]
33+
pub(crate) const SSL_ERROR_INVALID_ERROR_CODE: i32 = 10;
1634

1735
#[pyattr]
1836
#[pyexception(name = "SSLError", base = PyOSError)]
@@ -102,15 +120,15 @@ pub(crate) mod ssl_error {
102120
pub fn create_ssl_eof_error(vm: &VirtualMachine) -> PyRef<PyOSError> {
103121
vm.new_os_subtype_error(
104122
PySSLEOFError::class(&vm.ctx).to_owned(),
105-
None,
123+
Some(SSL_ERROR_EOF),
106124
"EOF occurred in violation of protocol",
107125
)
108126
}
109127

110128
pub fn create_ssl_zero_return_error(vm: &VirtualMachine) -> PyRef<PyOSError> {
111129
vm.new_os_subtype_error(
112130
PySSLZeroReturnError::class(&vm.ctx).to_owned(),
113-
None,
131+
Some(SSL_ERROR_ZERO_RETURN),
114132
"TLS/SSL connection has been closed (EOF)",
115133
)
116134
}

0 commit comments

Comments
 (0)