summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorJesus Cea <jcea@jcea.es>2011-04-20 15:09:23 (GMT)
committerJesus Cea <jcea@jcea.es>2011-04-20 15:09:23 (GMT)
commitac4515063c18157645d135bca8c4cf39542ccd6e (patch)
treea9f65e370ab063ec2c611215da1525fdf247c035 /Lib/test
parentd07eaf177c7d43a2479c1eb1fbab55e26e3822fa (diff)
downloadcpython-ac4515063c18157645d135bca8c4cf39542ccd6e.zip
cpython-ac4515063c18157645d135bca8c4cf39542ccd6e.tar.gz
cpython-ac4515063c18157645d135bca8c4cf39542ccd6e.tar.bz2
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/string_tests.py57
-rw-r--r--Lib/test/test_bytes.py62
2 files changed, 119 insertions, 0 deletions
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index a813616..09622dd 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -1156,6 +1156,63 @@ class MixinStrUnicodeUserStringTest:
self.checkraises(ValueError, S, 'rpartition', '')
self.checkraises(TypeError, S, 'rpartition', None)
+ def test_none_arguments(self):
+ # issue 11828
+ s = 'hello'
+ self.checkequal(2, s, 'find', 'l', None)
+ self.checkequal(3, s, 'find', 'l', -2, None)
+ self.checkequal(2, s, 'find', 'l', None, -2)
+ self.checkequal(0, s, 'find', 'h', None, None)
+
+ self.checkequal(3, s, 'rfind', 'l', None)
+ self.checkequal(3, s, 'rfind', 'l', -2, None)
+ self.checkequal(2, s, 'rfind', 'l', None, -2)
+ self.checkequal(0, s, 'rfind', 'h', None, None)
+
+ self.checkequal(2, s, 'index', 'l', None)
+ self.checkequal(3, s, 'index', 'l', -2, None)
+ self.checkequal(2, s, 'index', 'l', None, -2)
+ self.checkequal(0, s, 'index', 'h', None, None)
+
+ self.checkequal(3, s, 'rindex', 'l', None)
+ self.checkequal(3, s, 'rindex', 'l', -2, None)
+ self.checkequal(2, s, 'rindex', 'l', None, -2)
+ self.checkequal(0, s, 'rindex', 'h', None, None)
+
+ self.checkequal(2, s, 'count', 'l', None)
+ self.checkequal(1, s, 'count', 'l', -2, None)
+ self.checkequal(1, s, 'count', 'l', None, -2)
+ self.checkequal(0, s, 'count', 'x', None, None)
+
+ self.checkequal(True, s, 'endswith', 'o', None)
+ self.checkequal(True, s, 'endswith', 'lo', -2, None)
+ self.checkequal(True, s, 'endswith', 'l', None, -2)
+ self.checkequal(False, s, 'endswith', 'x', None, None)
+
+ self.checkequal(True, s, 'startswith', 'h', None)
+ self.checkequal(True, s, 'startswith', 'l', -2, None)
+ self.checkequal(True, s, 'startswith', 'h', None, -2)
+ self.checkequal(False, s, 'startswith', 'x', None, None)
+
+ def test_find_etc_raise_correct_error_messages(self):
+ # issue 11828
+ s = 'hello'
+ x = 'x'
+ self.assertRaisesRegexp(TypeError, r'^find\(', s.find,
+ x, None, None, None)
+ self.assertRaisesRegexp(TypeError, r'^rfind\(', s.rfind,
+ x, None, None, None)
+ self.assertRaisesRegexp(TypeError, r'^index\(', s.index,
+ x, None, None, None)
+ self.assertRaisesRegexp(TypeError, r'^rindex\(', s.rindex,
+ x, None, None, None)
+ self.assertRaisesRegexp(TypeError, r'^count\(', s.count,
+ x, None, None, None)
+ self.assertRaisesRegexp(TypeError, r'^startswith\(', s.startswith,
+ x, None, None, None)
+ self.assertRaisesRegexp(TypeError, r'^endswith\(', s.endswith,
+ x, None, None, None)
+
class MixinStrUnicodeTest:
# Additional tests that only work with str and unicode.
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 8b964ca..a607bef 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -459,6 +459,68 @@ class BaseBytesTest(unittest.TestCase):
self.assertRaises(ValueError, self.type2test.maketrans, b'abc', b'xyzq')
self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def')
+ def test_none_arguments(self):
+ # issue 11828
+ b = self.type2test(b'hello')
+ l = self.type2test(b'l')
+ h = self.type2test(b'h')
+ x = self.type2test(b'x')
+ o = self.type2test(b'o')
+
+ self.assertEqual(2, b.find(l, None))
+ self.assertEqual(3, b.find(l, -2, None))
+ self.assertEqual(2, b.find(l, None, -2))
+ self.assertEqual(0, b.find(h, None, None))
+
+ self.assertEqual(3, b.rfind(l, None))
+ self.assertEqual(3, b.rfind(l, -2, None))
+ self.assertEqual(2, b.rfind(l, None, -2))
+ self.assertEqual(0, b.rfind(h, None, None))
+
+ self.assertEqual(2, b.index(l, None))
+ self.assertEqual(3, b.index(l, -2, None))
+ self.assertEqual(2, b.index(l, None, -2))
+ self.assertEqual(0, b.index(h, None, None))
+
+ self.assertEqual(3, b.rindex(l, None))
+ self.assertEqual(3, b.rindex(l, -2, None))
+ self.assertEqual(2, b.rindex(l, None, -2))
+ self.assertEqual(0, b.rindex(h, None, None))
+
+ self.assertEqual(2, b.count(l, None))
+ self.assertEqual(1, b.count(l, -2, None))
+ self.assertEqual(1, b.count(l, None, -2))
+ self.assertEqual(0, b.count(x, None, None))
+
+ self.assertEqual(True, b.endswith(o, None))
+ self.assertEqual(True, b.endswith(o, -2, None))
+ self.assertEqual(True, b.endswith(l, None, -2))
+ self.assertEqual(False, b.endswith(x, None, None))
+
+ self.assertEqual(True, b.startswith(h, None))
+ self.assertEqual(True, b.startswith(l, -2, None))
+ self.assertEqual(True, b.startswith(h, None, -2))
+ self.assertEqual(False, b.startswith(x, None, None))
+
+ def test_find_etc_raise_correct_error_messages(self):
+ # issue 11828
+ b = self.type2test(b'hello')
+ x = self.type2test(b'x')
+ self.assertRaisesRegexp(TypeError, r'\bfind\b', b.find,
+ x, None, None, None)
+ self.assertRaisesRegexp(TypeError, r'\brfind\b', b.rfind,
+ x, None, None, None)
+ self.assertRaisesRegexp(TypeError, r'\bindex\b', b.index,
+ x, None, None, None)
+ self.assertRaisesRegexp(TypeError, r'\brindex\b', b.rindex,
+ x, None, None, None)
+ self.assertRaisesRegexp(TypeError, r'\bcount\b', b.count,
+ x, None, None, None)
+ self.assertRaisesRegexp(TypeError, r'\bstartswith\b', b.startswith,
+ x, None, None, None)
+ self.assertRaisesRegexp(TypeError, r'\bendswith\b', b.endswith,
+ x, None, None, None)
+
class BytesTest(BaseBytesTest):
type2test = bytes