mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
Merge pull request #2689 from DimitrisJim/notimplemented_length_hint
Handle NotImplemented return from length_hint.
This commit is contained in:
@@ -516,11 +516,6 @@ class PyOperatorTestCase(OperatorTestCase, unittest.TestCase):
|
||||
class COperatorTestCase(OperatorTestCase, unittest.TestCase):
|
||||
module = c_operator
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_length_hint(self):
|
||||
super().test_length_hint()
|
||||
|
||||
|
||||
class OperatorPickleTestCase:
|
||||
def copy(self, obj, proto):
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::builtins::int::{self, PyInt};
|
||||
use crate::builtins::iter::PySequenceIterator;
|
||||
use crate::exceptions::PyBaseExceptionRef;
|
||||
use crate::vm::VirtualMachine;
|
||||
use crate::{PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol};
|
||||
use crate::{IdProtocol, PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol};
|
||||
use num_traits::Signed;
|
||||
|
||||
/*
|
||||
@@ -122,7 +122,12 @@ pub fn length_hint(vm: &VirtualMachine, iter: PyObjectRef) -> PyResult<Option<us
|
||||
None => return Ok(None),
|
||||
};
|
||||
let result = match vm.invoke(&hint, ()) {
|
||||
Ok(res) => res,
|
||||
Ok(res) => {
|
||||
if res.is(&vm.ctx.not_implemented) {
|
||||
return Ok(None);
|
||||
}
|
||||
res
|
||||
}
|
||||
Err(e) => {
|
||||
return if e.isinstance(&vm.ctx.exceptions.type_error) {
|
||||
Ok(None)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::builtins::int;
|
||||
use crate::builtins::int::PyIntRef;
|
||||
use crate::builtins::pystr::PyStrRef;
|
||||
use crate::byteslike::PyBytesLike;
|
||||
@@ -7,19 +8,21 @@ use crate::iterator;
|
||||
use crate::utils::Either;
|
||||
use crate::VirtualMachine;
|
||||
use crate::{PyObjectRef, PyResult, TypeProtocol};
|
||||
use int::PyInt;
|
||||
|
||||
fn _operator_length_hint(obj: PyObjectRef, default: OptionalArg, vm: &VirtualMachine) -> PyResult {
|
||||
let default = default.unwrap_or_else(|| vm.ctx.new_int(0));
|
||||
if !default.isinstance(&vm.ctx.types.int_type) {
|
||||
return Err(vm.new_type_error(format!(
|
||||
"'{}' type cannot be interpreted as an integer",
|
||||
default.class().name
|
||||
)));
|
||||
}
|
||||
let hint = iterator::length_hint(vm, obj)?
|
||||
.map(|i| vm.ctx.new_int(i))
|
||||
.unwrap_or(default);
|
||||
Ok(hint)
|
||||
let default: usize = default
|
||||
.map(|v| {
|
||||
if !v.isinstance(&vm.ctx.types.int_type) {
|
||||
return Err(vm.new_type_error(format!(
|
||||
"'{}' type cannot be interpreted as an integer",
|
||||
v.class().name
|
||||
)));
|
||||
}
|
||||
int::try_to_primitive(v.payload::<PyInt>().unwrap().as_bigint(), vm)
|
||||
})
|
||||
.unwrap_or(Ok(0))?;
|
||||
iterator::length_hint(vm, obj).map(|v| vm.ctx.new_int(v.unwrap_or(default)))
|
||||
}
|
||||
|
||||
fn _operator_compare_digest(
|
||||
|
||||
Reference in New Issue
Block a user