Skip to content

Commit 7d01323

Browse files
committed
Merge branch 'master' into property_refactor
# Conflicts: # vm/src/function.rs
2 parents 3c3c1f2 + 010969f commit 7d01323

24 files changed

+844
-932
lines changed

tests/snippets/stdlib_socket.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,29 @@
2929
with assertRaises(TypeError):
3030
s.connect(("127.0.0.1", 8888, 8888))
3131

32+
with assertRaises(OSError):
33+
# Lets hope nobody is listening on port 1
34+
s.connect(("127.0.0.1", 1))
35+
3236
with assertRaises(TypeError):
3337
s.bind(("127.0.0.1", 8888, 8888))
3438

39+
with assertRaises(OSError):
40+
# Lets hope nobody run this test on machine with ip 1.2.3.4
41+
s.bind(("1.2.3.4", 8888))
42+
3543
with assertRaises(TypeError):
3644
s.bind((888, 8888))
3745

46+
s.close()
47+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
48+
s.bind(("127.0.0.1", 0))
49+
with assertRaises(OSError):
50+
s.recv(100)
51+
52+
with assertRaises(OSError):
53+
s.send(MESSAGE_A)
54+
3855
s.close()
3956

4057
# UDP
@@ -73,3 +90,15 @@
7390
assert recv_b == MESSAGE_B
7491
sock1.close()
7592
sock3.close()
93+
94+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
95+
with assertRaises(OSError):
96+
s.bind(("1.2.3.4", 888))
97+
98+
s.close()
99+
### Errors
100+
with assertRaises(OSError):
101+
socket.socket(100, socket.SOCK_STREAM)
102+
103+
with assertRaises(OSError):
104+
socket.socket(socket.AF_INET, 1000)

tests/snippets/type_hints.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11

22
# See also: https://github.com/RustPython/RustPython/issues/587
33

4-
def curry(foo: int, bla=2) -> float:
4+
def curry(foo: int, bla: int =2) -> float:
55
return foo * 3.1415926 * bla
66

77
assert curry(2) > 10
8+
9+
print(curry.__annotations__)
10+
assert curry.__annotations__['foo'] is int
11+
assert curry.__annotations__['return'] is float
12+
assert curry.__annotations__['bla'] is int

vm/src/builtins.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
802802
"StopIteration" => ctx.exceptions.stop_iteration.clone(),
803803
"ZeroDivisionError" => ctx.exceptions.zero_division_error.clone(),
804804
"KeyError" => ctx.exceptions.key_error.clone(),
805+
"OSError" => ctx.exceptions.os_error.clone(),
805806
});
806807

807808
#[cfg(not(target_arch = "wasm32"))]

vm/src/frame.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::obj::objstr;
2121
use crate::obj::objtype;
2222
use crate::pyobject::{
2323
DictProtocol, IdProtocol, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult,
24-
TypeProtocol,
24+
TryFromObject, TypeProtocol,
2525
};
2626
use crate::vm::VirtualMachine;
2727

@@ -138,14 +138,7 @@ impl Frame {
138138
vm.ctx.new_int(lineno.get_row()),
139139
vm.ctx.new_str(run_obj_name.clone()),
140140
]);
141-
objlist::list_append(
142-
vm,
143-
PyFuncArgs {
144-
args: vec![traceback, pos],
145-
kwargs: vec![],
146-
},
147-
)
148-
.unwrap();
141+
objlist::PyListRef::try_from_object(vm, traceback)?.append(pos, vm);
149142
// exception.__trace
150143
match self.unwind_exception(vm, exception) {
151144
None => {}
@@ -312,13 +305,7 @@ impl Frame {
312305
bytecode::Instruction::ListAppend { i } => {
313306
let list_obj = self.nth_value(*i);
314307
let item = self.pop_value();
315-
objlist::list_append(
316-
vm,
317-
PyFuncArgs {
318-
args: vec![list_obj.clone(), item],
319-
kwargs: vec![],
320-
},
321-
)?;
308+
objlist::PyListRef::try_from_object(vm, list_obj)?.append(item, vm);
322309
Ok(None)
323310
}
324311
bytecode::Instruction::SetAdd { i } => {
@@ -453,7 +440,7 @@ impl Frame {
453440
let _qualified_name = self.pop_value();
454441
let code_obj = self.pop_value();
455442

456-
let _annotations = if flags.contains(bytecode::FunctionOpArg::HAS_ANNOTATIONS) {
443+
let annotations = if flags.contains(bytecode::FunctionOpArg::HAS_ANNOTATIONS) {
457444
self.pop_value()
458445
} else {
459446
vm.new_dict()
@@ -470,13 +457,8 @@ impl Frame {
470457
let scope = self.scope.clone();
471458
let obj = vm.ctx.new_function(code_obj, scope, defaults);
472459

473-
let annotation_repr = vm.to_pystr(&_annotations)?;
460+
vm.ctx.set_attr(&obj, "__annotations__", annotations);
474461

475-
warn!(
476-
"Type annotation must be stored in attribute! {:?}",
477-
annotation_repr
478-
);
479-
// TODO: use annotations with set_attr here!
480462
self.push_value(obj);
481463
Ok(None)
482464
}

vm/src/function.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ where
6464
Err(vm.new_type_error(format!("{} is not a subtype of {}", subtype, basetype)))
6565
}
6666
}
67+
68+
pub fn as_object(&self) -> &PyObjectRef {
69+
&self.obj
70+
}
71+
pub fn into_object(self) -> PyObjectRef {
72+
self.obj
73+
}
6774
}
6875

6976
impl<T> Deref for PyRef<T>

vm/src/obj/objenumerate.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@ use std::ops::AddAssign;
33

44
use super::objint;
55
use super::objiter;
6-
use crate::pyobject::{PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyResult, TypeProtocol};
6+
use crate::pyobject::{
7+
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
8+
TypeProtocol,
9+
};
710
use crate::vm::VirtualMachine;
811
use num_bigint::BigInt;
912
use num_traits::Zero;
1013

14+
#[derive(Debug)]
15+
pub struct PyEnumerate {
16+
counter: RefCell<BigInt>,
17+
iterator: PyObjectRef,
18+
}
19+
20+
impl PyObjectPayload2 for PyEnumerate {
21+
fn required_type(ctx: &PyContext) -> PyObjectRef {
22+
ctx.enumerate_type()
23+
}
24+
}
25+
1126
fn enumerate_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1227
arg_check!(
1328
vm,
@@ -22,9 +37,11 @@ fn enumerate_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2237
};
2338
let iterator = objiter::get_iter(vm, iterable)?;
2439
Ok(PyObject::new(
25-
PyObjectPayload::EnumerateIterator {
26-
counter: RefCell::new(counter),
27-
iterator,
40+
PyObjectPayload::AnyRustValue {
41+
value: Box::new(PyEnumerate {
42+
counter: RefCell::new(counter),
43+
iterator,
44+
}),
2845
},
2946
cls.clone(),
3047
))
@@ -37,10 +54,10 @@ fn enumerate_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3754
required = [(enumerate, Some(vm.ctx.enumerate_type()))]
3855
);
3956

40-
if let PyObjectPayload::EnumerateIterator {
57+
if let Some(PyEnumerate {
4158
ref counter,
4259
ref iterator,
43-
} = enumerate.payload
60+
}) = enumerate.payload()
4461
{
4562
let next_obj = objiter::call_next(vm, iterator)?;
4663
let result = vm

vm/src/obj/objfilter.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
use super::objbool;
2-
use super::objiter;
31
use crate::pyobject::{
4-
IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyResult, TypeProtocol,
2+
IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef,
3+
PyResult, TypeProtocol,
54
};
65
use crate::vm::VirtualMachine; // Required for arg_check! to use isinstance
76

7+
use super::objbool;
8+
use super::objiter;
9+
10+
#[derive(Debug)]
11+
pub struct PyFilter {
12+
predicate: PyObjectRef,
13+
iterator: PyObjectRef,
14+
}
15+
16+
impl PyObjectPayload2 for PyFilter {
17+
fn required_type(ctx: &PyContext) -> PyObjectRef {
18+
ctx.filter_type()
19+
}
20+
}
21+
822
fn filter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
923
arg_check!(
1024
vm,
@@ -13,9 +27,11 @@ fn filter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1327
);
1428
let iterator = objiter::get_iter(vm, iterable)?;
1529
Ok(PyObject::new(
16-
PyObjectPayload::FilterIterator {
17-
predicate: function.clone(),
18-
iterator,
30+
PyObjectPayload::AnyRustValue {
31+
value: Box::new(PyFilter {
32+
predicate: function.clone(),
33+
iterator,
34+
}),
1935
},
2036
cls.clone(),
2137
))
@@ -24,10 +40,10 @@ fn filter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2440
fn filter_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2541
arg_check!(vm, args, required = [(filter, Some(vm.ctx.filter_type()))]);
2642

27-
if let PyObjectPayload::FilterIterator {
43+
if let Some(PyFilter {
2844
ref predicate,
2945
ref iterator,
30-
} = filter.payload
46+
}) = filter.payload()
3147
{
3248
loop {
3349
let next_obj = objiter::call_next(vm, iterator)?;

vm/src/obj/objfloat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl PyFloatRef {
152152
}
153153
}
154154

155-
fn new_str(cls: PyObjectRef, arg: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
155+
fn new_float(cls: PyObjectRef, arg: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
156156
let value = if objtype::isinstance(&arg, &vm.ctx.float_type()) {
157157
get_value(&arg)
158158
} else if objtype::isinstance(&arg, &vm.ctx.int_type()) {
@@ -373,7 +373,7 @@ pub fn init(context: &PyContext) {
373373
context.set_attr(&float_type, "__radd__", context.new_rustfunc(PyFloatRef::add));
374374
context.set_attr(&float_type, "__divmod__", context.new_rustfunc(PyFloatRef::divmod));
375375
context.set_attr(&float_type, "__floordiv__", context.new_rustfunc(PyFloatRef::floordiv));
376-
context.set_attr(&float_type, "__new__", context.new_rustfunc(PyFloatRef::new_str));
376+
context.set_attr(&float_type, "__new__", context.new_rustfunc(PyFloatRef::new_float));
377377
context.set_attr(&float_type, "__mod__", context.new_rustfunc(PyFloatRef::mod_));
378378
context.set_attr(&float_type, "__neg__", context.new_rustfunc(PyFloatRef::neg));
379379
context.set_attr(&float_type, "__pow__", context.new_rustfunc(PyFloatRef::pow));

0 commit comments

Comments
 (0)