Skip to content

Commit 324462e

Browse files
committed
Make get_method crate-private
1 parent 7d81376 commit 324462e

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

vm/src/vm.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,8 +1228,8 @@ impl VirtualMachine {
12281228
}
12291229
}
12301230

1231-
/// May return exception, if `__get__` descriptor raises one
1232-
pub fn get_method(&self, obj: PyObjectRef, method_name: &str) -> Option<PyResult> {
1231+
// TODO: remove + transfer over to get_special_method
1232+
pub(crate) fn get_method(&self, obj: PyObjectRef, method_name: &str) -> Option<PyResult> {
12331233
let method = obj.get_class_attr(method_name)?;
12341234
Some(self.call_if_get_descriptor(method, obj))
12351235
}
@@ -1698,25 +1698,30 @@ impl VirtualMachine {
16981698
}
16991699

17001700
pub fn obj_len_opt(&self, obj: &PyObjectRef) -> Option<PyResult<usize>> {
1701-
self.get_method(obj.clone(), "__len__").map(|len| {
1702-
let len = self.invoke(&len?, ())?;
1703-
let len = len
1704-
.payload_if_subclass::<PyInt>(self)
1705-
.ok_or_else(|| {
1706-
self.new_type_error(format!(
1707-
"'{}' object cannot be interpreted as an integer",
1708-
len.class().name
1709-
))
1710-
})?
1711-
.borrow_value();
1712-
if len.is_negative() {
1713-
return Err(self.new_value_error("__len__() should return >= 0".to_owned()));
1714-
}
1715-
let len = len.to_isize().ok_or_else(|| {
1716-
self.new_overflow_error("cannot fit 'int' into an index-sized integer".to_owned())
1717-
})?;
1718-
Ok(len as usize)
1719-
})
1701+
self.get_special_method(obj.clone(), "__len__")
1702+
.map(Result::ok)
1703+
.transpose()
1704+
.map(|meth| {
1705+
let len = meth?.invoke((), self)?;
1706+
let len = len
1707+
.payload_if_subclass::<PyInt>(self)
1708+
.ok_or_else(|| {
1709+
self.new_type_error(format!(
1710+
"'{}' object cannot be interpreted as an integer",
1711+
len.class().name
1712+
))
1713+
})?
1714+
.borrow_value();
1715+
if len.is_negative() {
1716+
return Err(self.new_value_error("__len__() should return >= 0".to_owned()));
1717+
}
1718+
let len = len.to_isize().ok_or_else(|| {
1719+
self.new_overflow_error(
1720+
"cannot fit 'int' into an index-sized integer".to_owned(),
1721+
)
1722+
})?;
1723+
Ok(len as usize)
1724+
})
17201725
}
17211726

17221727
pub fn obj_len(&self, obj: &PyObjectRef) -> PyResult<usize> {

0 commit comments

Comments
 (0)