@@ -721,6 +721,24 @@ def test_mod(self):
721
721
self .assertEqual (b , b'hello,\x00 world!' )
722
722
self .assertIs (type (b ), self .type2test )
723
723
724
+ def check (fmt , vals , result ):
725
+ b = self .type2test (fmt )
726
+ b = b % vals
727
+ self .assertEqual (b , result )
728
+ self .assertIs (type (b ), self .type2test )
729
+
730
+ # A set of tests adapted from test_unicode:UnicodeTest.test_formatting
731
+ check (b'...%(foo)b...' , {b'foo' :b"abc" }, b'...abc...' )
732
+ check (b'...%(f(o)o)b...' , {b'f(o)o' :b"abc" , b'foo' :b'bar' }, b'...abc...' )
733
+ check (b'...%(foo)b...' , {b'foo' :b"abc" ,b'def' :123 }, b'...abc...' )
734
+ check (b'%*b' , (5 , b'abc' ,), b' abc' )
735
+ check (b'%*b' , (- 5 , b'abc' ,), b'abc ' )
736
+ check (b'%*.*b' , (5 , 2 , b'abc' ,), b' ab' )
737
+ check (b'%*.*b' , (5 , 3 , b'abc' ,), b' abc' )
738
+ check (b'%i %*.*b' , (10 , 5 , 3 , b'abc' ,), b'10 abc' )
739
+ check (b'%i%b %*.*b' , (10 , b'3' , 5 , 3 , b'abc' ,), b'103 abc' )
740
+ check (b'%c' , b'a' , b'a' )
741
+
724
742
def test_imod (self ):
725
743
b = self .type2test (b'hello, %b!' )
726
744
orig = b
@@ -991,6 +1009,18 @@ def test_sq_item(self):
991
1009
class BytesTest (BaseBytesTest , unittest .TestCase ):
992
1010
type2test = bytes
993
1011
1012
+ def test__bytes__ (self ):
1013
+ foo = b'foo\x00 bar'
1014
+ self .assertEqual (foo .__bytes__ (), foo )
1015
+ self .assertEqual (type (foo .__bytes__ ()), self .type2test )
1016
+
1017
+ class bytes_subclass (bytes ):
1018
+ pass
1019
+
1020
+ bar = bytes_subclass (b'bar\x00 foo' )
1021
+ self .assertEqual (bar .__bytes__ (), bar )
1022
+ self .assertEqual (type (bar .__bytes__ ()), self .type2test )
1023
+
994
1024
def test_getitem_error (self ):
995
1025
b = b'python'
996
1026
msg = "byte indices must be integers or slices"
@@ -1658,8 +1688,8 @@ def delslice():
1658
1688
1659
1689
@test .support .cpython_only
1660
1690
def test_obsolete_write_lock (self ):
1661
- from _testcapi import getbuffer_with_null_view
1662
- self .assertRaises (BufferError , getbuffer_with_null_view , bytearray ())
1691
+ _testcapi = import_helper . import_module ( '_testcapi' )
1692
+ self .assertRaises (BufferError , _testcapi . getbuffer_with_null_view , bytearray ())
1663
1693
1664
1694
def test_iterator_pickling2 (self ):
1665
1695
orig = bytearray (b'abc' )
@@ -1718,6 +1748,23 @@ def test_repeat_after_setslice(self):
1718
1748
self .assertEqual (b1 , b )
1719
1749
self .assertEqual (b3 , b'xcxcxc' )
1720
1750
1751
+ def test_mutating_index (self ):
1752
+ class Boom :
1753
+ def __index__ (self ):
1754
+ b .clear ()
1755
+ return 0
1756
+
1757
+ with self .subTest ("tp_as_mapping" ):
1758
+ b = bytearray (b'Now you see me...' )
1759
+ with self .assertRaises (IndexError ):
1760
+ b [0 ] = Boom ()
1761
+
1762
+ with self .subTest ("tp_as_sequence" ):
1763
+ _testcapi = import_helper .import_module ('_testcapi' )
1764
+ b = bytearray (b'Now you see me...' )
1765
+ with self .assertRaises (IndexError ):
1766
+ _testcapi .sequence_setitem (b , 0 , Boom ())
1767
+
1721
1768
1722
1769
class AssortedBytesTest (unittest .TestCase ):
1723
1770
#
@@ -1948,28 +1995,30 @@ def test_join(self):
1948
1995
def test_pickle (self ):
1949
1996
a = self .type2test (b"abcd" )
1950
1997
a .x = 10
1951
- a .y = self .type2test (b"efgh" )
1998
+ a .z = self .type2test (b"efgh" )
1952
1999
for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
1953
2000
b = pickle .loads (pickle .dumps (a , proto ))
1954
2001
self .assertNotEqual (id (a ), id (b ))
1955
2002
self .assertEqual (a , b )
1956
2003
self .assertEqual (a .x , b .x )
1957
- self .assertEqual (a .y , b .y )
2004
+ self .assertEqual (a .z , b .z )
1958
2005
self .assertEqual (type (a ), type (b ))
1959
- self .assertEqual (type (a .y ), type (b .y ))
2006
+ self .assertEqual (type (a .z ), type (b .z ))
2007
+ self .assertFalse (hasattr (b , 'y' ))
1960
2008
1961
2009
def test_copy (self ):
1962
2010
a = self .type2test (b"abcd" )
1963
2011
a .x = 10
1964
- a .y = self .type2test (b"efgh" )
2012
+ a .z = self .type2test (b"efgh" )
1965
2013
for copy_method in (copy .copy , copy .deepcopy ):
1966
2014
b = copy_method (a )
1967
2015
self .assertNotEqual (id (a ), id (b ))
1968
2016
self .assertEqual (a , b )
1969
2017
self .assertEqual (a .x , b .x )
1970
- self .assertEqual (a .y , b .y )
2018
+ self .assertEqual (a .z , b .z )
1971
2019
self .assertEqual (type (a ), type (b ))
1972
- self .assertEqual (type (a .y ), type (b .y ))
2020
+ self .assertEqual (type (a .z ), type (b .z ))
2021
+ self .assertFalse (hasattr (b , 'y' ))
1973
2022
1974
2023
def test_fromhex (self ):
1975
2024
b = self .type2test .fromhex ('1a2B30' )
@@ -2002,6 +2051,9 @@ def __init__(me, *args, **kwargs):
2002
2051
class ByteArraySubclass (bytearray ):
2003
2052
pass
2004
2053
2054
+ class ByteArraySubclassWithSlots (bytearray ):
2055
+ __slots__ = ('x' , 'y' , '__dict__' )
2056
+
2005
2057
class BytesSubclass (bytes ):
2006
2058
pass
2007
2059
@@ -2022,6 +2074,9 @@ def __init__(me, newarg=1, *args, **kwargs):
2022
2074
x = subclass (newarg = 4 , source = b"abcd" )
2023
2075
self .assertEqual (x , b"abcd" )
2024
2076
2077
+ class ByteArraySubclassWithSlotsTest (SubclassTest , unittest .TestCase ):
2078
+ basetype = bytearray
2079
+ type2test = ByteArraySubclassWithSlots
2025
2080
2026
2081
class BytesSubclassTest (SubclassTest , unittest .TestCase ):
2027
2082
basetype = bytes
0 commit comments