Skip to content
Prev Previous commit
Next Next commit
pyproperty generates PyGetSet instead of PyProperty
  • Loading branch information
youknowone committed Feb 5, 2020
commit 5d6c82d432459a93ecc83e26f8e212784df6d869
17 changes: 10 additions & 7 deletions derive/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ fn extract_impl_items(mut items: Vec<ItemSig>) -> Result<TokenStream2, Diagnosti
let properties = properties
.into_iter()
.map(|(name, prop)| {
let getter = match prop.0 {
Some(getter) => getter,
let getter_func = match prop.0 {
Some(func) => func,
None => {
push_err_span!(
diagnostics,
Expand All @@ -417,14 +417,17 @@ fn extract_impl_items(mut items: Vec<ItemSig>) -> Result<TokenStream2, Diagnosti
return TokenStream2::new();
}
};
let add_setter = prop.1.map(|setter| quote!(.add_setter(Self::#setter)));
let (new, setter) = match prop.1 {
Some(func) => (quote! { with_get_set }, quote! { , &Self::#func }),
None => (quote! { with_get }, quote! { }),
};
let str_name = name.to_string();
quote! {
class.set_str_attr(
#name,
::rustpython_vm::obj::objproperty::PropertyBuilder::new(ctx)
.add_getter(Self::#getter)
#add_setter
.create(),
::rustpython_vm::pyobject::PyObject::new(
::rustpython_vm::obj::objgetset::PyGetSet::#new(#str_name.into(), &Self::#getter_func #setter),
ctx.getset_type(), None)
);
}
})
Expand Down
16 changes: 13 additions & 3 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl PyBaseException {
}

#[pyproperty(name = "__traceback__")]
fn get_traceback(&self, _vm: &VirtualMachine) -> Option<PyTracebackRef> {
fn get_traceback(&self) -> Option<PyTracebackRef> {
self.traceback.borrow().clone()
}

Expand All @@ -95,8 +95,13 @@ impl PyBaseException {
}

#[pyproperty(name = "__cause__", setter)]
fn setter_cause(&self, cause: Option<PyBaseExceptionRef>, _vm: &VirtualMachine) {
fn setter_cause(
&self,
cause: Option<PyBaseExceptionRef>,
_vm: &VirtualMachine,
) -> PyResult<()> {
self.cause.replace(cause);
Ok(())
}

#[pyproperty(name = "__context__")]
Expand All @@ -105,8 +110,13 @@ impl PyBaseException {
}

#[pyproperty(name = "__context__", setter)]
fn setter_context(&self, context: Option<PyBaseExceptionRef>, _vm: &VirtualMachine) {
fn setter_context(
&self,
context: Option<PyBaseExceptionRef>,
_vm: &VirtualMachine,
) -> PyResult<()> {
self.context.replace(context);
Ok(())
}

#[pyproperty(name = "__suppress_context__")]
Expand Down
4 changes: 2 additions & 2 deletions vm/src/obj/objfloat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,12 +498,12 @@ impl PyFloat {
pyhash::hash_float(self.value)
}

#[pyproperty(name = "real")]
#[pyproperty]
fn real(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyFloatRef {
zelf
}

#[pyproperty(name = "imag")]
#[pyproperty]
fn imag(&self, _vm: &VirtualMachine) -> f64 {
0.0f64
}
Expand Down
6 changes: 3 additions & 3 deletions vm/src/obj/objfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,17 @@ impl PyFunction {
self.invoke(args, vm)
}

#[pyproperty(name = "__code__")]
#[pyproperty(magic)]
fn code(&self, _vm: &VirtualMachine) -> PyCodeRef {
self.code.clone()
}

#[pyproperty(name = "__defaults__")]
#[pyproperty(magic)]
fn defaults(&self, _vm: &VirtualMachine) -> Option<PyTupleRef> {
self.defaults.clone()
}

#[pyproperty(name = "__kwdefaults__")]
#[pyproperty(magic)]
fn kwdefaults(&self, _vm: &VirtualMachine) -> Option<PyDictRef> {
self.kw_only_defaults.clone()
}
Expand Down
1 change: 1 addition & 0 deletions vm/src/obj/objint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ impl PyInt {
}
#[pyproperty]
fn real(&self, vm: &VirtualMachine) -> PyObjectRef {
// subclasses must return int here
vm.ctx.new_bigint(&self.value)
}

Expand Down
7 changes: 7 additions & 0 deletions vm/src/obj/objtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ impl PyClassRef {
.unwrap_or_else(|| vm.ctx.new_str("builtins".to_owned()))
}

#[pyproperty(magic, setter)]
fn set_module(self, value: PyObjectRef) {
self.attributes
.borrow_mut()
.insert("__module__".to_owned(), value);
}

#[pymethod(magic)]
fn prepare(_name: PyStringRef, _bases: PyObjectRef, vm: &VirtualMachine) -> PyDictRef {
vm.ctx.new_dict()
Expand Down