Skip to content

Commit f823eb9

Browse files
authored
Merge branch 'master' into master
2 parents ff244df + 4d02a1e commit f823eb9

34 files changed

+440
-211
lines changed

src/main.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ extern crate log;
77
extern crate rustpython_parser;
88
extern crate rustpython_vm;
99
extern crate rustyline;
10-
extern crate xdg;
1110

1211
use clap::{App, Arg};
1312
use rustpython_parser::parser;
@@ -143,6 +142,22 @@ fn shell_exec(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> bool
143142
true
144143
}
145144

145+
#[cfg(not(target_family = "unix"))]
146+
fn get_history_path() -> PathBuf {
147+
//Path buffer
148+
PathBuf::from(".repl_history.txt")
149+
}
150+
151+
#[cfg(target_family = "unix")]
152+
fn get_history_path() -> PathBuf {
153+
//work around for windows dependent builds. The xdg crate is unix specific
154+
//so access to the BaseDirectories struct breaks builds on python.
155+
extern crate xdg;
156+
157+
let xdg_dirs = xdg::BaseDirectories::with_prefix("rustpython").unwrap();
158+
xdg_dirs.place_cache_file("repl_history.txt").unwrap()
159+
}
160+
146161
fn run_shell(vm: &mut VirtualMachine) -> PyResult {
147162
println!(
148163
"Welcome to the magnificent Rust Python {} interpreter",
@@ -155,9 +170,8 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
155170
let mut input = String::new();
156171
let mut rl = Editor::<()>::new();
157172

158-
let xdg_dirs = xdg::BaseDirectories::with_prefix("rustpython").unwrap();
159-
let repl_history_path = xdg_dirs.place_cache_file("repl_history.txt").unwrap();
160-
let repl_history_path_str = repl_history_path.to_str().unwrap();
173+
//retrieve a history_path_str dependent to the os
174+
let repl_history_path_str = &get_history_path();
161175
if rl.load_history(repl_history_path_str).is_err() {
162176
println!("No previous history.");
163177
}

tests/snippets/bytearray.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#__getitem__ not implemented yet
2+
#a = bytearray(b'abc')
3+
#assert a[0] == b'a'
4+
#assert a[1] == b'b'
5+
6+
assert len(bytearray([1,2,3])) == 3
7+
8+
assert bytearray(b'1a23').isalnum()
9+
assert not bytearray(b'1%a23').isalnum()
10+
11+
assert bytearray(b'abc').isalpha()
12+
assert not bytearray(b'abc1').isalpha()
13+
14+
# travis doesn't like this
15+
#assert bytearray(b'xyz').isascii()
16+
#assert not bytearray([128, 157, 32]).isascii()
17+
18+
assert bytearray(b'1234567890').isdigit()
19+
assert not bytearray(b'12ab').isdigit()
20+
21+
assert bytearray(b'lower').islower()
22+
assert not bytearray(b'Super Friends').islower()
23+
24+
assert bytearray(b' \n\t').isspace()
25+
assert not bytearray(b'\td\n').isspace()
26+
27+
assert bytearray(b'UPPER').isupper()
28+
assert not bytearray(b'tuPpEr').isupper()
29+
30+
assert bytearray(b'Is Title Case').istitle()
31+
assert not bytearray(b'is Not title casE').istitle()

tests/snippets/numbers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
assert int.__doc__ == "int(x=0) -> integer\nint(x, base=10) -> integer\n\nConvert a number or string to an integer, or return 0 if no arguments\nare given. If x is a number, return x.__int__(). For floating point\nnumbers, this truncates towards zero.\n\nIf x is not a number or if base is given, then x must be a string,\nbytes, or bytearray instance representing an integer literal in the\ngiven base. The literal can be preceded by '+' or '-' and be surrounded\nby whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\nBase 0 means to interpret the base from the string as an integer literal.\n>>> int('0b100', base=0)\n4"
66

7+
78
class A(int):
89
pass
910

11+
1012
x = A(7)
1113
assert x == 7
1214
assert type(x) is A
@@ -18,3 +20,10 @@ class A(int):
1820
assert int(0).__invert__() == -1
1921
assert int(-3).__invert__() == 2
2022
assert int(4).__invert__() == -5
23+
24+
assert int(0).__rxor__(0) == 0
25+
assert int(1).__rxor__(0) == 1
26+
assert int(0).__rxor__(1) == 1
27+
assert int(1).__rxor__(1) == 0
28+
assert int(3).__rxor__(-3) == -2
29+
assert int(3).__rxor__(4) == 7

tests/snippets/tuple.py

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

1616
assert y < x, "tuple __lt__ failed"
1717
assert x > y, "tuple __gt__ failed"
18+
19+
20+
b = (1,2,3)
21+
assert b.index(2) == 1

vm/src/builtins.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
238238
// TODO: handle optional globals
239239
// Construct new scope:
240240
let scope_inner = Scope {
241-
locals: locals,
241+
locals,
242242
parent: None,
243243
};
244244
let scope = PyObject {
@@ -288,7 +288,7 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
288288

289289
// Construct new scope:
290290
let scope_inner = Scope {
291-
locals: locals,
291+
locals,
292292
parent: None,
293293
};
294294
let scope = PyObject {
@@ -359,7 +359,7 @@ fn builtin_hex(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
359359
fn builtin_id(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
360360
arg_check!(vm, args, required = [(obj, None)]);
361361

362-
Ok(vm.context().new_int(obj.get_id().to_bigint().unwrap()))
362+
Ok(vm.context().new_int(obj.get_id()))
363363
}
364364

365365
// builtin_input
@@ -553,9 +553,7 @@ fn builtin_ord(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
553553
)));
554554
}
555555
match string.chars().next() {
556-
Some(character) => Ok(vm
557-
.context()
558-
.new_int((character as i32).to_bigint().unwrap())),
556+
Some(character) => Ok(vm.context().new_int(character as i32)),
559557
None => Err(vm.new_type_error(
560558
"ord() could not guess the integer representing this character".to_string(),
561559
)),
@@ -635,7 +633,7 @@ fn builtin_sum(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
635633
let items = vm.extract_elements(iterable)?;
636634

637635
// Start with zero and add at will:
638-
let mut sum = vm.ctx.new_int(Zero::zero());
636+
let mut sum = vm.ctx.new_int(0);
639637
for item in items {
640638
sum = vm._add(sum, item)?;
641639
}

vm/src/bytecode.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ impl CodeObject {
4040
instructions: Vec::new(),
4141
label_map: HashMap::new(),
4242
locations: Vec::new(),
43-
arg_names: arg_names,
44-
varargs: varargs,
45-
kwonlyarg_names: kwonlyarg_names,
46-
varkeywords: varkeywords,
47-
source_path: source_path,
48-
obj_name: obj_name,
43+
arg_names,
44+
varargs,
45+
kwonlyarg_names,
46+
varkeywords,
47+
source_path,
48+
obj_name,
4949
is_generator: false,
5050
}
5151
}

vm/src/compile.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! https://github.com/micropython/micropython/blob/master/py/compile.c
88
99
use super::bytecode::{self, CallType, CodeObject, Instruction};
10-
use super::pyobject::{PyObject, PyObjectPayload, PyResult};
10+
use super::pyobject::PyResult;
1111
use super::vm::VirtualMachine;
1212
use num_complex::Complex64;
1313
use rustpython_parser::{ast, parser};
@@ -52,10 +52,7 @@ pub fn compile(
5252

5353
let code = compiler.pop_code_object();
5454
trace!("Compilation completed: {:?}", code);
55-
Ok(PyObject::new(
56-
PyObjectPayload::Code { code: code },
57-
vm.ctx.code_type(),
58-
))
55+
Ok(vm.ctx.new_code_object(code))
5956
}
6057

6158
pub enum Mode {
@@ -432,7 +429,7 @@ impl Compiler {
432429

433430
self.prepare_decorators(decorator_list)?;
434431
self.emit(Instruction::LoadConst {
435-
value: bytecode::Constant::Code { code: code },
432+
value: bytecode::Constant::Code { code },
436433
});
437434
self.emit(Instruction::LoadConst {
438435
value: bytecode::Constant::String {
@@ -441,7 +438,7 @@ impl Compiler {
441438
});
442439

443440
// Turn code object into function object:
444-
self.emit(Instruction::MakeFunction { flags: flags });
441+
self.emit(Instruction::MakeFunction { flags });
445442
self.apply_decorators(decorator_list);
446443

447444
self.emit(Instruction::StoreName {
@@ -477,7 +474,7 @@ impl Compiler {
477474

478475
let code = self.pop_code_object();
479476
self.emit(Instruction::LoadConst {
480-
value: bytecode::Constant::Code { code: code },
477+
value: bytecode::Constant::Code { code },
481478
});
482479
self.emit(Instruction::LoadConst {
483480
value: bytecode::Constant::String {
@@ -905,23 +902,23 @@ impl Compiler {
905902
let size = elements.len();
906903
let must_unpack = self.gather_elements(elements)?;
907904
self.emit(Instruction::BuildList {
908-
size: size,
905+
size,
909906
unpack: must_unpack,
910907
});
911908
}
912909
ast::Expression::Tuple { elements } => {
913910
let size = elements.len();
914911
let must_unpack = self.gather_elements(elements)?;
915912
self.emit(Instruction::BuildTuple {
916-
size: size,
913+
size,
917914
unpack: must_unpack,
918915
});
919916
}
920917
ast::Expression::Set { elements } => {
921918
let size = elements.len();
922919
let must_unpack = self.gather_elements(elements)?;
923920
self.emit(Instruction::BuildSet {
924-
size: size,
921+
size,
925922
unpack: must_unpack,
926923
});
927924
}
@@ -932,7 +929,7 @@ impl Compiler {
932929
self.compile_expression(value)?;
933930
}
934931
self.emit(Instruction::BuildMap {
935-
size: size,
932+
size,
936933
unpack: false,
937934
});
938935
}
@@ -941,7 +938,7 @@ impl Compiler {
941938
for element in elements {
942939
self.compile_expression(element)?;
943940
}
944-
self.emit(Instruction::BuildSlice { size: size });
941+
self.emit(Instruction::BuildSlice { size });
945942
}
946943
ast::Expression::Yield { value } => {
947944
self.mark_generator();
@@ -1003,13 +1000,13 @@ impl Compiler {
10031000
self.emit(Instruction::ReturnValue);
10041001
let code = self.pop_code_object();
10051002
self.emit(Instruction::LoadConst {
1006-
value: bytecode::Constant::Code { code: code },
1003+
value: bytecode::Constant::Code { code },
10071004
});
10081005
self.emit(Instruction::LoadConst {
10091006
value: bytecode::Constant::String { value: name },
10101007
});
10111008
// Turn code object into function object:
1012-
self.emit(Instruction::MakeFunction { flags: flags });
1009+
self.emit(Instruction::MakeFunction { flags });
10131010
}
10141011
ast::Expression::Comprehension { kind, generators } => {
10151012
self.compile_comprehension(kind, generators)?;
@@ -1286,7 +1283,7 @@ impl Compiler {
12861283

12871284
// List comprehension code:
12881285
self.emit(Instruction::LoadConst {
1289-
value: bytecode::Constant::Code { code: code },
1286+
value: bytecode::Constant::Code { code },
12901287
});
12911288

12921289
// List comprehension function name:

vm/src/frame.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use super::pyobject::{
2020
PyResult, TypeProtocol,
2121
};
2222
use super::vm::VirtualMachine;
23-
use num_bigint::ToBigInt;
2423
use num_traits::ToPrimitive;
2524

2625
#[derive(Clone, Debug)]
@@ -76,7 +75,7 @@ impl Frame {
7675
blocks: vec![],
7776
// save the callargs as locals
7877
// globals: locals.clone(),
79-
locals: locals,
78+
locals,
8079
lasti: 0,
8180
}
8281
}
@@ -121,7 +120,7 @@ impl Frame {
121120
trace!("Adding to traceback: {:?} {:?}", traceback, lineno);
122121
let pos = vm.ctx.new_tuple(vec![
123122
vm.ctx.new_str(filename.clone()),
124-
vm.ctx.new_int(lineno.get_row().to_bigint().unwrap()),
123+
vm.ctx.new_int(lineno.get_row()),
125124
vm.ctx.new_str(run_obj_name.clone()),
126125
]);
127126
objlist::list_append(
@@ -440,7 +439,7 @@ impl Frame {
440439
bytecode::CallType::Positional(count) => {
441440
let args: Vec<PyObjectRef> = self.pop_multiple(*count);
442441
PyFuncArgs {
443-
args: args,
442+
args,
444443
kwargs: vec![],
445444
}
446445
}
@@ -1036,15 +1035,13 @@ impl Frame {
10361035

10371036
fn unwrap_constant(&self, vm: &VirtualMachine, value: &bytecode::Constant) -> PyObjectRef {
10381037
match *value {
1039-
bytecode::Constant::Integer { ref value } => vm.ctx.new_int(value.to_bigint().unwrap()),
1038+
bytecode::Constant::Integer { ref value } => vm.ctx.new_int(value.clone()),
10401039
bytecode::Constant::Float { ref value } => vm.ctx.new_float(*value),
10411040
bytecode::Constant::Complex { ref value } => vm.ctx.new_complex(*value),
10421041
bytecode::Constant::String { ref value } => vm.new_str(value.clone()),
10431042
bytecode::Constant::Bytes { ref value } => vm.ctx.new_bytes(value.clone()),
10441043
bytecode::Constant::Boolean { ref value } => vm.new_bool(value.clone()),
1045-
bytecode::Constant::Code { ref code } => {
1046-
PyObject::new(PyObjectPayload::Code { code: code.clone() }, vm.get_type())
1047-
}
1044+
bytecode::Constant::Code { ref code } => vm.ctx.new_code_object(code.clone()),
10481045
bytecode::Constant::Tuple { ref elements } => vm.ctx.new_tuple(
10491046
elements
10501047
.iter()

0 commit comments

Comments
 (0)