Switch to using parking_lot::Mutex instead of std::sync::Mutex

This commit is contained in:
Noah committed 2020-07-04 22:58:18 -05:00
1 parent 7ac07fd902
commit ebab829f60
28 files changed
+137 -158

No files matched your search

+1 -1
View File
@@ -275,7 +275,7 @@ fn extract_impl_items(mut items: Vec<ItemIdent>) -> Result<TokenStream2, Diagnos
#transform(Self::#item_ident)
};
Some(quote! {
(*class.slots.write().unwrap()).#slot_ident = Some(#into_func);
(*class.slots.write()).#slot_ident = Some(#into_func);
})
}
_ => None,
+4 -4
View File
@@ -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<T: Clone> Clone for InnerDict<T> {
impl<T: Clone> Clone for Dict<T> {
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<T: Clone> Dict<T> {
fn borrow_value(&self) -> RwLockReadGuard<'_, InnerDict<T>> {
self.inner.read().unwrap()
self.inner.read()
}
fn borrow_value_mut(&self) -> RwLockWriteGuard<'_, InnerDict<T>> {
self.inner.write().unwrap()
self.inner.write()
}
fn resize(&self) {
+13 -14
View File
@@ -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::<PyResult<Vec<_>>>()?;
*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<PyTracebackRef> {
self.traceback.read().unwrap().clone()
self.traceback.read().clone()
}
#[pyproperty(name = "__traceback__", setter)]
pub fn set_traceback(&self, traceback: Option<PyTracebackRef>) {
*self.traceback.write().unwrap() = traceback;
*self.traceback.write() = traceback;
}
#[pyproperty(name = "__cause__")]
pub fn cause(&self) -> Option<PyBaseExceptionRef> {
self.cause.read().unwrap().clone()
self.cause.read().clone()
}
#[pyproperty(name = "__cause__", setter)]
pub fn set_cause(&self, cause: Option<PyBaseExceptionRef>) {
*self.cause.write().unwrap() = cause;
*self.cause.write() = cause;
}
#[pyproperty(name = "__context__")]
pub fn context(&self) -> Option<PyBaseExceptionRef> {
self.context.read().unwrap().clone()
self.context.read().clone()
}
#[pyproperty(name = "__context__", setter)]
pub fn set_context(&self, context: Option<PyBaseExceptionRef>) {
*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<Self>, tb: Option<PyTracebackRef>) -> PyResult {
*zelf.traceback.write().unwrap() = tb;
*zelf.traceback.write() = tb;
Ok(zelf.as_object().clone())
}
@@ -216,7 +216,7 @@ pub fn write_exception_inner<W: Write>(
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()
+3 -6
View File
@@ -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<R>(&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()
+2 -2
View File
@@ -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)
);
};
+3 -3
View File
@@ -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()
}
}
+3 -3
View File
@@ -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()
+2 -2
View File
@@ -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))
+3 -3
View File
@@ -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<PyObjectRef>> {
self.elements.read().unwrap()
self.elements.read()
}
pub fn borrow_elements_mut(&self) -> RwLockWriteGuard<'_, Vec<PyObjectRef>> {
self.elements.write().unwrap()
self.elements.write()
}
pub(crate) fn get_byte_inner(
+4 -4
View File
@@ -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![])
+3 -3
View File
@@ -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<PyObjectRef> {
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
+14 -29
View File
@@ -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<V: Into<PyObjectRef>>(&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<PyObjectRef> {
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<PyCla
// return NULL;
// }
if !base_i
.slots
.read()
.unwrap()
.flags
.has_feature(PyTpFlags::BASETYPE)
{
if !base_i.slots.read().flags.has_feature(PyTpFlags::BASETYPE) {
return Err(vm.new_type_error(format!(
"type '{}' is not an acceptable base type",
base_i.name
+6 -5
View File
@@ -3,12 +3,13 @@ use std::collections::HashMap;
use std::fmt;
use std::marker::PhantomData;
use std::ops::Deref;
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use indexmap::IndexMap;
use num_bigint::BigInt;
use num_complex::Complex64;
use num_traits::{One, ToPrimitive, Zero};
use parking_lot::Mutex;
use crate::bytecode;
use crate::dictdatatype::DictKey;
@@ -616,7 +617,7 @@ impl PyContext {
}
pub fn add_tp_new_wrapper(&self, ty: &PyClassRef) {
if !ty.attributes.read().unwrap().contains_key("__new__") {
if !ty.attributes.read().contains_key("__new__") {
let new_wrapper =
self.new_bound_method(self.tp_new_wrapper.clone(), ty.clone().into_object());
ty.set_str_attr("__new__", new_wrapper);
@@ -1185,14 +1186,14 @@ where
T: ?Sized + PyObjectPayload,
{
pub fn dict(&self) -> Option<PyDictRef> {
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));
+1 -1
View File
@@ -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<dyn ExactSizeIterator<Item = &'a PyObjectRef> + 'a>;
+3 -3
View File
@@ -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]
+3 -3
View File
@@ -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<PyObjectRef>> {
self.deque.read().unwrap()
self.deque.read()
}
fn borrow_deque_mut(&self) -> RwLockWriteGuard<'_, VecDeque<PyObjectRef>> {
self.deque.write().unwrap()
self.deque.write()
}
}
+3 -3
View File
@@ -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<Self>, 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 {
+3 -3
View File
@@ -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]
+3 -3
View File
@@ -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<RwLockWriteGuard<'_, BufferedIO>> {
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<RwLockWriteGuard<'_, BufferedIO>> {
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()))
}
+22 -22
View File
@@ -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<PyInt> {
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::<Vec<PyObjectRef>>(),
);
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
+2 -2
View File
@@ -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,
+4 -4
View File
@@ -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;
+4 -4
View File
@@ -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<PySocket>;
#[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(())
}
+9 -13
View File
@@ -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<F, R>(&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<PySocketRef> {
std::mem::replace(&mut *self.stream.write().unwrap(), None).unwrap()
std::mem::replace(&mut *self.stream.write(), None).unwrap()
}
fn exec_stream<F, R>(&self, func: F) -> R
where
F: Fn(&mut ssl::SslStream<PySocketRef>) -> 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<PySocketRef>) {
*self.stream.write().unwrap() = Some(unsafe { std::mem::transmute(stream) });
*self.stream.write() = Some(unsafe { std::mem::transmute(stream) });
}
#[pyproperty]
fn owner(&self) -> Option<PyObjectRef> {
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 {
+3 -3
View File
@@ -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]
+8 -8
View File
@@ -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();
+2 -2
View File
@@ -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)
+6 -5
View File
@@ -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<PyObjectRef>,
) -> Option<PyResult> {
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<usize>> {
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