8000 Move set_attr and set_item to context. · rmliddle/RustPython@0215830 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0215830

Browse files
committed
Move set_attr and set_item to context.
1 parent 5c04ad8 commit 0215830

File tree

16 files changed

+278
-250
lines changed

16 files changed

+278
-250
lines changed

vm/src/builtins.rs

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -733,75 +733,92 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
733733
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
734734
py_mod.set_item("__name__", ctx.new_str(String::from("__main__")));
735735

736-
py_mod.set_item("abs", ctx.new_rustfunc(builtin_abs));
737-
py_mod.set_item("all", ctx.new_rustfunc(builtin_all));
738-
py_mod.set_item("any", ctx.new_rustfunc(builtin_any));
739-
py_mod.set_item("bin", ctx.new_rustfunc(builtin_bin));
740-
py_mod.set_item("bool", ctx.bool_type());
741-
py_mod.set_item("bytearray", ctx.bytearray_type());
742-
py_mod.set_item("bytes", ctx.bytes_type());
743-
py_mod.set_item("callable", ctx.new_rustfunc(builtin_callable));
744-
py_mod.set_item("chr", ctx.new_rustfunc(builtin_chr));
745-
py_mod.set_item("classmethod", ctx.classmethod_type());
746-
py_mod.set_item("compile", ctx.new_rustfunc(builtin_compile));
747-
py_mod.set_item("complex", ctx.complex_type());
748-
py_mod.set_item("delattr", ctx.new_rustfunc(builtin_delattr));
749-
py_mod.set_item("dict", ctx.dict_type());
750-
py_mod.set_item("divmod", ctx.new_rustfunc(builtin_divmod));
751-
py_mod.set_item("dir", ctx.new_rustfunc(builtin_dir));
752-
py_mod.set_item("enumerate", ctx.new_rustfunc(builtin_enumerate));
753-
py_mod.set_item("eval", ctx.new_rustfunc(builtin_eval));
754-
py_mod.set_item("exec", ctx.new_rustfunc(builtin_exec));
755-
py_mod.set_item("float", ctx.float_type());
756-
py_mod.set_item("frozenset", ctx.frozenset_type());
757-
py_mod.set_item("filter", ctx.new_rustfunc(builtin_filter));
758-
py_mod.set_item("getattr", ctx.new_rustfunc(builtin_getattr));
759-
py_mod.set_item("hasattr", ctx.new_rustfunc(builtin_hasattr));
760-
py_mod.set_item("hash", ctx.new_rustfunc(builtin_hash));
761-
py_mod.set_item("hex", ctx.new_rustfunc(builtin_hex));
762-
py_mod.set_item("id", ctx.new_rustfunc(builtin_id));
763-
py_mod.set_item("int", ctx.int_type());
764-
py_mod.set_item("isinstance", ctx.new_rustfunc(builtin_isinstance));
765-
py_mod.set_item("issubclass", ctx.new_rustfunc(builtin_issubclass));
766-
py_mod.set_item("iter", ctx.new_rustfunc(builtin_iter));
767-
py_mod.set_item("len", ctx.new_rustfunc(builtin_len));
768-
py_mod.set_item("list", ctx.list_type());
769-
py_mod.set_item("locals", ctx.new_rustfunc(builtin_locals));
770-
py_mod.set_item("map", ctx.new_rustfunc(builtin_map));
771-
py_mod.set_item("max", ctx.new_rustfunc(builtin_max));
772-
py_mod.set_item("min", ctx.new_rustfunc(builtin_min));
773-
py_mod.set_item("object", ctx.object());
774-
py_mod.set_item("oct", ctx.new_rustfunc(builtin_oct));
775-
py_mod.set_item("ord", ctx.new_rustfunc(builtin_ord));
776-
py_mod.set_item("next", ctx.new_rustfunc(builtin_next));
777-
py_mod.set_item("pow", ctx.new_rustfunc(builtin_pow));
778-
py_mod.set_item("print", ctx.new_rustfunc(builtin_print));
779-
py_mod.set_item("property", ctx.property_type());
780-
py_mod.set_item("range", ctx.new_rustfunc(builtin_range));
781-
py_mod.set_item("repr", ctx.new_rustfunc(builtin_repr));
782-
py_mod.set_item("set", ctx.set_type());
783-
py_mod.set_item("setattr", ctx.new_rustfunc(builtin_setattr));
784-
py_mod.set_item("staticmethod", ctx.staticmethod_type());
785-
py_mod.set_item("str", ctx.str_type());
786-
py_mod.set_item("sum", ctx.new_rustfunc(builtin_sum));
787-
py_mod.set_item("super", ctx.super_type());
788-
py_mod.set_item("tuple", ctx.tuple_type());
789-
py_mod.set_item("type", ctx.type_type());
790-
py_mod.set_item("zip", ctx.new_rustfunc(builtin_zip));
736+
ctx.set_item(&py_mod, "abs", ctx.new_rustfunc(builtin_abs));
737+
ctx.set_attr(&py_mod, "all", ctx.new_rustfunc(builtin_all));
738+
ctx.set_attr(&py_mod, "any", ctx.new_rustfunc(builtin_any));
739+
ctx.set_attr(&py_mod, "bin", ctx.new_rustfunc(builtin_bin));
740+
ctx.set_attr(&py_mod, "bool", ctx.bool_type());
741+
ctx.set_attr(&py_mod, "bytearray", ctx.bytearray_type());
742+
ctx.set_attr(&py_mod, "bytes", ctx.bytes_type());
743+
ctx.set_attr(&py_mod, "callable", ctx.new_rustfunc(builtin_callable));
744+
ctx.set_attr(&py_mod, "chr", ctx.new_rustfunc(builtin_chr));
745+
ctx.set_attr(&py_mod, "classmethod", ctx.classmethod_type());
746+
ctx.set_attr(&py_mod, "compile", ctx.new_rustfunc(builtin_compile));
747+
ctx.set_attr(&py_mod, "complex", ctx.complex_type());
748+
ctx.set_attr(&py_mod, "delattr", ctx.new_rustfunc(builtin_delattr));
749+
ctx.set_attr(&py_mod, "dict", ctx.dict_type());
750+
ctx.set_attr(&py_mod, "divmod", ctx.new_rustfunc(builtin_divmod));
751+
ctx.set_attr(&py_mod, "dir", ctx.new_rustfunc(builtin_dir));
752+
ctx.set_attr(&py_mod, "enumerate", ctx.new_rustfunc(builtin_enumerate));
753+
ctx.set_attr(&py_mod, "eval", ctx.new_rustfunc(builtin_eval));
754+
ctx.set_attr(&py_mod, "exec", ctx.new_rustfunc(builtin_exec));
755+
ctx.set_attr(&py_mod, "float", ctx.float_type());
756+
ctx.set_attr(&py_mod, "frozenset", ctx.frozenset_type());
757+
ctx.set_attr(&py_mod, "filter", ctx.new_rustfunc(builtin_filter));
758+
ctx.set_attr(&py_mod, "getattr", ctx.new_rustfunc(builtin_getattr));
759+
ctx.set_attr(&py_mod, "hasattr", ctx.new_rustfunc(builtin_hasattr));
760+
ctx.set_attr(&py_mod, "hash", ctx.new_rustfunc(builtin_hash));
761+
ctx.set_attr(&py_mod, "hex", ctx.new_rustfunc(builtin_hex));
762+
ctx.set_attr(&py_mod, "id", ctx.new_rustfunc(builtin_id));
763+
ctx.set_attr(&py_mod, "int", ctx.int_type());
764+
ctx.set_attr(&py_mod, "isinstance", ctx.new_rustfunc(builtin_isinstance));
765+
ctx.set_attr(&py_mod, "issubclass", ctx.new_rustfunc(builtin_issubclass));
766+
ctx.set_attr(&py_mod, "iter", ctx.new_rustfunc(builtin_iter));
767+
ctx.set_attr(&py_mod, "len", ctx.new_rustfunc(builtin_len));
768+
ctx.set_attr(&py_mod, "list", ctx.list_type());
769+
ctx.set_attr(&py_mod, "locals", ctx.new_rustfunc(builtin_locals));
770+
ctx.set_attr(&py_mod, "map", ctx.new_rustfunc(builtin_map));
771+
ctx.set_attr(&py_mod, "max", ctx.new_rustfunc(builtin_max));
772+
ctx.set_attr(&py_mod, "min", ctx.new_rustfunc(builtin_min));
773+
ctx.set_attr(&py_mod, "object", ctx.object());
774+
ctx.set_attr(&py_mod, "oct", ctx.new_rustfunc(builtin_oct));
775+
ctx.set_attr(&py_mod, "ord", ctx.new_rustfunc(builtin_ord));
776+
ctx.set_attr(&py_mod, "next", ctx.new_rustfunc(builtin_next));
777+
ctx.set_attr(&py_mod, "pow", ctx.new_rustfunc(builtin_pow));
778+
ctx.set_attr(&py_mod, "print", ctx.new_rustfunc(builtin_print));
779+
ctx.set_attr(&py_mod, "property", ctx.property_type());
780+
ctx.set_attr(&py_mod, "range", ctx.new_rustfunc(builtin_range));
781+
ctx.set_attr(&py_mod, "repr", ctx.new_rustfunc(builtin_repr));
782+
ctx.set_attr(&py_mod, "set", ctx.set_type());
783+
ctx.set_attr(&py_mod, "setattr", ctx.new_rustfunc(builtin_setattr));
784+
ctx.set_attr(&py_mod, "staticmethod", ctx.staticmethod_type());
785+
ctx.set_attr(&py_mod, "str", ctx.str_type());
786+
ctx.set_attr(&py_mod, "sum", ctx.new_rustfunc(builtin_sum));
787+
ctx.set_attr(&py_mod, "super", ctx.super_type());
788+
ctx.set_attr(&py_mod, "tuple", ctx.tuple_type());
789+
ctx.set_attr(&py_mod, "type", ctx.type_type());
790+
ctx.set_attr(&py_mod, "zip", ctx.new_rustfunc(builtin_zip));
791791

792792
// Exceptions:
793-
py_mod.set_item("BaseException", ctx.exceptions.base_exception_type.clone());
794-
py_mod.set_item("Exception", ctx.exceptions.exception_type.clone());
795-
py_mod.set_item("AssertionError", ctx.exceptions.assertion_error.clone());
796-
py_mod.set_item("AttributeError", ctx.exceptions.attribute_error.clone());
797-
py_mod.set_item("NameError", ctx.exceptions.name_error.clone());
798-
py_mod.set_item("RuntimeError", ctx.exceptions.runtime_error.clone());
799-
py_mod.set_item(
793+
ctx.set_attr(
794+
&py_mod,
795+
"BaseException",
796+
ctx.exceptions.base_exception_type.clone(),
797+
);
798+
ctx.set_attr(&py_mod, "Exception", ctx.exceptions.exception_type.clone());
799+
ctx.set_attr(
800+
&py_mod,
801+
"AssertionError",
802+
ctx.exceptions.assertion_error.clone(),
803+
);
804+
ctx.set_attr(
805+
&py_mod,
806+
"AttributeError",
807+
ctx.exceptions.attribute_error.clone(),
808+
);
809+
ctx.set_attr(&py_mod, "NameError", ctx.exceptions.name_error.clone());
810+
ctx.set_attr(
811+
&py_mod,
812+
"RuntimeError",
813+
ctx.exceptions.runtime_error.clone(),
814+
);
815+
ctx.set_attr(
816+
&py_mod,
800817
"NotImplementedError",
801818
ctx.exceptions.not_implemented_error.clone(),
802819
);
803 F438 -
py_mod.set_item("TypeError", ctx.exceptions.type_error.clone());
804-
py_mod.set_item("ValueError", ctx.exceptions.value_error.clone());
820+
ctx.set_attr(&py_mod, "TypeError", ctx.exceptions.type_error.clone());
821+
ctx.set_attr(&py_mod, "ValueError", ctx.exceptions.value_error.clone());
805822

806823
py_mod
807824
}

vm/src/obj/objdict.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,19 @@ fn get_mut_elements<'a>(obj: &'a PyObjectRef) -> impl DerefMut<Target = DictCont
4747
pub fn set_item(dict: &PyObjectRef, needle: &PyObjectRef, value: &PyObjectRef) {
4848
// XXX: Currently, we only support String keys, so we have to unwrap the
4949
// PyObject (and ensure it is a String).
50-
let needle = objstr::get_value(needle);
5150

5251
let mut elements = get_mut_elements(dict);
53-
elements.insert(needle, value.clone());
52+
set_item_in_content(&mut elements, needle, value);
53+
}
54+
55+
pub fn set_item_in_content(
56+
elements: &mut DictContentType,
57+
needle: &PyObjectRef,
58+
value: &PyObjectRef,
59+
) {
60+
let needle_str = objstr::get_value(needle);
61+
// TODO: elements.insert(needle_str, (needle.clone(), value.clone()));
62+
elements.insert(needle_str, value.clone());
5463
}
5564

5665
pub fn get_key_value_pairs(
@@ -60,6 +69,7 @@ pub fn get_key_value_pairs(
6069
let dict_elements = get_elements(dict);
6170
let mut pairs = Vec::new();
6271
for (key, obj) in dict_elements.iter() {
72+
// let (key, obj) = pair;
6373
let key = vm.ctx.new_str(key.to_string());
6474
pairs.push((key, obj.clone()));
6575
}
@@ -79,12 +89,13 @@ fn dict_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
7989
fn dict_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
8090
arg_check!(vm, args, required = [(o, Some(vm.ctx.dict_type()))]);
8191

82-
let elements = get_elements(o);
92+
let elements = get_key_value_pairs(vm, o);
8393
let mut str_parts = vec![];
84-
for elem in elements.iter() {
85-
let s = vm.to_repr(&elem.1)?;
94+
for (key, value) in elements {
95+
let s = vm.to_repr(&value)?;
96+
let key_str = objstr::get_value(&key);
8697
let value_str = objstr::get_value(&s);
87-
str_parts.push(format!("{}: {}", elem.0, value_str));
98+
str_parts.push(format!("{}: {}", key_str, value_str));
8899
}
89100

90101
let s = format!("{{{}}}", str_parts.join(", "));

vm/src/pyobject.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,15 @@ impl PyContext {
514514
pub fn new_instance(&self, dict: PyObjectRef, class: PyObjectRef) -> PyObjectRef {
515515
PyObject::new(PyObjectKind::Instance { dict: dict }, class)
516516
}
517+
518+
// Item set/get:
519+
pub fn set_item(&self, obj: &PyObjectRef, key: &str, v: PyObjectRef) {
520+
obj.set_item(key, v);
521+
}
522+
523+
pub fn set_attr(&self, obj: &PyObjectRef, key: &str, v: PyObjectRef) {
524+
obj.set_attr(key, v);
525+
}
517526
}
518527

519528
/// This is an actual python object. It consists of a `typ` which is the
@@ -687,6 +696,7 @@ impl DictProtocol for PyObjectRef {
687696
PyObjectKind::Dict {
688697
elements: ref mut el,
689698
} => {
699+
// objdict::set_item_in_elements(elements, key, v);
690700
el.insert(k.to_string(), v);
691701
}
692702
PyObjectKind::Module {

0 commit comments

Comments
 (0)
0