Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type_check to _check() #5095

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions vm/src/builtins/getset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,19 @@ impl GetDescriptor for PyGetSet {
_cls: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult {
let (zelf, obj) = match Self::_check(&zelf, obj, vm) {
Some(obj) => obj,
None => return Ok(zelf),
};
if let Some(ref f) = zelf.getter {
f(vm, obj)
if let Some(obj) = obj {
let (zelf, obj) = Self::_check(&zelf, obj, vm)?;
if let Some(ref f) = zelf.getter {
f(vm, obj)
} else {
Err(vm.new_attribute_error(format!(
"attribute '{}' of '{}' objects is not readable",
zelf.name,
Self::class(&vm.ctx).name()
)))
}
} else {
Err(vm.new_attribute_error(format!(
"attribute '{}' of '{}' objects is not readable",
zelf.name,
Self::class(&vm.ctx).name()
)))
Ok(zelf)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions vm/src/protocol/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,13 +559,13 @@ impl PyObject {

// type protocol
// PyObject *PyObject_Type(PyObject *o)
pub fn obj_type(&self) -> PyObjectRef {
self.class().to_owned().into()
pub fn obj_type(&self) -> PyTypeRef {
self.class().to_owned()
}

// int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
pub fn type_check(&self, typ: PyTypeRef) -> bool {
self.class().fast_isinstance(&typ)
self.fast_isinstance(&typ)
}

pub fn length_opt(&self, vm: &VirtualMachine) -> Option<PyResult<usize>> {
Expand Down
26 changes: 12 additions & 14 deletions vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,22 +893,20 @@ pub trait GetDescriptor: PyPayload {
#[inline]
fn _check<'a>(
zelf: &'a PyObject,
obj: Option<PyObjectRef>,
obj: PyObjectRef,
vm: &VirtualMachine,
) -> Option<(&'a Py<Self>, PyObjectRef)> {
) -> PyResult<(&'a Py<Self>, PyObjectRef)> {
// CPython descr_check
let obj = obj?;
// if (!PyObject_TypeCheck(obj, descr->d_type)) {
// PyErr_Format(PyExc_TypeError,
// "descriptor '%V' for '%.100s' objects "
// "doesn't apply to a '%.100s' object",
// descr_name((PyDescrObject *)descr), "?",
// descr->d_type->slot_name,
// obj->ob_type->slot_name);
// *pres = NULL;
// return 1;
// } else {
Some((Self::_as_pyref(zelf, vm).unwrap(), obj))
if !obj.type_check(zelf.obj_type()) {
Err(vm.new_type_error(format!(
"descriptor {:?} for {} objects doesn't apply to a {} object",
zelf,
zelf.obj_type(),
obj.obj_type()
)))
} else {
Ok((Self::_as_pyref(zelf, vm).unwrap(), obj))
}
}

#[inline]
Expand Down