From 096c745e602a5c3f2fcf697ab6323422ef4a2f68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lemburg?= Date: Fri, 25 Feb 2011 15:42:01 +0000 Subject: Normalize the encoding names for Latin-1 and UTF-8 to 'latin-1' and 'utf-8'. These are optimized in the Python Unicode implementation to result in more direct processing, bypassing the codec registry. Also see issue11303. --- Lib/test/test_bytes.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'Lib/test/test_bytes.py') diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index e5c7ccd4ff..84867bb64f 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -188,24 +188,26 @@ class BaseBytesTest(unittest.TestCase): def test_encoding(self): sample = "Hello world\n\u1234\u5678\u9abc" - for enc in ("utf8", "utf16"): + for enc in ("utf-8", "utf-16"): b = self.type2test(sample, enc) self.assertEqual(b, self.type2test(sample.encode(enc))) - self.assertRaises(UnicodeEncodeError, self.type2test, sample, "latin1") - b = self.type2test(sample, "latin1", "ignore") + self.assertRaises(UnicodeEncodeError, self.type2test, sample, "latin-1") + b = self.type2test(sample, "latin-1", "ignore") self.assertEqual(b, self.type2test(sample[:-3], "utf-8")) def test_decode(self): sample = "Hello world\n\u1234\u5678\u9abc\def0\def0" - for enc in ("utf8", "utf16"): + for enc in ("utf-8", "utf-16"): b = self.type2test(sample, enc) self.assertEqual(b.decode(enc), sample) sample = "Hello world\n\x80\x81\xfe\xff" - b = self.type2test(sample, "latin1") - self.assertRaises(UnicodeDecodeError, b.decode, "utf8") - self.assertEqual(b.decode("utf8", "ignore"), "Hello world\n") - self.assertEqual(b.decode(errors="ignore", encoding="utf8"), + b = self.type2test(sample, "latin-1") + self.assertRaises(UnicodeDecodeError, b.decode, "utf-8") + self.assertEqual(b.decode("utf-8", "ignore"), "Hello world\n") + self.assertEqual(b.decode(errors="ignore", encoding="utf-8"), "Hello world\n") + # Default encoding is utf-8 + self.assertEqual(self.type2test(b'\xe2\x98\x83').decode(), '\u2603') def test_from_int(self): b = self.type2test(0) -- cgit v1.2.1 From bd53d9e9ad64441f2fce66cba4fa40040f4d4ee5 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Thu, 3 Mar 2011 18:21:02 +0000 Subject: Issue #10516: added copy() and clear() methods to bytearrays as well --- Lib/test/test_bytes.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'Lib/test/test_bytes.py') diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 84867bb64f..a6f1826a0b 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -564,6 +564,39 @@ class ByteArrayTest(BaseBytesTest): b.reverse() self.assertFalse(b) + def test_clear(self): + b = bytearray(b'python') + b.clear() + self.assertEqual(b, b'') + + b = bytearray(b'') + b.clear() + self.assertEqual(b, b'') + + b = bytearray(b'') + b.append(ord('r')) + b.clear() + b.append(ord('p')) + self.assertEqual(b, b'p') + + def test_copy(self): + b = bytearray(b'abc') + bb = b.copy() + self.assertEqual(bb, b'abc') + + b = bytearray(b'') + bb = b.copy() + self.assertEqual(bb, b'') + + # test that it's indeed a copy and not a reference + b = bytearray(b'abc') + bb = b.copy() + self.assertEqual(b, bb) + self.assertIsNot(b, bb) + bb.append(ord('d')) + self.assertEqual(bb, b'abcd') + self.assertEqual(b, b'abc') + def test_regexps(self): def by(s): return bytearray(map(ord, s)) -- cgit v1.2.1 From 42f4dde4c2f19e2105933a810d614e01d3616d36 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Fri, 4 Mar 2011 04:55:25 +0000 Subject: Issue #11386: Fixed the exception thrown by bytearray.pop() for empty bytearrays --- Lib/test/test_bytes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Lib/test/test_bytes.py') diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index a6f1826a0b..0b70c3a35c 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -790,7 +790,7 @@ class ByteArrayTest(BaseBytesTest): self.assertEqual(b.pop(0), ord('w')) self.assertEqual(b.pop(-2), ord('r')) self.assertRaises(IndexError, lambda: b.pop(10)) - self.assertRaises(OverflowError, lambda: bytearray().pop()) + self.assertRaises(IndexError, lambda: bytearray().pop()) # test for issue #6846 self.assertEqual(bytearray(b'\xff').pop(), 0xff) -- cgit v1.2.1 From 9efd2ec8bde1aa42298194474edd5b1c0bdcffbf Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Fri, 29 Jul 2011 07:05:08 +0300 Subject: Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format. As a side effect, this now allows the rjust, ljust and center methods of bytes and bytearray to accept a bytearray argument. Patch by Petri Lehtinen --- Lib/test/test_bytes.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'Lib/test/test_bytes.py') diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 234b56c8b1..d32a44b7b4 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -475,6 +475,27 @@ class BaseBytesTest(unittest.TestCase): self.assertRaises(TypeError, self.type2test(b'abc').lstrip, 'b') self.assertRaises(TypeError, self.type2test(b'abc').rstrip, 'b') + def test_center(self): + # Fill character can be either bytes or bytearray (issue 12380) + b = self.type2test(b'abc') + for fill_type in (bytes, bytearray): + self.assertEqual(b.center(7, fill_type(b'-')), + self.type2test(b'--abc--')) + + def test_ljust(self): + # Fill character can be either bytes or bytearray (issue 12380) + b = self.type2test(b'abc') + for fill_type in (bytes, bytearray): + self.assertEqual(b.ljust(7, fill_type(b'-')), + self.type2test(b'abc----')) + + def test_rjust(self): + # Fill character can be either bytes or bytearray (issue 12380) + b = self.type2test(b'abc') + for fill_type in (bytes, bytearray): + self.assertEqual(b.rjust(7, fill_type(b'-')), + self.type2test(b'----abc')) + def test_ord(self): b = self.type2test(b'\0A\x7f\x80\xff') self.assertEqual([ord(b[i:i+1]) for i in range(len(b))], -- cgit v1.2.1 From efd6a08773fab4c4a972e4f04a79902f64a67990 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Thu, 20 Oct 2011 23:54:17 +0200 Subject: Issue #12170: The count(), find(), rfind(), index() and rindex() methods of bytes and bytearray objects now accept an integer between 0 and 255 as their first argument. Patch by Petri Lehtinen. --- Lib/test/test_bytes.py | 103 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 88 insertions(+), 15 deletions(-) (limited to 'Lib/test/test_bytes.py') diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index d32a44b7b4..7acfde56f2 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -293,10 +293,27 @@ class BaseBytesTest(unittest.TestCase): def test_count(self): b = self.type2test(b'mississippi') + i = 105 + p = 112 + w = 119 + self.assertEqual(b.count(b'i'), 4) self.assertEqual(b.count(b'ss'), 2) self.assertEqual(b.count(b'w'), 0) + self.assertEqual(b.count(i), 4) + self.assertEqual(b.count(w), 0) + + self.assertEqual(b.count(b'i', 6), 2) + self.assertEqual(b.count(b'p', 6), 2) + self.assertEqual(b.count(b'i', 1, 3), 1) + self.assertEqual(b.count(b'p', 7, 9), 1) + + self.assertEqual(b.count(i, 6), 2) + self.assertEqual(b.count(p, 6), 2) + self.assertEqual(b.count(i, 1, 3), 1) + self.assertEqual(b.count(p, 7, 9), 1) + def test_startswith(self): b = self.type2test(b'hello') self.assertFalse(self.type2test().startswith(b"anything")) @@ -327,35 +344,81 @@ class BaseBytesTest(unittest.TestCase): def test_find(self): b = self.type2test(b'mississippi') + i = 105 + w = 119 + self.assertEqual(b.find(b'ss'), 2) + self.assertEqual(b.find(b'w'), -1) + self.assertEqual(b.find(b'mississippian'), -1) + + self.assertEqual(b.find(i), 1) + self.assertEqual(b.find(w), -1) + self.assertEqual(b.find(b'ss', 3), 5) self.assertEqual(b.find(b'ss', 1, 7), 2) self.assertEqual(b.find(b'ss', 1, 3), -1) - self.assertEqual(b.find(b'w'), -1) - self.assertEqual(b.find(b'mississippian'), -1) + + self.assertEqual(b.find(i, 6), 7) + self.assertEqual(b.find(i, 1, 3), 1) + self.assertEqual(b.find(w, 1, 3), -1) def test_rfind(self): b = self.type2test(b'mississippi') + i = 105 + w = 119 + self.assertEqual(b.rfind(b'ss'), 5) - self.assertEqual(b.rfind(b'ss', 3), 5) - self.assertEqual(b.rfind(b'ss', 0, 6), 2) self.assertEqual(b.rfind(b'w'), -1) self.assertEqual(b.rfind(b'mississippian'), -1) + self.assertEqual(b.rfind(i), 10) + self.assertEqual(b.rfind(w), -1) + + self.assertEqual(b.rfind(b'ss', 3), 5) + self.assertEqual(b.rfind(b'ss', 0, 6), 2) + + self.assertEqual(b.rfind(i, 1, 3), 1) + self.assertEqual(b.rfind(i, 3, 9), 7) + self.assertEqual(b.rfind(w, 1, 3), -1) + def test_index(self): - b = self.type2test(b'world') - self.assertEqual(b.index(b'w'), 0) - self.assertEqual(b.index(b'orl'), 1) - self.assertRaises(ValueError, b.index, b'worm') - self.assertRaises(ValueError, b.index, b'ldo') + b = self.type2test(b'mississippi') + i = 105 + w = 119 + + self.assertEqual(b.index(b'ss'), 2) + self.assertRaises(ValueError, b.index, b'w') + self.assertRaises(ValueError, b.index, b'mississippian') + + self.assertEqual(b.index(i), 1) + self.assertRaises(ValueError, b.index, w) + + self.assertEqual(b.index(b'ss', 3), 5) + self.assertEqual(b.index(b'ss', 1, 7), 2) + self.assertRaises(ValueError, b.index, b'ss', 1, 3) + + self.assertEqual(b.index(i, 6), 7) + self.assertEqual(b.index(i, 1, 3), 1) + self.assertRaises(ValueError, b.index, w, 1, 3) def test_rindex(self): - # XXX could be more rigorous - b = self.type2test(b'world') - self.assertEqual(b.rindex(b'w'), 0) - self.assertEqual(b.rindex(b'orl'), 1) - self.assertRaises(ValueError, b.rindex, b'worm') - self.assertRaises(ValueError, b.rindex, b'ldo') + b = self.type2test(b'mississippi') + i = 105 + w = 119 + + self.assertEqual(b.rindex(b'ss'), 5) + self.assertRaises(ValueError, b.rindex, b'w') + self.assertRaises(ValueError, b.rindex, b'mississippian') + + self.assertEqual(b.rindex(i), 10) + self.assertRaises(ValueError, b.rindex, w) + + self.assertEqual(b.rindex(b'ss', 3), 5) + self.assertEqual(b.rindex(b'ss', 0, 6), 2) + + self.assertEqual(b.rindex(i, 1, 3), 1) + self.assertEqual(b.rindex(i, 3, 9), 7) + self.assertRaises(ValueError, b.rindex, w, 1, 3) def test_replace(self): b = self.type2test(b'mississippi') @@ -552,6 +615,14 @@ class BaseBytesTest(unittest.TestCase): self.assertEqual(True, b.startswith(h, None, -2)) self.assertEqual(False, b.startswith(x, None, None)) + def test_integer_arguments_out_of_byte_range(self): + b = self.type2test(b'hello') + + for method in (b.count, b.find, b.index, b.rfind, b.rindex): + self.assertRaises(ValueError, method, -1) + self.assertRaises(ValueError, method, 256) + self.assertRaises(ValueError, method, 9999) + def test_find_etc_raise_correct_error_messages(self): # issue 11828 b = self.type2test(b'hello') @@ -1161,9 +1232,11 @@ class FixedStringTest(test.string_tests.BaseTest): class ByteArrayAsStringTest(FixedStringTest): type2test = bytearray + contains_bytes = True class BytesAsStringTest(FixedStringTest): type2test = bytes + contains_bytes = True class SubclassTest(unittest.TestCase): -- cgit v1.2.1 From 71b3c95f90118e96275d7f417609017c6ff6e662 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 18 Dec 2011 01:17:41 +0100 Subject: Issue #13623: Fix a performance regression introduced by issue #12170 in bytes.find() and handle correctly OverflowError (raise the same ValueError than the error for -1). --- Lib/test/test_bytes.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'Lib/test/test_bytes.py') diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 7acfde56f2..bfb88de3e6 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -362,6 +362,11 @@ class BaseBytesTest(unittest.TestCase): self.assertEqual(b.find(i, 1, 3), 1) self.assertEqual(b.find(w, 1, 3), -1) + for index in (-1, 256, sys.maxsize + 1): + self.assertRaisesRegex( + ValueError, r'byte must be in range\(0, 256\)', + b.find, index) + def test_rfind(self): b = self.type2test(b'mississippi') i = 105 -- cgit v1.2.1 From abd66a1487dcadc26eaeb733cd252059e9ae98c1 Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Sun, 26 Feb 2012 09:39:55 +0200 Subject: #14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments. --- Lib/test/test_bytes.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'Lib/test/test_bytes.py') diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index bfb88de3e6..203fc5c49d 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -435,6 +435,14 @@ class BaseBytesTest(unittest.TestCase): self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b'']) self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi']) self.assertEqual(b.split(b'w'), [b]) + # with keyword args + b = self.type2test(b'a|b|c|d') + self.assertEqual(b.split(sep=b'|'), [b'a', b'b', b'c', b'd']) + self.assertEqual(b.split(b'|', maxsplit=1), [b'a', b'b|c|d']) + self.assertEqual(b.split(sep=b'|', maxsplit=1), [b'a', b'b|c|d']) + self.assertEqual(b.split(maxsplit=1, sep=b'|'), [b'a', b'b|c|d']) + b = self.type2test(b'a b c d') + self.assertEqual(b.split(maxsplit=1), [b'a', b'b c d']) def test_split_whitespace(self): for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf', @@ -463,6 +471,14 @@ class BaseBytesTest(unittest.TestCase): self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b'']) self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi']) self.assertEqual(b.rsplit(b'w'), [b]) + # with keyword args + b = self.type2test(b'a|b|c|d') + self.assertEqual(b.rsplit(sep=b'|'), [b'a', b'b', b'c', b'd']) + self.assertEqual(b.rsplit(b'|', maxsplit=1), [b'a|b|c', b'd']) + self.assertEqual(b.rsplit(sep=b'|', maxsplit=1), [b'a|b|c', b'd']) + self.assertEqual(b.rsplit(maxsplit=1, sep=b'|'), [b'a|b|c', b'd']) + b = self.type2test(b'a b c d') + self.assertEqual(b.rsplit(maxsplit=1), [b'a b c', b'd']) def test_rsplit_whitespace(self): for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf', -- cgit v1.2.1 From 85f68bdee6df6f2426de597a97f9c33c7066570f Mon Sep 17 00:00:00 2001 From: Kristj?n Valur J?nsson Date: Tue, 3 Apr 2012 10:49:41 +0000 Subject: Issue #14288: Serialization support for builtin iterators. --- Lib/test/test_bytes.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'Lib/test/test_bytes.py') diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 203fc5c49d..8ce6c22e69 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -518,6 +518,24 @@ class BaseBytesTest(unittest.TestCase): q = pickle.loads(ps) self.assertEqual(b, q) + def test_iterator_pickling(self): + for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0": + it = itorg = iter(self.type2test(b)) + data = list(self.type2test(b)) + d = pickle.dumps(it) + it = pickle.loads(d) + self.assertEqual(type(itorg), type(it)) + self.assertEqual(list(it), data) + + it = pickle.loads(d) + try: + next(it) + except StopIteration: + continue + d = pickle.dumps(it) + it = pickle.loads(d) + self.assertEqual(list(it), data[1:]) + def test_strip(self): b = self.type2test(b'mississippi') self.assertEqual(b.strip(b'i'), b'mississipp') -- cgit v1.2.1