Skip to content

Commit 37d1872

Browse files
committed
SSLError
1 parent fb0d3d9 commit 37d1872

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed

crates/stdlib/src/ssl.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,18 +1254,29 @@ mod _ssl {
12541254
} else {
12551255
// [SSL] PEM lib
12561256
super::compat::SslError::create_ssl_error_with_reason(
1257-
vm, "SSL", "", "PEM lib",
1257+
vm,
1258+
Some("SSL"),
1259+
"",
1260+
"PEM lib",
12581261
)
12591262
}
12601263
}
12611264
// PEM parsing errors - [SSL] PEM lib
12621265
_ => super::compat::SslError::create_ssl_error_with_reason(
1263-
vm, "SSL", "", "PEM lib",
1266+
vm,
1267+
Some("SSL"),
1268+
"",
1269+
"PEM lib",
12641270
),
12651271
}
12661272
} else {
12671273
// Unknown error type - [SSL] PEM lib
1268-
super::compat::SslError::create_ssl_error_with_reason(vm, "SSL", "", "PEM lib")
1274+
super::compat::SslError::create_ssl_error_with_reason(
1275+
vm,
1276+
Some("SSL"),
1277+
"",
1278+
"PEM lib",
1279+
)
12691280
}
12701281
})?;
12711282

@@ -1866,7 +1877,7 @@ mod _ssl {
18661877
// [PEM: NO_START_LINE] no start line
18671878
return Err(super::compat::SslError::create_ssl_error_with_reason(
18681879
vm,
1869-
"PEM",
1880+
Some("PEM"),
18701881
"NO_START_LINE",
18711882
"[PEM: NO_START_LINE] no start line",
18721883
));
@@ -2127,7 +2138,10 @@ mod _ssl {
21272138
}
21282139
// PEM parsing errors
21292140
_ => super::compat::SslError::create_ssl_error_with_reason(
2130-
vm, "X509", "", "PEM lib",
2141+
vm,
2142+
Some("X509"),
2143+
"",
2144+
"PEM lib",
21312145
),
21322146
}
21332147
})

crates/stdlib/src/ssl/compat.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -473,19 +473,25 @@ impl SslError {
473473
/// If attribute setting fails (extremely rare), returns the exception without attributes
474474
pub(super) fn create_ssl_error_with_reason(
475475
vm: &VirtualMachine,
476-
library: &str,
476+
library: Option<&str>,
477477
reason: &str,
478478
message: impl Into<String>,
479479
) -> PyBaseExceptionRef {
480-
let exc = vm.new_exception_msg(PySSLError::class(&vm.ctx).to_owned(), message.into());
480+
let msg = message.into();
481+
// SSLError args should be (errno, message) format
482+
// FIXME: Use 1 as generic SSL error code
483+
let exc = vm.new_exception(
484+
PySSLError::class(&vm.ctx).to_owned(),
485+
vec![vm.new_pyobj(1i32), vm.new_pyobj(msg)],
486+
);
481487

482488
// Set library and reason attributes
483489
// Ignore errors as they're extremely rare (e.g., out of memory)
484-
let _ = exc.as_object().set_attr(
485-
"library",
486-
vm.ctx.new_str(library).as_object().to_owned(),
487-
vm,
488-
);
490+
let library_obj = match library {
491+
Some(lib) => vm.ctx.new_str(lib).as_object().to_owned(),
492+
None => vm.ctx.none(),
493+
};
494+
let _ = exc.as_object().set_attr("library", library_obj, vm);
489495
let _ =
490496
exc.as_object()
491497
.set_attr("reason", vm.ctx.new_str(reason).as_object().to_owned(), vm);
@@ -524,7 +530,12 @@ impl SslError {
524530
.unwrap_or("UNKNOWN");
525531

526532
// Delegate to create_ssl_error_with_reason for actual exception creation
527-
Self::create_ssl_error_with_reason(vm, lib_str, reason_str, format!("[SSL] {reason_str}"))
533+
Self::create_ssl_error_with_reason(
534+
vm,
535+
Some(lib_str),
536+
reason_str,
537+
format!("[SSL] {reason_str}"),
538+
)
528539
}
529540

530541
/// Convert to Python exception
@@ -533,7 +544,10 @@ impl SslError {
533544
SslError::WantRead => create_ssl_want_read_error(vm),
534545
SslError::WantWrite => create_ssl_want_write_error(vm),
535546
SslError::Timeout(msg) => timeout_error_msg(vm, msg),
536-
SslError::Syscall(msg) => vm.new_os_error(msg),
547+
SslError::Syscall(msg) => {
548+
// Create SSLError with library=None for syscall errors during SSL operations
549+
Self::create_ssl_error_with_reason(vm, None, &msg, msg.clone())
550+
}
537551
SslError::Ssl(msg) => vm.new_exception_msg(
538552
PySSLError::class(&vm.ctx).to_owned(),
539553
format!("SSL error: {msg}"),
@@ -1006,7 +1020,13 @@ fn handshake_write_loop(
10061020
// Non-blocking socket would block - return SSLWantWriteError
10071021
return Err(SslError::WantWrite);
10081022
}
1009-
return Err(SslError::Py(e));
1023+
// Convert socket error to SSLError during handshake
1024+
let msg = e
1025+
.as_object()
1026+
.str(vm)
1027+
.map(|s| s.to_string())
1028+
.unwrap_or_else(|_| "socket error".to_string());
1029+
return Err(SslError::Syscall(msg));
10101030
}
10111031
}
10121032
} else if written == 0 {
@@ -1072,7 +1092,13 @@ fn handshake_read_data(
10721092
return Ok((false, false));
10731093
}
10741094
}
1075-
return Err(SslError::Py(e));
1095+
// Convert socket error to SSLError during handshake
1096+
let msg = e
1097+
.as_object()
1098+
.str(vm)
1099+
.map(|s| s.to_string())
1100+
.unwrap_or_else(|_| "socket error".to_string());
1101+
return Err(SslError::Syscall(msg));
10761102
}
10771103
};
10781104

0 commit comments

Comments
 (0)