Merge pull request #3310 from youknowone/as-mapping-never-fail

as_mapping never fails
This commit is contained in:
Jim Fasarakis-Hilliard
2021-10-15 06:39:39 +03:00
committed by GitHub
12 changed files with 42 additions and 37 deletions

View File

@@ -1253,12 +1253,12 @@ mod array {
}
impl AsMapping for PyArray {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
Ok(PyMappingMethods {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
PyMappingMethods {
length: Some(Self::length),
subscript: Some(Self::subscript),
ass_subscript: Some(Self::ass_subscript),
})
}
}
fn length(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {

View File

@@ -756,12 +756,12 @@ impl<'a> BufferResizeGuard<'a> for PyByteArray {
}
impl AsMapping for PyByteArray {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
Ok(PyMappingMethods {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
PyMappingMethods {
length: Some(Self::length),
subscript: Some(Self::subscript),
ass_subscript: Some(Self::ass_subscript),
})
}
}
#[inline]

View File

@@ -569,12 +569,12 @@ impl BufferInternal for PyRef<PyBytes> {
}
impl AsMapping for PyBytes {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
Ok(PyMappingMethods {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
PyMappingMethods {
length: Some(Self::length),
subscript: Some(Self::subscript),
ass_subscript: None,
})
}
}
#[inline]

View File

@@ -416,12 +416,12 @@ impl PyDict {
}
impl AsMapping for PyDict {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
Ok(PyMappingMethods {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
PyMappingMethods {
length: Some(Self::length),
subscript: Some(Self::subscript),
ass_subscript: Some(Self::ass_subscript),
})
}
}
#[inline]

View File

@@ -418,12 +418,12 @@ impl PyList {
}
impl AsMapping for PyList {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
Ok(PyMappingMethods {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
PyMappingMethods {
length: Some(Self::length),
subscript: Some(Self::subscript),
ass_subscript: Some(Self::ass_subscript),
})
}
}
#[inline]

View File

@@ -151,12 +151,12 @@ impl PyMappingProxy {
}
impl AsMapping for PyMappingProxy {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
Ok(PyMappingMethods {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
PyMappingMethods {
length: None,
subscript: Some(Self::subscript),
ass_subscript: None,
})
}
}
#[inline]

View File

@@ -737,12 +737,12 @@ impl BufferInternal for PyRef<PyMemoryView> {
}
impl AsMapping for PyMemoryView {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
Ok(PyMappingMethods {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
PyMappingMethods {
length: Some(Self::length),
subscript: Some(Self::subscript),
ass_subscript: Some(Self::ass_subscript),
})
}
}
#[inline]

View File

@@ -377,12 +377,12 @@ impl PyRange {
}
impl AsMapping for PyRange {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
Ok(PyMappingMethods {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
PyMappingMethods {
length: Some(Self::length),
subscript: Some(Self::subscript),
ass_subscript: None,
})
}
}
#[inline]

View File

@@ -306,12 +306,12 @@ impl PyTuple {
}
impl AsMapping for PyTuple {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
Ok(PyMappingMethods {
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
PyMappingMethods {
length: Some(Self::length),
subscript: Some(Self::subscript),
ass_subscript: None,
})
}
}
#[inline]

View File

@@ -36,7 +36,7 @@ impl PyMapping<PyObjectRef> {
let obj_cls = self.0.class();
for cls in obj_cls.iter_mro() {
if let Some(f) = cls.slots.as_mapping.load() {
return f(&self.0, vm).unwrap();
return f(&self.0, vm);
}
}
PyMappingMethods::default()

View File

@@ -237,6 +237,13 @@ impl PyObjectRef {
PyRef::from_obj_unchecked(self)
}
/// # Safety
/// T must be the exact payload type
pub unsafe fn downcast_unchecked_ref<T: PyObjectPayload>(&self) -> &PyRef<T> {
debug_assert!(self.payload_is::<T>());
&*(self as *const PyObjectRef as *const PyRef<T>)
}
pub(crate) fn class_lock(&self) -> &PyRwLock<PyTypeRef> {
&self.rc.inner.typ
}

View File

@@ -128,7 +128,7 @@ impl Default for PyTypeFlags {
}
pub(crate) type GenericMethod = fn(&PyObjectRef, FuncArgs, &VirtualMachine) -> PyResult;
pub(crate) type AsMappingFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult<PyMappingMethods>;
pub(crate) type AsMappingFunc = fn(&PyObjectRef, &VirtualMachine) -> PyMappingMethods;
pub(crate) type HashFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult<PyHash>;
// CallFunc = GenericMethod
pub(crate) type GetattroFunc = fn(PyObjectRef, PyStrRef, &VirtualMachine) -> PyResult;
@@ -150,7 +150,7 @@ pub(crate) type DescrSetFunc =
pub(crate) type NewFunc = fn(PyTypeRef, FuncArgs, &VirtualMachine) -> PyResult;
pub(crate) type DelFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult<()>;
fn as_mapping_wrapper(zelf: &PyObjectRef, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
fn as_mapping_wrapper(zelf: &PyObjectRef, _vm: &VirtualMachine) -> PyMappingMethods {
macro_rules! then_some_closure {
($cond:expr, $closure:expr) => {
if $cond {
@@ -160,7 +160,7 @@ fn as_mapping_wrapper(zelf: &PyObjectRef, _vm: &VirtualMachine) -> PyResult<PyMa
}
};
}
Ok(PyMappingMethods {
PyMappingMethods {
length: then_some_closure!(zelf.has_class_attr("__len__"), |zelf, vm| {
vm.call_special_method(zelf, "__len__", ()).map(|obj| {
obj.payload_if_subclass::<PyInt>(vm)
@@ -189,7 +189,7 @@ fn as_mapping_wrapper(zelf: &PyObjectRef, _vm: &VirtualMachine) -> PyResult<PyMa
.map(|_| Ok(()))?,
}
),
})
}
}
fn hash_wrapper(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyResult<PyHash> {
@@ -763,10 +763,8 @@ pub trait AsBuffer: PyValue {
pub trait AsMapping: PyValue {
#[inline]
#[pyslot]
fn slot_as_mapping(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
let zelf = zelf
.downcast_ref()
.ok_or_else(|| vm.new_type_error("unexpected payload for as_mapping".to_owned()))?;
fn slot_as_mapping(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyMappingMethods {
let zelf = unsafe { zelf.downcast_unchecked_ref::<Self>() };
Self::as_mapping(zelf, vm)
}
@@ -792,7 +790,7 @@ pub trait AsMapping: PyValue {
})
}
fn as_mapping(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyMappingMethods>;
fn as_mapping(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyMappingMethods;
fn length(zelf: PyObjectRef, _vm: &VirtualMachine) -> PyResult<usize>;