diff options
-rw-r--r-- | Lib/_abcoll.py | 16 | ||||
-rw-r--r-- | Lib/test/test_collections.py | 15 |
2 files changed, 26 insertions, 5 deletions
diff --git a/Lib/_abcoll.py b/Lib/_abcoll.py index ec3e2f8..6363de9 100644 --- a/Lib/_abcoll.py +++ b/Lib/_abcoll.py @@ -16,6 +16,7 @@ __all__ = ["Hashable", "Iterable", "Iterator", "Mapping", "MutableMapping", "MappingView", "KeysView", "ItemsView", "ValuesView", "Sequence", "MutableSequence", + "ByteString", ] ### ONE-TRICK PONIES ### @@ -489,8 +490,17 @@ class Sequence(metaclass=ABCMeta): Sequence.register(tuple) Sequence.register(str) -Sequence.register(bytes) -Sequence.register(memoryview) + + +class ByteString(Sequence): + + """This unifies bytes and bytearray. + + XXX Should add all their methods. + """ + +ByteString.register(bytes) +ByteString.register(bytearray) class MutableSequence(Sequence): @@ -531,4 +541,4 @@ class MutableSequence(Sequence): self.extend(values) MutableSequence.register(list) -MutableSequence.register(bytes) +MutableSequence.register(bytearray) # Multiply inheriting, see ByteString diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 3ccc06c..62da9c5 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -8,6 +8,7 @@ from collections import Sized, Container, Callable from collections import Set, MutableSet from collections import Mapping, MutableMapping from collections import Sequence, MutableSequence +from collections import ByteString class TestNamedTuple(unittest.TestCase): @@ -260,11 +261,21 @@ class TestCollectionABCs(unittest.TestCase): self.failUnless(issubclass(sample, Sequence)) self.failUnless(issubclass(str, Sequence)) + def test_ByteString(self): + for sample in [bytes, bytearray]: + self.failUnless(isinstance(sample(), ByteString)) + self.failUnless(issubclass(sample, ByteString)) + for sample in [str, list, tuple]: + self.failIf(isinstance(sample(), ByteString)) + self.failIf(issubclass(sample, ByteString)) + self.failIf(isinstance(memoryview(b""), ByteString)) + self.failIf(issubclass(memoryview, ByteString)) + def test_MutableSequence(self): - for sample in [tuple, str]: + for sample in [tuple, str, bytes]: self.failIf(isinstance(sample(), MutableSequence)) self.failIf(issubclass(sample, MutableSequence)) - for sample in [list, bytes]: + for sample in [list, bytearray]: self.failUnless(isinstance(sample(), MutableSequence)) self.failUnless(issubclass(sample, MutableSequence)) self.failIf(issubclass(str, MutableSequence)) |