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