Skip to content

Commit d9e3f9c

Browse files
committed
Tidy up ssl a little
1 parent e08252c commit d9e3f9c

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

stdlib/src/ssl.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ mod _ssl {
2626
use crate::{
2727
common::{
2828
ascii,
29-
lock::{PyMutex, PyRwLock, PyRwLockWriteGuard},
29+
lock::{
30+
PyMappedRwLockReadGuard, PyMutex, PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard,
31+
},
3032
},
3133
socket::{self, PySocket},
3234
vm::{
@@ -504,12 +506,8 @@ mod _ssl {
504506
fn builder(&self) -> PyRwLockWriteGuard<'_, SslContextBuilder> {
505507
self.ctx.write()
506508
}
507-
fn exec_ctx<F, R>(&self, func: F) -> R
508-
where
509-
F: Fn(&ssl::SslContextRef) -> R,
510-
{
511-
let c = self.ctx.read();
512-
func(builder_as_ctx(&c))
509+
fn ctx(&self) -> PyMappedRwLockReadGuard<'_, ssl::SslContextRef> {
510+
PyRwLockReadGuard::map(self.ctx.read(), builder_as_ctx)
513511
}
514512

515513
#[pygetset]
@@ -554,7 +552,7 @@ mod _ssl {
554552
}
555553
#[pygetset]
556554
fn verify_mode(&self) -> i32 {
557-
let mode = self.exec_ctx(|ctx| ctx.verify_mode());
555+
let mode = self.ctx().verify_mode();
558556
if mode == SslVerifyMode::NONE {
559557
CertRequirements::None.into()
560558
} else if mode == SslVerifyMode::PEER {
@@ -703,16 +701,15 @@ mod _ssl {
703701
vm: &VirtualMachine,
704702
) -> PyResult<Vec<PyObjectRef>> {
705703
let binary_form = binary_form.unwrap_or(false);
706-
self.exec_ctx(|ctx| {
707-
let certs = ctx
708-
.cert_store()
709-
.objects()
710-
.iter()
711-
.filter_map(|obj| obj.x509())
712-
.map(|cert| cert_to_py(vm, cert, binary_form))
713-
.collect::<Result<Vec<_>, _>>()?;
714-
Ok(certs)
715-
})
704+
let certs = self
705+
.ctx()
706+
.cert_store()
707+
.objects()
708+
.iter()
709+
.filter_map(|obj| obj.x509())
710+
.map(|cert| cert_to_py(vm, cert, binary_form))
711+
.collect::<Result<Vec<_>, _>>()?;
712+
Ok(certs)
716713
}
717714

718715
#[pymethod]
@@ -746,9 +743,7 @@ mod _ssl {
746743
args: WrapSocketArgs,
747744
vm: &VirtualMachine,
748745
) -> PyResult<PySslSocket> {
749-
let mut ssl = zelf
750-
.exec_ctx(ssl::Ssl::new)
751-
.map_err(|e| convert_openssl_error(vm, e))?;
746+
let mut ssl = ssl::Ssl::new(&zelf.ctx()).map_err(|e| convert_openssl_error(vm, e))?;
752747

753748
let socket_type = if args.server_side {
754749
ssl.set_accept_state();

0 commit comments

Comments
 (0)