Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"dedentations",
"dedents",
"deduped",
"downcastable",
"downcasted",
"dumpable",
"emscripten",
Expand Down
14 changes: 7 additions & 7 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ mod array {
ArrayContentType::from_char(spec).map_err(|err| vm.new_value_error(err))?;

if let OptionalArg::Present(init) = init {
if let Some(init) = init.payload::<Self>() {
if let Some(init) = init.downcast_ref::<Self>() {
match (spec, init.read().typecode()) {
(spec, ch) if spec == ch => array.frombytes(&init.get_bytes()),
(spec, 'u') => {
Expand All @@ -681,7 +681,7 @@ mod array {
}
}
}
} else if let Some(wtf8) = init.payload::<PyStr>() {
} else if let Some(wtf8) = init.downcast_ref::<PyStr>() {
if spec == 'u' {
let bytes = Self::_unicode_to_wchar_bytes(wtf8.as_wtf8(), array.itemsize());
array.frombytes_move(bytes);
Expand All @@ -690,7 +690,7 @@ mod array {
"cannot use a str to initialize an array with typecode '{spec}'"
)));
}
} else if init.payload_is::<PyBytes>() || init.payload_is::<PyByteArray>() {
} else if init.downcastable::<PyBytes>() || init.downcastable::<PyByteArray>() {
init.try_bytes_like(vm, |x| array.frombytes(x))?;
} else if let Ok(iter) = ArgIterable::try_from_object(vm, init.clone()) {
for obj in iter.iter(vm)? {
Expand Down Expand Up @@ -765,7 +765,7 @@ mod array {
let mut w = zelf.try_resizable(vm)?;
if zelf.is(&obj) {
w.imul(2, vm)
} else if let Some(array) = obj.payload::<Self>() {
} else if let Some(array) = obj.downcast_ref::<Self>() {
w.iadd(&array.read(), vm)
} else {
let iter = ArgIterable::try_from_object(vm, obj)?;
Expand Down Expand Up @@ -1013,7 +1013,7 @@ mod array {
cloned = zelf.read().clone();
&cloned
} else {
match value.payload::<Self>() {
match value.downcast_ref::<Self>() {
Some(array) => {
guard = array.read();
&*guard
Expand Down Expand Up @@ -1059,7 +1059,7 @@ mod array {

#[pymethod]
fn __add__(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
if let Some(other) = other.payload::<Self>() {
if let Some(other) = other.downcast_ref::<Self>() {
self.read()
.add(&other.read(), vm)
.map(|array| Self::from(array).into_ref(&vm.ctx))
Expand All @@ -1079,7 +1079,7 @@ mod array {
) -> PyResult<PyRef<Self>> {
if zelf.is(&other) {
zelf.try_resizable(vm)?.imul(2, vm)?;
} else if let Some(other) = other.payload::<Self>() {
} else if let Some(other) = other.downcast_ref::<Self>() {
zelf.try_resizable(vm)?.iadd(&other.read(), vm)?;
} else {
return Err(vm.new_type_error(format!(
Expand Down
6 changes: 3 additions & 3 deletions stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ mod _csv {
mut _rest: FuncArgs,
vm: &VirtualMachine,
) -> PyResult<()> {
let Some(name) = name.payload_if_subclass::<PyStr>(vm) else {
let Some(name) = name.downcast_ref::<PyStr>() else {
return Err(vm.new_type_error("argument 0 must be a string"));
};
let dialect = match dialect {
Expand All @@ -290,7 +290,7 @@ mod _csv {
mut _rest: FuncArgs,
vm: &VirtualMachine,
) -> PyResult<PyDialect> {
let Some(name) = name.payload_if_subclass::<PyStr>(vm) else {
let Some(name) = name.downcast_ref::<PyStr>() else {
return Err(vm.new_exception_msg(
super::_csv::error(vm),
format!("argument 0 must be a string, not '{}'", name.class()),
Expand All @@ -309,7 +309,7 @@ mod _csv {
mut _rest: FuncArgs,
vm: &VirtualMachine,
) -> PyResult<()> {
let Some(name) = name.payload_if_subclass::<PyStr>(vm) else {
let Some(name) = name.downcast_ref::<PyStr>() else {
return Err(vm.new_exception_msg(
super::_csv::error(vm),
format!("argument 0 must be a string, not '{}'", name.class()),
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ mod decl {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let timeout = if vm.is_none(&obj) {
None
} else if let Some(float) = obj.payload::<PyFloat>() {
} else if let Some(float) = obj.downcast_ref::<PyFloat>() {
let float = float.to_f64();
if float.is_nan() {
return Err(vm.new_value_error("Invalid value NaN (not a number)"));
Expand Down
34 changes: 17 additions & 17 deletions stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ mod _sqlite {
let access = ptr_to_str(access, vm)?;

let val = callable.call((action, arg1, arg2, db_name, access), vm)?;
let Some(val) = val.payload::<PyInt>() else {
let Some(val) = val.downcast_ref::<PyInt>() else {
return Ok(SQLITE_DENY);
};
val.try_to_primitive::<c_int>(vm)
Expand Down Expand Up @@ -1897,18 +1897,18 @@ mod _sqlite {
Ok(self
.description
.iter()
.map(|x| x.payload::<PyTuple>().unwrap().as_slice()[0].clone())
.map(|x| x.downcast_ref::<PyTuple>().unwrap().as_slice()[0].clone())
.collect())
}

fn subscript(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {
if let Some(i) = needle.payload::<PyInt>() {
if let Some(i) = needle.downcast_ref::<PyInt>() {
let i = i.try_to_primitive::<isize>(vm)?;
self.data.getitem_by_index(vm, i)
} else if let Some(name) = needle.payload::<PyStr>() {
} else if let Some(name) = needle.downcast_ref::<PyStr>() {
for (obj, i) in self.description.iter().zip(0..) {
let obj = &obj.payload::<PyTuple>().unwrap().as_slice()[0];
let Some(obj) = obj.payload::<PyStr>() else {
let obj = &obj.downcast_ref::<PyTuple>().unwrap().as_slice()[0];
let Some(obj) = obj.downcast_ref::<PyStr>() else {
break;
};
let a_iter = name.as_str().chars().flat_map(|x| x.to_uppercase());
Expand All @@ -1919,7 +1919,7 @@ mod _sqlite {
}
}
Err(vm.new_index_error("No item with that key"))
} else if let Some(slice) = needle.payload::<PySlice>() {
} else if let Some(slice) = needle.downcast_ref::<PySlice>() {
let list = self.data.getitem_by_slice(vm, slice.to_saturated(vm)?)?;
Ok(vm.ctx.new_tuple(list).into())
} else {
Expand Down Expand Up @@ -1962,7 +1962,7 @@ mod _sqlite {
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
op.eq_only(|| {
if let Some(other) = other.payload::<Self>() {
if let Some(other) = other.downcast_ref::<Self>() {
let eq = vm
.bool_eq(zelf.description.as_object(), other.description.as_object())?
&& vm.bool_eq(zelf.data.as_object(), other.data.as_object())?;
Expand Down Expand Up @@ -2179,7 +2179,7 @@ mod _sqlite {
let mut byte: u8 = 0;
let ret = inner.blob.read_single(&mut byte, index);
self.check(ret, vm).map(|_| vm.ctx.new_int(byte).into())
} else if let Some(slice) = needle.payload::<PySlice>() {
} else if let Some(slice) = needle.downcast_ref::<PySlice>() {
let blob_len = inner.blob.bytes();
let slice = slice.to_saturated(vm)?;
let (range, step, length) = slice.adjust_indices(blob_len as usize);
Expand Down Expand Up @@ -2220,7 +2220,7 @@ mod _sqlite {
let inner = self.inner(vm)?;

if let Some(index) = needle.try_index_opt(vm) {
let Some(value) = value.payload::<PyInt>() else {
let Some(value) = value.downcast_ref::<PyInt>() else {
return Err(vm.new_type_error(format!(
"'{}' object cannot be interpreted as an integer",
value.class()
Expand All @@ -2232,7 +2232,7 @@ mod _sqlite {
Self::expect_write(blob_len, 1, index, vm)?;
let ret = inner.blob.write_single(value, index);
self.check(ret, vm)
} else if let Some(_slice) = needle.payload::<PySlice>() {
} else if let Some(_slice) = needle.downcast_ref::<PySlice>() {
Err(vm.new_not_implemented_error("Blob slice assignment is not implemented"))
// let blob_len = inner.blob.bytes();
// let slice = slice.to_saturated(vm)?;
Expand Down Expand Up @@ -2645,15 +2645,15 @@ mod _sqlite {

let ret = if vm.is_none(obj) {
unsafe { sqlite3_bind_null(self.st, pos) }
} else if let Some(val) = obj.payload::<PyInt>() {
} else if let Some(val) = obj.downcast_ref::<PyInt>() {
let val = val.try_to_primitive::<i64>(vm).map_err(|_| {
vm.new_overflow_error("Python int too large to convert to SQLite INTEGER")
})?;
unsafe { sqlite3_bind_int64(self.st, pos, val) }
} else if let Some(val) = obj.payload::<PyFloat>() {
} else if let Some(val) = obj.downcast_ref::<PyFloat>() {
let val = val.to_f64();
unsafe { sqlite3_bind_double(self.st, pos, val) }
} else if let Some(val) = obj.payload::<PyStr>() {
} else if let Some(val) = obj.downcast_ref::<PyStr>() {
let (ptr, len) = str_to_ptr_len(val, vm)?;
unsafe { sqlite3_bind_text(self.st, pos, ptr, len, SQLITE_TRANSIENT()) }
} else if let Ok(buffer) = PyBuffer::try_from_borrowed_object(vm, obj) {
Expand Down Expand Up @@ -2900,11 +2900,11 @@ mod _sqlite {
unsafe {
if vm.is_none(val) {
sqlite3_result_null(self.ctx)
} else if let Some(val) = val.payload::<PyInt>() {
} else if let Some(val) = val.downcast_ref::<PyInt>() {
sqlite3_result_int64(self.ctx, val.try_to_primitive(vm)?)
} else if let Some(val) = val.payload::<PyFloat>() {
} else if let Some(val) = val.downcast_ref::<PyFloat>() {
sqlite3_result_double(self.ctx, val.to_f64())
} else if let Some(val) = val.payload::<PyStr>() {
} else if let Some(val) = val.downcast_ref::<PyStr>() {
let (ptr, len) = str_to_ptr_len(val, vm)?;
sqlite3_result_text(self.ctx, ptr, len, SQLITE_TRANSIENT())
} else if let Ok(buffer) = PyBuffer::try_from_borrowed_object(vm, val) {
Expand Down
7 changes: 5 additions & 2 deletions stdlib/src/termios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,12 @@ mod termios {
))
})?;
for (cc, x) in termios.c_cc.iter_mut().zip(cc.iter()) {
*cc = if let Some(c) = x.payload::<PyBytes>().filter(|b| b.as_bytes().len() == 1) {
*cc = if let Some(c) = x
.downcast_ref::<PyBytes>()
.filter(|b| b.as_bytes().len() == 1)
{
c.as_bytes()[0] as _
} else if let Some(i) = x.payload::<PyInt>() {
} else if let Some(i) = x.downcast_ref::<PyInt>() {
i.try_to_primitive(vm)?
} else {
return Err(vm.new_type_error(
Expand Down
6 changes: 4 additions & 2 deletions vm/src/builtins/asyncgenerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ impl PyAsyncGenAThrow {
let ret = self.ag.inner.send(self.ag.as_object(), val, vm);
if self.aclose {
match ret {
Ok(PyIterReturn::Return(v)) if v.payload_is::<PyAsyncGenWrappedValue>() => {
Ok(PyIterReturn::Return(v))
if v.downcastable::<PyAsyncGenWrappedValue>() =>
{
Err(self.yield_close(vm))
}
other => other
Expand Down Expand Up @@ -392,7 +394,7 @@ impl PyAsyncGenAThrow {

fn ignored_close(&self, res: &PyResult<PyIterReturn>) -> bool {
res.as_ref().is_ok_and(|v| match v {
PyIterReturn::Return(obj) => obj.payload_is::<PyAsyncGenWrappedValue>(),
PyIterReturn::Return(obj) => obj.downcastable::<PyAsyncGenWrappedValue>(),
PyIterReturn::StopIteration(_) => false,
})
}
Expand Down
16 changes: 8 additions & 8 deletions vm/src/builtins/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl PyObjectRef {
Some(method_or_err) => {
let method = method_or_err?;
let bool_obj = method.call((), vm)?;
let int_obj = bool_obj.payload::<PyInt>().ok_or_else(|| {
let int_obj = bool_obj.downcast_ref::<PyInt>().ok_or_else(|| {
vm.new_type_error(format!(
"'{}' object cannot be interpreted as an integer",
bool_obj.class().name()
Expand Down Expand Up @@ -128,8 +128,8 @@ impl PyBool {
let lhs = get_value(&lhs);
let rhs = get_value(&rhs);
(lhs || rhs).to_pyobject(vm)
} else if let Some(lhs) = lhs.payload::<PyInt>() {
lhs.__or__(rhs, vm).to_pyobject(vm)
} else if let Some(lhs) = lhs.downcast_ref::<PyInt>() {
lhs.__or__(rhs).to_pyobject(vm)
} else {
vm.ctx.not_implemented()
}
Expand All @@ -144,8 +144,8 @@ impl PyBool {
let lhs = get_value(&lhs);
let rhs = get_value(&rhs);
(lhs && rhs).to_pyobject(vm)
} else if let Some(lhs) = lhs.payload::<PyInt>() {
lhs.__and__(rhs, vm).to_pyobject(vm)
} else if let Some(lhs) = lhs.downcast_ref::<PyInt>() {
lhs.__and__(rhs).to_pyobject(vm)
} else {
vm.ctx.not_implemented()
}
Expand All @@ -160,8 +160,8 @@ impl PyBool {
let lhs = get_value(&lhs);
let rhs = get_value(&rhs);
(lhs ^ rhs).to_pyobject(vm)
} else if let Some(lhs) = lhs.payload::<PyInt>() {
lhs.__xor__(rhs, vm).to_pyobject(vm)
} else if let Some(lhs) = lhs.downcast_ref::<PyInt>() {
lhs.__xor__(rhs).to_pyobject(vm)
} else {
vm.ctx.not_implemented()
}
Expand Down Expand Up @@ -212,5 +212,5 @@ pub(crate) fn init(context: &Context) {

// Retrieve inner int value:
pub(crate) fn get_value(obj: &PyObject) -> bool {
!obj.payload::<PyInt>().unwrap().as_bigint().is_zero()
!obj.downcast_ref::<PyInt>().unwrap().as_bigint().is_zero()
}
2 changes: 1 addition & 1 deletion vm/src/builtins/builtin_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl Comparable for PyNativeMethod {
_vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
op.eq_only(|| {
if let Some(other) = other.payload::<Self>() {
if let Some(other) = other.downcast_ref::<Self>() {
let eq = match (zelf.func.zelf.as_ref(), other.func.zelf.as_ref()) {
(Some(z), Some(o)) => z.is(o),
(None, None) => true,
Expand Down
12 changes: 6 additions & 6 deletions vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl PyObjectRef {
/// Tries converting a python object into a complex, returns an option of whether the complex
/// and whether the object was a complex originally or coerced into one
pub fn try_complex(&self, vm: &VirtualMachine) -> PyResult<Option<(Complex64, bool)>> {
if let Some(complex) = self.payload_if_exact::<PyComplex>(vm) {
if let Some(complex) = self.downcast_ref_if_exact::<PyComplex>(vm) {
return Ok(Some((complex.value, true)));
}
if let Some(method) = vm.get_method(self.clone(), identifier!(vm, __complex__)) {
Expand All @@ -79,7 +79,7 @@ impl PyObjectRef {

return Ok(Some((ret.value, true)));
} else {
return match result.payload::<PyComplex>() {
return match result.downcast_ref::<PyComplex>() {
Some(complex_obj) => Ok(Some((complex_obj.value, true))),
None => Err(vm.new_type_error(format!(
"__complex__ returned non-complex (type '{}')",
Expand All @@ -90,7 +90,7 @@ impl PyObjectRef {
}
// `complex` does not have a `__complex__` by default, so subclasses might not either,
// use the actual stored value in this case
if let Some(complex) = self.payload_if_subclass::<PyComplex>(vm) {
if let Some(complex) = self.downcast_ref::<PyComplex>() {
return Ok(Some((complex.value, true)));
}
if let Some(float) = self.try_float_opt(vm) {
Expand All @@ -105,7 +105,7 @@ pub fn init(context: &Context) {
}

fn to_op_complex(value: &PyObject, vm: &VirtualMachine) -> PyResult<Option<Complex64>> {
let r = if let Some(complex) = value.payload_if_subclass::<PyComplex>(vm) {
let r = if let Some(complex) = value.downcast_ref::<PyComplex>() {
Some(complex.value)
} else {
float::to_op_float(value, vm)?.map(|float| Complex64::new(float, 0.0))
Expand Down Expand Up @@ -175,7 +175,7 @@ impl Constructor for PyComplex {

if let Some(c) = val.try_complex(vm)? {
c
} else if let Some(s) = val.payload_if_subclass::<PyStr>(vm) {
} else if let Some(s) = val.downcast_ref::<PyStr>() {
if args.imag.is_present() {
return Err(vm.new_type_error(
"complex() can't take second arg if first is a string",
Expand Down Expand Up @@ -419,7 +419,7 @@ impl Comparable for PyComplex {
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
op.eq_only(|| {
let result = if let Some(other) = other.payload_if_subclass::<Self>(vm) {
let result = if let Some(other) = other.downcast_ref::<Self>() {
if zelf.value.re.is_nan()
&& zelf.value.im.is_nan()
&& other.value.re.is_nan()
Expand Down
Loading
Loading