Skip to content

Commit 5b86b91

Browse files
committed
q!
Merge branch 'master' of https://github.com/RustPython/RustPython into break-continue-fix
2 parents 6229efc + cd8c1ef commit 5b86b91

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed

tests/snippets/bytearray.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@
2929

3030
assert bytearray(b'Is Title Case').istitle()
3131
assert not bytearray(b'is Not title casE').istitle()
32+
33+
a = bytearray(b'abcd')
34+
a.clear()
35+
assert len(a) == 0

tests/snippets/dict.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ def dict_eq(d1, d2):
1414
c['a']['g'] = 2
1515
assert dict_eq(a, {'g': 2})
1616
assert dict_eq(b, {'a': a, 'd': 9})
17+
18+
a.clear()
19+
assert len(a) == 0

vm/src/macros.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// count number of tokens given as arguments.
12
// see: https://danielkeep.github.io/tlborm/book/blk-counting.html
23
macro_rules! replace_expr {
34
($_t:tt $sub:expr) => {
@@ -44,9 +45,11 @@ macro_rules! arg_check {
4445
( $vm: ident, $args:ident, required=[$( ($arg_name:ident, $arg_type:expr) ),*], optional=[$( ($optional_arg_name:ident, $optional_arg_type:expr) ),*] ) => {
4546
let mut arg_count = 0;
4647

48+
// use macro magic to compile-time count number of required and optional arguments
4749
let minimum_arg_count = count_tts!($($arg_name)*);
4850
let maximum_arg_count = minimum_arg_count + count_tts!($($optional_arg_name)*);
4951

52+
// verify that the number of given arguments is right
5053
if $args.args.len() < minimum_arg_count || $args.args.len() > maximum_arg_count {
5154
let expected_str = if minimum_arg_count == maximum_arg_count {
5255
format!("{}", minimum_arg_count)
@@ -60,6 +63,9 @@ macro_rules! arg_check {
6063
)));
6164
};
6265

66+
// for each required parameter:
67+
// check if the type matches. If not, return with error
68+
// assign the arg to a variable
6369
$(
6470
type_check!($vm, $args, arg_count, $arg_name, $arg_type);
6571
let $arg_name = &$args.args[arg_count];
@@ -69,6 +75,9 @@ macro_rules! arg_check {
6975
}
7076
)*
7177

78+
// for each optional parameter, if there are enough positional arguments:
79+
// check if the type matches. If not, return with error
80+
// assign the arg to a variable
7281
$(
7382
let $optional_arg_name = if arg_count < $args.args.len() {
7483
type_check!($vm, $args, arg_count, $optional_arg_name, $optional_arg_type);

vm/src/obj/objbytearray.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ pub fn init(context: &PyContext) {
9595
"istitle",
9696
context.new_rustfunc(bytearray_istitle),
9797
);
98+
context.set_attr(
99+
&bytearray_type,
100+
"clear",
101+
context.new_rustfunc(bytearray_clear),
102+
);
98103
}
99104

100105
fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -261,3 +266,15 @@ fn bytearray_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
261266
let data = String::from_utf8(value.to_vec()).unwrap();
262267
Ok(vm.new_str(format!("bytearray(b'{}')", data)))
263268
}
269+
270+
fn bytearray_clear(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
271+
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytearray_type()))]);
272+
let mut mut_obj = zelf.borrow_mut();
273+
match mut_obj.payload {
274+
PyObjectPayload::Bytes { ref mut value } => {
275+
value.clear();
276+
Ok(vm.get_none())
277+
}
278+
_ => panic!("Bytearray has incorrect payload."),
279+
}
280+
}

vm/src/obj/objdict.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ fn dict_delitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
244244
}
245245
}
246246

247+
fn dict_clear(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
248+
arg_check!(vm, args, required = [(dict, Some(vm.ctx.dict_type()))]);
249+
get_mut_elements(dict).clear();
250+
Ok(vm.get_none())
251+
}
252+
247253
/// When iterating over a dictionary, we iterate over the keys of it.
248254
fn dict_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
249255
arg_check!(vm, args, required = [(dict, Some(vm.ctx.dict_type()))]);
@@ -337,4 +343,5 @@ pub fn init(context: &PyContext) {
337343
"__setitem__",
338344
context.new_rustfunc(dict_setitem),
339345
);
346+
context.set_attr(&dict_type, "clear", context.new_rustfunc(dict_clear));
340347
}

0 commit comments

Comments
 (0)