Skip to content

Commit 0aee78d

Browse files
committed
pyproperty generates PyGetSet instead of PyProperty
1 parent 23381b9 commit 0aee78d

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

derive/src/pyclass.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,8 @@ fn extract_impl_items(mut items: Vec<ItemSig>) -> Result<TokenStream2, Diagnosti
405405
let properties = properties
406406
.into_iter()
407407
.map(|(name, prop)| {
408-
let getter = match prop.0 {
409-
Some(getter) => getter,
408+
let getter_func = match prop.0 {
409+
Some(func) => func,
410410
None => {
411411
push_err_span!(
412412
diagnostics,
@@ -417,14 +417,17 @@ fn extract_impl_items(mut items: Vec<ItemSig>) -> Result<TokenStream2, Diagnosti
417417
return TokenStream2::new();
418418
}
419419
};
420-
let add_setter = prop.1.map(|setter| quote!(.add_setter(Self::#setter)));
420+
let (new, setter) = match prop.1 {
421+
Some(func) => (quote! { with_get_set }, quote! { , &Self::#func }),
422+
None => (quote! { with_get }, quote! { }),
423+
};
424+
let str_name = name.to_string();
421425
quote! {
422426
class.set_str_attr(
423427
#name,
424-
::rustpython_vm::obj::objproperty::PropertyBuilder::new(ctx)
425-
.add_getter(Self::#getter)
426-
#add_setter
427-
.create(),
428+
::rustpython_vm::pyobject::PyObject::new(
429+
::rustpython_vm::obj::objgetset::PyGetSet::#new(#str_name.into(), &Self::#getter_func #setter),
430+
ctx.getset_type(), None)
428431
);
429432
}
430433
})

vm/src/exceptions.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl PyBaseException {
8080
}
8181

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

@@ -95,8 +95,13 @@ impl PyBaseException {
9595
}
9696

9797
#[pyproperty(name = "__cause__", setter)]
98-
fn setter_cause(&self, cause: Option<PyBaseExceptionRef>, _vm: &VirtualMachine) {
98+
fn setter_cause(
99+
&self,
100+
cause: Option<PyBaseExceptionRef>,
101+
_vm: &VirtualMachine,
102+
) -> PyResult<()> {
99103
self.cause.replace(cause);
104+
Ok(())
100105
}
101106

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

107112
#[pyproperty(name = "__context__", setter)]
108-
fn setter_context(&self, context: Option<PyBaseExceptionRef>, _vm: &VirtualMachine) {
113+
fn setter_context(
114+
&self,
115+
context: Option<PyBaseExceptionRef>,
116+
_vm: &VirtualMachine,
117+
) -> PyResult<()> {
109118
self.context.replace(context);
119+
Ok(())
110120
}
111121

112122
#[pyproperty(name = "__suppress_context__")]

vm/src/obj/objfloat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,12 @@ impl PyFloat {
498498
pyhash::hash_float(self.value)
499499
}
500500

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

506-
#[pyproperty(name = "imag")]
506+
#[pyproperty]
507507
fn imag(&self, _vm: &VirtualMachine) -> f64 {
508508
0.0f64
509509
}

vm/src/obj/objfunction.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,17 @@ impl PyFunction {
249249
self.invoke(args, vm)
250250
}
251251

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

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

262-
#[pyproperty(name = "__kwdefaults__")]
262+
#[pyproperty(magic)]
263263
fn kwdefaults(&self, _vm: &VirtualMachine) -> Option<PyDictRef> {
264264
self.kw_only_defaults.clone()
265265
}

vm/src/obj/objint.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ impl PyInt {
626626
}
627627
#[pyproperty]
628628
fn real(&self, vm: &VirtualMachine) -> PyObjectRef {
629+
// subclasses must return int here
629630
vm.ctx.new_bigint(&self.value)
630631
}
631632

vm/src/obj/objtype.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ impl PyClassRef {
145145
.unwrap_or_else(|| vm.ctx.new_str("builtins".to_owned()))
146146
}
147147

148+
#[pyproperty(magic, setter)]
149+
fn set_module(self, value: PyObjectRef) {
150+
self.attributes
151+
.borrow_mut()
152+
.insert("__module__".to_owned(), value);
153+
}
154+
148155
#[pymethod(magic)]
149156
fn prepare(_name: PyStringRef, _bases: PyObjectRef, vm: &VirtualMachine) -> PyDictRef {
150157
vm.ctx.new_dict()

0 commit comments

Comments
 (0)