Skip to content

Commit c4b3c5b

Browse files
youknowoneSnowapril
authored andcommitted
vm.ctx.new_bool returns PyIntRef
1 parent 4efb0b8 commit c4b3c5b

File tree

17 files changed

+44
-38
lines changed

17 files changed

+44
-38
lines changed

stdlib/src/json.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ mod _json {
126126
}
127127

128128
parse_const!("null", vm.ctx.none());
129-
parse_const!("true", vm.ctx.new_bool(true));
130-
parse_const!("false", vm.ctx.new_bool(false));
129+
parse_const!("true", true);
130+
parse_const!("false", false);
131131

132132
if let Some((res, len)) = self.parse_number(s, vm) {
133133
return Ok(PyIterReturn::Return(vm.new_tuple((res?, idx + len)).into()));

stdlib/src/pyexpat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ mod _pyexpat {
7171
end_element: MutableObject::new(vm.ctx.none()),
7272
character_data: MutableObject::new(vm.ctx.none()),
7373
entity_decl: MutableObject::new(vm.ctx.none()),
74-
buffer_text: MutableObject::new(vm.ctx.new_bool(false)),
74+
buffer_text: MutableObject::new(vm.ctx.new_bool(false).into()),
7575
}
7676
.into_ref(vm))
7777
}

stdlib/src/scproxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mod _scproxy {
4747
.and_then(|v| v.downcast::<CFNumber>())
4848
.and_then(|v| v.to_i32())
4949
.unwrap_or(0);
50-
result.set_item("exclude_simple", vm.ctx.new_bool(v), vm)?;
50+
result.set_item("exclude_simple", vm.ctx.new_bool(v).into(), vm)?;
5151

5252
if let Some(an_array) = proxy_dict
5353
.find(unsafe { kSCPropNetProxiesExceptionsList })

stdlib/src/ssl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ fn _ssl_enum_certificates(store_name: PyStrRef, vm: &VirtualMachine) -> PyResult
191191
wincrypt::PKCS_7_ASN_ENCODING => vm.ctx.new_ascii_literal(ascii!("pkcs_7_asn")),
192192
other => vm.ctx.new_int(other).into(),
193193
};
194-
let usage = match c.valid_uses()? {
195-
ValidUses::All => vm.ctx.new_bool(true),
194+
let usage: PyObjectRef = match c.valid_uses()? {
195+
ValidUses::All => vm.ctx.new_bool(true).into(),
196196
ValidUses::Oids(oids) => {
197197
PyFrozenSet::from_iter(vm, oids.into_iter().map(|oid| vm.ctx.new_utf8_str(oid)))
198198
.unwrap()

vm/src/builtins/code.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl ConstantBag for PyObjBag<'_> {
8888
}
8989
bytecode::ConstantData::Str { value } => vm.ctx.new_utf8_str(value),
9090
bytecode::ConstantData::Bytes { value } => ctx.new_bytes(value.to_vec()),
91-
bytecode::ConstantData::Boolean { value } => ctx.new_bool(value),
91+
bytecode::ConstantData::Boolean { value } => ctx.new_bool(value).into(),
9292
bytecode::ConstantData::Code { code } => ctx.new_code_object(code.map_bag(self)).into(),
9393
bytecode::ConstantData::Tuple { elements } => {
9494
let elements = elements
@@ -114,7 +114,7 @@ impl ConstantBag for PyObjBag<'_> {
114114
}
115115
bytecode::BorrowedConstant::Str { value } => vm.ctx.new_utf8_str(value),
116116
bytecode::BorrowedConstant::Bytes { value } => ctx.new_bytes(value.to_vec()),
117-
bytecode::BorrowedConstant::Boolean { value } => ctx.new_bool(value),
117+
bytecode::BorrowedConstant::Boolean { value } => ctx.new_bool(value).into(),
118118
bytecode::BorrowedConstant::Code { code } => {
119119
ctx.new_code_object(code.map_clone_bag(self)).into()
120120
}

vm/src/builtins/int.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,7 @@ impl PyInt {
254254
if cls.is(&vm.ctx.types.int_type) {
255255
Ok(vm.ctx.new_int(value))
256256
} else if cls.is(&vm.ctx.types.bool_type) {
257-
Ok(vm
258-
.ctx
259-
.new_bool(!value.into().eq(&BigInt::zero()))
260-
.downcast()
261-
.unwrap())
257+
Ok(vm.ctx.new_bool(!value.into().eq(&BigInt::zero())))
262258
} else {
263259
PyInt::from(value).into_ref_with_type(vm, cls)
264260
}

vm/src/builtins/mappingproxy.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ impl PyMappingProxy {
9292
let key = PyStrRef::try_from_object(vm, key)?;
9393
Ok(vm
9494
.ctx
95-
.new_bool(class.attributes.read().contains_key(key.as_str())))
95+
.new_bool(class.attributes.read().contains_key(key.as_str()))
96+
.into())
9697
}
9798
MappingProxyInner::Dict(obj) => vm._membership(obj.clone(), key),
9899
}

vm/src/builtins/pybool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::fmt::{Debug, Formatter};
1111

1212
impl IntoPyObject for bool {
1313
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
14-
vm.ctx.new_bool(self)
14+
vm.ctx.new_bool(self).into()
1515
}
1616
}
1717

@@ -105,7 +105,7 @@ impl SlotConstructor for PyBool {
105105
)));
106106
}
107107
let val = x.map_or(Ok(false), |val| val.try_to_bool(vm))?;
108-
Ok(vm.ctx.new_bool(val))
108+
Ok(vm.ctx.new_bool(val).into())
109109
}
110110
}
111111

vm/src/dictdatatype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ mod tests {
763763
let dict = Dict::default();
764764
assert_eq!(0, dict.len());
765765

766-
let key1 = vm.ctx.new_bool(true);
766+
let key1 = vm.new_pyobj(true);
767767
let value1 = vm.ctx.new_ascii_literal(ascii!("abc"));
768768
dict.insert(&vm, key1.clone(), value1.clone()).unwrap();
769769
assert_eq!(1, dict.len());

vm/src/frame.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ impl ExecutingFrame<'_> {
895895
// TODO: figure out a better way to communicate PyMethod::Attribute - CPython uses
896896
// target==NULL, maybe we could use a sentinel value or something?
897897
self.push_value(target);
898-
self.push_value(vm.ctx.new_bool(is_method));
898+
self.push_value(vm.ctx.new_bool(is_method).into());
899899
self.push_value(func);
900900
Ok(None)
901901
}
@@ -1635,7 +1635,7 @@ impl ExecutingFrame<'_> {
16351635
bytecode::UnaryOperator::Invert => vm._invert(&a)?,
16361636
bytecode::UnaryOperator::Not => {
16371637
let value = a.try_to_bool(vm)?;
1638-
vm.ctx.new_bool(!value)
1638+
vm.ctx.new_bool(!value).into()
16391639
}
16401640
};
16411641
self.push_value(value);
@@ -1690,11 +1690,13 @@ impl ExecutingFrame<'_> {
16901690
bytecode::ComparisonOperator::LessOrEqual => vm.obj_cmp(a, b, PyComparisonOp::Le)?,
16911691
bytecode::ComparisonOperator::Greater => vm.obj_cmp(a, b, PyComparisonOp::Gt)?,
16921692
bytecode::ComparisonOperator::GreaterOrEqual => vm.obj_cmp(a, b, PyComparisonOp::Ge)?,
1693-
bytecode::ComparisonOperator::Is => vm.ctx.new_bool(self._is(a, b)),
1694-
bytecode::ComparisonOperator::IsNot => vm.ctx.new_bool(self._is_not(a, b)),
1695-
bytecode::ComparisonOperator::In => vm.ctx.new_bool(self._in(vm, a, b)?),
1696-
bytecode::ComparisonOperator::NotIn => vm.ctx.new_bool(self._not_in(vm, a, b)?),
1697-
bytecode::ComparisonOperator::ExceptionMatch => vm.ctx.new_bool(vm.isinstance(&a, &b)?),
1693+
bytecode::ComparisonOperator::Is => vm.ctx.new_bool(self._is(a, b)).into(),
1694+
bytecode::ComparisonOperator::IsNot => vm.ctx.new_bool(self._is_not(a, b)).into(),
1695+
bytecode::ComparisonOperator::In => vm.ctx.new_bool(self._in(vm, a, b)?).into(),
1696+
bytecode::ComparisonOperator::NotIn => vm.ctx.new_bool(self._not_in(vm, a, b)?).into(),
1697+
bytecode::ComparisonOperator::ExceptionMatch => {
1698+
vm.ctx.new_bool(vm.isinstance(&a, &b)?).into()
1699+
}
16981700
};
16991701

17001702
self.push_value(value);

vm/src/py_serde.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'de> Visitor<'de> for PyObjectDeserializer<'de> {
139139
where
140140
E: serde::de::Error,
141141
{
142-
Ok(self.vm.ctx.new_bool(value))
142+
Ok(self.vm.ctx.new_bool(value).into())
143143
}
144144

145145
// Other signed integers delegate to this method by default, it’s the only one needed

vm/src/pyobject.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ impl PyContext {
219219
}
220220

221221
#[inline]
222-
pub fn new_bool(&self, b: bool) -> PyObjectRef {
222+
pub fn new_bool(&self, b: bool) -> PyIntRef {
223223
let value = if b {
224224
&self.true_value
225225
} else {
226226
&self.false_value
227227
};
228-
value.clone().into()
228+
value.clone()
229229
}
230230

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

vm/src/stdlib/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl Node for ast::Constant {
188188
fn ast_to_object(self, vm: &VirtualMachine) -> PyObjectRef {
189189
match self {
190190
ast::Constant::None => vm.ctx.none(),
191-
ast::Constant::Bool(b) => vm.ctx.new_bool(b),
191+
ast::Constant::Bool(b) => vm.ctx.new_bool(b).into(),
192192
ast::Constant::Str(s) => vm.ctx.new_utf8_str(s),
193193
ast::Constant::Bytes(b) => vm.ctx.new_bytes(b),
194194
ast::Constant::Int(i) => vm.ctx.new_int(i).into(),

vm/src/stdlib/io.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ mod _io {
7878
};
7979
use crate::{
8080
builtins::{
81-
PyBaseExceptionRef, PyByteArray, PyBytes, PyBytesRef, PyMemoryView, PyStr, PyStrRef,
82-
PyType, PyTypeRef,
81+
PyBaseExceptionRef, PyByteArray, PyBytes, PyBytesRef, PyIntRef, PyMemoryView, PyStr,
82+
PyStrRef, PyType, PyTypeRef,
8383
},
8484
exceptions,
8585
function::{
@@ -370,7 +370,7 @@ mod _io {
370370
}
371371

372372
#[pyattr]
373-
fn __closed(ctx: &PyContext) -> PyObjectRef {
373+
fn __closed(ctx: &PyContext) -> PyIntRef {
374374
ctx.new_bool(false)
375375
}
376376

@@ -543,7 +543,7 @@ mod _io {
543543
pub(super) fn iobase_close(file: &PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
544544
if !file_closed(file, vm)? {
545545
let res = vm.call_method(file, "flush", ());
546-
vm.set_attr(file, "__closed", vm.ctx.new_bool(true))?;
546+
vm.set_attr(file, "__closed", vm.new_pyobj(true))?;
547547
res?;
548548
}
549549
Ok(())

vm/src/stdlib/pystruct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ pub(crate) mod _struct {
698698

699699
fn unpack<E: ByteOrder>(vm: &VirtualMachine, rdr: &[u8]) -> PyObjectRef {
700700
let i = u8::unpack_int::<E>(rdr);
701-
vm.ctx.new_bool(i != 0)
701+
vm.ctx.new_bool(i != 0).into()
702702
}
703703
}
704704

vm/src/stdlib/sys.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,10 @@ pub(crate) fn make_module(vm: &VirtualMachine, module: PyObjectRef, builtins: Py
540540

541541
let xopts = ctx.new_dict();
542542
for (key, value) in &vm.state.settings.xopts {
543-
let value = value
544-
.as_ref()
545-
.map_or_else(|| ctx.new_bool(true), |s| ctx.new_utf8_str(s.clone()));
543+
let value = value.as_ref().map_or_else(
544+
|| ctx.new_bool(true).into(),
545+
|s| ctx.new_utf8_str(s.clone()),
546+
);
546547
xopts.set_item(&**key, value, vm).unwrap();
547548
}
548549

vm/src/vm.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,11 @@ impl VirtualMachine {
19991999
}
20002000

20012001
// https://docs.python.org/3/reference/expressions.html#membership-test-operations
2002-
fn _membership_iter_search(&self, haystack: PyObjectRef, needle: PyObjectRef) -> PyResult {
2002+
fn _membership_iter_search(
2003+
&self,
2004+
haystack: PyObjectRef,
2005+
needle: PyObjectRef,
2006+
) -> PyResult<PyIntRef> {
20032007
let iter = haystack.get_iter(self)?;
20042008
loop {
20052009
if let PyIterReturn::Return(element) = iter.next(self)? {
@@ -2017,7 +2021,9 @@ impl VirtualMachine {
20172021
pub fn _membership(&self, haystack: PyObjectRef, needle: PyObjectRef) -> PyResult {
20182022
match PyMethod::get_special(haystack, "__contains__", self)? {
20192023
Ok(method) => method.invoke((needle,), self),
2020-
Err(haystack) => self._membership_iter_search(haystack, needle),
2024+
Err(haystack) => self
2025+
._membership_iter_search(haystack, needle)
2026+
.map(Into::into),
20212027
}
20222028
}
20232029

0 commit comments

Comments
 (0)