Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use PyTryFrom explicitly (fixes for 1.26.0-nightly) #137

Merged
merged 1 commit into from
Mar 29, 2018
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
4 changes: 2 additions & 2 deletions src/argparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ pub fn parse_args<'p>(fname: Option<&str>, params: &[ParamDescription],
if !accept_kwargs && used_keywords != nkeywords {
// check for extraneous keyword arguments
for item in kwargs.unwrap().items().iter() {
let item = PyTuple::try_from(item)?;
let key = PyString::try_from(item.get_item(0))?.to_string()?;
let item = <PyTuple as PyTryFrom>::try_from(item)?;
let key = <PyString as PyTryFrom>::try_from(item.get_item(0))?.to_string()?;
if !params.iter().any(|p| p.name == key) {
return Err(exc::TypeError::new(
format!("'{}' is an invalid keyword argument for this function", key)));
Expand Down
2 changes: 1 addition & 1 deletion src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<'a, T> FromPyObject<'a> for &'a T
{
#[inline]
default fn extract(ob: &'a PyObjectRef) -> PyResult<&'a T> {
Ok(T::try_from(ob)?)
Ok(<T as PyTryFrom>::try_from(ob)?)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/objectprotocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ mod test {
let gil = Python::acquire_gil();
let py = gil.python();
let v = "Hello\n".to_object(py);
let s = PyString::try_from(v.as_ref(py)).unwrap();
let s = <PyString as PyTryFrom>::try_from(v.as_ref(py)).unwrap();
assert_eq!(format!("{:?}", s), "'Hello\\n'");
}

Expand All @@ -483,7 +483,7 @@ mod test {
let gil = Python::acquire_gil();
let py = gil.python();
let v = "Hello\n".to_object(py);
let s = PyString::try_from(v.as_ref(py)).unwrap();
let s = <PyString as PyTryFrom>::try_from(v.as_ref(py)).unwrap();
assert_eq!(format!("{}", s), "Hello\n");
}

Expand Down
44 changes: 22 additions & 22 deletions src/objects/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,11 @@ mod test {
let py = gil.python();
let mut v = HashMap::new();
let ob = v.to_object(py);
let dict = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert_eq!(0, dict.len());
v.insert(7, 32);
let ob = v.to_object(py);
let dict2 = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict2 = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert_eq!(1, dict2.len());
}

Expand All @@ -346,7 +346,7 @@ mod test {
let mut v = HashMap::new();
v.insert(7, 32);
let ob = v.to_object(py);
let dict = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert_eq!(true, dict.contains(7i32).unwrap());
assert_eq!(false, dict.contains(8i32).unwrap());
}
Expand All @@ -358,7 +358,7 @@ mod test {
let mut v = HashMap::new();
v.insert(7, 32);
let ob = v.to_object(py);
let dict = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert_eq!(32, dict.get_item(7i32).unwrap().extract::<i32>().unwrap());
assert_eq!(None, dict.get_item(8i32));
}
Expand All @@ -370,7 +370,7 @@ mod test {
let mut v = HashMap::new();
v.insert(7, 32);
let ob = v.to_object(py);
let dict = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert!(dict.set_item(7i32, 42i32).is_ok()); // change
assert!(dict.set_item(8i32, 123i32).is_ok()); // insert
assert_eq!(42i32, dict.get_item(7i32).unwrap().extract::<i32>().unwrap());
Expand Down Expand Up @@ -402,7 +402,7 @@ mod test {
let mut v = HashMap::new();
v.insert(7, 32);
let ob = v.to_object(py);
let dict = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert!(dict.set_item(7i32, 42i32).is_ok()); // change
assert!(dict.set_item(8i32, 123i32).is_ok()); // insert
assert_eq!(32i32, *v.get(&7i32).unwrap()); // not updated!
Expand All @@ -416,7 +416,7 @@ mod test {
let mut v = HashMap::new();
v.insert(7, 32);
let ob = v.to_object(py);
let dict = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert!(dict.del_item(7i32).is_ok());
assert_eq!(0, dict.len());
assert_eq!(None, dict.get_item(7i32));
Expand All @@ -429,7 +429,7 @@ mod test {
let mut v = HashMap::new();
v.insert(7, 32);
let ob = v.to_object(py);
let dict = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert!(dict.del_item(7i32).is_ok()); // change
assert_eq!(32i32, *v.get(&7i32).unwrap()); // not updated!
}
Expand All @@ -443,7 +443,7 @@ mod test {
v.insert(8, 42);
v.insert(9, 123);
let ob = v.to_object(py);
let dict = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
let mut key_sum = 0;
let mut value_sum = 0;
Expand All @@ -465,7 +465,7 @@ mod test {
v.insert(8, 42);
v.insert(9, 123);
let ob = v.to_object(py);
let dict = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
let mut key_sum = 0;
for el in dict.keys().iter() {
Expand All @@ -483,7 +483,7 @@ mod test {
v.insert(8, 42);
v.insert(9, 123);
let ob = v.to_object(py);
let dict = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
let mut values_sum = 0;
for el in dict.values().iter() {
Expand All @@ -501,7 +501,7 @@ mod test {
v.insert(8, 42);
v.insert(9, 123);
let ob = v.to_object(py);
let dict = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let mut key_sum = 0;
let mut value_sum = 0;
for (key, value) in dict.iter() {
Expand All @@ -521,7 +521,7 @@ mod test {
v.insert(8, 42);
v.insert(9, 123);
let ob = v.to_object(py);
let dict = PyDict::try_from(ob.as_ref(py)).unwrap();
let dict = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let mut key_sum = 0;
let mut value_sum = 0;
for (key, value) in dict {
Expand All @@ -541,7 +541,7 @@ mod test {
map.insert(1, 1);

let m = map.to_object(py);
let py_map = PyDict::try_from(m.as_ref(py)).unwrap();
let py_map = <PyDict as PyTryFrom>::try_from(m.as_ref(py)).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
Expand All @@ -556,7 +556,7 @@ mod test {
map.insert(1, 1);

let m = map.to_object(py);
let py_map = PyDict::try_from(m.as_ref(py)).unwrap();
let py_map = <PyDict as PyTryFrom>::try_from(m.as_ref(py)).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
Expand All @@ -571,7 +571,7 @@ mod test {
map.insert(1, 1);

let m = map.into_object(py);
let py_map = PyDict::try_from(m.as_ref(py)).unwrap();
let py_map = <PyDict as PyTryFrom>::try_from(m.as_ref(py)).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
Expand All @@ -587,7 +587,7 @@ mod test {

let m = map.into_dict_ptr(py);
let ob = unsafe{PyObject::from_owned_ptr(py, m)};
let py_map = PyDict::try_from(ob.as_ref(py)).unwrap();
let py_map = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
Expand All @@ -602,7 +602,7 @@ mod test {
map.insert(1, 1);

let m = map.into_object(py);
let py_map = PyDict::try_from(m.as_ref(py)).unwrap();
let py_map = <PyDict as PyTryFrom>::try_from(m.as_ref(py)).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
Expand All @@ -618,7 +618,7 @@ mod test {

let m = map.into_dict_ptr(py);
let ob = unsafe{PyObject::from_owned_ptr(py, m)};
let py_map = PyDict::try_from(ob.as_ref(py)).unwrap();
let py_map = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
Expand All @@ -631,14 +631,14 @@ mod test {

let m = ((1, 1),).into_dict_ptr(py);
let ob = unsafe{PyObject::from_owned_ptr(py, m)};
let py_map = PyDict::try_from(ob.as_ref(py)).unwrap();
let py_map = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();

assert!(py_map.len() == 1);
assert!( py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);

let m = ((1, 1), (2, 3)).into_dict_ptr(py);
let ob = unsafe{PyObject::from_owned_ptr(py, m)};
let py_map = PyDict::try_from(ob.as_ref(py)).unwrap();
let py_map = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();

assert!(py_map.len() == 2);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
Expand All @@ -652,7 +652,7 @@ mod test {

let m = (1, 1).into_dict_ptr(py);
let ob = unsafe{PyObject::from_owned_ptr(py, m)};
let py_map = PyDict::try_from(ob.as_ref(py)).unwrap();
let py_map = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
Expand Down
6 changes: 3 additions & 3 deletions src/objects/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mod tests {
let gil_guard = Python::acquire_gil();
let py = gil_guard.python();
let obj = vec![10, 20].to_object(py);
let inst = PyObjectRef::try_from(obj.as_ref(py)).unwrap();
let inst = <PyObjectRef as PyTryFrom>::try_from(obj.as_ref(py)).unwrap();
let mut it = inst.iter().unwrap();
assert_eq!(10, it.next().unwrap().unwrap().extract().unwrap());
assert_eq!(20, it.next().unwrap().unwrap().extract().unwrap());
Expand All @@ -102,7 +102,7 @@ mod tests {
{
let gil_guard = Python::acquire_gil();
let py = gil_guard.python();
let inst = PyObjectRef::try_from(obj.as_ref(py)).unwrap();
let inst = <PyObjectRef as PyTryFrom>::try_from(obj.as_ref(py)).unwrap();
let mut it = inst.iter().unwrap();

assert_eq!(10, it.next().unwrap().unwrap().extract().unwrap());
Expand Down Expand Up @@ -130,7 +130,7 @@ mod tests {

{
let _pool = GILPool::new();
let inst = PyObjectRef::try_from(obj.as_ref(py)).unwrap();
let inst = <PyObjectRef as PyTryFrom>::try_from(obj.as_ref(py)).unwrap();
let mut it = inst.iter().unwrap();

assert_eq!(10, it.next().unwrap().unwrap().extract().unwrap());
Expand Down
20 changes: 10 additions & 10 deletions src/objects/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ mod test {
let py = gil.python();
let v = vec![1,2,3,4];
let ob = v.to_object(py);
let list = PyList::try_from(ob.as_ref(py)).unwrap();
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert_eq!(4, list.len());
}

Expand All @@ -210,7 +210,7 @@ mod test {
let py = gil.python();
let v = vec![2, 3, 5, 7];
let ob = v.to_object(py);
let list = PyList::try_from(ob.as_ref(py)).unwrap();
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
assert_eq!(3, list.get_item(1).extract::<i32>().unwrap());
assert_eq!(5, list.get_item(2).extract::<i32>().unwrap());
Expand All @@ -223,7 +223,7 @@ mod test {
let py = gil.python();
let v = vec![2, 3, 5, 7];
let ob = v.to_object(py);
let list = PyList::try_from(ob.as_ref(py)).unwrap();
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert_eq!(2, list.get_parked_item(0).extract::<i32>(py).unwrap());
assert_eq!(3, list.get_parked_item(1).extract::<i32>(py).unwrap());
assert_eq!(5, list.get_parked_item(2).extract::<i32>(py).unwrap());
Expand All @@ -236,7 +236,7 @@ mod test {
let py = gil.python();
let v = vec![2, 3, 5, 7];
let ob = v.to_object(py);
let list = PyList::try_from(ob.as_ref(py)).unwrap();
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let val = 42i32.to_object(py);
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
list.set_item(0, val).unwrap();
Expand All @@ -253,7 +253,7 @@ mod test {
let _pool = ::GILPool::new();
let v = vec![2];
let ob = v.to_object(py);
let list = PyList::try_from(ob.as_ref(py)).unwrap();
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let none = py.None();
cnt = none.get_refcnt();
list.set_item(0, none).unwrap();
Expand All @@ -268,7 +268,7 @@ mod test {
let py = gil.python();
let v = vec![2, 3, 5, 7];
let ob = v.to_object(py);
let list = PyList::try_from(ob.as_ref(py)).unwrap();
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let val = 42i32.to_object(py);
assert_eq!(4, list.len());
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
Expand Down Expand Up @@ -301,7 +301,7 @@ mod test {
let py = gil.python();
let v = vec![2];
let ob = v.to_object(py);
let list = PyList::try_from(ob.as_ref(py)).unwrap();
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
list.append(3).unwrap();
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
assert_eq!(3, list.get_item(1).extract::<i32>().unwrap());
Expand Down Expand Up @@ -329,7 +329,7 @@ mod test {
let py = gil.python();
let v = vec![2, 3, 5, 7];
let ob = v.to_object(py);
let list = PyList::try_from(ob.as_ref(py)).unwrap();
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let mut idx = 0;
for el in list.iter() {
assert_eq!(v[idx], el.extract::<i32>().unwrap());
Expand All @@ -344,7 +344,7 @@ mod test {
let py = gil.python();
let v = vec![1, 2, 3, 4];
let ob = v.to_object(py);
let list = PyList::try_from(ob.as_ref(py)).unwrap();
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let mut i = 0;
for el in list {
i += 1;
Expand All @@ -358,7 +358,7 @@ mod test {
let py = gil.python();
let v = vec![2, 3, 5, 7];
let ob = v.to_object(py);
let list = PyList::try_from(ob.as_ref(py)).unwrap();
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let v2 = list.as_ref().extract::<Vec<i32>>().unwrap();
assert_eq!(v, v2);
}
Expand Down
10 changes: 5 additions & 5 deletions src/objects/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl <'source, T> FromPyObject<'source> for Vec<T>

fn extract_sequence<'s, T>(obj: &'s PyObjectRef) -> PyResult<Vec<T>> where T: FromPyObject<'s>
{
let seq = PySequence::try_from(obj)?;
let seq = <PySequence as PyTryFrom>::try_from(obj)?;
let mut v = Vec::with_capacity(seq.len().unwrap_or(0) as usize);
for item in seq.iter()? {
v.push(item?.extract::<T>()?);
Expand All @@ -261,7 +261,7 @@ impl PyTryFrom for PySequence
}

fn try_from_exact(value: &PyObjectRef) -> Result<&PySequence, Self::Error> {
PySequence::try_from(value)
<PySequence as PyTryFrom>::try_from(value)
}

fn try_from_mut(value: &PyObjectRef) -> Result<&mut PySequence, Self::Error> {
Expand Down Expand Up @@ -294,15 +294,15 @@ mod test {
let gil = Python::acquire_gil();
let py = gil.python();
let v = 42i32;
assert!(PySequence::try_from(v.to_object(py).as_ref(py)).is_err());
assert!(<PySequence as PyTryFrom>::try_from(v.to_object(py).as_ref(py)).is_err());
}

#[test]
fn test_strings_are_sequences() {
let gil = Python::acquire_gil();
let py = gil.python();
let v = "London Calling";
assert!(PySequence::try_from(v.to_object(py).as_ref(py)).is_ok());
assert!(<PySequence as PyTryFrom>::try_from(v.to_object(py).as_ref(py)).is_ok());
}
#[test]
fn test_seq_empty() {
Expand Down Expand Up @@ -539,7 +539,7 @@ mod test {
let py = gil.python();
let v = "foo";
let ob = v.to_object(py);
let seq = PySequence::try_from(ob.as_ref(py)).unwrap();
let seq = <PySequence as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert!(seq.list().is_ok());
}

Expand Down
Loading