Skip to content

Commit e060982

Browse files
authored
Merge pull request RustPython#133 from RustPython/bools_are_a_doubleton
Add true_value and false_value to context.
2 parents 9394fec + 6068ef6 commit e060982

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

tests/snippets/json_snippet.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ def round_trip_test(obj):
3434
# TODO: uncomment once negative floats are implemented
3535
# assert -1.0 == json.loads("-1.0")
3636
assert "str" == json.loads('"str"')
37-
# TODO: Use "is" once implemented
38-
assert True == json.loads('true')
39-
assert False == json.loads('false')
40-
assert None == json.loads('null')
37+
assert True is json.loads('true')
38+
assert False is json.loads('false')
39+
assert None is json.loads('null')
4140
assert [] == json.loads('[]')
4241
assert ['a'] == json.loads('["a"]')
4342
assert [['a'], 'b'] == json.loads('[["a"], "b"]')

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)