1111import io
1212import math
1313import struct
14+ import warnings
1415
1516import array
1617from array import _array_reconstructor as array_reconstructor
@@ -367,15 +368,35 @@ def test_tofromlist(self):
367368 self .assertEqual (a , b )
368369
369370 def test_tofromstring (self ):
371+ nb_warnings = 4
372+ with warnings .catch_warnings (record = True ) as r :
373+ warnings .filterwarnings ("always" ,
374+ message = r"(to|from)string\(\) is deprecated" ,
375+ category = DeprecationWarning )
376+ a = array .array (self .typecode , 2 * self .example )
377+ b = array .array (self .typecode )
378+ self .assertRaises (TypeError , a .tostring , 42 )
379+ self .assertRaises (TypeError , b .fromstring )
380+ self .assertRaises (TypeError , b .fromstring , 42 )
381+ b .fromstring (a .tostring ())
382+ self .assertEqual (a , b )
383+ if a .itemsize > 1 :
384+ self .assertRaises (ValueError , b .fromstring , "x" )
385+ nb_warnings += 1
386+ self .assertEqual (len (r ), nb_warnings )
387+
388+ def test_tofrombytes (self ):
370389 a = array .array (self .typecode , 2 * self .example )
371390 b = array .array (self .typecode )
372- self .assertRaises (TypeError , a .tostring , 42 )
373- self .assertRaises (TypeError , b .fromstring )
374- self .assertRaises (TypeError , b .fromstring , 42 )
375- b .fromstring (a .tostring ())
391+ self .assertRaises (TypeError , a .tobytes , 42 )
392+ self .assertRaises (TypeError , b .frombytes )
393+ self .assertRaises (TypeError , b .frombytes , 42 )
394+ b .frombytes (a .tobytes ())
395+ c = array .array (self .typecode , bytearray (a .tobytes ()))
376396 self .assertEqual (a , b )
397+ self .assertEqual (a , c )
377398 if a .itemsize > 1 :
378- self .assertRaises (ValueError , b .fromstring , "x" )
399+ self .assertRaises (ValueError , b .frombytes , b "x" )
379400
380401 def test_repr (self ):
381402 a = array .array (self .typecode , 2 * self .example )
@@ -898,8 +919,8 @@ def test_buffer(self):
898919 a = array .array (self .typecode , self .example )
899920 m = memoryview (a )
900921 expected = m .tobytes ()
901- self .assertEqual (a .tostring (), expected )
902- self .assertEqual (a .tostring ()[0 ], expected [0 ])
922+ self .assertEqual (a .tobytes (), expected )
923+ self .assertEqual (a .tobytes ()[0 ], expected [0 ])
903924 # Resizing is forbidden when there are buffer exports.
904925 # For issue 4509, we also check after each error that
905926 # the array was not modified.
@@ -913,7 +934,7 @@ def test_buffer(self):
913934 self .assertEqual (m .tobytes (), expected )
914935 self .assertRaises (BufferError , a .fromlist , a .tolist ())
915936 self .assertEqual (m .tobytes (), expected )
916- self .assertRaises (BufferError , a .fromstring , a .tostring ())
937+ self .assertRaises (BufferError , a .frombytes , a .tobytes ())
917938 self .assertEqual (m .tobytes (), expected )
918939 if self .typecode == 'u' :
919940 self .assertRaises (BufferError , a .fromunicode , a .tounicode ())
@@ -932,7 +953,7 @@ def test_buffer(self):
932953 def test_weakref (self ):
933954 s = array .array (self .typecode , self .example )
934955 p = weakref .proxy (s )
935- self .assertEqual (p .tostring (), s .tostring ())
956+ self .assertEqual (p .tobytes (), s .tobytes ())
936957 s = None
937958 self .assertRaises (ReferenceError , len , p )
938959
@@ -1110,6 +1131,23 @@ def test_overflow(self):
11101131 upper = int (pow (2 , a .itemsize * 8 )) - 1
11111132 self .check_overflow (lower , upper )
11121133
1134+ def test_bytes_extend (self ):
1135+ s = bytes (self .example )
1136+
1137+ a = array .array (self .typecode , self .example )
1138+ a .extend (s )
1139+ self .assertEqual (
1140+ a ,
1141+ array .array (self .typecode , self .example + self .example )
1142+ )
1143+
1144+ a = array .array (self .typecode , self .example )
1145+ a .extend (bytearray (reversed (s )))
1146+ self .assertEqual (
1147+ a ,
1148+ array .array (self .typecode , self .example + self .example [::- 1 ])
1149+ )
1150+
11131151
11141152class ByteTest (SignedNumberTest ):
11151153 typecode = 'b'
@@ -1172,7 +1210,7 @@ def test_byteswap(self):
11721210 # On alphas treating the byte swapped bit patters as
11731211 # floats/doubles results in floating point exceptions
11741212 # => compare the 8bit string values instead
1175- self .assertNotEqual (a .tostring (), b .tostring ())
1213+ self .assertNotEqual (a .tobytes (), b .tobytes ())
11761214 b .byteswap ()
11771215 self .assertEqual (a , b )
11781216
0 commit comments