Skip to content

Commit 70b63eb

Browse files
committed
new_rustfunc -> new_function & new_method to distinguish bindable functions
1 parent 78b268a commit 70b63eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+470
-433
lines changed

derive/src/pyclass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ pub fn impl_pyimpl(_attr: AttributeArgs, item: Item) -> Result<TokenStream2, Dia
306306
item_ident,
307307
py_name,
308308
} => Some(quote! {
309-
class.set_str_attr(#py_name, ctx.new_rustfunc(Self::#item_ident));
309+
class.set_str_attr(#py_name, ctx.new_method(Self::#item_ident));
310310
}),
311311
ClassItem::ClassMethod {
312312
item_ident,

vm/src/builtins.rs

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -760,13 +760,13 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
760760
#[cfg(target_arch = "wasm32")]
761761
let open = vm.ctx.none();
762762
#[cfg(not(target_arch = "wasm32"))]
763-
let open = vm.ctx.new_rustfunc(io_open);
763+
let open = vm.ctx.new_function(io_open);
764764

765765
#[cfg(feature = "rustpython-compiler")]
766766
{
767767
extend_module!(vm, module, {
768-
"eval" => ctx.new_rustfunc(builtin_eval),
769-
"exec" => ctx.new_rustfunc(builtin_exec),
768+
"eval" => ctx.new_function(builtin_eval),
769+
"exec" => ctx.new_function(builtin_exec),
770770
});
771771
}
772772

@@ -776,73 +776,73 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
776776
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
777777
"__name__" => ctx.new_str(String::from("__main__")),
778778

779-
"abs" => ctx.new_rustfunc(builtin_abs),
780-
"all" => ctx.new_rustfunc(builtin_all),
781-
"any" => ctx.new_rustfunc(builtin_any),
782-
"ascii" => ctx.new_rustfunc(builtin_ascii),
783-
"bin" => ctx.new_rustfunc(builtin_bin),
779+
"abs" => ctx.new_function(builtin_abs),
780+
"all" => ctx.new_function(builtin_all),
781+
"any" => ctx.new_function(builtin_any),
782+
"ascii" => ctx.new_function(builtin_ascii),
783+
"bin" => ctx.new_function(builtin_bin),
784784
"bool" => ctx.bool_type(),
785785
"bytearray" => ctx.bytearray_type(),
786786
"bytes" => ctx.bytes_type(),
787-
"callable" => ctx.new_rustfunc(builtin_callable),
788-
"chr" => ctx.new_rustfunc(builtin_chr),
787+
"callable" => ctx.new_function(builtin_callable),
788+
"chr" => ctx.new_function(builtin_chr),
789789
"classmethod" => ctx.classmethod_type(),
790-
"compile" => ctx.new_rustfunc(builtin_compile),
790+
"compile" => ctx.new_function(builtin_compile),
791791
"complex" => ctx.complex_type(),
792-
"delattr" => ctx.new_rustfunc(builtin_delattr),
792+
"delattr" => ctx.new_function(builtin_delattr),
793793
"dict" => ctx.dict_type(),
794-
"divmod" => ctx.new_rustfunc(builtin_divmod),
795-
"dir" => ctx.new_rustfunc(builtin_dir),
794+
"divmod" => ctx.new_function(builtin_divmod),
795+
"dir" => ctx.new_function(builtin_dir),
796796
"enumerate" => ctx.enumerate_type(),
797797
"float" => ctx.float_type(),
798798
"frozenset" => ctx.frozenset_type(),
799799
"filter" => ctx.filter_type(),
800-
"format" => ctx.new_rustfunc(builtin_format),
801-
"getattr" => ctx.new_rustfunc(builtin_getattr),
802-
"globals" => ctx.new_rustfunc(builtin_globals),
803-
"hasattr" => ctx.new_rustfunc(builtin_hasattr),
804-
"hash" => ctx.new_rustfunc(builtin_hash),
805-
"hex" => ctx.new_rustfunc(builtin_hex),
806-
"id" => ctx.new_rustfunc(builtin_id),
800+
"format" => ctx.new_function(builtin_format),
801+
"getattr" => ctx.new_function(builtin_getattr),
802+
"globals" => ctx.new_function(builtin_globals),
803+
"hasattr" => ctx.new_function(builtin_hasattr),
804+
"hash" => ctx.new_function(builtin_hash),
805+
"hex" => ctx.new_function(builtin_hex),
806+
"id" => ctx.new_function(builtin_id),
807807
"int" => ctx.int_type(),
808-
"isinstance" => ctx.new_rustfunc(builtin_isinstance),
809-
"issubclass" => ctx.new_rustfunc(builtin_issubclass),
810-
"iter" => ctx.new_rustfunc(builtin_iter),
811-
"len" => ctx.new_rustfunc(builtin_len),
808+
"isinstance" => ctx.new_function(builtin_isinstance),
809+
"issubclass" => ctx.new_function(builtin_issubclass),
810+
"iter" => ctx.new_function(builtin_iter),
811+
"len" => ctx.new_function(builtin_len),
812812
"list" => ctx.list_type(),
813-
"locals" => ctx.new_rustfunc(builtin_locals),
813+
"locals" => ctx.new_function(builtin_locals),
814814
"map" => ctx.map_type(),
815-
"max" => ctx.new_rustfunc(builtin_max),
815+
"max" => ctx.new_function(builtin_max),
816816
"memoryview" => ctx.memoryview_type(),
817-
"min" => ctx.new_rustfunc(builtin_min),
817+
"min" => ctx.new_function(builtin_min),
818818
"object" => ctx.object(),
819-
"oct" => ctx.new_rustfunc(builtin_oct),
819+
"oct" => ctx.new_function(builtin_oct),
820820
"open" => open,
821-
"ord" => ctx.new_rustfunc(builtin_ord),
822-
"next" => ctx.new_rustfunc(builtin_next),
823-
"pow" => ctx.new_rustfunc(builtin_pow),
824-
"print" => ctx.new_rustfunc(builtin_print),
821+
"ord" => ctx.new_function(builtin_ord),
822+
"next" => ctx.new_function(builtin_next),
823+
"pow" => ctx.new_function(builtin_pow),
824+
"print" => ctx.new_function(builtin_print),
825825
"property" => ctx.property_type(),
826826
"range" => ctx.range_type(),
827-
"repr" => ctx.new_rustfunc(builtin_repr),
828-
"reversed" => ctx.new_rustfunc(builtin_reversed),
829-
"round" => ctx.new_rustfunc(builtin_round),
827+
"repr" => ctx.new_function(builtin_repr),
828+
"reversed" => ctx.new_function(builtin_reversed),
829+
"round" => ctx.new_function(builtin_round),
830830
"set" => ctx.set_type(),
831-
"setattr" => ctx.new_rustfunc(builtin_setattr),
832-
"sorted" => ctx.new_rustfunc(builtin_sorted),
831+
"setattr" => ctx.new_function(builtin_setattr),
832+
"sorted" => ctx.new_function(builtin_sorted),
833833
"slice" => ctx.slice_type(),
834834
"staticmethod" => ctx.staticmethod_type(),
835835
"str" => ctx.str_type(),
836-
"sum" => ctx.new_rustfunc(builtin_sum),
836+
"sum" => ctx.new_function(builtin_sum),
837837
"super" => ctx.super_type(),
838838
"tuple" => ctx.tuple_type(),
839839
"type" => ctx.type_type(),
840-
"vars" => ctx.new_rustfunc(builtin_vars),
840+
"vars" => ctx.new_function(builtin_vars),
841841
"zip" => ctx.zip_type(),
842-
"exit" => ctx.new_rustfunc(builtin_exit),
843-
"quit" => ctx.new_rustfunc(builtin_exit),
844-
"__import__" => ctx.new_rustfunc(builtin_import),
845-
"__build_class__" => ctx.new_rustfunc(builtin_build_class_),
842+
"exit" => ctx.new_function(builtin_exit),
843+
"quit" => ctx.new_function(builtin_exit),
844+
"__import__" => ctx.new_function(builtin_import),
845+
"__build_class__" => ctx.new_function(builtin_build_class_),
846846

847847
// Constants
848848
"NotImplemented" => ctx.not_implemented(),

vm/src/exceptions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ pub fn init(ctx: &PyContext) {
624624
});
625625

626626
extend_class!(ctx, &excs.import_error, {
627-
"__init__" => ctx.new_rustfunc(import_error_init),
627+
"__init__" => ctx.new_method(import_error_init),
628628
"msg" => ctx.new_property(make_arg_getter(0)),
629629
});
630630

vm/src/frame.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ impl Frame {
11461146
let scope = self.scope.clone();
11471147
let func_obj = vm
11481148
.ctx
1149-
.new_function(code_obj, scope, defaults, kw_only_defaults);
1149+
.new_pyfunction(code_obj, scope, defaults, kw_only_defaults);
11501150

11511151
let name = qualified_name.as_str().split('.').next_back().unwrap();
11521152
vm.set_attr(&func_obj, "__name__", vm.new_str(name.to_string()))?;

vm/src/obj/objbool.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ The class bool is a subclass of the class int, and cannot be subclassed.";
8888
let bool_type = &context.types.bool_type;
8989
extend_class!(context, bool_type, {
9090
(slot new) => bool_new,
91-
"__repr__" => context.new_rustfunc(bool_repr),
92-
"__format__" => context.new_rustfunc(bool_format),
93-
"__or__" => context.new_rustfunc(bool_or),
94-
"__ror__" => context.new_rustfunc(bool_or),
95-
"__and__" => context.new_rustfunc(bool_and),
96-
"__rand__" => context.new_rustfunc(bool_and),
97-
"__xor__" => context.new_rustfunc(bool_xor),
98-
"__rxor__" => context.new_rustfunc(bool_xor),
91+
"__repr__" => context.new_method(bool_repr),
92+
"__format__" => context.new_method(bool_format),
93+
"__or__" => context.new_method(bool_or),
94+
"__ror__" => context.new_method(bool_or),
95+
"__and__" => context.new_method(bool_and),
96+
"__rand__" => context.new_method(bool_and),
97+
"__xor__" => context.new_method(bool_xor),
98+
"__rxor__" => context.new_method(bool_xor),
9999
"__doc__" => context.new_str(bool_doc.to_string()),
100100
});
101101
}

vm/src/obj/objbuiltinfunc.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
use std::fmt;
22

3-
use crate::function::PyNativeFunc;
3+
use crate::function::{PyFuncArgs, PyNativeFunc};
44
use crate::obj::objtype::PyClassRef;
5-
use crate::pyobject::PyValue;
5+
use crate::pyobject::{
6+
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
7+
};
68
use crate::vm::VirtualMachine;
79

10+
#[pyclass]
811
pub struct PyBuiltinFunction {
912
value: PyNativeFunc,
13+
bindable: bool,
1014
}
1115

1216
impl PyValue for PyBuiltinFunction {
@@ -22,11 +26,37 @@ impl fmt::Debug for PyBuiltinFunction {
2226
}
2327

2428
impl PyBuiltinFunction {
25-
pub fn new(value: PyNativeFunc) -> Self {
26-
Self { value }
29+
pub fn new(value: PyNativeFunc, bindable: bool) -> Self {
30+
Self { value, bindable }
2731
}
2832

2933
pub fn as_func(&self) -> &PyNativeFunc {
3034
&self.value
3135
}
3236
}
37+
38+
#[pyimpl]
39+
impl PyBuiltinFunction {
40+
#[pymethod(name = "__call__")]
41+
pub fn call(&self, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
42+
(self.value)(vm, args)
43+
}
44+
45+
#[pymethod(name = "__get__")]
46+
fn bind_method(
47+
function: PyRef<Self>,
48+
obj: PyObjectRef,
49+
cls: PyObjectRef,
50+
vm: &VirtualMachine,
51+
) -> PyResult {
52+
if !function.bindable || obj.is(&vm.get_none()) && !cls.is(&obj.class()) {
53+
Ok(function.into_object())
54+
} else {
55+
Ok(vm.ctx.new_bound_method(function.into_object(), obj))
56+
}
57+
}
58+
}
59+
60+
pub fn init(context: &PyContext) {
61+
PyBuiltinFunction::extend_class(context, &context.types.builtin_function_or_method_type);
62+
}

vm/src/obj/objbytearray.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ pub fn init(context: &PyContext) {
8383
PyByteArrayRef::extend_class(context, &context.types.bytearray_type);
8484
let bytearray_type = &context.types.bytearray_type;
8585
extend_class!(context, bytearray_type, {
86-
"fromhex" => context.new_rustfunc(PyByteArrayRef::fromhex),
87-
"maketrans" => context.new_rustfunc(PyByteInner::maketrans),
86+
"fromhex" => context.new_method(PyByteArrayRef::fromhex),
87+
"maketrans" => context.new_method(PyByteInner::maketrans),
8888
});
8989

9090
PyByteArrayIterator::extend_class(context, &context.types.bytearrayiterator_type);

vm/src/obj/objbytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ pub fn init(context: &PyContext) {
8080
PyBytesRef::extend_class(context, &context.types.bytes_type);
8181
let bytes_type = &context.types.bytes_type;
8282
extend_class!(context, bytes_type, {
83-
"fromhex" => context.new_rustfunc(PyBytesRef::fromhex),
84-
"maketrans" => context.new_rustfunc(PyByteInner::maketrans),
83+
"fromhex" => context.new_method(PyBytesRef::fromhex),
84+
"maketrans" => context.new_method(PyByteInner::maketrans),
8585

8686
});
8787
PyBytesIterator::extend_class(context, &context.types.bytesiterator_type);

vm/src/obj/objcode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl PyCodeRef {
9595
pub fn init(context: &PyContext) {
9696
extend_class!(context, &context.types.code_type, {
9797
(slot new) => PyCodeRef::new,
98-
"__repr__" => context.new_rustfunc(PyCodeRef::repr),
98+
"__repr__" => context.new_method(PyCodeRef::repr),
9999

100100
"co_argcount" => context.new_property(PyCodeRef::co_argcount),
101101
"co_consts" => context.new_property(PyCodeRef::co_consts),

vm/src/obj/objdict.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -596,30 +596,30 @@ dict_iterator! {
596596

597597
pub fn init(context: &PyContext) {
598598
extend_class!(context, &context.types.dict_type, {
599-
"__bool__" => context.new_rustfunc(PyDictRef::bool),
600-
"__len__" => context.new_rustfunc(PyDictRef::len),
601-
"__sizeof__" => context.new_rustfunc(PyDictRef::sizeof),
602-
"__contains__" => context.new_rustfunc(PyDictRef::contains),
603-
"__delitem__" => context.new_rustfunc(PyDictRef::inner_delitem),
604-
"__eq__" => context.new_rustfunc(PyDictRef::eq),
605-
"__ne__" => context.new_rustfunc(PyDictRef::ne),
606-
"__getitem__" => context.new_rustfunc(PyDictRef::inner_getitem),
607-
"__iter__" => context.new_rustfunc(PyDictRef::iter),
599+
"__bool__" => context.new_method(PyDictRef::bool),
600+
"__len__" => context.new_method(PyDictRef::len),
601+
"__sizeof__" => context.new_method(PyDictRef::sizeof),
602+
"__contains__" => context.new_method(PyDictRef::contains),
603+
"__delitem__" => context.new_method(PyDictRef::inner_delitem),
604+
"__eq__" => context.new_method(PyDictRef::eq),
605+
"__ne__" => context.new_method(PyDictRef::ne),
606+
"__getitem__" => context.new_method(PyDictRef::inner_getitem),
607+
"__iter__" => context.new_method(PyDictRef::iter),
608608
(slot new) => PyDictRef::new,
609-
"__repr__" => context.new_rustfunc(PyDictRef::repr),
610-
"__setitem__" => context.new_rustfunc(PyDictRef::inner_setitem),
611-
"__hash__" => context.new_rustfunc(PyDictRef::hash),
612-
"clear" => context.new_rustfunc(PyDictRef::clear),
613-
"values" => context.new_rustfunc(PyDictRef::values),
614-
"items" => context.new_rustfunc(PyDictRef::items),
615-
"keys" => context.new_rustfunc(PyDictRef::keys),
609+
"__repr__" => context.new_method(PyDictRef::repr),
610+
"__setitem__" => context.new_method(PyDictRef::inner_setitem),
611+
"__hash__" => context.new_method(PyDictRef::hash),
612+
"clear" => context.new_method(PyDictRef::clear),
613+
"values" => context.new_method(PyDictRef::values),
614+
"items" => context.new_method(PyDictRef::items),
615+
"keys" => context.new_method(PyDictRef::keys),
616616
"fromkeys" => context.new_classmethod(PyDictRef::fromkeys),
617-
"get" => context.new_rustfunc(PyDictRef::get),
618-
"setdefault" => context.new_rustfunc(PyDictRef::setdefault),
619-
"copy" => context.new_rustfunc(PyDictRef::copy),
620-
"update" => context.new_rustfunc(PyDictRef::update),
621-
"pop" => context.new_rustfunc(PyDictRef::pop),
622-
"popitem" => context.new_rustfunc(PyDictRef::popitem),
617+
"get" => context.new_method(PyDictRef::get),
618+
"setdefault" => context.new_method(PyDictRef::setdefault),
619+
"copy" => context.new_method(PyDictRef::copy),
620+
"update" => context.new_method(PyDictRef::update),
621+
"pop" => context.new_method(PyDictRef::pop),
622+
"popitem" => context.new_method(PyDictRef::popitem),
623623
});
624624

625625
PyDictKeys::extend_class(context, &context.types.dictkeys_type);

vm/src/obj/objellipsis.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use crate::vm::VirtualMachine;
55
pub fn init(context: &PyContext) {
66
extend_class!(context, &context.ellipsis_type, {
77
(slot new) => ellipsis_new,
8-
"__repr__" => context.new_rustfunc(ellipsis_repr),
9-
"__reduce__" => context.new_rustfunc(ellipsis_reduce),
8+
"__repr__" => context.new_method(ellipsis_repr),
9+
"__reduce__" => context.new_method(ellipsis_reduce),
1010
});
1111
}
1212

vm/src/obj/objfunction.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,16 @@ impl PyValue for PyMethod {
8585
pub fn init(context: &PyContext) {
8686
let function_type = &context.types.function_type;
8787
extend_class!(context, function_type, {
88-
"__get__" => context.new_rustfunc(bind_method),
89-
"__call__" => context.new_rustfunc(PyFunctionRef::call),
88+
"__get__" => context.new_method(bind_method),
89+
"__call__" => context.new_method(PyFunctionRef::call),
9090
"__code__" => context.new_property(PyFunctionRef::code),
9191
"__defaults__" => context.new_property(PyFunctionRef::defaults),
9292
"__kwdefaults__" => context.new_property(PyFunctionRef::kwdefaults),
9393
});
9494

95-
let builtin_function_or_method_type = &context.types.builtin_function_or_method_type;
96-
extend_class!(context, builtin_function_or_method_type, {
97-
"__get__" => context.new_rustfunc(bind_method),
98-
"__call__" => context.new_rustfunc(PyFunctionRef::call),
99-
});
100-
10195
let method_type = &context.types.bound_method_type;
10296
extend_class!(context, method_type, {
103-
"__getattribute__" => context.new_rustfunc(PyMethod::getattribute),
97+
"__getattribute__" => context.new_method(PyMethod::getattribute),
10498
});
10599
}
106100

vm/src/obj/objmodule.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl PyModuleRef {
9191
pub fn init(context: &PyContext) {
9292
extend_class!(&context, &context.types.module_type, {
9393
(slot new) => PyModuleRef::new,
94-
"__getattribute__" => context.new_rustfunc(PyModuleRef::getattribute),
95-
"__repr__" => context.new_rustfunc(PyModuleRef::repr),
94+
"__getattribute__" => context.new_method(PyModuleRef::getattribute),
95+
"__repr__" => context.new_method(PyModuleRef::repr),
9696
});
9797
}

0 commit comments

Comments
 (0)