Skip to content

Commit 9fa10c7

Browse files
committed
Merge with master
2 parents 947c17e + e060982 commit 9fa10c7

File tree

5 files changed

+170
-160
lines changed

5 files changed

+170
-160
lines changed

tests/snippets/basic_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
except TypeError:
3434
pass
3535

36+
assert int() == 0

tests/snippets/json_snippet.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,35 @@ def round_trip_test(obj):
1616
assert '[]' == json.dumps([])
1717
assert '[1]' == json.dumps([1])
1818
assert '[[1]]' == json.dumps([[1]])
19-
# round_trip_test([1, "string", 1.0, True])
19+
round_trip_test([1, "string", 1.0, True])
2020

2121
assert '[]' == json.dumps(())
2222
assert '[1]' == json.dumps((1,))
2323
assert '[[1]]' == json.dumps(((1,),))
2424
# tuples don't round-trip through json
25-
# assert [1, "string", 1.0, True] == json.loads(json.dumps((1, "string", 1.0, True)))
26-
27-
# assert '{}' == json.dumps({})
28-
# # TODO: uncomment once dict comparison is implemented
29-
# # round_trip_test({'a': 'b'})
30-
31-
# assert 1 == json.loads("1")
32-
# assert -1 == json.loads("-1")
33-
# assert 1.0 == json.loads("1.0")
34-
# # TODO: uncomment once negative floats are implemented
35-
# # assert -1.0 == json.loads("-1.0")
36-
# assert "str" == json.loads('"str"')
37-
# # TODO: Use "is" once implemented
38-
# assert True == json.loads('true')
39-
# assert False == json.loads('false')
40-
# # TODO: uncomment once None comparison is implemented
41-
# assert None == json.loads('null')
42-
# assert [] == json.loads('[]')
43-
# assert ['a'] == json.loads('["a"]')
44-
# assert [['a'], 'b'] == json.loads('[["a"], "b"]')
45-
46-
# class String(str): pass
47-
48-
# assert '"string"' == json.dumps(String("string"))
25+
assert [1, "string", 1.0, True] == json.loads(json.dumps((1, "string", 1.0, True)))
26+
27+
assert '{}' == json.dumps({})
28+
# TODO: uncomment once dict comparison is implemented
29+
# round_trip_test({'a': 'b'})
30+
31+
assert 1 == json.loads("1")
32+
assert -1 == json.loads("-1")
33+
assert 1.0 == json.loads("1.0")
34+
# TODO: uncomment once negative floats are implemented
35+
# assert -1.0 == json.loads("-1.0")
36+
assert "str" == json.loads('"str"')
37+
assert True is json.loads('true')
38+
assert False is json.loads('false')
39+
assert None is json.loads('null')
40+
assert [] == json.loads('[]')
41+
assert ['a'] == json.loads('["a"]')
42+
assert [['a'], 'b'] == json.loads('[["a"], "b"]')
43+
44+
class String(str): pass
45+
46+
assert "string" == json.loads(String('"string"'))
47+
assert '"string"' == json.dumps(String("string"))
4948

5049
# TODO: Uncomment and test once int/float construction is supported
5150
# class Int(int): pass

vm/src/obj/objint.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ fn int_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1414
}
1515

1616
fn int_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
17-
let ref cls = args.args[0];
17+
arg_check!(
18+
vm,
19+
args,
20+
required = [(cls, None)],
21+
optional = [(val_option, None)]
22+
);
1823
if !objtype::issubclass(cls, vm.ctx.int_type()) {
1924
return Err(vm.new_type_error(format!("{:?} is not a subtype of int", cls)));
2025
}
2126

2227
// TODO: extract kwargs:
2328
let base = 10;
24-
let val = to_int(vm, &args.args[1].clone(), base)?;
29+
let val = match val_option {
30+
Some(val) => to_int(vm, val, base)?,
31+
None => 0,
32+
};
2533
Ok(PyObject::new(
2634
PyObjectKind::Integer { value: val },
2735
cls.clone(),

vm/src/pyobject.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pub struct PyContext {
5656
pub float_type: PyObjectRef,
5757
pub bytes_type: PyObjectRef,
5858
pub bool_type: PyObjectRef,
59+
pub true_value: PyObjectRef,
60+
pub false_value: PyObjectRef,
5961
pub list_type: PyObjectRef,
6062
pub tuple_type: PyObjectRef,
6163
pub str_type: PyObjectRef,
@@ -127,12 +129,16 @@ impl PyContext {
127129
create_type("NoneType", &type_type, &object_type, &dict_type),
128130
);
129131

132+
let true_value = PyObject::new(PyObjectKind::Integer { value: 1 }, bool_type.clone());
133+
let false_value = PyObject::new(PyObjectKind::Integer { value: 0 }, bool_type.clone());
130134
let context = PyContext {
131135
int_type: int_type,
132136
float_type: float_type,
133137
bytes_type: bytes_type,
134138
list_type: list_type,
135139
bool_type: bool_type,
140+
true_value: true_value,
141+
false_value: false_value,
136142
tuple_type: tuple_type,
137143
dict_type: dict_type,
138144
none: none,
@@ -220,12 +226,11 @@ impl PyContext {
220226
}
221227

222228
pub fn new_bool(&self, b: bool) -> PyObjectRef {
223-
PyObject::new(
224-
PyObjectKind::Integer {
225-
value: if b { 1 } else { 0 },
226-
},
227-
self.bool_type(),
228-
)
229+
if b {
230+
self.true_value.clone()
231+
} else {
232+
self.false_value.clone()
233+
}
229234
}
230235

231236
pub fn new_tuple(&self, elements: Vec<PyObjectRef>) -> PyObjectRef {

0 commit comments

Comments
 (0)