Skip to content

Commit 4095e0c

Browse files
committed
Modify location of set_attr so that we are able to create str python objects for the dictionary.
1 parent 31f50ed commit 4095e0c

25 files changed

+380
-222
lines changed

vm/src/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ fn builtin_setattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
679679
required = [(obj, None), (attr, Some(vm.ctx.str_type())), (value, None)]
680680
);
681681
let name = objstr::get_value(attr);
682-
obj.clone().set_attr(&name, value.clone());
682+
vm.ctx.set_attr(obj, &name, value.clone());
683683
Ok(vm.get_none())
684684
}
685685

vm/src/exceptions.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ fn exception_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1414
vm.new_str("No msg".to_string())
1515
};
1616
let traceback = vm.ctx.new_list(Vec::new());
17-
zelf.set_attr("msg", msg);
18-
zelf.set_attr("__traceback__", traceback);
17+
vm.ctx.set_attr(&zelf, "msg", msg);
18+
vm.ctx.set_attr(&zelf, "__traceback__", traceback);
1919
Ok(vm.get_none())
2020
}
2121

@@ -198,7 +198,15 @@ impl ExceptionZoo {
198198

199199
pub fn init(context: &PyContext) {
200200
let ref base_exception_type = context.exceptions.base_exception_type;
201-
base_exception_type.set_attr("__init__", context.new_rustfunc(exception_init));
201+
context.set_attr(
202+
&base_exception_type,
203+
"__init__",
204+
context.new_rustfunc(exception_init),
205+
);
202206
let ref exception_type = context.exceptions.exception_type;
203-
exception_type.set_attr("__str__", context.new_rustfunc(exception_str));
207+
context.set_attr(
208+
&exception_type,
209+
"__str__",
210+
context.new_rustfunc(exception_str),
211+
);
204212
}

vm/src/frame.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use super::obj::objlist;
1616
use super::obj::objstr;
1717
use super::obj::objtype;
1818
use super::pyobject::{
19-
AttributeProtocol, DictProtocol, IdProtocol, ParentProtocol, PyFuncArgs, PyObject,
20-
PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
19+
DictProtocol, IdProtocol, ParentProtocol, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
20+
PyResult, TypeProtocol,
2121
};
2222
use super::vm::VirtualMachine;
2323
use num_bigint::ToBigInt;
@@ -309,7 +309,7 @@ impl Frame {
309309
}
310310
bytecode::Instruction::BinaryOperation { ref op } => self.execute_binop(vm, op),
311311
bytecode::Instruction::LoadAttr { ref name } => self.load_attr(vm, name),
312-
bytecode::Instruction::StoreAttr { ref name } => self.store_attr(name),
312+
bytecode::Instruction::StoreAttr { ref name } => self.store_attr(vm, name),
313313
bytecode::Instruction::DeleteAttr { ref name } => self.delete_attr(vm, name),
314314
bytecode::Instruction::UnaryOperation { ref op } => self.execute_unop(vm, op),
315315
bytecode::Instruction::CompareOperation { ref op } => self.execute_compare(vm, op),
@@ -998,10 +998,10 @@ impl Frame {
998998
Ok(None)
999999
}
10001000

1001-
fn store_attr(&mut self, attr_name: &str) -> FrameResult {
1001+
fn store_attr(&mut self, vm: &mut VirtualMachine, attr_name: &str) -> FrameResult {
10021002
let parent = self.pop_value();
10031003
let value = self.pop_value();
1004-
parent.set_attr(attr_name, value);
1004+
vm.ctx.set_attr(&parent, attr_name, value);
10051005
Ok(None)
10061006
}
10071007

vm/src/obj/objbool.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::super::pyobject::{
2-
AttributeProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
2+
PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
33
};
44
use super::super::vm::VirtualMachine;
55
use super::objtype;
@@ -31,8 +31,8 @@ pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result<bool, PyObje
3131

3232
pub fn init(context: &PyContext) {
3333
let ref bool_type = context.bool_type;
34-
bool_type.set_attr("__new__", context.new_rustfunc(bool_new));
35-
bool_type.set_attr("__repr__", context.new_rustfunc(bool_repr));
34+
context.set_attr(&bool_type, "__new__", context.new_rustfunc(bool_new));
35+
context.set_attr(&bool_type, "__repr__", context.new_rustfunc(bool_repr));
3636
}
3737

3838
pub fn not(vm: &mut VirtualMachine, obj: &PyObjectRef) -> PyResult {

vm/src/obj/objbytearray.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Implementation of the python bytearray object.
22
33
use super::super::pyobject::{
4-
AttributeProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult,
5-
TypeProtocol,
4+
PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
65
};
76
use super::super::vm::VirtualMachine;
87
use super::objbytes::get_value;
@@ -14,9 +13,21 @@ use num_traits::ToPrimitive;
1413
/// Fill bytearray class methods dictionary.
1514
pub fn init(context: &PyContext) {
1615
let ref bytearray_type = context.bytearray_type;
17-
bytearray_type.set_attr("__eq__", context.new_rustfunc(bytearray_eq));
18-
bytearray_type.set_attr("__new__", context.new_rustfunc(bytearray_new));
19-
bytearray_type.set_attr("__repr__", context.new_rustfunc(bytearray_repr));
16+
context.set_attr(
17+
&bytearray_type,
18+
"__eq__",
19+
context.new_rustfunc(bytearray_eq),
20+
);
21+
context.set_attr(
22+
&bytearray_type,
23+
"__new__",
24+
context.new_rustfunc(bytearray_new),
25+
);
26+
context.set_attr(
27+
&bytearray_type,
28+
"__repr__",
29+
context.new_rustfunc(bytearray_repr),
30+
);
2031
}
2132

2233
fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

vm/src/obj/objbytes.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::super::pyobject::{
2-
AttributeProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult,
3-
TypeProtocol,
2+
PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
43
};
54
use super::super::vm::VirtualMachine;
65
use super::objint;
@@ -15,10 +14,10 @@ use std::ops::Deref;
1514
// Fill bytes class methods:
1615
pub fn init(context: &PyContext) {
1716
let ref bytes_type = context.bytes_type;
18-
bytes_type.set_attr("__eq__", context.new_rustfunc(bytes_eq));
19-
bytes_type.set_attr("__hash__", context.new_rustfunc(bytes_hash));
20-
bytes_type.set_attr("__new__", context.new_rustfunc(bytes_new));
21-
bytes_type.set_attr("__repr__", context.new_rustfunc(bytes_repr));
17+
context.set_attr(bytes_type, "__eq__", context.new_rustfunc(bytes_eq));
18+
context.set_attr(bytes_type, "__hash__", context.new_rustfunc(bytes_hash));
19+
context.set_attr(bytes_type, "__new__", context.new_rustfunc(bytes_new));
20+
context.set_attr(bytes_type, "__repr__", context.new_rustfunc(bytes_repr));
2221
}
2322

2423
fn bytes_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

vm/src/obj/objcode.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use super::super::bytecode;
66
use super::super::pyobject::{
7-
AttributeProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
7+
PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
88
};
99
use super::super::vm::VirtualMachine;
1010
use super::objtype;
1111

1212
pub fn init(context: &PyContext) {
1313
let ref code_type = context.code_type;
14-
code_type.set_attr("__new__", context.new_rustfunc(code_new));
15-
code_type.set_attr("__repr__", context.new_rustfunc(code_repr));
14+
context.set_attr(code_type, "__new__", context.new_rustfunc(code_new));
15+
context.set_attr(code_type, "__repr__", context.new_rustfunc(code_repr));
1616
}
1717

1818
/// Extract rust bytecode object from a python code object.

vm/src/obj/objcomplex.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::super::pyobject::{
2-
AttributeProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult,
3-
TypeProtocol,
2+
PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
43
};
54
use super::super::vm::VirtualMachine;
65
use super::objfloat;
@@ -9,10 +8,18 @@ use num_complex::Complex64;
98

109
pub fn init(context: &PyContext) {
1110
let ref complex_type = context.complex_type;
12-
complex_type.set_attr("__add__", context.new_rustfunc(complex_add));
13-
complex_type.set_attr("__new__", context.new_rustfunc(complex_new));
14-
complex_type.set_attr("__repr__", context.new_rustfunc(complex_repr));
15-
complex_type.set_attr("conjugate", context.new_rustfunc(complex_conjugate));
11+
context.set_attr(&complex_type, "__add__", context.new_rustfunc(complex_add));
12+
context.set_attr(&complex_type, "__new__", context.new_rustfunc(complex_new));
13+
context.set_attr(
14+
&complex_type,
15+
"__repr__",
16+
context.new_rustfunc(complex_repr),
17+
);
18+
context.set_attr(
19+
&complex_type,
20+
"conjugate",
21+
context.new_rustfunc(complex_conjugate),
22+
);
1623
}
1724

1825
pub fn get_value(obj: &PyObjectRef) -> Complex64 {

vm/src/obj/objdict.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::super::pyobject::{
2-
AttributeProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult,
3-
TypeProtocol,
2+
PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
43
};
54
use super::super::vm::VirtualMachine;
65
use super::objstr;
@@ -212,12 +211,28 @@ pub fn create_type(type_type: PyObjectRef, object_type: PyObjectRef, dict_type:
212211

213212
pub fn init(context: &PyContext) {
214213
let ref dict_type = context.dict_type;
215-
dict_type.set_attr("__len__", context.new_rustfunc(dict_len));
216-
dict_type.set_attr("__contains__", context.new_rustfunc(dict_contains));
217-
dict_type.set_attr("__delitem__", context.new_rustfunc(dict_delitem));
218-
dict_type.set_attr("__getitem__", context.new_rustfunc(dict_getitem));
219-
dict_type.set_attr("__iter__", context.new_rustfunc(dict_iter));
220-
dict_type.set_attr("__new__", context.new_rustfunc(dict_new));
221-
dict_type.set_attr("__repr__", context.new_rustfunc(dict_repr));
222-
dict_type.set_attr("__setitem__", context.new_rustfunc(dict_setitem));
214+
context.set_attr(&dict_type, "__len__", context.new_rustfunc(dict_len));
215+
context.set_attr(
216+
&dict_type,
217+
"__contains__",
218+
context.new_rustfunc(dict_contains),
219+
);
220+
context.set_attr(
221+
&dict_type,
222+
"__delitem__",
223+
context.new_rustfunc(dict_delitem),
224+
);
225+
context.set_attr(
226+
&dict_type,
227+
"__getitem__",
228+
context.new_rustfunc(dict_getitem),
229+
);
230+
context.set_attr(&dict_type, "__iter__", context.new_rustfunc(dict_iter));
231+
context.set_attr(&dict_type, "__new__", context.new_rustfunc(dict_new));
232+
context.set_attr(&dict_type, "__repr__", context.new_rustfunc(dict_repr));
233+
context.set_attr(
234+
&dict_type,
235+
"__setitem__",
236+
context.new_rustfunc(dict_setitem),
237+
);
223238
}

vm/src/obj/objfloat.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::super::pyobject::{
2-
AttributeProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
2+
PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
33
};
44
use super::super::vm::VirtualMachine;
55
use super::objint;
@@ -260,19 +260,27 @@ fn float_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
260260

261261
pub fn init(context: &PyContext) {
262262
let ref float_type = context.float_type;
263-
float_type.set_attr("__eq__", context.new_rustfunc(float_eq));
264-
float_type.set_attr("__lt__", context.new_rustfunc(float_lt));
265-
float_type.set_attr("__le__", context.new_rustfunc(float_le));
266-
float_type.set_attr("__gt__", context.new_rustfunc(float_gt));
267-
float_type.set_attr("__ge__", context.new_rustfunc(float_ge));
268-
float_type.set_attr("__abs__", context.new_rustfunc(float_abs));
269-
float_type.set_attr("__add__", context.new_rustfunc(float_add));
270-
float_type.set_attr("__divmod__", context.new_rustfunc(float_divmod));
271-
float_type.set_attr("__floordiv__", context.new_rustfunc(float_floordiv));
272-
float_type.set_attr("__init__", context.new_rustfunc(float_init));
273-
float_type.set_attr("__mod__", context.new_rustfunc(float_mod));
274-
float_type.set_attr("__neg__", context.new_rustfunc(float_neg));
275-
float_type.set_attr("__pow__", context.new_rustfunc(float_pow));
276-
float_type.set_attr("__sub__", context.new_rustfunc(float_sub));
277-
float_type.set_attr("__repr__", context.new_rustfunc(float_repr));
263+
context.set_attr(&float_type, "__eq__", context.new_rustfunc(float_eq));
264+
context.set_attr(&float_type, "__lt__", context.new_rustfunc(float_lt));
265+
context.set_attr(&float_type, "__le__", context.new_rustfunc(float_le));
266+
context.set_attr(&float_type, "__gt__", context.new_rustfunc(float_gt));
267+
context.set_attr(&float_type, "__ge__", context.new_rustfunc(float_ge));
268+
context.set_attr(&float_type, "__abs__", context.new_rustfunc(float_abs));
269+
context.set_attr(&float_type, "__add__", context.new_rustfunc(float_add));
270+
context.set_attr(
271+
&float_type,
272+
"__divmod__",
273+
context.new_rustfunc(float_divmod),
274+
);
275+
context.set_attr(
276+
&float_type,
277+
"__floordiv__",
278+
context.new_rustfunc(float_floordiv),
279+
);
280+
context.set_attr(&float_type, "__init__", context.new_rustfunc(float_init));
281+
context.set_attr(&float_type, "__mod__", context.new_rustfunc(float_mod));
282+
context.set_attr(&float_type, "__neg__", context.new_rustfunc(float_neg));
283+
context.set_attr(&float_type, "__pow__", context.new_rustfunc(float_pow));
284+
context.set_attr(&float_type, "__sub__", context.new_rustfunc(float_sub));
285+
context.set_attr(&float_type, "__repr__", context.new_rustfunc(float_repr));
278286
}

vm/src/obj/objframe.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
use super::super::frame::Frame;
66
use super::super::pyobject::{
7-
AttributeProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
7+
PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
88
};
99
use super::super::vm::VirtualMachine;
1010
use super::objtype;
1111

1212
pub fn init(context: &PyContext) {
1313
let ref frame_type = context.frame_type;
14-
frame_type.set_attr("__new__", context.new_rustfunc(frame_new));
15-
frame_type.set_attr("__repr__", context.new_rustfunc(frame_repr));
16-
frame_type.set_attr("f_locals", context.new_property(frame_flocals));
17-
frame_type.set_attr("f_code", context.new_property(frame_fcode));
14+
context.set_attr(&frame_type, "__new__", context.new_rustfunc(frame_new));
15+
context.set_attr(&frame_type, "__repr__", context.new_rustfunc(frame_repr));
16+
context.set_attr(&frame_type, "f_locals", context.new_property(frame_flocals));
17+
context.set_attr(&frame_type, "f_code", context.new_property(frame_fcode));
1818
}
1919

2020
fn frame_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

vm/src/obj/objfunction.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,38 @@ use super::objtype;
77

88
pub fn init(context: &PyContext) {
99
let ref function_type = context.function_type;
10-
function_type.set_attr("__get__", context.new_rustfunc(bind_method));
10+
context.set_attr(&function_type, "__get__", context.new_rustfunc(bind_method));
1111

1212
let ref member_descriptor_type = context.member_descriptor_type;
13-
member_descriptor_type.set_attr("__get__", context.new_rustfunc(member_get));
13+
context.set_attr(
14+
&member_descriptor_type,
15+
"__get__",
16+
context.new_rustfunc(member_get),
17+
);
1418

1519
let ref classmethod_type = context.classmethod_type;
16-
classmethod_type.set_attr("__get__", context.new_rustfunc(classmethod_get));
17-
classmethod_type.set_attr("__new__", context.new_rustfunc(classmethod_new));
20+
context.set_attr(
21+
&classmethod_type,
22+
"__get__",
23+
context.new_rustfunc(classmethod_get),
24+
);
25+
context.set_attr(
26+
&classmethod_type,
27+
"__new__",
28+
context.new_rustfunc(classmethod_new),
29+
);
1830

1931
let ref staticmethod_type = context.staticmethod_type;
20-
staticmethod_type.set_attr("__get__", context.new_rustfunc(staticmethod_get));
21-
staticmethod_type.set_attr("__new__", context.new_rustfunc(staticmethod_new));
32+
context.set_attr(
33+
staticmethod_type,
34+
"__get__",
35+
context.new_rustfunc(staticmethod_get),
36+
);
37+
context.set_attr(
38+
staticmethod_type,
39+
"__new__",
40+
context.new_rustfunc(staticmethod_new),
41+
);
2242
}
2343

2444
fn bind_method(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -83,7 +103,7 @@ fn classmethod_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
83103
},
84104
cls.clone(),
85105
);
86-
py_obj.set_attr("function", callable.clone());
106+
vm.ctx.set_attr(&py_obj, "function", callable.clone());
87107
Ok(py_obj)
88108
}
89109

@@ -121,6 +141,6 @@ fn staticmethod_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
121141
},
122142
cls.clone(),
123143
);
124-
py_obj.set_attr("function", callable.clone());
144+
vm.ctx.set_attr(&py_obj, "function", callable.clone());
125145
Ok(py_obj)
126146
}

0 commit comments

Comments
 (0)