Skip to content

Commit b6a88cc

Browse files
committed
Add test script for bytearray
1 parent 810ef7d commit b6a88cc

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

tests/snippets/basic_types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
except TypeError:
3737
pass
3838

39+
a = bytearray([1, 2, 3])
40+
# assert a[1] == 2
41+
3942
assert int() == 0
4043

4144
a = complex(2, 4)

vm/src/obj/objbytearray.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ fn bytearray_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
6565
Ok(vm.ctx.new_bool(result))
6666
}
6767

68+
/*
69+
fn bytearray_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
70+
arg_check!(
71+
vm,
72+
args,
73+
required = [(obj, Some(vm.ctx.bytearray_type())), (needle, None)]
74+
);
75+
let elements = get_elements(obj);
76+
get_item(vm, list, &, needle.clone())
77+
}
78+
*/
6879
/*
6980
fn set_value(obj: &PyObjectRef, value: Vec<u8>) {
7081
obj.borrow_mut().kind = PyObjectKind::Bytes { value };

vm/src/obj/objlist.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use super::super::pyobject::{
55
use super::super::vm::VirtualMachine;
66
use super::objbool;
77
use super::objint;
8-
use super::objiter;
98
use super::objsequence::{get_item, seq_equal, PySliceableSequence};
109
use super::objstr;
1110
use super::objtype;

vm/src/pyobject.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,17 @@ The PyRef type implements
4545
https://doc.rust-lang.org/std/cell/index.html#introducing-mutability-inside-of-something-immutable
4646
*/
4747
pub type PyRef<T> = Rc<RefCell<T>>;
48+
49+
/// The `PyObjectRef` is one of the most used types. It is a reference to a
50+
/// python object. A single python object can have multiple references, and
51+
/// this reference counting is accounted for by this type. Use the `.clone()`
52+
/// method to create a new reference and increment the amount of references
53+
/// to the python object by 1.
4854
pub type PyObjectRef = PyRef<PyObject>;
55+
56+
/// Use this type for function which return a python object or and exception.
57+
/// Both the python object and the python exception are `PyObjectRef` types
58+
/// since exceptions are also python objects.
4959
pub type PyResult = Result<PyObjectRef, PyObjectRef>; // A valid value, or an exception
5060

5161
/*
@@ -407,6 +417,9 @@ impl PyContext {
407417
}
408418
}
409419

420+
/// This is an actual python object. It consists of a `typ` which is the
421+
/// python class, and carries some rust payload optionally. This rust
422+
/// payload can be a rust float or rust int in case of float and int objects.
410423
pub struct PyObject {
411424
pub kind: PyObjectKind,
412425
pub typ: Option<PyObjectRef>,
@@ -594,6 +607,9 @@ impl fmt::Debug for PyObject {
594607
}
595608
}
596609

610+
/// The `PyFuncArgs` struct is one of the most used structs then creating
611+
/// a rust function that can be called from python. It holds both positional
612+
/// arguments, as well as keyword arguments passed to the function.
597613
#[derive(Debug, Default, Clone)]
598614
pub struct PyFuncArgs {
599615
pub args: Vec<PyObjectRef>,
@@ -637,6 +653,10 @@ impl PyFuncArgs {
637653

638654
type RustPyFunc = fn(vm: &mut VirtualMachine, PyFuncArgs) -> PyResult;
639655

656+
/// Rather than determining the type of a python object, this enum is more
657+
/// a holder for the rust payload of a python object. It is more a carrier
658+
/// of rust data for a particular python object. Determine the python type
659+
/// by using for example the `.typ()` method on a python object.
640660
pub enum PyObjectKind {
641661
String {
642662
value: String,

0 commit comments

Comments
 (0)