mirror of https://github.com/python/cpython
Validate that __length_hint__ returns a usable result.
This commit is contained in:
parent
24e2872f0d
commit
2d03602576
|
@ -208,6 +208,11 @@ class BadLengthHint(object):
|
||||||
def __length_hint__(self):
|
def __length_hint__(self):
|
||||||
raise RuntimeError('hello')
|
raise RuntimeError('hello')
|
||||||
|
|
||||||
|
class NoneLengthHint(object):
|
||||||
|
def __iter__(self): return iter(range(10))
|
||||||
|
def __length_hint__(self):
|
||||||
|
return None
|
||||||
|
|
||||||
class TestLengthHintExceptions(unittest.TestCase):
|
class TestLengthHintExceptions(unittest.TestCase):
|
||||||
|
|
||||||
def test_issue1242657(self):
|
def test_issue1242657(self):
|
||||||
|
@ -225,6 +230,11 @@ class TestLengthHintExceptions(unittest.TestCase):
|
||||||
self.assertRaises(RuntimeError, b.extend, BadLen())
|
self.assertRaises(RuntimeError, b.extend, BadLen())
|
||||||
self.assertRaises(RuntimeError, b.extend, BadLengthHint())
|
self.assertRaises(RuntimeError, b.extend, BadLengthHint())
|
||||||
|
|
||||||
|
def test_invalid_hint(self):
|
||||||
|
# Make sure an invalid result doesn't muck-up the works
|
||||||
|
self.assertEqual(list(NoneLengthHint()), list(range(10)))
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
unittests = [
|
unittests = [
|
||||||
TestRepeat,
|
TestRepeat,
|
||||||
|
|
|
@ -123,7 +123,7 @@ _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
return defaultvalue;
|
return defaultvalue;
|
||||||
}
|
}
|
||||||
rv = PyInt_AsLong(ro);
|
rv = rv = PyLong_Check(ro) ? PyLong_AsSsize_t(ro) : defaultvalue;
|
||||||
Py_DECREF(ro);
|
Py_DECREF(ro);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue