mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 19:24:57 +08:00
pyproperty generates PyGetSet instead of PyProperty
This commit is contained in:
1 parent
23381b9937
commit
0aee78de18
6 files changed
+36
-15
No files matched your search
+10
-7
@@ -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,
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
})
|
||||
|
||||
+13
-3
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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__")]
|
||||
@@ -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__")]
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -626,6 +626,7 @@ impl PyInt {
|
||||
}
|
||||
#[pyproperty]
|
||||
fn real(&self, vm: &VirtualMachine) -> PyObjectRef {
|
||||
// subclasses must return int here
|
||||
vm.ctx.new_bigint(&self.value)
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user