Skip to content

Commit b14dc20

Browse files
Merge branch 'master' into map_doc
2 parents c05f7dc + 3c25c14 commit b14dc20

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1594
-337
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser/src/lexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! This module takes care of lexing python source text. This means source
2-
//! code is translated into seperate tokens.
2+
//! code is translated into separate tokens.
33
44
pub use super::token::Tok;
55
use num_bigint::BigInt;

py_code_object/src/vm_old.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl VirtualMachine {
5656
}
5757
}
5858

59-
// Can we get rid of the code paramter?
59+
// Can we get rid of the code parameter?
6060

6161
fn make_frame(&self, code: PyCodeObject, callargs: HashMap<String, Rc<NativeType>>, globals: Option<HashMap<String, Rc<NativeType>>>) -> Frame {
6262
//populate the globals and locals
@@ -345,7 +345,7 @@ impl VirtualMachine {
345345
let exception = match argc {
346346
1 => curr_frame.stack.pop().unwrap(),
347347
0 | 2 | 3 => panic!("Not implemented!"),
348-
_ => panic!("Invalid paramter for RAISE_VARARGS, must be between 0 to 3")
348+
_ => panic!("Invalid parameter for RAISE_VARARGS, must be between 0 to 3")
349349
};
350350
panic!("{:?}", exception);
351351
}

src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn main() {
6868
handle_exception(&mut vm, result);
6969
}
7070

71-
fn _run_string(vm: &mut VirtualMachine, source: &str, source_path: Option<String>) -> PyResult {
71+
fn _run_string(vm: &mut VirtualMachine, source: &str, source_path: String) -> PyResult {
7272
let code_obj = compile::compile(vm, source, &compile::Mode::Exec, source_path)?;
7373
// trace!("Code object: {:?}", code_obj.borrow());
7474
let builtins = vm.get_builtin_scope();
@@ -91,7 +91,7 @@ fn run_command(vm: &mut VirtualMachine, mut source: String) -> PyResult {
9191

9292
// This works around https://github.com/RustPython/RustPython/issues/17
9393
source.push_str("\n");
94-
_run_string(vm, &source, None)
94+
_run_string(vm, &source, "<stdin>".to_string())
9595
}
9696

9797
fn run_module(vm: &mut VirtualMachine, module: &str) -> PyResult {
@@ -105,7 +105,7 @@ fn run_script(vm: &mut VirtualMachine, script_file: &str) -> PyResult {
105105
// Parse an ast from it:
106106
let filepath = Path::new(script_file);
107107
match parser::read_file(filepath) {
108-
Ok(source) => _run_string(vm, &source, Some(filepath.to_str().unwrap().to_string())),
108+
Ok(source) => _run_string(vm, &source, filepath.to_str().unwrap().to_string()),
109109
Err(msg) => {
110110
error!("Parsing went horribly wrong: {}", msg);
111111
std::process::exit(1);
@@ -114,7 +114,7 @@ fn run_script(vm: &mut VirtualMachine, script_file: &str) -> PyResult {
114114
}
115115

116116
fn shell_exec(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> bool {
117-
match compile::compile(vm, source, &compile::Mode::Single, None) {
117+
match compile::compile(vm, source, &compile::Mode::Single, "<stdin>".to_string()) {
118118
Ok(code) => {
119119
match vm.run_code_obj(code, scope) {
120120
Ok(_value) => {
@@ -177,7 +177,7 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
177177
}
178178

179179
loop {
180-
// TODO: modules dont support getattr / setattr yet
180+
// TODO: modules don't support getattr / setattr yet
181181
//let prompt = match vm.get_attribute(vm.sys_module.clone(), "ps1") {
182182
// Ok(value) => objstr::get_value(&value),
183183
// Err(_) => ">>>>> ".to_string(),

tests/snippets/bools.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ def __bool__(self):
4040
assert not (False and fake)
4141
assert (True and 5) == 5
4242

43-
assert bool.__doc__ == "bool(x) -> bool\n\nReturns True when the argument x is true, False otherwise.\nThe builtins True and False are the only two instances of the class bool.\nThe class bool is a subclass of the class int, and cannot be subclassed."
44-
4543
# Bools are also ints.
4644
assert isinstance(True, int)
4745
assert True + True == 2

tests/snippets/builtin_divmod.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
assert divmod(11, 3) == (3, 2)
22
assert divmod(8,11) == (0, 8)
33
assert divmod(0.873, 0.252) == (3.0, 0.11699999999999999)
4+
5+
try:
6+
divmod(5, 0)
7+
except ZeroDivisionError:
8+
pass
9+
else:
10+
assert False, "Expected divmod by zero to throw ZeroDivisionError"
11+
12+
try:
13+
divmod(5.0, 0.0)
14+
except ZeroDivisionError:
15+
pass
16+
else:
17+
assert False, "Expected divmod by zero to throw ZeroDivisionError"

tests/snippets/builtin_enumerate.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
assert list(enumerate(['a', 'b', 'c'])) == [(0, 'a'), (1, 'b'), (2, 'c')]
2+
3+
assert type(enumerate([])) == enumerate
4+
5+
assert list(enumerate(['a', 'b', 'c'], -100)) == [(-100, 'a'), (-99, 'b'), (-98, 'c')]
6+
assert list(enumerate(['a', 'b', 'c'], 2**200)) == [(2**200, 'a'), (2**200 + 1, 'b'), (2**200 + 2, 'c')]
7+
8+
9+
# test infinite iterator
10+
class Counter(object):
11+
counter = 0
12+
13+
def __next__(self):
14+
self.counter += 1
15+
return self.counter
16+
17+
def __iter__(self):
18+
return self
19+
20+
21+
it = enumerate(Counter())
22+
assert next(it) == (0, 1)
23+
assert next(it) == (1, 2)

tests/snippets/builtin_open.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fd = open('README.md')
2+
assert 'RustPython' in fd.read()
3+
4+
try:
5+
open('DoesNotExist')
6+
assert False
7+
except FileNotFoundError:
8+
pass

tests/snippets/builtin_range.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ def assert_raises(expr, exc_type):
1818

1919
assert range(2**63+1)[2**63] == 9223372036854775808
2020

21+
# len tests
22+
assert len(range(10, 5)) == 0, 'Range with no elements should have length = 0'
23+
assert len(range(10, 5, -2)) == 3, 'Expected length 3, for elements: 10, 8, 6'
24+
assert len(range(5, 10, 2)) == 3, 'Expected length 3, for elements: 5, 7, 9'
25+
2126
# index tests
2227
assert range(10).index(6) == 6
2328
assert range(4, 10).index(6) == 2

tests/snippets/builtin_slice.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
a = []
3+
assert a[:] == []
4+
assert a[:2**100] == []
5+
assert a[-2**100:] == []
6+
assert a[::2**100] == []
7+
assert a[10:20] == []
8+
assert a[-20:-10] == []
9+
10+
b = [1, 2]
11+
12+
assert b[:] == [1, 2]
13+
assert b[:2**100] == [1, 2]
14+
assert b[-2**100:] == [1, 2]
15+
assert b[2**100:] == []
16+
assert b[::2**100] == [1]
17+
assert b[-10:1] == [1]
18+
assert b[0:0] == []
19+
assert b[1:0] == []
20+
21+
try:
22+
_ = b[::0]
23+
except ValueError:
24+
pass
25+
else:
26+
assert False, "Zero step slice should raise ValueError"
27+
28+
assert b[::-1] == [2, 1]
29+
assert b[1::-1] == [2, 1]
30+
assert b[0::-1] == [1]
31+
assert b[0:-5:-1] == [1]
32+
assert b[:0:-1] == [2]
33+
assert b[5:0:-1] == [2]
34+
35+
c = list(range(10))
36+
37+
assert c[9:6:-3] == [9]
38+
assert c[9::-3] == [9, 6, 3, 0]
39+
assert c[9::-4] == [9, 5, 1]
40+
assert c[8::-2**100] == [8]
41+
42+
assert c[7:7:-2] == []
43+
assert c[7:8:-2] == []
44+
45+
d = "123456"
46+
47+
assert d[3::-1] == "4321"
48+
assert d[4::-3] == "52"
49+
50+
51+
slice_a = slice(5)
52+
assert slice_a.start is None
53+
assert slice_a.stop == 5
54+
assert slice_a.step is None
55+
56+
slice_b = slice(1, 5)
57+
assert slice_b.start == 1
58+
assert slice_b.stop == 5
59+
assert slice_b.step is None
60+
61+
slice_c = slice(1, 5, 2)
62+
assert slice_c.start == 1
63+
assert slice_c.stop == 5
64+
assert slice_c.step == 2
65+
66+
67+
class SubScript(object):
68+
def __getitem__(self, item):
69+
assert type(item) == slice
70+
71+
def __setitem__(self, key, value):
72+
assert type(key) == slice
73+
74+
75+
ss = SubScript()
76+
_ = ss[:]
77+
ss[:1] = 1

0 commit comments

Comments
 (0)