Skip to content

Commit 2a42772

Browse files
committed
Use a custom vtable for PyObjectRef
1 parent 1c6d771 commit 2a42772

File tree

13 files changed

+479
-598
lines changed

13 files changed

+479
-598
lines changed

vm/src/builtins/code.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl PyCode {}
198198
#[pyimpl]
199199
impl PyCodeRef {
200200
#[pyslot]
201-
fn tp_new(_cls: PyTypeRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
201+
fn tp_new(_cls: PyTypeRef, vm: &VirtualMachine) -> PyResult<Self> {
202202
Err(vm.new_type_error("Cannot directly create code object".to_owned()))
203203
}
204204

vm/src/builtins/object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ impl PyBaseObject {
217217
#[pyproperty(name = "__class__", setter)]
218218
fn set_class(instance: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
219219
if instance.payload_is::<PyBaseObject>() {
220-
match value.downcast_generic::<PyType>() {
220+
match value.downcast::<PyType>() {
221221
Ok(cls) => {
222222
// FIXME(#1979) cls instances might have a payload
223-
*instance.typ.write() = cls;
223+
*instance.class_lock().write() = cls;
224224
Ok(())
225225
}
226226
Err(value) => {

vm/src/builtins/pystr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ impl PyStr {
240240
if other.isinstance(&vm.ctx.types.str_type) {
241241
Ok(self.value.py_add(borrow_value(&other)))
242242
} else {
243-
Err(vm.new_type_error(format!("Cannot add {} and {}", self, other)))
243+
Err(vm.new_type_error(format!(
244+
"can only concatenate str (not \"{}\") to str",
245+
other.class().name
246+
)))
244247
}
245248
}
246249

vm/src/builtins/weakref.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use super::pytype::PyTypeRef;
22
use crate::common::hash::PyHash;
33
use crate::function::{FuncArgs, OptionalArg};
44
use crate::pyobject::{
5-
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
5+
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue,
6+
TypeProtocol,
67
};
7-
use crate::pyobjectrc::{PyObjectRc, PyObjectWeak};
88
use crate::slots::{Callable, Comparable, Hashable, PyComparisonOp};
99
use crate::vm::VirtualMachine;
1010

@@ -20,7 +20,7 @@ pub struct PyWeak {
2020
impl PyWeak {
2121
pub fn downgrade(obj: &PyObjectRef) -> PyWeak {
2222
PyWeak {
23-
referent: PyObjectRc::downgrade(obj),
23+
referent: PyObjectRef::downgrade(obj),
2424
hash: AtomicCell::new(None),
2525
}
2626
}

vm/src/bytesinner.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::byteslike::try_bytes_like;
1414
use crate::cformat::CFormatBytes;
1515
use crate::function::{OptionalArg, OptionalOption};
1616
use crate::pyobject::{
17-
BorrowValue, Either, IdProtocol, PyComparisonValue, PyIterable, PyObjectRef, PyRef, PyResult,
18-
PyValue, TryFromObject, TypeProtocol,
17+
BorrowValue, Either, IdProtocol, PyComparisonValue, PyIterable, PyObjectRef, PyResult, PyValue,
18+
TryFromObject, TypeProtocol,
1919
};
2020
use crate::sliceable::PySliceableSequence;
2121
use crate::slots::PyComparisonOp;
@@ -91,10 +91,10 @@ impl ByteInnerNewOptions {
9191

9292
pub fn get_bytes(mut self, cls: PyTypeRef, vm: &VirtualMachine) -> PyResult<PyBytesRef> {
9393
let inner = if let OptionalArg::Present(source) = self.source.take() {
94-
if source.class().is(&PyBytes::class(vm)) && cls.is(&PyBytes::class(vm)) {
95-
return self
96-
.check_args(vm)
97-
.map(|_| unsafe { PyRef::from_obj_unchecked(source) });
94+
if cls.is(&PyBytes::class(vm)) {
95+
if let Ok(source) = source.clone().downcast_exact(vm) {
96+
return self.check_args(vm).map(|()| source);
97+
}
9898
}
9999

100100
match_class!(match source {

0 commit comments

Comments
 (0)