Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ proc-macro = true

[dependencies]
syn = { version = "1.0", features = ["full", "extra-traits"] }
syn-ext = { version = "0.2.1", features = ["full"] }
syn-ext = { version = "0.2.3", features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0"
rustpython-compiler = { path = "../compiler/porcelain", version = "0.1.1" }
Expand Down
48 changes: 43 additions & 5 deletions derive/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,51 @@ where

let item_attr = args.attrs.remove(self.index());
let item_meta = MethodItemMeta::from_attr(ident.clone(), &item_attr)?;

let py_name = item_meta.method_name()?;

let sig_doc = args.item.function_or_method_impl().ok().map(|item| {
let sig = item.sig();
let args: Vec<_> = sig
.inputs
.iter()
.filter_map(|arg| {
use syn::FnArg::*;
let arg = match arg {
Receiver(_) => return Some("$self".to_owned()),
Typed(typed) => typed,
};
let ty = arg.ty.as_ref();
let ty = quote!(#ty).to_string();
if ty == "FuncArgs" {
return Some("*args, **kwargs".to_owned());
}
if ty == "& VirtualMachine" {
return None;
}
let ident = match arg.pat.as_ref() {
syn::Pat::Ident(p) => p.ident.to_string(),
// FIXME: other => unreachable!("function arg pattern must be ident but found `{}`", quote!(fn #ident(.. #other ..))),
other => quote!(#other).to_string(),
};
if ident == "zelf" {
return Some("$self".to_owned());
}
if ident == "vm" {
unreachable!("type &VirtualMachine(`{}`) must be filtered already", ty);
}
Some(ident)
})
.collect();
format!("{}({})", py_name, args.join(", "))
});

let tokens = {
let doc = args.attrs.doc().map_or_else(
TokenStream::new,
|doc| quote!(.with_doc(#doc.to_owned(), ctx)),
);
let doc = args.attrs.doc().map_or_else(TokenStream::new, |mut doc| {
if let Some(sig_doc) = sig_doc {
doc = format!("{}\n--\n\n{}", sig_doc, doc);
}
quote!(.with_doc(#doc.to_owned(), ctx))
});
let build_func = match self.method_type.as_str() {
"method" => quote!(.build_method(ctx, class.clone())),
"classmethod" => quote!(.build_classmethod(ctx, class.clone())),
Expand Down
71 changes: 16 additions & 55 deletions vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ impl PyValue for PyBaseObject {

#[pyimpl(flags(BASETYPE))]
impl PyBaseObject {
/// __new__($type, *args, **kwargs)
/// --
///
/// Create and return a new object. See help(type) for accurate signature.
#[pyslot]
fn tp_new(mut args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Expand Down Expand Up @@ -92,105 +89,81 @@ impl PyBaseObject {
Ok(res)
}

/// __eq__($self, value, /)
/// --
///
/// Return self==value.
#[pymethod(magic)]
fn eq(
zelf: PyObjectRef,
other: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
Self::cmp(&zelf, &other, PyComparisonOp::Eq, vm)
Self::cmp(&zelf, &value, PyComparisonOp::Eq, vm)
}

/// __ne__($self, value, /)
/// --
///
/// Return self!=value.
#[pymethod(magic)]
fn ne(
zelf: PyObjectRef,
other: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
Self::cmp(&zelf, &other, PyComparisonOp::Ne, vm)
Self::cmp(&zelf, &value, PyComparisonOp::Ne, vm)
}

/// __lt__($self, value, /)
/// --
///
/// Return self<value.
#[pymethod(magic)]
fn lt(
zelf: PyObjectRef,
other: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
Self::cmp(&zelf, &other, PyComparisonOp::Lt, vm)
Self::cmp(&zelf, &value, PyComparisonOp::Lt, vm)
}

/// __le__($self, value, /)
/// --
///
/// Return self<=value.
#[pymethod(magic)]
fn le(
zelf: PyObjectRef,
other: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
Self::cmp(&zelf, &other, PyComparisonOp::Le, vm)
Self::cmp(&zelf, &value, PyComparisonOp::Le, vm)
}

/// __ge__($self, value, /)
/// --
///
/// Return self>=value.
#[pymethod(magic)]
fn ge(
zelf: PyObjectRef,
other: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
Self::cmp(&zelf, &other, PyComparisonOp::Ge, vm)
Self::cmp(&zelf, &value, PyComparisonOp::Ge, vm)
}

/// __gt__($self, value, /)
/// --
///
/// Return self>value.
#[pymethod(magic)]
fn gt(
zelf: PyObjectRef,
other: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
Self::cmp(&zelf, &other, PyComparisonOp::Gt, vm)
Self::cmp(&zelf, &value, PyComparisonOp::Gt, vm)
}

/// __setattr__($self, name, value /)
/// --
///
/// Implement setattr(self, name, value).
#[pymethod]
fn __setattr__(
obj: PyObjectRef,
attr_name: PyStrRef,
name: PyStrRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<()> {
setattr(&obj, attr_name, Some(value), vm)
setattr(&obj, name, Some(value), vm)
}

/// __delattr__($self, name, /)
/// --
///
/// Implement delattr(self, name).
#[pymethod]
fn __delattr__(obj: PyObjectRef, attr_name: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
setattr(&obj, attr_name, None, vm)
fn __delattr__(obj: PyObjectRef, name: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
setattr(&obj, name, None, vm)
}

#[pyslot]
Expand All @@ -203,18 +176,12 @@ impl PyBaseObject {
setattr(obj, attr_name, value, vm)
}

/// __str__($self, /)
/// --
///
/// Return str(self).
#[pymethod(magic)]
fn str(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyStrRef> {
vm.to_repr(&zelf)
}

/// __repr__($self, /)
/// --
///
/// Return repr(self).
#[pymethod(magic)]
fn repr(zelf: PyObjectRef) -> String {
Expand Down Expand Up @@ -288,9 +255,6 @@ impl PyBaseObject {
}
}

/// __getattribute__($self, name, /)
/// --
///
/// Return getattr(self, name).
#[pymethod(name = "__getattribute__")]
#[pyslot]
Expand Down Expand Up @@ -321,9 +285,6 @@ impl PyBaseObject {
Ok(zelf.get_id() as _)
}

/// __hash__($self, /)
/// --
///
/// Return hash(self).
#[pymethod(magic)]
fn hash(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyHash> {
Expand Down