forked from Rust-related/RustPython
Move get_item_option back into ItemProtocol
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user