Skip to content

Commit ded6be5

Browse files
dannasmanyouknowone
authored andcommitted
add type_check call to _check
1 parent ade45c2 commit ded6be5

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

vm/src/builtins/getset.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,19 @@ impl GetDescriptor for PyGetSet {
5151
_cls: Option<PyObjectRef>,
5252
vm: &VirtualMachine,
5353
) -> PyResult {
54-
let (zelf, obj) = match Self::_check(&zelf, obj, vm) {
55-
Some(obj) => obj,
56-
None => return Ok(zelf),
57-
};
58-
if let Some(ref f) = zelf.getter {
59-
f(vm, obj)
54+
if let Some(obj) = obj {
55+
let (zelf, obj) = Self::_check(&zelf, obj, vm)?;
56+
if let Some(ref f) = zelf.getter {
57+
f(vm, obj)
58+
} else {
59+
Err(vm.new_attribute_error(format!(
60+
"attribute '{}' of '{}' objects is not readable",
61+
zelf.name,
62+
Self::class(&vm.ctx).name()
63+
)))
64+
}
6065
} else {
61-
Err(vm.new_attribute_error(format!(
62-
"attribute '{}' of '{}' objects is not readable",
63-
zelf.name,
64-
Self::class(&vm.ctx).name()
65-
)))
66+
Ok(zelf)
6667
}
6768
}
6869
}

vm/src/protocol/object.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,8 @@ impl PyObject {
558558

559559
// type protocol
560560
// PyObject *PyObject_Type(PyObject *o)
561-
pub fn obj_type(&self) -> PyObjectRef {
562-
self.class().to_owned().into()
561+
pub fn obj_type(&self) -> PyTypeRef {
562+
self.class().to_owned()
563563
}
564564

565565
// int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)

vm/src/types/slot.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -925,22 +925,20 @@ pub trait GetDescriptor: PyPayload {
925925
#[inline]
926926
fn _check<'a>(
927927
zelf: &'a PyObject,
928-
obj: Option<PyObjectRef>,
928+
obj: PyObjectRef,
929929
vm: &VirtualMachine,
930-
) -> Option<(&'a Py<Self>, PyObjectRef)> {
930+
) -> PyResult<(&'a Py<Self>, PyObjectRef)> {
931931
// CPython descr_check
932-
let obj = obj?;
933-
// if (!PyObject_TypeCheck(obj, descr->d_type)) {
934-
// PyErr_Format(PyExc_TypeError,
935-
// "descriptor '%V' for '%.100s' objects "
936-
// "doesn't apply to a '%.100s' object",
937-
// descr_name((PyDescrObject *)descr), "?",
938-
// descr->d_type->slot_name,
939-
// obj->ob_type->slot_name);
940-
// *pres = NULL;
941-
// return 1;
942-
// } else {
943-
Some((Self::_as_pyref(zelf, vm).unwrap(), obj))
932+
if !obj.type_check(zelf.obj_type()) {
933+
Err(vm.new_type_error(format!(
934+
"descriptor {:?} for {} objects doesn't apply to a {} object",
935+
zelf,
936+
zelf.obj_type(),
937+
obj.obj_type()
938+
)))
939+
} else {
940+
Ok((Self::_as_pyref(zelf, vm).unwrap(), obj))
941+
}
944942
}
945943

946944
#[inline]

0 commit comments

Comments
 (0)