Skip to content

Commit be462af

Browse files
committed
Add method to list and tuple class.
1 parent a17fb3f commit be462af

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ Documentation HTML files can then be found in the `target/doc` directory.
4242

4343
# Code organization
4444

45-
- `parser`: python lexing, parsing and ast
46-
- `vm`: python virtual machine
45+
- `parser/src`: python lexing, parsing and ast
46+
- `vm/src`: python virtual machine
47+
- `builtins.rs`: Builtin functions
48+
- `compile.rs`: the python compiler from ast to bytecode
49+
- `obj`: python builtin types
4750
- `src`: using the other subcrates to bring rustpython to life.
4851
- `docs`: documentation (work in progress)
4952
- `py_code_object`: CPython bytecode to rustpython bytecode convertor (work in progress)
@@ -53,12 +56,15 @@ Documentation HTML files can then be found in the `target/doc` directory.
5356
# Contributing
5457

5558
To start contributing, there are a lot of things that need to be done.
59+
5660
Most tasks are listed in the [issue tracker](https://github.com/RustPython/RustPython/issues).
61+
Check issues labeled with `good first issue` if you wish to start coding.
62+
5763
Another approach is to checkout the sourcecode: builtin functions and object methods are often the simplest
5864
and easiest way to contribute.
5965

6066
You can also simply run
61-
`cargo run tests/snippets/todo.py` to assist in finding any
67+
`cargo run tests/snippets/whats_left_to_implement.py` to assist in finding any
6268
unimplemented method.
6369

6470
# Testing

vm/src/obj/objlist.rs

+27
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ fn list_clear(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
125125
Ok(vm.get_none())
126126
}
127127

128+
fn list_count(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
129+
arg_check!(
130+
vm,
131+
args,
132+
required = [(zelf, Some(vm.ctx.list_type())), (value, None)]
133+
);
134+
let elements = get_elements(zelf);
135+
let mut count: usize = 0;
136+
for element in elements.iter() {
137+
let is_eq = vm._eq(element, value.clone())?;
138+
if objbool::boolval(vm, is_eq)? {
139+
count = count + 1;
140+
}
141+
}
142+
Ok(vm.context().new_int(count.to_bigint().unwrap()))
143+
}
144+
128145
pub fn list_extend(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
129146
arg_check!(
130147
vm,
@@ -152,6 +169,14 @@ fn list_reverse(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
152169
Ok(vm.get_none())
153170
}
154171

172+
fn list_sort(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
173+
arg_check!(vm, args, required = [(list, Some(vm.ctx.list_type()))]);
174+
let mut _elements = get_mut_elements(list);
175+
unimplemented!("TODO: figure out how to invoke `sort_by` on a Vec");
176+
// elements.sort_by();
177+
// Ok(vm.get_none())
178+
}
179+
155180
fn list_contains(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
156181
trace!("list.contains called with: {:?}", args);
157182
arg_check!(
@@ -221,6 +246,8 @@ pub fn init(context: &PyContext) {
221246
list_type.set_attr("__repr__", context.new_rustfunc(list_repr));
222247
list_type.set_attr("append", context.new_rustfunc(list_append));
223248
list_type.set_attr("clear", context.new_rustfunc(list_clear));
249+
list_type.set_attr("count", context.new_rustfunc(list_count));
224250
list_type.set_attr("extend", context.new_rustfunc(list_extend));
225251
list_type.set_attr("reverse", context.new_rustfunc(list_reverse));
252+
list_type.set_attr("sort", context.new_rustfunc(list_sort));
226253
}

vm/src/obj/objtuple.rs

+18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ use super::objtype;
1111
use num_bigint::ToBigInt;
1212
use std::hash::{Hash, Hasher};
1313

14+
fn tuple_count(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
15+
arg_check!(
16+
vm,
17+
args,
18+
required = [(zelf, Some(vm.ctx.tuple_type())), (value, None)]
19+
);
20+
let elements = get_elements(zelf);
21+
let mut count: usize = 0;
22+
for element in elements.iter() {
23+
let is_eq = vm._eq(element, value.clone())?;
24+
if objbool::boolval(vm, is_eq)? {
25+
count = count + 1;
26+
}
27+
}
28+
Ok(vm.context().new_int(count.to_bigint().unwrap()))
29+
}
30+
1431
fn tuple_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1532
arg_check!(
1633
vm,
@@ -142,4 +159,5 @@ pub fn init(context: &PyContext) {
142159
tuple_type.set_attr("__len__", context.new_rustfunc(tuple_len));
143160
tuple_type.set_attr("__new__", context.new_rustfunc(tuple_new));
144161
tuple_type.set_attr("__repr__", context.new_rustfunc(tuple_repr));
162+
tuple_type.set_attr("count", context.new_rustfunc(tuple_count));
145163
}

vm/src/pyobject.rs

+1
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ impl AttributeProtocol for PyObjectRef {
642642

643643
fn set_attr(&self, attr_name: &str, value: PyObjectRef) {
644644
match self.borrow().kind {
645+
PyObjectKind::Module { name: _, ref dict } => dict.set_item(attr_name, value),
645646
PyObjectKind::Instance { ref dict } => dict.set_item(attr_name, value),
646647
PyObjectKind::Class {
647648
name: _,

0 commit comments

Comments
 (0)