Skip to content

Commit b93f96d

Browse files
committed
Increase usage of extend_class macro.
1 parent b7fa08e commit b93f96d

10 files changed

Lines changed: 100 additions & 168 deletions

File tree

vm/src/obj/objbool.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ The builtins True and False are the only two instances of the class bool.
6565
The class bool is a subclass of the class int, and cannot be subclassed.";
6666

6767
let bool_type = &context.bool_type;
68-
context.set_attr(bool_type, "__new__", context.new_rustfunc(bool_new));
69-
context.set_attr(bool_type, "__repr__", context.new_rustfunc(bool_repr));
70-
context.set_attr(bool_type, "__doc__", context.new_str(bool_doc.to_string()));
68+
extend_class!(context, bool_type, {
69+
"__new__" => context.new_rustfunc(bool_new),
70+
"__repr__" => context.new_rustfunc(bool_repr),
71+
"__doc__" => context.new_str(bool_doc.to_string())
72+
});
7173
}
7274

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

vm/src/obj/objbytearray.rs

Lines changed: 19 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -61,83 +61,25 @@ pub fn init(context: &PyContext) {
6161
- any object implementing the buffer API.\n \
6262
- an integer";
6363

64-
context.set_attr(bytearray_type, "__eq__", context.new_rustfunc(bytearray_eq));
65-
context.set_attr(
66-
bytearray_type,
67-
"__new__",
68-
context.new_rustfunc(bytearray_new),
69-
);
70-
context.set_attr(
71-
bytearray_type,
72-
"__repr__",
73-
context.new_rustfunc(bytearray_repr),
74-
);
75-
context.set_attr(
76-
bytearray_type,
77-
"__len__",
78-
context.new_rustfunc(bytesarray_len),
79-
);
80-
context.set_attr(
81-
bytearray_type,
82-
"__doc__",
83-
context.new_str(bytearray_doc.to_string()),
84-
);
85-
context.set_attr(
86-
bytearray_type,
87-
"isalnum",
88-
context.new_rustfunc(bytearray_isalnum),
89-
);
90-
context.set_attr(
91-
bytearray_type,
92-
"isalpha",
93-
context.new_rustfunc(bytearray_isalpha),
94-
);
95-
context.set_attr(
96-
bytearray_type,
97-
"isascii",
98-
context.new_rustfunc(bytearray_isascii),
99-
);
100-
context.set_attr(
101-
bytearray_type,
102-
"isdigit",
103-
context.new_rustfunc(bytearray_isdigit),
104-
);
105-
context.set_attr(
106-
bytearray_type,
107-
"islower",
108-
context.new_rustfunc(bytearray_islower),
109-
);
110-
context.set_attr(
111-
bytearray_type,
112-
"isspace",
113-
context.new_rustfunc(bytearray_isspace),
114-
);
115-
context.set_attr(
116-
bytearray_type,
117-
"isupper",
118-
context.new_rustfunc(bytearray_isupper),
119-
);
120-
context.set_attr(
121-
bytearray_type,
122-
"istitle",
123-
context.new_rustfunc(bytearray_istitle),
124-
);
125-
context.set_attr(
126-
bytearray_type,
127-
"clear",
128-
context.new_rustfunc(bytearray_clear),
129-
);
130-
context.set_attr(bytearray_type, "pop", context.new_rustfunc(bytearray_pop));
131-
context.set_attr(
132-
bytearray_type,
133-
"lower",
134-
context.new_rustfunc(bytearray_lower),
135-
);
136-
context.set_attr(
137-
bytearray_type,
138-
"upper",
139-
context.new_rustfunc(bytearray_upper),
140-
);
64+
extend_class!(context, bytearray_type, {
65+
"__doc__" => context.new_str(bytearray_doc.to_string()),
66+
"__eq__" => context.new_rustfunc(bytearray_eq),
67+
"__len__" => context.new_rustfunc(bytesarray_len),
68+
"__new__" => context.new_rustfunc(bytearray_new),
69+
"__repr__" => context.new_rustfunc(bytearray_repr),
70+
"clear" => context.new_rustfunc(bytearray_clear),
71+
"isalnum" => context.new_rustfunc(bytearray_isalnum),
72+
"isalpha" => context.new_rustfunc(bytearray_isalpha),
73+
"isascii" => context.new_rustfunc(bytearray_isascii),
74+
"isdigit" => context.new_rustfunc(bytearray_isdigit),
75+
"islower" => context.new_rustfunc(bytearray_islower),
76+
"isspace" => context.new_rustfunc(bytearray_isspace),
77+
"istitle" =>context.new_rustfunc(bytearray_istitle),
78+
"isupper" => context.new_rustfunc(bytearray_isupper),
79+
"lower" => context.new_rustfunc(bytearray_lower),
80+
"pop" => context.new_rustfunc(bytearray_pop),
81+
"upper" => context.new_rustfunc(bytearray_upper)
82+
});
14183
}
14284

14385
fn bytearray_new(

vm/src/obj/objbytes.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,19 @@ pub fn init(context: &PyContext) {
5656
- any object implementing the buffer API.\n \
5757
- an integer";
5858

59-
context.set_attr(bytes_type, "__eq__", context.new_rustfunc(bytes_eq));
60-
context.set_attr(bytes_type, "__lt__", context.new_rustfunc(bytes_lt));
61-
context.set_attr(bytes_type, "__le__", context.new_rustfunc(bytes_le));
62-
context.set_attr(bytes_type, "__gt__", context.new_rustfunc(bytes_gt));
63-
context.set_attr(bytes_type, "__ge__", context.new_rustfunc(bytes_ge));
64-
context.set_attr(bytes_type, "__hash__", context.new_rustfunc(bytes_hash));
65-
context.set_attr(bytes_type, "__new__", context.new_rustfunc(bytes_new));
66-
context.set_attr(bytes_type, "__repr__", context.new_rustfunc(bytes_repr));
67-
context.set_attr(bytes_type, "__len__", context.new_rustfunc(bytes_len));
68-
context.set_attr(bytes_type, "__iter__", context.new_rustfunc(bytes_iter));
69-
context.set_attr(
70-
bytes_type,
71-
"__doc__",
72-
context.new_str(bytes_doc.to_string()),
73-
);
59+
extend_class!(context, bytes_type, {
60+
"__eq__" => context.new_rustfunc(bytes_eq),
61+
"__lt__" => context.new_rustfunc(bytes_lt),
62+
"__le__" => context.new_rustfunc(bytes_le),
63+
"__gt__" => context.new_rustfunc(bytes_gt),
64+
"__ge__" => context.new_rustfunc(bytes_ge),
65+
"__hash__" => context.new_rustfunc(bytes_hash),
66+
"__new__" => context.new_rustfunc(bytes_new),
67+
"__repr__" => context.new_rustfunc(bytes_repr),
68+
"__len__" => context.new_rustfunc(bytes_len),
69+
"__iter__" => context.new_rustfunc(bytes_iter),
70+
"__doc__" => context.new_str(bytes_doc.to_string())
71+
});
7472
}
7573

7674
fn bytes_new(

vm/src/obj/objcode.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ impl PyValue for PyCode {
3333

3434
pub fn init(context: &PyContext) {
3535
let code_type = context.code_type.as_object();
36-
context.set_attr(code_type, "__new__", context.new_rustfunc(code_new));
37-
context.set_attr(code_type, "__repr__", context.new_rustfunc(code_repr));
36+
extend_class!(context, code_type, {
37+
"__new__" => context.new_rustfunc(code_new),
38+
"__repr__" => context.new_rustfunc(code_repr)
39+
});
3840

3941
for (name, f) in &[
4042
(

vm/src/obj/objcomplex.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,19 @@ pub fn init(context: &PyContext) {
3434
"Create a complex number from a real part and an optional imaginary part.\n\n\
3535
This is equivalent to (real + imag*1j) where imag defaults to 0.";
3636

37-
context.set_attr(complex_type, "__abs__", context.new_rustfunc(complex_abs));
38-
context.set_attr(complex_type, "__add__", context.new_rustfunc(complex_add));
39-
context.set_attr(complex_type, "__radd__", context.new_rustfunc(complex_radd));
40-
context.set_attr(complex_type, "__eq__", context.new_rustfunc(complex_eq));
41-
context.set_attr(complex_type, "__neg__", context.new_rustfunc(complex_neg));
42-
context.set_attr(complex_type, "__new__", context.new_rustfunc(complex_new));
43-
context.set_attr(complex_type, "real", context.new_property(complex_real));
44-
context.set_attr(complex_type, "imag", context.new_property(complex_imag));
45-
context.set_attr(
46-
complex_type,
47-
"__doc__",
48-
context.new_str(complex_doc.to_string()),
49-
);
50-
context.set_attr(complex_type, "__repr__", context.new_rustfunc(complex_repr));
51-
context.set_attr(
52-
complex_type,
53-
"conjugate",
54-
context.new_rustfunc(complex_conjugate),
55-
);
37+
extend_class!(context, complex_type, {
38+
"__abs__" => context.new_rustfunc(complex_abs),
39+
"__add__" => context.new_rustfunc(complex_add),
40+
"__doc__" => context.new_str(complex_doc.to_string()),
41+
"__eq__" => context.new_rustfunc(complex_eq),
42+
"__neg__" => context.new_rustfunc(complex_neg),
43+
"__new__" => context.new_rustfunc(complex_new),
44+
"__radd__" => context.new_rustfunc(complex_radd),
45+
"__repr__" => context.new_rustfunc(complex_repr),
46+
"conjugate" => context.new_rustfunc(complex_conjugate),
47+
"imag" => context.new_property(complex_imag),
48+
"real" => context.new_property(complex_real)
49+
});
5650
}
5751

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

vm/src/obj/objellipsis.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ use crate::vm::VirtualMachine;
44

55
pub fn init(context: &PyContext) {
66
let ellipsis_type = context.ellipsis_type.as_object();
7-
context.set_attr(ellipsis_type, "__new__", context.new_rustfunc(ellipsis_new));
8-
context.set_attr(
9-
ellipsis_type,
10-
"__repr__",
11-
context.new_rustfunc(ellipsis_repr),
12-
);
7+
extend_class!(context, ellipsis_type, {
8+
"__new__" => context.new_rustfunc(ellipsis_new),
9+
"__repr__" => context.new_rustfunc(ellipsis_repr)
10+
});
1311
}
1412

1513
fn ellipsis_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {

vm/src/obj/objenumerate.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,8 @@ fn enumerate_next(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
7272
pub fn init(context: &PyContext) {
7373
let enumerate_type = &context.enumerate_type;
7474
objiter::iter_type_init(context, enumerate_type);
75-
context.set_attr(
76-
enumerate_type,
77-
"__new__",
78-
context.new_rustfunc(enumerate_new),
79-
);
80-
context.set_attr(
81-
enumerate_type,
82-
"__next__",
83-
context.new_rustfunc(enumerate_next),
84-
);
75+
extend_class!(context, enumerate_type, {
76+
"__new__" => context.new_rustfunc(enumerate_new),
77+
"__next__" => context.new_rustfunc(enumerate_next)
78+
});
8579
}

vm/src/obj/objframe.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ use crate::vm::VirtualMachine;
99

1010
pub fn init(context: &PyContext) {
1111
let frame_type = &context.frame_type;
12-
context.set_attr(frame_type, "__new__", context.new_rustfunc(frame_new));
13-
context.set_attr(frame_type, "__repr__", context.new_rustfunc(frame_repr));
14-
context.set_attr(frame_type, "f_locals", context.new_property(frame_flocals));
15-
context.set_attr(frame_type, "f_code", context.new_property(frame_fcode));
12+
extend_class!(context, frame_type, {
13+
"__new__" => context.new_rustfunc(frame_new),
14+
"__repr__" => context.new_rustfunc(frame_repr),
15+
"f_locals" => context.new_property(frame_flocals),
16+
"f_code" => context.new_property(frame_fcode)
17+
});
1618
}
1719

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

vm/src/obj/objlist.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -420,31 +420,33 @@ pub fn init(context: &PyContext) {
420420
If no argument is given, the constructor creates a new empty list.\n\
421421
The argument must be an iterable if specified.";
422422

423-
context.set_attr(list_type, "__add__", context.new_rustfunc(PyListRef::add));
424-
context.set_attr(list_type, "__iadd__", context.new_rustfunc(PyListRef::iadd));
425-
context.set_attr(list_type, "__contains__", context.new_rustfunc(PyListRef::contains));
426-
context.set_attr(list_type, "__eq__", context.new_rustfunc(PyListRef::eq));
427-
context.set_attr(list_type, "__lt__", context.new_rustfunc(PyListRef::lt));
428-
context.set_attr(list_type, "__gt__", context.new_rustfunc(PyListRef::gt));
429-
context.set_attr(list_type, "__le__", context.new_rustfunc(PyListRef::le));
430-
context.set_attr(list_type, "__ge__", context.new_rustfunc(PyListRef::ge));
431-
context.set_attr(list_type, "__getitem__", context.new_rustfunc(PyListRef::getitem));
432-
context.set_attr(list_type, "__iter__", context.new_rustfunc(PyListRef::iter));
433-
context.set_attr(list_type, "__setitem__", context.new_rustfunc(PyListRef::setitem));
434-
context.set_attr(list_type, "__mul__", context.new_rustfunc(PyListRef::mul));
435-
context.set_attr(list_type, "__len__", context.new_rustfunc(PyListRef::len));
436-
context.set_attr(list_type, "__new__", context.new_rustfunc(list_new));
437-
context.set_attr(list_type, "__repr__", context.new_rustfunc(PyListRef::repr));
438-
context.set_attr(list_type, "__doc__", context.new_str(list_doc.to_string()));
439-
context.set_attr(list_type, "append", context.new_rustfunc(PyListRef::append));
440-
context.set_attr(list_type, "clear", context.new_rustfunc(PyListRef::clear));
441-
context.set_attr(list_type, "copy", context.new_rustfunc(PyListRef::copy));
442-
context.set_attr(list_type, "count", context.new_rustfunc(PyListRef::count));
443-
context.set_attr(list_type, "extend", context.new_rustfunc(PyListRef::extend));
444-
context.set_attr(list_type, "index", context.new_rustfunc(PyListRef::index));
445-
context.set_attr(list_type, "insert", context.new_rustfunc(PyListRef::insert));
446-
context.set_attr(list_type, "reverse", context.new_rustfunc(PyListRef::reverse));
447-
context.set_attr(list_type, "sort", context.new_rustfunc(list_sort));
448-
context.set_attr(list_type, "pop", context.new_rustfunc(PyListRef::pop));
449-
context.set_attr(list_type, "remove", context.new_rustfunc(PyListRef::remove));
423+
extend_class!(context, list_type, {
424+
"__add__" => context.new_rustfunc(PyListRef::add),
425+
"__iadd__" => context.new_rustfunc(PyListRef::iadd),
426+
"__contains__" => context.new_rustfunc(PyListRef::contains),
427+
"__eq__" => context.new_rustfunc(PyListRef::eq),
428+
"__lt__" => context.new_rustfunc(PyListRef::lt),
429+
"__gt__" => context.new_rustfunc(PyListRef::gt),
430+
"__le__" => context.new_rustfunc(PyListRef::le),
431+
"__ge__" => context.new_rustfunc(PyListRef::ge),
432+
"__getitem__" => context.new_rustfunc(PyListRef::getitem),
433+
"__iter__" => context.new_rustfunc(PyListRef::iter),
434+
"__setitem__" => context.new_rustfunc(PyListRef::setitem),
435+
"__mul__" => context.new_rustfunc(PyListRef::mul),
436+
"__len__" => context.new_rustfunc(PyListRef::len),
437+
"__new__" => context.new_rustfunc(list_new),
438+
"__repr__" => context.new_rustfunc(PyListRef::repr),
439+
"__doc__" => context.new_str(list_doc.to_string()),
440+
"append" => context.new_rustfunc(PyListRef::append),
441+
"clear" => context.new_rustfunc(PyListRef::clear),
442+
"copy" => context.new_rustfunc(PyListRef::copy),
443+
"count" => context.new_rustfunc(PyListRef::count),
444+
"extend" => context.new_rustfunc(PyListRef::extend),
445+
"index" => context.new_rustfunc(PyListRef::index),
446+
"insert" => context.new_rustfunc(PyListRef::insert),
447+
"reverse" => context.new_rustfunc(PyListRef::reverse),
448+
"sort" => context.new_rustfunc(list_sort),
449+
"pop" => context.new_rustfunc(PyListRef::pop),
450+
"remove" => context.new_rustfunc(PyListRef::remove)
451+
});
450452
}

vm/src/obj/objmemory.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ pub fn new_memory_view(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
2626

2727
pub fn init(ctx: &PyContext) {
2828
let memoryview_type = &ctx.memoryview_type;
29-
ctx.set_attr(
30-
memoryview_type,
31-
"__new__",
32-
ctx.new_rustfunc(new_memory_view),
33-
);
29+
extend_class!(ctx, memoryview_type, {
30+
"__new__" => ctx.new_rustfunc(new_memory_view)
31+
});
3432
}

0 commit comments

Comments
 (0)