Skip to content

Commit 0cd1ca5

Browse files
authored
Merge pull request #3 from RustPython/master
pull
2 parents bc6e3eb + b01d756 commit 0cd1ca5

Some content is hidden

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

41 files changed

+1448
-241
lines changed

Cargo.lock

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

Lib/posixpath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def _joinrealpath(path, rest, seen):
444444
return path, True
445445

446446

447-
supports_unicode_filenames = (hasattr(sys, "platform") and sys.platform == 'darwin')
447+
supports_unicode_filenames = (sys.platform == 'darwin')
448448

449449
def relpath(path, start=None):
450450
"""Return a relative version of a path"""

benchmarks/bench.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn bench_rustpy_nbody(b: &mut test::Bencher) {
9999
};
100100

101101
b.iter(|| {
102-
let scope = vm.ctx.new_scope();
102+
let scope = vm.new_scope_with_builtins();
103103
let res: PyResult = vm.run_code_obj(code.clone(), scope);
104104
assert!(res.is_ok());
105105
})
@@ -118,7 +118,7 @@ fn bench_rustpy_mandelbrot(b: &mut test::Bencher) {
118118
};
119119

120120
b.iter(|| {
121-
let scope = vm.ctx.new_scope();
121+
let scope = vm.new_scope_with_builtins();
122122
let res: PyResult = vm.run_code_obj(code.clone(), scope);
123123
assert!(res.is_ok());
124124
})

src/main.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ extern crate rustyline;
1010
use clap::{App, Arg};
1111
use rustpython_parser::error::ParseError;
1212
use rustpython_vm::{
13-
compile, error::CompileError, error::CompileErrorType, frame::Scope, import, obj::objstr,
14-
print_exception, pyobject::PyResult, util, VirtualMachine,
13+
compile,
14+
error::CompileError,
15+
error::CompileErrorType,
16+
frame::Scope,
17+
import,
18+
obj::objstr,
19+
print_exception,
20+
pyobject::{ItemProtocol, PyResult},
21+
util, VirtualMachine,
1522
};
1623
use rustyline::{error::ReadlineError, Editor};
1724
use std::path::PathBuf;
@@ -65,11 +72,12 @@ fn main() {
6572
}
6673

6774
fn _run_string(vm: &VirtualMachine, source: &str, source_path: String) -> PyResult {
68-
let code_obj = compile::compile(vm, source, &compile::Mode::Exec, source_path)
75+
let code_obj = compile::compile(vm, source, &compile::Mode::Exec, source_path.clone())
6976
.map_err(|err| vm.new_syntax_error(&err))?;
7077
// trace!("Code object: {:?}", code_obj.borrow());
71-
let vars = vm.ctx.new_scope(); // Keep track of local variables
72-
vm.run_code_obj(code_obj, vars)
78+
let attrs = vm.ctx.new_dict();
79+
attrs.set_item("__file__", vm.new_str(source_path), vm)?;
80+
vm.run_code_obj(code_obj, Scope::with_builtins(None, attrs, vm))
7381
}
7482

7583
fn handle_exception(vm: &VirtualMachine, result: PyResult) {
@@ -209,7 +217,7 @@ fn run_shell(vm: &VirtualMachine) -> PyResult {
209217
"Welcome to the magnificent Rust Python {} interpreter \u{1f631} \u{1f596}",
210218
crate_version!()
211219
);
212-
let vars = vm.ctx.new_scope();
220+
let vars = vm.new_scope_with_builtins();
213221

214222
// Read a single line:
215223
let mut input = String::new();

tests/snippets/builtin_file.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
3+
from import_file import import_file
4+
5+
import_file()
6+
7+
assert os.path.basename(__file__) == "builtin_file.py"

tests/snippets/builtins_module.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from testutils import assertRaises
2+
3+
assert '__builtins__' in globals()
4+
# assert type(__builtins__).__name__ == 'module'
5+
with assertRaises(AttributeError):
6+
__builtins__.__builtins__
7+
8+
__builtins__.x = 'new'
9+
assert x == 'new'
10+
11+
exec('assert "__builtins__" in globals()', dict())
12+
exec('assert __builtins__ == 7', {'__builtins__': 7})
13+
exec('assert not isinstance(__builtins__, dict)')
14+
exec('assert isinstance(__builtins__, dict)', {})
15+
16+
namespace = {}
17+
exec('', namespace)
18+
assert namespace['__builtins__'] == __builtins__.__dict__
19+
20+
# with assertRaises(NameError):
21+
# exec('print(__builtins__)', {'__builtins__': {}})
22+
23+
# __builtins__ is deletable but names are alive
24+
del __builtins__
25+
with assertRaises(NameError):
26+
__builtins__
27+
28+
assert print

tests/snippets/bytearray.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,63 @@
631631
bytearray(b"they're bill's friends from the UK").title()
632632
== bytearray(b"They'Re Bill'S Friends From The Uk")
633633
)
634+
635+
636+
# repeat by multiply
637+
a = bytearray(b'abcd')
638+
assert a * 0 == bytearray(b'')
639+
assert a * -1 == bytearray(b'')
640+
assert a * 1 == bytearray(b'abcd')
641+
assert a * 3 == bytearray(b'abcdabcdabcd')
642+
assert 3 * a == bytearray(b'abcdabcdabcd')
643+
644+
a = bytearray(b'abcd')
645+
a.__imul__(3)
646+
assert a == bytearray(b'abcdabcdabcd')
647+
a.__imul__(0)
648+
assert a == bytearray(b'')
649+
650+
651+
# copy
652+
a = bytearray(b"my bytearray")
653+
b = a.copy()
654+
assert a == b
655+
assert a is not b
656+
b.append(100)
657+
assert a != b
658+
659+
660+
# extend
661+
a = bytearray(b"hello,")
662+
# any iterable of ints should work
663+
a.extend([32, 119, 111, 114])
664+
a.extend(b"ld")
665+
assert a == bytearray(b"hello, world")
666+
667+
668+
# insert
669+
a = bytearray(b"hello, world")
670+
a.insert(0, 119)
671+
assert a == bytearray(b"whello, world"), a
672+
# -1 is not at the end, but one before
673+
a.insert(-1, 119)
674+
assert a == bytearray(b"whello, worlwd"), a
675+
# inserting before the beginning just inserts at the beginning
676+
a.insert(-1000, 111)
677+
assert a == bytearray(b"owhello, worlwd"), a
678+
# inserting after the end just inserts at the end
679+
a.insert(1000, 111)
680+
assert a == bytearray(b"owhello, worlwdo"), a
681+
682+
683+
# remove
684+
a = bytearray(b'abcdabcd')
685+
a.remove(99) # the letter c
686+
# Only the first is removed
687+
assert a == bytearray(b'abdabcd')
688+
689+
690+
# reverse
691+
a = bytearray(b'hello, world')
692+
a.reverse()
693+
assert a == bytearray(b'dlrow ,olleh')

tests/snippets/bytes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,12 @@
588588
b"they're bill's friends from the UK".title()
589589
== b"They'Re Bill'S Friends From The Uk"
590590
)
591+
592+
593+
# repeat by multiply
594+
a = b'abcd'
595+
assert a * 0 == b''
596+
assert a * -1 == b''
597+
assert a * 1 == b'abcd'
598+
assert a * 3 == b'abcdabcdabcd'
599+
assert 3 * a == b'abcdabcdabcd'

tests/snippets/class.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,6 @@ def t1(self):
152152

153153
assert T4.__doc__ == "test4"
154154
assert T4.t1.__doc__ == "t1"
155+
156+
cm = classmethod(lambda cls: cls)
157+
assert cm.__func__(int) is int

tests/snippets/import_file.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import os
2+
3+
def import_file():
4+
assert os.path.basename(__file__) == "import_file.py"

tests/snippets/ints.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
assert (2).__pow__(3.0) == NotImplemented
6565
assert (2).__rpow__(3.0) == NotImplemented
6666

67+
assert 10 // 4 == 2
68+
assert -10 // 4 == -3
69+
assert 10 // -4 == -3
70+
assert -10 // -4 == 2
6771

6872
assert int() == 0
6973
assert int("101", 2) == 5

tests/snippets/property.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def foo(self):
5959
with assertRaises(TypeError):
6060
property.__new__(object)
6161

62+
# assert p.__doc__ is None
63+
6264

6365
p1 = property("a", "b", "c")
6466

@@ -75,3 +77,7 @@ def foo(self):
7577
assert p1.deleter(None).fdel == "c"
7678

7779
assert p1.__get__(None, object) is p1
80+
# assert p1.__doc__ is 'a'.__doc__
81+
82+
p2 = property('a', doc='pdoc')
83+
# assert p2.__doc__ == 'pdoc'

tests/snippets/stdlib_binascii.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import binascii
2+
from testutils import assertRaises
3+
4+
5+
# hexlify tests
6+
h = binascii.hexlify
7+
8+
assert h(b"abc") == b"616263"
9+
assert h(1000 * b"x") == 1000 * b"78"
10+
# bytearray not supported yet
11+
# assert h(bytearray(b"a")) = b"61"
12+
assert binascii.b2a_hex(b"aa") == b"6161"
13+
14+
with assertRaises(TypeError):
15+
h("a")
16+
17+
18+
# unhexlify tests
19+
uh = binascii.unhexlify
20+
21+
assert uh(b"616263") == b"abc"
22+
assert uh(1000 * b"78") == 1000 * b"x"
23+
x = 1000 * b"1234"
24+
assert uh(h(x)) == x
25+
assert uh(b"ABCDEF") == b"\xab\xcd\xef"
26+
assert binascii.a2b_hex(b"6161") == b"aa"
27+
28+
# unhexlify on strings not supported yet
29+
# assert uh("abcd") == b"\xab\xcd"
30+
31+
with assertRaises(ValueError):
32+
uh(b"a") # Odd-length string
33+
34+
with assertRaises(ValueError):
35+
uh(b"nn") # Non-hexadecimal digit found

tests/snippets/stdlib_itertools.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import itertools
2+
3+
from testutils import assertRaises
4+
5+
6+
# itertools.count tests
7+
8+
# default arguments
9+
c = itertools.count()
10+
assert next(c) == 0
11+
assert next(c) == 1
12+
assert next(c) == 2
13+
14+
# positional
15+
c = itertools.count(2, 3)
16+
assert next(c) == 2
17+
assert next(c) == 5
18+
assert next(c) == 8
19+
20+
# backwards
21+
c = itertools.count(1, -10)
22+
assert next(c) == 1
23+
assert next(c) == -9
24+
assert next(c) == -19
25+
26+
# step = 0
27+
c = itertools.count(5, 0)
28+
assert next(c) == 5
29+
assert next(c) == 5
30+
31+
# itertools.count TODOs: kwargs and floats
32+
33+
# step kwarg
34+
# c = itertools.count(step=5)
35+
# assert next(c) == 0
36+
# assert next(c) == 5
37+
38+
# start kwarg
39+
# c = itertools.count(start=10)
40+
# assert next(c) == 10
41+
42+
# float start
43+
# c = itertools.count(0.5)
44+
# assert next(c) == 0.5
45+
# assert next(c) == 1.5
46+
# assert next(c) == 2.5
47+
48+
# float step
49+
# c = itertools.count(1, 0.5)
50+
# assert next(c) == 1
51+
# assert next(c) == 1.5
52+
# assert next(c) == 2
53+
54+
# float start + step
55+
# c = itertools.count(0.5, 0.5)
56+
# assert next(c) == 0.5
57+
# assert next(c) == 1
58+
# assert next(c) == 1.5
59+
60+
61+
# itertools.repeat tests
62+
63+
# no times
64+
r = itertools.repeat(5)
65+
assert next(r) == 5
66+
assert next(r) == 5
67+
assert next(r) == 5
68+
69+
# times
70+
r = itertools.repeat(1, 2)
71+
assert next(r) == 1
72+
assert next(r) == 1
73+
with assertRaises(StopIteration):
74+
next(r)
75+
76+
# timees = 0
77+
r = itertools.repeat(1, 0)
78+
with assertRaises(StopIteration):
79+
next(r)
80+
81+
# negative times
82+
r = itertools.repeat(1, -1)
83+
with assertRaises(StopIteration):
84+
next(r)

0 commit comments

Comments
 (0)