Skip to content
This repository was archived by the owner on May 5, 2023. It is now read-only.

Commit 88927a3

Browse files
author
Shing Lyu
committed
Fixed tuple indexing
1 parent bfc21e6 commit 88927a3

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

RustPython/src/main.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,10 @@ impl VirtualMachine {
509509
let curr_frame = self.curr_frame();
510510
let tos = curr_frame.stack.pop().unwrap();
511511
let tos1 = curr_frame.stack.pop().unwrap();
512-
if let (NativeType::List(v), NativeType::Int(idx)) = (tos1, tos) {
513-
if idx as usize >= v.len() {
514-
// TODO: change this to a exception
515-
panic!("IndexError: list index out of range");
516-
}
517-
curr_frame.stack.push(v[idx as usize].clone());
518-
} else {
519-
panic!("TypeError in BINARY_SUBSTRACT");
512+
match (&tos1, &tos) {
513+
(&NativeType::List(ref l), &NativeType::Int(ref index)) => curr_frame.stack.push(l[*index as usize].clone()),
514+
(&NativeType::Tuple(ref t), &NativeType::Int(ref index)) => curr_frame.stack.push(t[*index as usize].clone()),
515+
_ => panic!("TypeError: indexing type {:?} with index {:?} is not supported", tos1, tos)
520516
};
521517
None
522518
},
@@ -750,13 +746,17 @@ fn main() {
750746
let mut s = String::new();
751747
f.read_to_string(&mut s).unwrap();
752748
// println!("Read string");
753-
let code: PyCodeObject = serde_json::from_str(&s).unwrap();
749+
let code: PyCodeObject = match serde_json::from_str(&s) {
750+
Ok(c) => c,
751+
Err(_) => panic!("Fail to parse the bytecode")
752+
};
754753

755754
let mut vm = VirtualMachine::new();
756755
vm.run_code(code);
757756
// println!("Done");
758757
}
759758

759+
/*
760760
#[test]
761761
fn test_parse_native_type() {
762762
@@ -860,3 +860,10 @@ let input = "{\"co_consts\":[{\"Int\":1},\"NoneType\",{\"Int\":2}],\"co_names\":
860860
let deserialized: PyCodeObject = serde_json::from_str(&input).unwrap();
861861
assert_eq!(expected, deserialized)
862862
}
863+
*/
864+
865+
#[test]
866+
fn test_tuple_serialization(){
867+
let tuple = NativeType::Tuple(vec![NativeType::Int(1),NativeType::Int(2)]);
868+
println!("{}", serde_json::to_string(&tuple).unwrap());
869+
}

compile_code.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ def capitalize_first(s):
2828
return s[0].upper() + s[1:]
2929

3030
def const_to_rust_enum(const):
31-
return {capitalize_first(str(type(const).__name__)): const}
31+
if type(const).__name__ == "tuple":
32+
return {capitalize_first(str(type(const).__name__)): list(map(const_to_rust_enum, const))}
33+
else:
34+
return {capitalize_first(str(type(const).__name__)): const}
3235
return list(map(const_to_rust_enum, consts))
3336

3437

tests/tuple.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x = (1,2)
2+
assert x[0] == 1

0 commit comments

Comments
 (0)