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] #[pymethods]
impl TzClass { impl TzClass {
#[new] #[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> { fn new(obj: &PyRawObject) {
Ok(obj.init(TzClass {})) obj.init(TzClass {})
} }
fn utcoffset(&self, py: Python<'_>, _dt: &PyDateTime) -> PyResult<Py<PyDelta>> { fn utcoffset(&self, py: Python<'_>, _dt: &PyDateTime) -> PyResult<Py<PyDelta>> {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -436,7 +436,7 @@ mod test {
let dict = <PyDict as PyTryFrom>::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(7i32, 42i32).is_ok()); // change
assert!(dict.set_item(8i32, 123i32).is_ok()); // insert 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)); assert_eq!(None, v.get(&8i32));
} }

View file

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

View file

@ -114,7 +114,7 @@ mod test {
let v = std::u32::MAX; let v = std::u32::MAX;
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<u32>(py).unwrap()); 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()); 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(); let tuple = <PyTuple as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
assert_eq!(3, tuple.len()); assert_eq!(3, tuple.len());
let mut i = 0; for (i, item) in tuple.iter().enumerate() {
for item in tuple { assert_eq!(i + 1, item.extract().unwrap());
i += 1;
assert_eq!(i, item.extract().unwrap());
} }
} }
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -98,7 +98,7 @@ fn pytuple_primitive_iter() {
let gil = Python::acquire_gil(); let gil = Python::acquire_gil();
let py = gil.python(); 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)"); py_assert!(py, tup, "tup == (1, 2, 3)");
} }