Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions src/rdict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,8 @@ impl Rdict {
/// Use list of keys for batch get.
fn __getitem__<'py>(&self, key: &Bound<PyAny>, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
match self.get(key, None, None, py) {
Ok(v) => {
if v.is_none() {
Err(PyKeyError::new_err(format!("key {key} not found")))
} else {
Ok(v)
}
}
Ok(None) => Err(PyKeyError::new_err(format!("key {key} not found"))),
Ok(Some(v)) => Ok(v),
Err(e) => Err(e),
}
}
Expand All @@ -377,7 +372,7 @@ impl Rdict {
default: Option<Bound<'py, PyAny>>,
read_opt: Option<&ReadOptionsPy>,
py: Python<'py>,
) -> PyResult<Bound<'py, PyAny>> {
) -> PyResult<Option<Bound<'py, PyAny>>> {
let db = self.get_db()?;
let read_opt_option = match read_opt {
None => None,
Expand All @@ -395,7 +390,9 @@ impl Rdict {
Some(cf) => cf.clone(),
};
if let Ok(keys) = key.cast() {
return Ok(self.get_batch_inner(db, keys, default, py, &cf)?.into_any());
return Ok(Some(
self.get_batch_inner(db, keys, default, py, &cf)?.into_any(),
));
}
let key_bytes = encode_key(key, self.opt_py.raw_mode)?;
let value_result = db
Expand All @@ -405,17 +402,17 @@ impl Rdict {
None => {
// try to return default value
if let Some(default) = default {
Ok(default)
Ok(Some(default))
} else {
Ok(py.None().bind(py).to_owned())
Ok(None)
}
}
Some(slice) => Ok(decode_value(
Some(slice) => Ok(Some(decode_value(
py,
slice.as_ref(),
&self.loads,
self.opt_py.raw_mode,
)?),
)?)),
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/test_rdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def testGetItem(self):
lambda: self.test_dict[250] if self.test_dict is not None else None,
)

def testGetNoneValue(self):
assert self.test_dict is not None
self.test_dict["none"] = None
self.assertIsNone(self.test_dict["none"])
self.assertIsNone(self.test_dict.get("none"))

def testDelItem(self):
assert self.test_dict is not None
# no exception raise when deleting non-existing key
Expand Down
Loading