Simpler new and clippy fixes

This commit is contained in:
konstin 2019-02-23 18:38:00 +01:00
parent ad590bd158
commit 6cd07c369c
21 changed files with 60 additions and 76 deletions

View File

@ -190,8 +190,8 @@ pub struct TzClass {}
#[pymethods]
impl TzClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
Ok(obj.init(TzClass {}))
fn new(obj: &PyRawObject) {
obj.init(TzClass {})
}
fn utcoffset(&self, py: Python<'_>, _dt: &PyDateTime) -> PyResult<Py<PyDelta>> {

View File

@ -16,8 +16,8 @@ pub struct DictSize {
#[pymethods]
impl DictSize {
#[new]
fn __new__(obj: &PyRawObject, expected: u32) -> PyResult<()> {
Ok(obj.init(DictSize { expected }))
fn new(obj: &PyRawObject, expected: u32) {
obj.init(DictSize { expected })
}
fn iter_dict(&mut self, _py: Python<'_>, dict: &PyDict) -> PyResult<u32> {

View File

@ -13,10 +13,10 @@ pub struct ModClass {
#[pymethods]
impl ModClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
Ok(obj.init(ModClass {
fn new(obj: &PyRawObject) {
obj.init(ModClass {
_somefield: String::from("contents"),
}))
})
}
fn noop(&self, x: usize) -> usize {

View File

@ -8,8 +8,8 @@ pub struct Subclassable {}
#[pymethods]
impl Subclassable {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
Ok(obj.init(Subclassable {}))
fn new(obj: &PyRawObject) {
obj.init(Subclassable {});
}
}

View File

@ -16,10 +16,10 @@ struct WordCounter {
#[pymethods]
impl WordCounter {
#[new]
fn new(obj: &PyRawObject, path: String) -> PyResult<()> {
Ok(obj.init(WordCounter {
fn new(obj: &PyRawObject, path: String) {
obj.init(WordCounter {
path: PathBuf::from(path),
}))
});
}
/// Searches for the word, parallelized by rayon

View File

@ -118,12 +118,12 @@ struct MyClass {
impl MyClass {
#[new]
fn __new__(obj: &PyRawObject, num: i32) -> PyResult<()> {
fn new(obj: &PyRawObject, num: i32) {
obj.init({
MyClass {
num,
}
})
});
}
}
```
@ -158,8 +158,8 @@ struct BaseClass {
#[pymethods]
impl BaseClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
Ok(obj.init(BaseClass{ val1: 10 }))
fn new(obj: &PyRawObject) {
obj.init(BaseClass{ val1: 10 });
}
pub fn method(&self) -> PyResult<()> {
@ -175,9 +175,9 @@ struct SubClass {
#[pymethods]
impl SubClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
fn new(obj: &PyRawObject) {
obj.init(SubClass{ val2: 10 });
BaseClass::__new__(obj)
BaseClass::new(obj);
}
fn method2(&self) -> PyResult<()> {

View File

@ -36,12 +36,12 @@ struct MyClass {
#[pymethods]
impl MyClass {
#[new]
fn __new__(obj: &PyRawObject, num: u32) -> PyResult<()> {
fn new(obj: &PyRawObject, num: u32) {
obj.init({
MyClass {
num,
}
})
});
}
fn half(&self) -> PyResult<u32> {

View File

@ -43,7 +43,7 @@ pub fn parse_fn_args<'p>(
output: &mut [Option<&'p PyObjectRef>],
) -> PyResult<()> {
let nargs = args.len();
let nkeywords = kwargs.map_or(0, |d| d.len());
let nkeywords = kwargs.map_or(0, PyDict::len);
if !accept_args && !accept_kwargs && (nargs + nkeywords > params.len()) {
return Err(TypeError::py_err(format!(
"{}{} takes at most {} argument{} ({} given)",

View File

@ -134,9 +134,9 @@ impl<'p> Python<'p> {
}
let globals = globals
.map(|g| g.as_ptr())
.map(ToPyPointer::as_ptr)
.unwrap_or_else(|| ffi::PyModule_GetDict(mptr));
let locals = locals.map(|l| l.as_ptr()).unwrap_or(globals);
let locals = locals.map(ToPyPointer::as_ptr).unwrap_or(globals);
let res_ptr = ffi::PyRun_StringFlags(
code.as_ptr(),

View File

@ -83,8 +83,8 @@ pub const PY_TYPE_FLAG_DICT: usize = 1 << 3;
/// #[pymethods]
/// impl MyClass {
/// #[new]
/// fn __new__(obj: &PyRawObject) -> PyResult<()> {
/// Ok(obj.init(MyClass { }))
/// fn new(obj: &PyRawObject) {
/// obj.init(MyClass { })
/// }
/// }
/// ```

View File

@ -436,7 +436,7 @@ mod test {
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!
assert_eq!(32i32, v[&7i32]); // not updated!
assert_eq!(None, v.get(&8i32));
}

View File

@ -376,10 +376,8 @@ mod test {
let v = vec![1, 2, 3, 4];
let ob = v.to_object(py);
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let mut i = 0;
for el in list {
i += 1;
assert_eq!(i, el.extract::<i32>().unwrap());
for (i, item) in list.iter().enumerate() {
assert_eq!((i + 1) as i32, item.extract::<i32>().unwrap());
}
}

View File

@ -114,7 +114,7 @@ mod test {
let v = std::u32::MAX;
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<u32>(py).unwrap());
assert_eq!(v as u64, obj.extract::<u64>(py).unwrap());
assert_eq!(u64::from(v), obj.extract::<u64>(py).unwrap());
assert!(obj.extract::<i32>(py).is_err());
}

View File

@ -308,10 +308,8 @@ mod test {
let tuple = <PyTuple as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert_eq!(3, tuple.len());
let mut i = 0;
for item in tuple {
i += 1;
assert_eq!(i, item.extract().unwrap());
for (i, item) in tuple.iter().enumerate() {
assert_eq!(i + 1, item.extract().unwrap());
}
}
}

View File

@ -6,9 +6,9 @@ struct EmptyClassWithNew {}
#[pymethods]
impl EmptyClassWithNew {
#[__new__]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
Ok(obj.init(EmptyClassWithNew {}))
#[new]
fn new(obj: &PyRawObject) {
obj.init(EmptyClassWithNew {});
}
}
@ -32,8 +32,8 @@ struct NewWithOneArg {
#[pymethods]
impl NewWithOneArg {
#[new]
fn __new__(obj: &PyRawObject, arg: i32) -> PyResult<()> {
Ok(obj.init(NewWithOneArg { _data: arg }))
fn new(obj: &PyRawObject, arg: i32) {
obj.init(NewWithOneArg { _data: arg })
}
}
@ -56,11 +56,11 @@ struct NewWithTwoArgs {
#[pymethods]
impl NewWithTwoArgs {
#[new]
fn __new__(obj: &PyRawObject, arg1: i32, arg2: i32) -> PyResult<()> {
Ok(obj.init(NewWithTwoArgs {
fn new(obj: &PyRawObject, arg1: i32, arg2: i32) {
obj.init(NewWithTwoArgs {
_data1: arg1,
_data2: arg2,
}))
})
}
}

View File

@ -148,11 +148,7 @@ fn test_pydate_out_of_bounds() {
for val in INVALID_DATES.into_iter() {
let (year, month, day) = val;
let dt = PyDate::new(py, *year, *month, *day);
let msg = format!("Should have raised an error: {:#?}", val);
match dt {
Ok(_) => assert!(false, msg),
Err(_) => assert!(true),
}
dt.unwrap_err();
}
}
@ -162,14 +158,10 @@ fn test_pytime_out_of_bounds() {
// This test is an XFAIL on Python < 3.6 until bounds checking is implemented
let gil = Python::acquire_gil();
let py = gil.python();
for val in INVALID_TIMES.into_iter() {
for val in INVALID_TIMES {
let (hour, minute, second, microsecond) = val;
let dt = PyTime::new(py, *hour, *minute, *second, *microsecond, None);
let msg = format!("Should have raised an error: {:#?}", val);
match dt {
Ok(_) => assert!(false, msg),
Err(_) => assert!(true),
}
dt.unwrap_err();
}
}
@ -182,8 +174,8 @@ fn test_pydatetime_out_of_bounds() {
let valid_time = (0, 0, 0, 0);
let valid_date = (2018, 1, 1);
let invalid_dates = INVALID_DATES.into_iter().zip(iter::repeat(&valid_time));
let invalid_times = iter::repeat(&valid_date).zip(INVALID_TIMES.into_iter());
let invalid_dates = INVALID_DATES.iter().zip(iter::repeat(&valid_time));
let invalid_times = iter::repeat(&valid_date).zip(INVALID_TIMES.iter());
let vals = invalid_dates.chain(invalid_times);
@ -202,10 +194,6 @@ fn test_pydatetime_out_of_bounds() {
*microsecond,
None,
);
let msg = format!("Should have raised an error: {:#?}", val);
match dt {
Ok(_) => assert!(false, msg),
Err(_) => assert!(true),
}
dt.unwrap_err();
}
}

View File

@ -213,8 +213,8 @@ struct BaseClassWithDrop {
#[pymethods]
impl BaseClassWithDrop {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
Ok(obj.init(BaseClassWithDrop { data: None }))
fn new(obj: &PyRawObject) {
obj.init(BaseClassWithDrop { data: None })
}
}
@ -234,9 +234,9 @@ struct SubClassWithDrop {
#[pymethods]
impl SubClassWithDrop {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
fn new(obj: &PyRawObject) {
obj.init(SubClassWithDrop { data: None });
BaseClassWithDrop::__new__(obj)
BaseClassWithDrop::new(obj);
}
}

View File

@ -34,8 +34,8 @@ fn subclass() {
#[pymethods]
impl BaseClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
Ok(obj.init(BaseClass { val1: 10 }))
fn new(obj: &PyRawObject) {
obj.init(BaseClass { val1: 10 })
}
}
@ -48,9 +48,9 @@ struct SubClass {
#[pymethods]
impl SubClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
fn new(obj: &PyRawObject) {
obj.init(SubClass { val2: 5 });
BaseClass::__new__(obj)
BaseClass::new(obj);
}
}

View File

@ -65,8 +65,8 @@ struct ClassMethod {}
#[pymethods]
impl ClassMethod {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
Ok(obj.init(ClassMethod {}))
fn new(obj: &PyRawObject) {
obj.init(ClassMethod {})
}
#[classmethod]
@ -129,8 +129,8 @@ struct StaticMethod {}
#[pymethods]
impl StaticMethod {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
Ok(obj.init(StaticMethod {}))
fn new(obj: &PyRawObject) {
obj.init(StaticMethod {})
}
#[staticmethod]

View File

@ -32,12 +32,12 @@ fn module_with_functions(py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m, "sum_as_string")]
fn sum_as_string_py(_py: Python, a: i64, b: i64) -> PyResult<String> {
let out = sum_as_string(a, b);
return Ok(out);
Ok(out)
}
#[pyfn(m, "no_parameters")]
fn no_parameters() -> PyResult<usize> {
return Ok(42);
Ok(42)
}
m.add_class::<EmptyClass>().unwrap();

View File

@ -98,7 +98,7 @@ fn pytuple_primitive_iter() {
let gil = Python::acquire_gil();
let py = gil.python();
let tup = PyTuple::new(py, [1u32, 2, 3].into_iter());
let tup = PyTuple::new(py, [1u32, 2, 3].iter());
py_assert!(py, tup, "tup == (1, 2, 3)");
}