Fix a corner case for PyClassInitializer

This commit is contained in:
kngwyu 2019-12-22 23:54:41 +09:00
parent efa16a6dc7
commit e2dc843de5
2 changed files with 22 additions and 6 deletions

View file

@ -344,7 +344,7 @@ impl<T: PyTypeInfo> PyClassInitializer<T> {
let PyClassInitializer { init, super_init } = self;
if let Some(value) = init {
unsafe { shell.py_init(value) };
} else if !T::ConcreteLayout::NEED_INIT {
} else if T::ConcreteLayout::NEED_INIT {
raise_err!(T::NAME);
}
if let Some(super_init) = super_init {

View file

@ -68,16 +68,30 @@ fn inheritance_with_new_methods() {
}
#[pyclass(extends=BaseClass)]
struct InvalidSubClass {
struct InvalidSubClass1 {
#[pyo3(get)]
val2: usize,
}
#[pymethods]
impl InvalidSubClass {
impl InvalidSubClass1 {
#[new]
fn new() -> PyClassInitializer<Self> {
PyClassInitializer::from_value(InvalidSubClass { val2: 5 })
PyClassInitializer::from_value(InvalidSubClass1 { val2: 5 })
}
}
#[pyclass(extends=BaseClass)]
struct InvalidSubClass2 {
#[pyo3(get)]
val2: usize,
}
#[pymethods]
impl InvalidSubClass2 {
#[new]
fn new() -> PyClassInitializer<Self> {
PyClassInitializer::new()
}
}
@ -86,7 +100,9 @@ fn uninit_baseclass_raise_exception() {
let gil = Python::acquire_gil();
let py = gil.python();
let _baseclass = py.get_type::<BaseClass>();
let subclass = py.get_type::<InvalidSubClass>();
let subclass = py.get_type::<InvalidSubClass1>();
py_expect_exception!(py, subclass, "subclass()", RuntimeError);
let subclass = py.get_type::<InvalidSubClass2>();
py_expect_exception!(py, subclass, "subclass()", RuntimeError);
}
@ -94,7 +110,7 @@ fn uninit_baseclass_raise_exception() {
fn uninit_baseclass_returns_err() {
let gil = Python::acquire_gil();
let py = gil.python();
let subclass = pyo3::pyclass::PyClassShell::new_ref(py, InvalidSubClass { val2: 5 });
let subclass = pyo3::pyclass::PyClassShell::new_ref(py, InvalidSubClass1 { val2: 5 });
if let Err(err) = subclass {
py_run!(
py,