[Python-checkins] [2.7] Add new tests for bytes and bytearray constructors. (GH-9843) (#9866)

Serhiy Storchaka webhook-mailer at python.org
Sun Oct 14 06:07:58 EDT 2018


https://github.com/python/cpython/commit/8ba72674bdf3d49afa6b6f79ae55d8a6120e8d36
commit: 8ba72674bdf3d49afa6b6f79ae55d8a6120e8d36
branch: 2.7
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2018-10-14T13:07:54+03:00
summary:

[2.7] Add new tests for bytes and bytearray constructors. (GH-9843) (#9866)

Covered all special cases: bytes, tuple, list, differend
kinds of iterables and iterators.
(cherry picked from commit 1a997eb291fdc5f5606c898fffbde61d899ed762)

files:
M Lib/test/test_bytes.py

diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index ce2c5b21ee3f..9b5f713b0b88 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -4,6 +4,7 @@
 the latter should be modernized).
 """
 
+import array
 import os
 import re
 import sys
@@ -58,11 +59,49 @@ def test_empty_sequence(self):
         self.assertRaises(IndexError, lambda: b[-sys.maxint-2])
         self.assertRaises(IndexError, lambda: b[-10**100])
 
+    def test_from_iterable(self):
+        b = self.type2test(range(256))
+        self.assertEqual(len(b), 256)
+        self.assertEqual(list(b), list(range(256)))
+
+        # Non-sequence iterable.
+        b = self.type2test({42})
+        self.assertEqual(b, b"*")
+        b = self.type2test({43, 45})
+        self.assertIn(tuple(b), {(43, 45), (45, 43)})
+
+        # Iterator that has a __length_hint__.
+        b = self.type2test(iter(range(256)))
+        self.assertEqual(len(b), 256)
+        self.assertEqual(list(b), list(range(256)))
+
+        # Iterator that doesn't have a __length_hint__.
+        b = self.type2test(i for i in range(256) if i % 2)
+        self.assertEqual(len(b), 128)
+        self.assertEqual(list(b), list(range(256))[1::2])
+
+        # Sequence without __iter__.
+        class S:
+            def __getitem__(self, i):
+                return (1, 2, 3)[i]
+        b = self.type2test(S())
+        self.assertEqual(b, b"\x01\x02\x03")
+
+    def test_from_tuple(self):
+        # There is a special case for tuples.
+        b = self.type2test(tuple(range(256)))
+        self.assertEqual(len(b), 256)
+        self.assertEqual(list(b), list(range(256)))
+        b = self.type2test((1, 2, 3))
+        self.assertEqual(b, b"\x01\x02\x03")
+
     def test_from_list(self):
-        ints = list(range(256))
-        b = self.type2test(i for i in ints)
+        # There is a special case for lists.
+        b = self.type2test(list(range(256)))
         self.assertEqual(len(b), 256)
-        self.assertEqual(list(b), ints)
+        self.assertEqual(list(b), list(range(256)))
+        b = self.type2test([1, 2, 3])
+        self.assertEqual(b, b"\x01\x02\x03")
 
     def test_from_index(self):
         b = self.type2test([Indexable(), Indexable(1), Indexable(254),
@@ -71,6 +110,20 @@ def test_from_index(self):
         self.assertRaises(ValueError, self.type2test, [Indexable(-1)])
         self.assertRaises(ValueError, self.type2test, [Indexable(256)])
 
+    def test_from_buffer(self):
+        a = self.type2test(array.array('B', [1, 2, 3]))
+        self.assertEqual(a, b"\x01\x02\x03")
+        a = self.type2test(b"\x01\x02\x03")
+        self.assertEqual(a, b"\x01\x02\x03")
+
+        # http://bugs.python.org/issue29159
+        # Fallback when __index__ raises exception other than OverflowError
+        class B(bytes):
+            def __index__(self):
+                raise TypeError
+
+        self.assertEqual(self.type2test(B(b"foobar")), b"foobar")
+
     def test_from_ssize(self):
         self.assertEqual(self.type2test(0), b'')
         self.assertEqual(self.type2test(1), b'\x00')



More information about the Python-checkins mailing list