Skip to content

Commit d97a398

Browse files
committed
flexible new_downcast_error
1 parent a696178 commit d97a398

File tree

6 files changed

+43
-24
lines changed

6 files changed

+43
-24
lines changed

vm/src/builtins/set.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,9 @@ impl PySet {
572572
}
573573

574574
#[pymethod(magic)]
575-
fn ior(zelf: PyRef<Self>, iterable: SetIterable, vm: &VirtualMachine) -> PyResult {
575+
fn ior(zelf: PyRef<Self>, iterable: SetIterable, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
576576
zelf.inner.update(iterable.iterable, vm)?;
577-
Ok(zelf.as_object().to_owned())
577+
Ok(zelf)
578578
}
579579

580580
#[pymethod]
@@ -594,9 +594,13 @@ impl PySet {
594594
}
595595

596596
#[pymethod(magic)]
597-
fn iand(zelf: PyRef<Self>, iterable: SetIterable, vm: &VirtualMachine) -> PyResult {
597+
fn iand(
598+
zelf: PyRef<Self>,
599+
iterable: SetIterable,
600+
vm: &VirtualMachine,
601+
) -> PyResult<PyRef<Self>> {
598602
zelf.inner.intersection_update(iterable.iterable, vm)?;
599-
Ok(zelf.as_object().to_owned())
603+
Ok(zelf)
600604
}
601605

602606
#[pymethod]
@@ -606,9 +610,13 @@ impl PySet {
606610
}
607611

608612
#[pymethod(magic)]
609-
fn isub(zelf: PyRef<Self>, iterable: SetIterable, vm: &VirtualMachine) -> PyResult {
613+
fn isub(
614+
zelf: PyRef<Self>,
615+
iterable: SetIterable,
616+
vm: &VirtualMachine,
617+
) -> PyResult<PyRef<Self>> {
610618
zelf.inner.difference_update(iterable.iterable, vm)?;
611-
Ok(zelf.as_object().to_owned())
619+
Ok(zelf)
612620
}
613621

614622
#[pymethod]
@@ -622,10 +630,14 @@ impl PySet {
622630
}
623631

624632
#[pymethod(magic)]
625-
fn ixor(zelf: PyRef<Self>, iterable: SetIterable, vm: &VirtualMachine) -> PyResult {
633+
fn ixor(
634+
zelf: PyRef<Self>,
635+
iterable: SetIterable,
636+
vm: &VirtualMachine,
637+
) -> PyResult<PyRef<Self>> {
626638
zelf.inner
627639
.symmetric_difference_update(iterable.iterable, vm)?;
628-
Ok(zelf.as_object().to_owned())
640+
Ok(zelf)
629641
}
630642

631643
#[pymethod(magic)]

vm/src/convert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ where
9595
let class = T::class(vm);
9696
if obj.fast_isinstance(class) {
9797
obj.downcast()
98-
.map_err(|obj| vm.new_downcast_runtime_error(class, obj))
98+
.map_err(|obj| vm.new_downcast_runtime_error(class, &obj))
9999
} else {
100100
T::special_retrieve(vm, &obj)
101-
.unwrap_or_else(|| Err(vm.new_downcast_type_error(class, obj)))
101+
.unwrap_or_else(|| Err(vm.new_downcast_type_error(class, &obj)))
102102
}
103103
}
104104
}

vm/src/protocol/object.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl PyObjectRef {
5252
pub fn bytes(self, vm: &VirtualMachine) -> PyResult {
5353
let bytes_type = &vm.ctx.types.bytes_type;
5454
match self.downcast_exact::<PyInt>(vm) {
55-
Ok(int) => Err(vm.new_downcast_type_error(bytes_type, int.as_object())),
55+
Ok(int) => Err(vm.new_downcast_type_error(bytes_type, &int)),
5656
Err(obj) => PyBytes::py_new(
5757
bytes_type.clone(),
5858
ByteInnerNewOptions {

vm/src/pyobject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl<T: PyValue> TryFromObject for PyRefExact<T> {
366366
drop(cls);
367367
let obj = obj
368368
.downcast()
369-
.map_err(|obj| vm.new_downcast_runtime_error(target_cls, obj))?;
369+
.map_err(|obj| vm.new_downcast_runtime_error(target_cls, &obj))?;
370370
Ok(Self { obj })
371371
} else if cls.fast_issubclass(target_cls) {
372372
Err(vm.new_type_error(format!(

vm/src/stdlib/operator.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,9 @@ mod _operator {
446446

447447
#[pymethod(magic)]
448448
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<(PyTypeRef, PyTupleRef)> {
449-
let attrs = vm.ctx.new_tuple(
450-
zelf.attrs
451-
.iter()
452-
.map(|v| v.as_object().to_owned())
453-
.collect(),
454-
);
449+
let attrs = vm
450+
.ctx
451+
.new_tuple(zelf.attrs.iter().map(|v| v.clone().into()).collect());
455452
Ok((zelf.class().clone(), attrs))
456453
}
457454

vm/src/vm_new.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,9 @@ impl VirtualMachine {
268268
msg: &'static str,
269269
error_type: &PyTypeRef,
270270
class: &PyTypeRef,
271-
obj: impl std::borrow::Borrow<PyObject>, // the impl Borrow allows to pass PyObjectRef or &PyObject
271+
obj: &PyObject, // the impl Borrow allows to pass PyObjectRef or &PyObject
272272
) -> PyBaseExceptionRef {
273-
let actual_class = obj.borrow().class();
273+
let actual_class = obj.class();
274274
let actual_type = &*actual_class.name();
275275
let expected_type = &*class.name();
276276
let msg = format!("Expected {msg} '{expected_type}' but '{actual_type}' found");
@@ -280,16 +280,26 @@ impl VirtualMachine {
280280
pub(crate) fn new_downcast_runtime_error(
281281
&self,
282282
class: &PyTypeRef,
283-
obj: impl std::borrow::Borrow<PyObject>,
283+
obj: &impl AsPyObject,
284284
) -> PyBaseExceptionRef {
285-
self.new_downcast_error("payload", &self.ctx.exceptions.runtime_error, class, obj)
285+
self.new_downcast_error(
286+
"payload",
287+
&self.ctx.exceptions.runtime_error,
288+
class,
289+
obj.as_object(),
290+
)
286291
}
287292

288293
pub(crate) fn new_downcast_type_error(
289294
&self,
290295
class: &PyTypeRef,
291-
obj: impl std::borrow::Borrow<PyObject>,
296+
obj: &impl AsPyObject,
292297
) -> PyBaseExceptionRef {
293-
self.new_downcast_error("type", &self.ctx.exceptions.type_error, class, obj)
298+
self.new_downcast_error(
299+
"type",
300+
&self.ctx.exceptions.type_error,
301+
class,
302+
obj.as_object(),
303+
)
294304
}
295305
}

0 commit comments

Comments
 (0)