Merge pull request #18 from traff/master

A method for accessing dictionary items.
This commit is contained in:
Daniel Grunwald 2015-07-13 20:30:08 +02:00
commit f6718a271b
1 changed files with 11 additions and 3 deletions

View File

@ -19,7 +19,7 @@
use ffi; use ffi;
use python::{Python, ToPythonPointer, PythonObject}; use python::{Python, ToPythonPointer, PythonObject};
use conversion::ToPyObject; use conversion::ToPyObject;
use objects::PyObject; use objects::{PyObject, PyList};
use err::{self, PyResult, PyErr}; use err::{self, PyResult, PyErr};
/// Represents a Python `dict`. /// Represents a Python `dict`.
@ -77,7 +77,7 @@ impl <'p> PyDict<'p> {
pub fn get_item<K>(&self, key: K) -> Option<PyObject<'p>> where K: ToPyObject<'p> { pub fn get_item<K>(&self, key: K) -> Option<PyObject<'p>> where K: ToPyObject<'p> {
let py = self.python(); let py = self.python();
key.with_borrowed_ptr(py, |key| unsafe { key.with_borrowed_ptr(py, |key| unsafe {
PyObject::from_borrowed_ptr_opt(py, PyObject::from_borrowed_ptr_opt(py,
ffi::PyDict_GetItem(self.as_ptr(), key)) ffi::PyDict_GetItem(self.as_ptr(), key))
}) })
} }
@ -102,5 +102,13 @@ impl <'p> PyDict<'p> {
ffi::PyDict_DelItem(self.as_ptr(), key)) ffi::PyDict_DelItem(self.as_ptr(), key))
}) })
} }
}
// List of dict items.
// This is equivalent to the `dict.items()` method.
pub fn items(&self) -> PyList {
let py = self.python();
unsafe {
err::cast_from_owned_ptr_or_panic(py, ffi::PyDict_Items(self.as_ptr()))
}
}
}