Move get_item_option back into ItemProtocol

This commit is contained in:
Windel Bouwman
2019-07-23 23:31:09 +02:00
parent e138a7303d
commit f994b0580f
3 changed files with 19 additions and 20 deletions

View File

@@ -12,7 +12,6 @@ use super::objbool;
use super::objiter;
use super::objstr;
use crate::dictdatatype;
use crate::obj::objtype;
use crate::obj::objtype::PyClassRef;
use crate::pyobject::PyClassImpl;
@@ -215,24 +214,6 @@ impl PyDictRef {
Err(vm.new_key_error(key.clone()))
}
#[cfg_attr(feature = "flame-it", flame("PyDictRef"))]
pub fn get_item_option<T: IntoPyObject>(
&self,
key: T,
vm: &VirtualMachine,
) -> PyResult<Option<PyObjectRef>> {
match self.clone().inner_getitem(key.into_pyobject(vm)?, vm) {
Ok(value) => Ok(Some(value)),
Err(exc) => {
if objtype::isinstance(&exc, &vm.ctx.exceptions.key_error) {
Ok(None)
} else {
Err(exc)
}
}
}
}
fn get(
self,
key: PyObjectRef,

View File

@@ -11,7 +11,7 @@ use crate::obj::objfunction::PyMethod;
use crate::obj::objstr;
use crate::obj::objtype::{PyClass, PyClassRef};
use crate::pyobject::{
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
ItemProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
};
use crate::scope::NameProtocol;
use crate::vm::VirtualMachine;

View File

@@ -1021,6 +1021,24 @@ pub trait ItemProtocol {
vm: &VirtualMachine,
) -> PyResult;
fn del_item<T: IntoPyObject>(&self, key: T, vm: &VirtualMachine) -> PyResult;
#[cfg_attr(feature = "flame-it", flame("PyDictRef"))]
fn get_item_option<T: IntoPyObject>(
&self,
key: T,
vm: &VirtualMachine,
) -> PyResult<Option<PyObjectRef>> {
match self.get_item(key, vm) {
Ok(value) => Ok(Some(value)),
Err(exc) => {
if objtype::isinstance(&exc, &vm.ctx.exceptions.key_error) {
Ok(None)
} else {
Err(exc)
}
}
}
}
}
impl ItemProtocol for PyObjectRef {