Skip to content

Fix proc macro for init fn.#651

Closed
sebpuetz wants to merge 1 commit intoPyO3:masterfrom
sebpuetz:fix-init-fn
Closed

Fix proc macro for init fn.#651
sebpuetz wants to merge 1 commit intoPyO3:masterfrom
sebpuetz:fix-init-fn

Conversation

@sebpuetz
Copy link
Contributor

The if statement evaluated to true if the return type was not "()" or "PyResult<()>" which was always the case since "()" != "PyResult<()>".

@sebpuetz
Copy link
Contributor Author

test_datetime_check failed on an earlier build for windows on python 3.6 with an unwrap on Err which must have originated this method:

fn _get_subclasses<'p>(
py: &'p Python,
py_type: &str,
args: &str,
) -> PyResult<(&'p PyAny, &'p PyAny, &'p PyAny)> {
// Import the class from Python and create some subclasses
let datetime = py.import("datetime")?;
let locals = [(py_type, datetime.get(py_type)?)].into_py_dict(*py);
let make_subclass_py = format!("class Subklass({}):\n pass", py_type);
let make_sub_subclass_py = "class SubSubklass(Subklass):\n pass";
py.run(&make_subclass_py, None, Some(&locals))?;
py.run(&make_sub_subclass_py, None, Some(&locals))?;
// Construct an instance of the base class
let obj = py.eval(&format!("{}({})", py_type, args), None, Some(&locals))?;
// Construct an instance of the subclass
let sub_obj = py.eval(&format!("Subklass({})", args), None, Some(&locals))?;
// Construct an instance of the sub-subclass
let sub_sub_obj = py.eval(&format!("SubSubklass({})", args), None, Some(&locals))?;
Ok((obj, sub_obj, sub_sub_obj))
}

The tests passed after pushing the same code again.

@kngwyu
Copy link
Member

kngwyu commented Oct 27, 2019

Thank you, but... is this feature actually used?
This is called for

impl Hoge {
    #[init] 
    fn __init__(...) -> {}
}

, but I can't find any document about this.
If this feature is not used, we should remove it instead of this fix.

@konstin
Do you know what's this?

@sebpuetz
Copy link
Contributor Author

It does get called, although I'm not sure about the use of it:

#[pyclass]
pub struct Foo {
    bar: usize,
}

#[pymethods]
impl Foo {
    #[init]
    fn init(&mut self) -> () {
        println!("{}", self.bar);
        self.bar = 10;
        println!("{}", self.bar);
    }

    #[new]
    fn new(obj: &PyRawObject) -> PyResult<()> {
        obj.init(Self { bar: 0 });
        Ok(())
    }
}
>>> Foo()
0
10

@konstin
Copy link
Member

konstin commented Oct 28, 2019

Do you know what's this?

Unfortunately not

@programmerjake
Copy link
Contributor

Do you know what's this?

Unfortunately not

I would guess #[init] is the equivalent of Python's __init__ method, whereas #[new] is the equivalent of __new__.

@Alexander-N
Copy link
Member

Relevant: dgrunwald/rust-cpython#11 (comment)

@kngwyu
Copy link
Member

kngwyu commented Oct 31, 2019

Thank you for comments.
I opened #658 which removes #[init] feature.
Please feel free to write your opinion on the thread.

@kngwyu
Copy link
Member

kngwyu commented Nov 9, 2019

I'm sorry for closing this PR via #658.
Thank you for your contribution, though.

@kngwyu kngwyu closed this Nov 9, 2019
@Alexander-N Alexander-N mentioned this pull request Jan 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants