From ebab829f60b25d70bbfae30d8b2fdfb34236ae48 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Sun, 24 May 2020 21:47:12 -0500 Subject: [PATCH] Switch to using parking_lot::Mutex instead of std::sync::Mutex --- derive/src/pyclass.rs | 2 +- vm/src/dictdatatype.rs | 8 +++---- vm/src/exceptions.rs | 27 +++++++++++---------- vm/src/frame.rs | 9 +++---- vm/src/macros.rs | 4 ++-- vm/src/obj/objbytearray.rs | 6 ++--- vm/src/obj/objcoroinner.rs | 6 ++--- vm/src/obj/objenumerate.rs | 4 ++-- vm/src/obj/objlist.rs | 6 ++--- vm/src/obj/objmappingproxy.rs | 8 +++---- vm/src/obj/objproperty.rs | 6 ++--- vm/src/obj/objtype.rs | 43 +++++++++++----------------------- vm/src/pyobject.rs | 11 +++++---- vm/src/sequence.rs | 2 +- vm/src/stdlib/array.rs | 6 ++--- vm/src/stdlib/collections.rs | 6 ++--- vm/src/stdlib/csv.rs | 6 ++--- vm/src/stdlib/hashlib.rs | 6 ++--- vm/src/stdlib/io.rs | 6 ++--- vm/src/stdlib/itertools.rs | 44 +++++++++++++++++------------------ vm/src/stdlib/os.rs | 4 ++-- vm/src/stdlib/random.rs | 8 +++---- vm/src/stdlib/socket.rs | 8 +++---- vm/src/stdlib/ssl.rs | 22 +++++++----------- vm/src/stdlib/winreg.rs | 6 ++--- vm/src/stdlib/zlib.rs | 16 ++++++------- vm/src/types.rs | 4 ++-- vm/src/vm.rs | 11 +++++---- 28 files changed, 137 insertions(+), 158 deletions(-) diff --git a/derive/src/pyclass.rs b/derive/src/pyclass.rs index 01d841d43..11af6cdcd 100644 --- a/derive/src/pyclass.rs +++ b/derive/src/pyclass.rs @@ -275,7 +275,7 @@ fn extract_impl_items(mut items: Vec) -> Result None, diff --git a/vm/src/dictdatatype.rs b/vm/src/dictdatatype.rs index 25b80fa7e..7ff9cb7db 100644 --- a/vm/src/dictdatatype.rs +++ b/vm/src/dictdatatype.rs @@ -3,6 +3,7 @@ use crate::pyhash; use crate::pyobject::{IdProtocol, IntoPyObject, PyObjectRef, PyResult}; use crate::vm::VirtualMachine; use num_bigint::ToBigInt; +use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard}; /// Ordered dictionary implementation. /// Inspired by: https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html /// And: https://www.youtube.com/watch?v=p33CVV29OG8 @@ -10,7 +11,6 @@ use num_bigint::ToBigInt; use std::collections::{hash_map::DefaultHasher, HashMap}; use std::hash::{Hash, Hasher}; use std::mem::size_of; -use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; /// hash value of an object returned by __hash__ type HashValue = pyhash::PyHash; @@ -42,7 +42,7 @@ impl Clone for InnerDict { impl Clone for Dict { fn clone(&self) -> Self { Dict { - inner: RwLock::new(self.inner.read().unwrap().clone()), + inner: RwLock::new(self.inner.read().clone()), } } } @@ -83,11 +83,11 @@ pub struct DictSize { impl Dict { fn borrow_value(&self) -> RwLockReadGuard<'_, InnerDict> { - self.inner.read().unwrap() + self.inner.read() } fn borrow_value_mut(&self) -> RwLockWriteGuard<'_, InnerDict> { - self.inner.write().unwrap() + self.inner.write() } fn resize(&self) { diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index bf1fa0b8a..0c0110bc3 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -14,10 +14,10 @@ use crate::types::create_type; use crate::VirtualMachine; use itertools::Itertools; +use parking_lot::RwLock; use std::fmt; use std::fs::File; use std::io::{self, BufRead, BufReader, Write}; -use std::sync::RwLock; use crossbeam_utils::atomic::AtomicCell; @@ -66,50 +66,50 @@ impl PyBaseException { #[pymethod(name = "__init__")] fn init(&self, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult<()> { - *self.args.write().unwrap() = PyTuple::from(args.args).into_ref(vm); + *self.args.write() = PyTuple::from(args.args).into_ref(vm); Ok(()) } #[pyproperty] pub fn args(&self) -> PyTupleRef { - self.args.read().unwrap().clone() + self.args.read().clone() } #[pyproperty(setter)] fn set_args(&self, args: PyIterable, vm: &VirtualMachine) -> PyResult<()> { let args = args.iter(vm)?.collect::>>()?; - *self.args.write().unwrap() = PyTuple::from(args).into_ref(vm); + *self.args.write() = PyTuple::from(args).into_ref(vm); Ok(()) } #[pyproperty(name = "__traceback__")] pub fn traceback(&self) -> Option { - self.traceback.read().unwrap().clone() + self.traceback.read().clone() } #[pyproperty(name = "__traceback__", setter)] pub fn set_traceback(&self, traceback: Option) { - *self.traceback.write().unwrap() = traceback; + *self.traceback.write() = traceback; } #[pyproperty(name = "__cause__")] pub fn cause(&self) -> Option { - self.cause.read().unwrap().clone() + self.cause.read().clone() } #[pyproperty(name = "__cause__", setter)] pub fn set_cause(&self, cause: Option) { - *self.cause.write().unwrap() = cause; + *self.cause.write() = cause; } #[pyproperty(name = "__context__")] pub fn context(&self) -> Option { - self.context.read().unwrap().clone() + self.context.read().clone() } #[pyproperty(name = "__context__", setter)] pub fn set_context(&self, context: Option) { - *self.context.write().unwrap() = context; + *self.context.write() = context; } #[pyproperty(name = "__suppress_context__")] @@ -124,7 +124,7 @@ impl PyBaseException { #[pymethod] fn with_traceback(zelf: PyRef, tb: Option) -> PyResult { - *zelf.traceback.write().unwrap() = tb; + *zelf.traceback.write() = tb; Ok(zelf.as_object().clone()) } @@ -216,7 +216,7 @@ pub fn write_exception_inner( vm: &VirtualMachine, exc: &PyBaseExceptionRef, ) -> io::Result<()> { - if let Some(tb) = exc.traceback.read().unwrap().clone() { + if let Some(tb) = exc.traceback.read().clone() { writeln!(output, "Traceback (most recent call last):")?; for tb in tb.iter() { write_traceback_entry(output, &tb)?; @@ -430,7 +430,7 @@ impl ExceptionZoo { pub fn new(type_type: &PyClassRef, object_type: &PyClassRef) -> Self { let create_exception_type = |name: &str, base: &PyClassRef| { let typ = create_type(name, type_type, base); - typ.slots.write().unwrap().flags |= PyTpFlags::BASETYPE; + typ.slots.write().flags |= PyTpFlags::BASETYPE; typ }; // Sorted By Hierarchy then alphabetized. @@ -609,7 +609,6 @@ fn make_arg_getter(idx: usize) -> impl Fn(PyBaseExceptionRef, &VirtualMachine) - move |exc, vm| { exc.args .read() - .unwrap() .as_slice() .get(idx) .cloned() diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 8716a2e1a..eef0147ce 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -1,9 +1,9 @@ use std::fmt; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::sync::Mutex; use indexmap::IndexMap; use itertools::Itertools; +use parking_lot::Mutex; use crate::builtins::builtin_isinstance; use crate::bytecode; @@ -173,7 +173,7 @@ impl Frame { impl FrameRef { fn with_exec(&self, f: impl FnOnce(ExecutingFrame) -> R) -> R { - let mut state = self.state.lock().unwrap(); + let mut state = self.state.lock(); let exec = ExecutingFrame { code: &self.code, scope: &self.scope, @@ -1491,10 +1491,7 @@ impl ExecutingFrame<'_> { impl fmt::Debug for Frame { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let state = match self.state.lock() { - Ok(state) => state, - Err(_posion) => return f.pad("Frame Object {{ poisoned }}"), - }; + let state = self.state.lock(); let stack_str = state .stack .iter() diff --git a/vm/src/macros.rs b/vm/src/macros.rs index e49d46a95..a879bdd3c 100644 --- a/vm/src/macros.rs +++ b/vm/src/macros.rs @@ -37,7 +37,7 @@ macro_rules! py_class { { let py_class = $ctx.new_class($class_name, $class_base); // FIXME: setting flag here probably wrong - py_class.slots.write().unwrap().flags |= $crate::slots::PyTpFlags::BASETYPE; + py_class.slots.write().flags |= $crate::slots::PyTpFlags::BASETYPE; $crate::extend_class!($ctx, &py_class, { $($name => $value),* }); py_class } @@ -54,7 +54,7 @@ macro_rules! extend_class { }; (@set_attr($ctx:expr, $class:expr, (slot $slot_name:ident), $value:expr)) => { - $class.slots.write().unwrap().$slot_name = Some( + $class.slots.write().$slot_name = Some( $crate::function::IntoPyNativeFunc::into_func($value) ); }; diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index b01e477e6..e70df6290 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -1,10 +1,10 @@ //! Implementation of the python bytearray object. use bstr::ByteSlice; use crossbeam_utils::atomic::AtomicCell; +use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use std::convert::TryFrom; use std::mem::size_of; use std::str::FromStr; -use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use super::objbyteinner::{ ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions, ByteInnerSplitOptions, @@ -58,11 +58,11 @@ impl PyByteArray { } pub fn borrow_value(&self) -> RwLockReadGuard<'_, PyByteInner> { - self.inner.read().unwrap() + self.inner.read() } pub fn borrow_value_mut(&self) -> RwLockWriteGuard<'_, PyByteInner> { - self.inner.write().unwrap() + self.inner.write() } } diff --git a/vm/src/obj/objcoroinner.rs b/vm/src/obj/objcoroinner.rs index 33b1c2f2a..61a43357d 100644 --- a/vm/src/obj/objcoroinner.rs +++ b/vm/src/obj/objcoroinner.rs @@ -5,7 +5,7 @@ use crate::pyobject::{PyObjectRef, PyResult}; use crate::vm::VirtualMachine; use crossbeam_utils::atomic::AtomicCell; -use std::sync::RwLock; +use parking_lot::RwLock; #[derive(Debug, PartialEq, Clone, Copy)] pub enum Variant { @@ -69,10 +69,10 @@ impl Coro { let curr_exception_stack_len = vm.exceptions.borrow().len(); vm.exceptions .borrow_mut() - .append(&mut self.exceptions.write().unwrap()); + .append(&mut self.exceptions.write()); let result = vm.with_frame(self.frame.clone(), func); std::mem::swap( - &mut *self.exceptions.write().unwrap(), + &mut *self.exceptions.write(), &mut vm .exceptions .borrow_mut() diff --git a/vm/src/obj/objenumerate.rs b/vm/src/obj/objenumerate.rs index 6966ae7fe..4adbfda78 100644 --- a/vm/src/obj/objenumerate.rs +++ b/vm/src/obj/objenumerate.rs @@ -1,5 +1,5 @@ +use parking_lot::RwLock; use std::ops::AddAssign; -use std::sync::RwLock; use num_bigint::BigInt; use num_traits::Zero; @@ -50,7 +50,7 @@ impl PyEnumerate { #[pymethod(name = "__next__")] fn next(&self, vm: &VirtualMachine) -> PyResult<(BigInt, PyObjectRef)> { let next_obj = objiter::call_next(vm, &self.iterator)?; - let mut counter = self.counter.write().unwrap(); + let mut counter = self.counter.write(); let position = counter.clone(); AddAssign::add_assign(&mut counter as &mut BigInt, 1); Ok((position, next_obj)) diff --git a/vm/src/obj/objlist.rs b/vm/src/obj/objlist.rs index 3dbb3ba74..cc69c9104 100644 --- a/vm/src/obj/objlist.rs +++ b/vm/src/obj/objlist.rs @@ -1,11 +1,11 @@ use std::fmt; use std::mem::size_of; use std::ops::{DerefMut, Range}; -use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use crossbeam_utils::atomic::AtomicCell; use num_bigint::{BigInt, ToBigInt}; use num_traits::{One, Signed, ToPrimitive, Zero}; +use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use super::objbool; use super::objbyteinner; @@ -55,11 +55,11 @@ impl PyValue for PyList { impl PyList { pub fn borrow_elements(&self) -> RwLockReadGuard<'_, Vec> { - self.elements.read().unwrap() + self.elements.read() } pub fn borrow_elements_mut(&self) -> RwLockWriteGuard<'_, Vec> { - self.elements.write().unwrap() + self.elements.write() } pub(crate) fn get_byte_inner( diff --git a/vm/src/obj/objmappingproxy.rs b/vm/src/obj/objmappingproxy.rs index e2b068162..2f31b1a18 100644 --- a/vm/src/obj/objmappingproxy.rs +++ b/vm/src/obj/objmappingproxy.rs @@ -87,7 +87,7 @@ impl PyMappingProxy { MappingProxyInner::Dict(d) => d.clone(), MappingProxyInner::Class(c) => { // TODO: something that's much more efficient than this - PyDictRef::from_attributes(c.attributes.read().unwrap().clone(), vm)?.into_object() + PyDictRef::from_attributes(c.attributes.read().clone(), vm)?.into_object() } }; objiter::get_iter(vm, &obj) @@ -97,7 +97,7 @@ impl PyMappingProxy { let obj = match &self.mapping { MappingProxyInner::Dict(d) => d.clone(), MappingProxyInner::Class(c) => { - PyDictRef::from_attributes(c.attributes.read().unwrap().clone(), vm)?.into_object() + PyDictRef::from_attributes(c.attributes.read().clone(), vm)?.into_object() } }; vm.call_method(&obj, "items", vec![]) @@ -107,7 +107,7 @@ impl PyMappingProxy { let obj = match &self.mapping { MappingProxyInner::Dict(d) => d.clone(), MappingProxyInner::Class(c) => { - PyDictRef::from_attributes(c.attributes.read().unwrap().clone(), vm)?.into_object() + PyDictRef::from_attributes(c.attributes.read().clone(), vm)?.into_object() } }; vm.call_method(&obj, "keys", vec![]) @@ -117,7 +117,7 @@ impl PyMappingProxy { let obj = match &self.mapping { MappingProxyInner::Dict(d) => d.clone(), MappingProxyInner::Class(c) => { - PyDictRef::from_attributes(c.attributes.read().unwrap().clone(), vm)?.into_object() + PyDictRef::from_attributes(c.attributes.read().clone(), vm)?.into_object() } }; vm.call_method(&obj, "values", vec![]) diff --git a/vm/src/obj/objproperty.rs b/vm/src/obj/objproperty.rs index 9a4d9fa05..909ca4321 100644 --- a/vm/src/obj/objproperty.rs +++ b/vm/src/obj/objproperty.rs @@ -1,7 +1,7 @@ /*! Python `property` descriptor class. */ -use std::sync::RwLock; +use parking_lot::RwLock; use super::objtype::PyClassRef; use crate::function::OptionalArg; @@ -141,11 +141,11 @@ impl PyProperty { } fn doc_getter(&self) -> Option { - self.doc.read().unwrap().clone() + self.doc.read().clone() } fn doc_setter(&self, value: PyObjectRef, vm: &VirtualMachine) { - *self.doc.write().unwrap() = py_none_to_option(vm, &value); + *self.doc.write() = py_none_to_option(vm, &value); } // Python builder functions diff --git a/vm/src/obj/objtype.rs b/vm/src/obj/objtype.rs index f6debf5f9..b85a54ea9 100644 --- a/vm/src/obj/objtype.rs +++ b/vm/src/obj/objtype.rs @@ -1,6 +1,6 @@ +use parking_lot::RwLock; use std::collections::{HashMap, HashSet}; use std::fmt; -use std::sync::RwLock; use super::objdict::PyDictRef; use super::objlist::PyList; @@ -98,7 +98,6 @@ impl PyClassRef { fn qualname(self, vm: &VirtualMachine) -> PyObjectRef { self.attributes .read() - .unwrap() .get("__qualname__") .cloned() .unwrap_or_else(|| vm.ctx.new_str(self.name.clone())) @@ -109,7 +108,6 @@ impl PyClassRef { // TODO: Implement getting the actual module a builtin type is from self.attributes .read() - .unwrap() .get("__module__") .cloned() .unwrap_or_else(|| vm.ctx.new_str("builtins".to_owned())) @@ -119,7 +117,6 @@ impl PyClassRef { fn set_module(self, value: PyObjectRef) { self.attributes .write() - .unwrap() .insert("__module__".to_owned(), value); } @@ -148,7 +145,7 @@ impl PyClassRef { if let Some(attr) = self.get_attr(&name) { let attr_class = attr.class(); - let slots = attr_class.slots.read().unwrap(); + let slots = attr_class.slots.read(); if let Some(ref descr_get) = slots.descr_get { return descr_get(vm, attr, None, OptionalArg::Present(self.into_object())); } else if let Some(ref descriptor) = attr_class.get_attr("__get__") { @@ -182,10 +179,7 @@ impl PyClassRef { } } - self.attributes - .write() - .unwrap() - .insert(attr_name.to_string(), value); + self.attributes.write().insert(attr_name.to_string(), value); Ok(()) } @@ -200,7 +194,7 @@ impl PyClassRef { } if self.get_attr(attr_name.as_str()).is_some() { - self.attributes.write().unwrap().remove(attr_name.as_str()); + self.attributes.write().remove(attr_name.as_str()); Ok(()) } else { Err(vm.new_attribute_error(attr_name.as_str().to_owned())) @@ -211,13 +205,12 @@ impl PyClassRef { pub fn set_str_attr>(&self, attr_name: &str, value: V) { self.attributes .write() - .unwrap() .insert(attr_name.to_owned(), value.into()); } #[pymethod(magic)] fn subclasses(self) -> PyList { - let mut subclasses = self.subclasses.write().unwrap(); + let mut subclasses = self.subclasses.write(); subclasses.retain(|x| x.upgrade().is_some()); PyList::from( subclasses @@ -275,7 +268,7 @@ impl PyClassRef { let winner = calculate_meta_class(metatype.clone(), &bases, vm)?; let metatype = if !winner.is(&metatype) { #[allow(clippy::redundant_clone)] // false positive - if let Some(ref tp_new) = winner.clone().slots.read().unwrap().new { + if let Some(ref tp_new) = winner.clone().slots.read().new { // Pass it to the winner return tp_new(vm, args.insert(winner.into_object())); @@ -300,10 +293,10 @@ impl PyClassRef { let typ = new(metatype, name.as_str(), base.clone(), bases, attributes) .map_err(|e| vm.new_type_error(e))?; - typ.slots.write().unwrap().flags = base.slots.read().unwrap().flags; + typ.slots.write().flags = base.slots.read().flags; vm.ctx.add_tp_new_wrapper(&typ); - for (name, obj) in typ.attributes.read().unwrap().clone().iter() { + for (name, obj) in typ.attributes.read().clone().iter() { if let Some(meth) = vm.get_method(obj.clone(), "__set_name__") { let set_name = meth?; vm.invoke( @@ -399,9 +392,9 @@ fn call_tp_new( let class_with_new_slot = typ .iter_mro() .cloned() - .find(|cls| cls.slots.read().unwrap().new.is_some()) + .find(|cls| cls.slots.read().new.is_some()) .expect("Should be able to find a new slot somewhere in the mro"); - let slots = class_with_new_slot.slots.read().unwrap(); + let slots = class_with_new_slot.slots.read(); let new_slot = slots.new.as_ref().unwrap(); new_slot(vm, args.insert(subtype.into_object())) } @@ -429,7 +422,6 @@ impl PyClassRef { self.attributes .read() - .unwrap() .get(attr_name) .cloned() .or_else(|| self.get_super_attr(attr_name)) @@ -438,13 +430,13 @@ impl PyClassRef { pub fn get_super_attr(&self, attr_name: &str) -> Option { self.mro .iter() - .find_map(|class| class.attributes.read().unwrap().get(attr_name).cloned()) + .find_map(|class| class.attributes.read().get(attr_name).cloned()) } // This is the internal has_attr implementation for fast lookup on a class. pub fn has_attr(&self, attr_name: &str) -> bool { self.iter_mro() - .any(|c| c.attributes.read().unwrap().contains_key(attr_name)) + .any(|c| c.attributes.read().contains_key(attr_name)) } pub fn get_attributes(self) -> PyAttributes { @@ -452,7 +444,7 @@ impl PyClassRef { let mut attributes = PyAttributes::new(); for bc in self.iter_mro().rev() { - for (name, value) in bc.attributes.read().unwrap().clone().iter() { + for (name, value) in bc.attributes.read().clone().iter() { attributes.insert(name.to_owned(), value.clone()); } } @@ -561,7 +553,6 @@ pub fn new( for base in &new_type.bases { base.subclasses .write() - .unwrap() .push(PyWeak::downgrade(new_type.as_object())); } @@ -611,13 +602,7 @@ fn best_base<'a>(bases: &'a [PyClassRef], vm: &VirtualMachine) -> PyResult Option { - self.dict.as_ref().map(|mu| mu.lock().unwrap().clone()) + self.dict.as_ref().map(|mu| mu.lock().clone()) } /// Set the dict field. Returns `Err(dict)` if this object does not have a dict field /// in the first place. pub fn set_dict(&self, dict: PyDictRef) -> Result<(), PyDictRef> { match self.dict { Some(ref mu) => { - *mu.lock().unwrap() = dict; + *mu.lock() = dict; Ok(()) } None => Err(dict), @@ -1333,7 +1334,7 @@ pub trait PyClassImpl: PyClassDef { fn extend_class(ctx: &PyContext, class: &PyClassRef) { Self::impl_extend_class(ctx, class); - class.slots.write().unwrap().flags = Self::TP_FLAGS; + class.slots.write().flags = Self::TP_FLAGS; ctx.add_tp_new_wrapper(&class); if let Some(doc) = Self::DOC { class.set_str_attr("__doc__", ctx.new_str(doc)); diff --git a/vm/src/sequence.rs b/vm/src/sequence.rs index 51c2368f5..2f062b545 100644 --- a/vm/src/sequence.rs +++ b/vm/src/sequence.rs @@ -1,7 +1,7 @@ use crate::pyobject::{IdProtocol, PyObjectRef, PyResult}; use crate::vm::VirtualMachine; +use parking_lot::{RwLockReadGuard, RwLockWriteGuard}; use std::ops::Deref; -use std::sync::{RwLockReadGuard, RwLockWriteGuard}; type DynPyIter<'a> = Box + 'a>; diff --git a/vm/src/stdlib/array.rs b/vm/src/stdlib/array.rs index 9071ee62f..3b90d38f8 100644 --- a/vm/src/stdlib/array.rs +++ b/vm/src/stdlib/array.rs @@ -10,8 +10,8 @@ use crate::pyobject::{ }; use crate::VirtualMachine; +use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use std::fmt; -use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use crossbeam_utils::atomic::AtomicCell; @@ -239,11 +239,11 @@ impl PyValue for PyArray { #[pyimpl(flags(BASETYPE))] impl PyArray { fn borrow_value(&self) -> RwLockReadGuard<'_, ArrayContentType> { - self.array.read().unwrap() + self.array.read() } fn borrow_value_mut(&self) -> RwLockWriteGuard<'_, ArrayContentType> { - self.array.write().unwrap() + self.array.write() } #[pyslot] diff --git a/vm/src/stdlib/collections.rs b/vm/src/stdlib/collections.rs index 8cd32d949..5439825d3 100644 --- a/vm/src/stdlib/collections.rs +++ b/vm/src/stdlib/collections.rs @@ -12,8 +12,8 @@ mod _collections { use crate::vm::ReprGuard; use crate::VirtualMachine; use itertools::Itertools; + use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use std::collections::VecDeque; - use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use crossbeam_utils::atomic::AtomicCell; @@ -39,11 +39,11 @@ mod _collections { impl PyDeque { fn borrow_deque(&self) -> RwLockReadGuard<'_, VecDeque> { - self.deque.read().unwrap() + self.deque.read() } fn borrow_deque_mut(&self) -> RwLockWriteGuard<'_, VecDeque> { - self.deque.write().unwrap() + self.deque.write() } } diff --git a/vm/src/stdlib/csv.rs b/vm/src/stdlib/csv.rs index 6880bf15a..bef432b00 100644 --- a/vm/src/stdlib/csv.rs +++ b/vm/src/stdlib/csv.rs @@ -1,5 +1,5 @@ +use parking_lot::RwLock; use std::fmt::{self, Debug, Formatter}; -use std::sync::RwLock; use csv as rust_csv; use itertools::join; @@ -152,13 +152,13 @@ impl Reader { impl Reader { #[pymethod(name = "__iter__")] fn iter(this: PyRef, vm: &VirtualMachine) -> PyResult { - this.state.write().unwrap().cast_to_reader(vm)?; + this.state.write().cast_to_reader(vm)?; this.into_pyobject(vm) } #[pymethod(name = "__next__")] fn next(&self, vm: &VirtualMachine) -> PyResult { - let mut state = self.state.write().unwrap(); + let mut state = self.state.write(); state.cast_to_reader(vm)?; if let ReadState::CsvIter(ref mut reader) = &mut *state { diff --git a/vm/src/stdlib/hashlib.rs b/vm/src/stdlib/hashlib.rs index f37426117..43f8442dc 100644 --- a/vm/src/stdlib/hashlib.rs +++ b/vm/src/stdlib/hashlib.rs @@ -4,8 +4,8 @@ use crate::obj::objstr::PyStringRef; use crate::obj::objtype::PyClassRef; use crate::pyobject::{PyClassImpl, PyObjectRef, PyResult, PyValue}; use crate::vm::VirtualMachine; +use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use std::fmt; -use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use blake2::{Blake2b, Blake2s}; use digest::DynDigest; @@ -42,11 +42,11 @@ impl PyHasher { } fn borrow_value(&self) -> RwLockReadGuard<'_, HashWrapper> { - self.buffer.read().unwrap() + self.buffer.read() } fn borrow_value_mut(&self) -> RwLockWriteGuard<'_, HashWrapper> { - self.buffer.write().unwrap() + self.buffer.write() } #[pyslot] diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 6ad89afcf..55f83da6a 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -1,9 +1,9 @@ /* * I/O core tools. */ +use parking_lot::{RwLock, RwLockWriteGuard}; use std::fs; use std::io::{self, prelude::*, Cursor, SeekFrom}; -use std::sync::{RwLock, RwLockWriteGuard}; use crossbeam_utils::atomic::AtomicCell; use num_traits::ToPrimitive; @@ -136,7 +136,7 @@ impl PyValue for PyStringIO { impl PyStringIORef { fn buffer(&self, vm: &VirtualMachine) -> PyResult> { if !self.closed.load() { - Ok(self.buffer.write().unwrap()) + Ok(self.buffer.write()) } else { Err(vm.new_value_error("I/O operation on closed file.".to_owned())) } @@ -259,7 +259,7 @@ impl PyValue for PyBytesIO { impl PyBytesIORef { fn buffer(&self, vm: &VirtualMachine) -> PyResult> { if !self.closed.load() { - Ok(self.buffer.write().unwrap()) + Ok(self.buffer.write()) } else { Err(vm.new_value_error("I/O operation on closed file.".to_owned())) } diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index a9dafaaf7..601fc373d 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -5,8 +5,9 @@ mod decl { use crossbeam_utils::atomic::AtomicCell; use num_bigint::BigInt; use num_traits::{One, Signed, ToPrimitive, Zero}; + use parking_lot::{RwLock, RwLockWriteGuard}; use std::iter; - use std::sync::{Arc, RwLock, RwLockWriteGuard}; + use std::sync::Arc; use crate::function::{Args, OptionalArg, OptionalOption, PyFuncArgs}; use crate::obj::objbool; @@ -52,12 +53,12 @@ mod decl { if pos >= self.iterables.len() { break; } - let cur_iter = if self.cached_iter.read().unwrap().is_none() { + let cur_iter = if self.cached_iter.read().is_none() { // We need to call "get_iter" outside of the lock. let iter = get_iter(vm, &self.iterables[pos])?; - *self.cached_iter.write().unwrap() = Some(iter.clone()); + *self.cached_iter.write() = Some(iter.clone()); iter - } else if let Some(cached_iter) = (*(self.cached_iter.read().unwrap())).clone() { + } else if let Some(cached_iter) = (*(self.cached_iter.read())).clone() { cached_iter } else { // Someone changed cached iter to None since we checked. @@ -70,7 +71,7 @@ mod decl { Err(err) => { if objtype::isinstance(&err, &vm.ctx.exceptions.stop_iteration) { self.cur_idx.fetch_add(1); - *self.cached_iter.write().unwrap() = None; + *self.cached_iter.write() = None; } else { return Err(err); } @@ -195,7 +196,7 @@ mod decl { #[pymethod(name = "__next__")] fn next(&self) -> PyResult { - let mut cur = self.cur.write().unwrap(); + let mut cur = self.cur.write(); let result = cur.clone(); *cur += &self.step; Ok(PyInt::new(result)) @@ -242,10 +243,10 @@ mod decl { #[pymethod(name = "__next__")] fn next(&self, vm: &VirtualMachine) -> PyResult { let item = if let Some(item) = get_next_object(vm, &self.iter)? { - self.saved.write().unwrap().push(item.clone()); + self.saved.write().push(item.clone()); item } else { - let saved = self.saved.read().unwrap(); + let saved = self.saved.read(); if saved.len() == 0 { return Err(new_stop_iteration(vm)); } @@ -305,7 +306,7 @@ mod decl { #[pymethod(name = "__next__")] fn next(&self, vm: &VirtualMachine) -> PyResult { if let Some(ref times) = self.times { - let mut times = times.write().unwrap(); + let mut times = times.write(); if *times <= BigInt::zero() { return Err(new_stop_iteration(vm)); } @@ -323,7 +324,7 @@ mod decl { #[pymethod(name = "__length_hint__")] fn length_hint(&self, vm: &VirtualMachine) -> PyObjectRef { match self.times { - Some(ref times) => vm.new_int(times.read().unwrap().clone()), + Some(ref times) => vm.new_int(times.read().clone()), None => vm.new_int(0), } } @@ -707,7 +708,7 @@ mod decl { let iterable = &self.iterable; let obj = call_next(vm, iterable)?; - let acc_value = self.acc_value.read().unwrap().clone(); + let acc_value = self.acc_value.read().clone(); let next_acc_value = match acc_value { None => obj.clone(), @@ -719,7 +720,7 @@ mod decl { } } }; - *self.acc_value.write().unwrap() = Some(next_acc_value.clone()); + *self.acc_value.write() = Some(next_acc_value.clone()); Ok(next_acc_value) } @@ -745,11 +746,11 @@ mod decl { } fn get_item(&self, vm: &VirtualMachine, index: usize) -> PyResult { - if self.values.read().unwrap().len() == index { + if self.values.read().len() == index { let result = call_next(vm, &self.iterable)?; - self.values.write().unwrap().push(result); + self.values.write().push(result); } - Ok(self.values.read().unwrap()[index].clone()) + Ok(self.values.read()[index].clone()) } } @@ -904,7 +905,7 @@ mod decl { } } - let idxs = self.idxs.write().unwrap(); + let idxs = self.idxs.write(); let res = PyTuple::from( pools @@ -1010,13 +1011,12 @@ mod decl { let res = PyTuple::from( self.indices .read() - .unwrap() .iter() .map(|&i| self.pool[i].clone()) .collect::>(), ); - let mut indices = self.indices.write().unwrap(); + let mut indices = self.indices.write(); // Scan indices right-to-left until finding one that is not at its maximum (i + n - r). let mut idx = r as isize - 1; @@ -1107,7 +1107,7 @@ mod decl { return Ok(vm.ctx.new_tuple(vec![])); } - let mut indices = self.indices.write().unwrap(); + let mut indices = self.indices.write(); let res = vm .ctx @@ -1215,11 +1215,11 @@ mod decl { return Ok(vm.ctx.new_tuple(vec![])); } - let mut result = self.result.write().unwrap(); + let mut result = self.result.write(); if let Some(ref mut result) = *result { - let mut indices = self.indices.write().unwrap(); - let mut cycles = self.cycles.write().unwrap(); + let mut indices = self.indices.write(); + let mut cycles = self.cycles.write(); let mut sentinel = false; // Decrement rightmost cycle, moving leftward upon zero rollover diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 2a1af9a38..56cbe39cc 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -1,3 +1,4 @@ +use parking_lot::RwLock; use std::ffi; use std::fs::File; use std::fs::OpenOptions; @@ -6,7 +7,6 @@ use std::io::{self, ErrorKind, Read, Write}; use std::os::unix::fs::OpenOptionsExt; #[cfg(windows)] use std::os::windows::fs::OpenOptionsExt; -use std::sync::RwLock; use std::time::{Duration, SystemTime}; use std::{env, fs}; @@ -727,7 +727,7 @@ impl ScandirIterator { return Err(objiter::new_stop_iteration(vm)); } - match self.entries.write().unwrap().next() { + match self.entries.write().next() { Some(entry) => match entry { Ok(entry) => Ok(DirEntry { entry, diff --git a/vm/src/stdlib/random.rs b/vm/src/stdlib/random.rs index acb8f9869..bbff45667 100644 --- a/vm/src/stdlib/random.rs +++ b/vm/src/stdlib/random.rs @@ -13,7 +13,7 @@ mod _random { use num_traits::Signed; use rand::{rngs::StdRng, RngCore, SeedableRng}; - use std::sync::Mutex; + use parking_lot::Mutex; #[derive(Debug)] enum PyRng { @@ -78,7 +78,7 @@ mod _random { #[pymethod] fn random(&self) -> f64 { - let mut rng = self.rng.lock().unwrap(); + let mut rng = self.rng.lock(); mt19937::gen_res53(&mut *rng) } @@ -95,12 +95,12 @@ mod _random { } }; - *self.rng.lock().unwrap() = new_rng; + *self.rng.lock() = new_rng; } #[pymethod] fn getrandbits(&self, k: usize) -> BigInt { - let mut rng = self.rng.lock().unwrap(); + let mut rng = self.rng.lock(); let mut k = k; let mut gen_u32 = |k| rng.next_u32() >> (32 - k) as u32; diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index 4bd36275b..25f7ab9da 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -1,6 +1,6 @@ +use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use std::io::{self, prelude::*}; use std::net::{Ipv4Addr, Shutdown, SocketAddr, ToSocketAddrs}; -use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use std::time::Duration; use byteorder::{BigEndian, ByteOrder}; @@ -76,11 +76,11 @@ pub type PySocketRef = PyRef; #[pyimpl(flags(BASETYPE))] impl PySocket { fn sock(&self) -> RwLockReadGuard<'_, Socket> { - self.sock.read().unwrap() + self.sock.read() } fn sock_mut(&self) -> RwLockWriteGuard<'_, Socket> { - self.sock.write().unwrap() + self.sock.write() } #[pyslot] @@ -127,7 +127,7 @@ impl PySocket { self.proto.store(proto); sock }; - *self.sock.write().unwrap() = sock; + *self.sock.write() = sock; Ok(()) } diff --git a/vm/src/stdlib/ssl.rs b/vm/src/stdlib/ssl.rs index 02c52a427..9601e3764 100644 --- a/vm/src/stdlib/ssl.rs +++ b/vm/src/stdlib/ssl.rs @@ -11,10 +11,10 @@ use crate::pyobject::{ use crate::types::create_type; use crate::VirtualMachine; +use parking_lot::{RwLock, RwLockWriteGuard}; use std::convert::TryFrom; use std::ffi::{CStr, CString}; use std::fmt; -use std::sync::{RwLock, RwLockWriteGuard}; use foreign_types_shared::{ForeignType, ForeignTypeRef}; use openssl::{ @@ -249,17 +249,17 @@ impl PyValue for PySslContext { #[pyimpl(flags(BASETYPE))] impl PySslContext { fn builder(&self) -> RwLockWriteGuard<'_, SslContextBuilder> { - self.ctx.write().unwrap() + self.ctx.write() } fn exec_ctx(&self, func: F) -> R where F: Fn(&ssl::SslContextRef) -> R, { - let c = self.ctx.read().unwrap(); + let c = self.ctx.read(); func(unsafe { &**(&*c as *const SslContextBuilder as *const ssl::SslContext) }) } fn ptr(&self) -> *mut sys::SSL_CTX { - (*self.ctx.write().unwrap()).as_ptr() + (*self.ctx.write()).as_ptr() } #[pyslot] @@ -528,32 +528,28 @@ impl PyValue for PySslSocket { #[pyimpl] impl PySslSocket { fn stream_builder(&self) -> ssl::SslStreamBuilder { - std::mem::replace(&mut *self.stream.write().unwrap(), None).unwrap() + std::mem::replace(&mut *self.stream.write(), None).unwrap() } fn exec_stream(&self, func: F) -> R where F: Fn(&mut ssl::SslStream) -> R, { - let mut b = self.stream.write().unwrap(); + let mut b = self.stream.write(); func(unsafe { &mut *(b.as_mut().unwrap() as *mut ssl::SslStreamBuilder<_> as *mut ssl::SslStream<_>) }) } fn set_stream(&self, stream: ssl::SslStream) { - *self.stream.write().unwrap() = Some(unsafe { std::mem::transmute(stream) }); + *self.stream.write() = Some(unsafe { std::mem::transmute(stream) }); } #[pyproperty] fn owner(&self) -> Option { - self.owner - .read() - .unwrap() - .as_ref() - .and_then(PyWeak::upgrade) + self.owner.read().as_ref().and_then(PyWeak::upgrade) } #[pyproperty(setter)] fn set_owner(&self, owner: PyObjectRef) { - *self.owner.write().unwrap() = Some(PyWeak::downgrade(&owner)) + *self.owner.write() = Some(PyWeak::downgrade(&owner)) } #[pyproperty] fn server_side(&self) -> bool { diff --git a/vm/src/stdlib/winreg.rs b/vm/src/stdlib/winreg.rs index dba3285db..c5a70a7c0 100644 --- a/vm/src/stdlib/winreg.rs +++ b/vm/src/stdlib/winreg.rs @@ -1,7 +1,7 @@ #![allow(non_snake_case)] +use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use std::convert::TryInto; use std::io; -use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use super::os; use crate::function::OptionalArg; @@ -38,11 +38,11 @@ impl PyHKEY { } fn key(&self) -> RwLockReadGuard<'_, RegKey> { - self.key.read().unwrap() + self.key.read() } fn key_mut(&self) -> RwLockWriteGuard<'_, RegKey> { - self.key.write().unwrap() + self.key.write() } #[pymethod] diff --git a/vm/src/stdlib/zlib.rs b/vm/src/stdlib/zlib.rs index 1bd266458..b14ca606d 100644 --- a/vm/src/stdlib/zlib.rs +++ b/vm/src/stdlib/zlib.rs @@ -13,8 +13,8 @@ use crossbeam_utils::atomic::AtomicCell; use flate2::{write::ZlibEncoder, Compression, Decompress, FlushDecompress, Status}; use libz_sys as libz; +use parking_lot::Mutex; use std::io::Write; -use std::sync::Mutex; // copied from zlibmodule.c (commit 530f506ac91338) const MAX_WBITS: u8 = 15; @@ -182,11 +182,11 @@ impl PyDecompress { } #[pyproperty] fn unused_data(&self) -> PyBytesRef { - self.unused_data.lock().unwrap().clone() + self.unused_data.lock().clone() } #[pyproperty] fn unconsumed_tail(&self) -> PyBytesRef { - self.unconsumed_tail.lock().unwrap().clone() + self.unconsumed_tail.lock().clone() } fn save_unconsumed_input( @@ -200,7 +200,7 @@ impl PyDecompress { let leftover = &data[(d.total_in() - orig_in) as usize..]; if stream_end && !leftover.is_empty() { - let mut unused_data = self.unused_data.lock().unwrap(); + let mut unused_data = self.unused_data.lock(); let unused = unused_data .get_value() .iter() @@ -210,7 +210,7 @@ impl PyDecompress { *unused_data = PyBytes::new(unused).into_ref(vm); } - let mut unconsumed_tail = self.unconsumed_tail.lock().unwrap(); + let mut unconsumed_tail = self.unconsumed_tail.lock(); if !leftover.is_empty() || unconsumed_tail.len() > 0 { *unconsumed_tail = PyBytes::new(leftover.to_owned()).into_ref(vm); } @@ -221,7 +221,7 @@ impl PyDecompress { let limited = args.max_length == 0; let data = args.data.get_value(); - let mut d = self.decompress.lock().unwrap(); + let mut d = self.decompress.lock(); let orig_in = d.total_in(); let mut buf = Vec::new(); @@ -285,8 +285,8 @@ impl PyDecompress { OptionalArg::Missing => DEF_BUF_SIZE, }; - let data = self.unconsumed_tail.lock().unwrap(); - let mut d = self.decompress.lock().unwrap(); + let data = self.unconsumed_tail.lock(); + let mut d = self.decompress.lock(); let orig_in = d.total_in(); let mut buf = Vec::new(); diff --git a/vm/src/types.rs b/vm/src/types.rs index 32b01ed0a..2ad3f0660 100644 --- a/vm/src/types.rs +++ b/vm/src/types.rs @@ -40,9 +40,10 @@ use crate::obj::objweakproxy; use crate::obj::objweakref; use crate::obj::objzip; use crate::pyobject::{PyAttributes, PyContext, PyObject}; +use parking_lot::RwLock; use std::mem::MaybeUninit; use std::ptr; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; /// Holder of references to builtin types. #[derive(Debug)] @@ -350,7 +351,6 @@ fn init_type_hierarchy() -> (PyClassRef, PyClassRef) { object_type .subclasses .write() - .unwrap() .push(objweakref::PyWeak::downgrade(&type_type.as_object())); (type_type, object_type) diff --git a/vm/src/vm.rs b/vm/src/vm.rs index f96aedf65..37f89b10b 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -7,7 +7,7 @@ use std::cell::{Cell, Ref, RefCell}; use std::collections::hash_map::HashMap; use std::collections::hash_set::HashSet; -use std::sync::{Arc, Mutex, MutexGuard}; +use std::sync::Arc; use std::{env, fmt}; use arr_macro::arr; @@ -15,6 +15,7 @@ use crossbeam_utils::atomic::AtomicCell; use num_bigint::BigInt; use num_traits::ToPrimitive; use once_cell::sync::Lazy; +use parking_lot::{Mutex, MutexGuard}; #[cfg(feature = "rustpython-compiler")] use rustpython_compiler::{ compile::{self, CompileOpts}, @@ -769,7 +770,7 @@ impl VirtualMachine { cls: Option, ) -> Option { let descr_class = descr.class(); - let slots = descr_class.slots.read().unwrap(); + let slots = descr_class.slots.read(); if let Some(descr_get) = slots.descr_get.as_ref() { Some(descr_get(self, descr, obj, OptionalArg::from_option(cls))) } else if let Some(ref descriptor) = descr_class.get_attr("__get__") { @@ -821,7 +822,7 @@ impl VirtualMachine { fn _invoke(&self, callable: &PyObjectRef, args: PyFuncArgs) -> PyResult { vm_trace!("Invoke: {:?} {:?}", callable, args); - if let Some(slot_call) = callable.class().slots.read().unwrap().call.as_ref() { + if let Some(slot_call) = callable.class().slots.read().call.as_ref() { self.trace_event(TraceEvent::Call)?; let args = args.insert(callable.clone()); let result = slot_call(self, args); @@ -1047,7 +1048,7 @@ impl VirtualMachine { } pub fn is_callable(&self, obj: &PyObjectRef) -> bool { - obj.class().slots.read().unwrap().call.is_some() || obj.class().has_attr("__call__") + obj.class().slots.read().call.is_some() || obj.class().has_attr("__call__") } #[inline] @@ -1503,7 +1504,7 @@ pub struct ReprGuard { /// A guard to protect repr methods from recursion into itself, impl ReprGuard { fn get_guards<'a>() -> MutexGuard<'a, HashSet> { - REPR_GUARDS.lock().expect("ReprGuard lock poisoned") + REPR_GUARDS.lock() } /// Returns None if the guard against 'obj' is still held otherwise returns the guard. The guard