<s> """<STR_LIT>""" <EOL> import unittest <EOL> import test . support <EOL> import sys <EOL> import gc <EOL> import weakref <EOL> import array <EOL> import io <EOL> class AbstractMemoryTests : <EOL> source_bytes = b"<STR_LIT>" <EOL> @ property <EOL> def _source ( self ) : <EOL> return self . source_bytes <EOL> @ property <EOL> def _types ( self ) : <EOL> return filter ( None , [ self . ro_type , self . rw_type ] ) <EOL> def check_getitem_with_type ( self , tp ) : <EOL> b = tp ( self . _source ) <EOL> oldrefcount = sys . getrefcount ( b ) <EOL> m = self . _view ( b ) <EOL> self . assertEqual ( m [ <NUM_LIT:0> ] , ord ( b"<STR_LIT:a>" ) ) <EOL> self . assertIsInstance ( m [ <NUM_LIT:0> ] , int ) <EOL> self . assertEqual ( m [ <NUM_LIT:5> ] , ord ( b"<STR_LIT:f>" ) ) <EOL> self . assertEqual ( m [ - <NUM_LIT:1> ] , ord ( b"<STR_LIT:f>" ) ) <EOL> self . assertEqual ( m [ - <NUM_LIT:6> ] , ord ( b"<STR_LIT:a>" ) ) <EOL> self . assertRaises ( IndexError , lambda : m [ <NUM_LIT:6> ] ) <EOL> self . assertRaises ( IndexError , lambda : m [ - <NUM_LIT:7> ] ) <EOL> self . assertRaises ( IndexError , lambda : m [ sys . maxsize ] ) <EOL> self . assertRaises ( IndexError , lambda : m [ - sys . maxsize ] ) <EOL> self . assertRaises ( TypeError , lambda : m [ None ] ) <EOL> self . assertRaises ( TypeError , lambda : m [ <NUM_LIT:0.0> ] ) <EOL> self . assertRaises ( TypeError , lambda : m [ "<STR_LIT:a>" ] ) <EOL> m = None <EOL> self . assertEqual ( sys . getrefcount ( b ) , oldrefcount ) <EOL> def test_getitem ( self ) : <EOL> for tp in self . _types : <EOL> self . check_getitem_with_type ( tp ) <EOL> def test_iter ( self ) : <EOL> for tp in self . _types : <EOL> b = tp ( self . _source ) <EOL> m = self . _view ( b ) <EOL> self . assertEqual ( list ( m ) , [ m [ i ] for i in range ( len ( m ) ) ] ) <EOL> def test_setitem_readonly ( self ) : <EOL> if not self . ro_type : <EOL> return <EOL> b = self . ro_type ( self . _source ) <EOL> oldrefcount = sys . getrefcount ( b ) <EOL> m = self . _view ( b ) <EOL> def setitem ( value ) : <EOL> m [ <NUM_LIT:0> ] = value <EOL> self . assertRaises ( TypeError , setitem , b"<STR_LIT:a>" ) <EOL> self . assertRaises ( TypeError , setitem , <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , setitem , memoryview ( b"<STR_LIT:a>" ) ) <EOL> m = None <EOL> self . assertEqual ( sys . getrefcount ( b ) , oldrefcount ) <EOL> def test_setitem_writable ( self ) : <EOL> if not self . rw_type : <EOL> return <EOL> tp = self . rw_type <EOL> b = self . rw_type ( self . _source ) <EOL> oldrefcount = sys . getrefcount ( b ) <EOL> m = self . _view ( b ) <EOL> m [ <NUM_LIT:0> ] = ord ( b'<STR_LIT:1>' ) <EOL> self . _check_contents ( tp , b , b"<STR_LIT>" ) <EOL> m [ <NUM_LIT:0> : <NUM_LIT:1> ] = tp ( b"<STR_LIT:0>" ) <EOL> self . _check_contents ( tp , b , b"<STR_LIT>" ) <EOL> m [ <NUM_LIT:1> : <NUM_LIT:3> ] = tp ( b"<STR_LIT>" ) <EOL> self . _check_contents ( tp , b , b"<STR_LIT>" ) <EOL> m [ <NUM_LIT:1> : <NUM_LIT:1> ] = tp ( b"<STR_LIT>" ) <EOL> self . _check_contents ( tp , b , b"<STR_LIT>" ) <EOL> m [ : ] = tp ( b"<STR_LIT>" ) <EOL> self . _check_contents ( tp , b , b"<STR_LIT>" ) <EOL> m [ <NUM_LIT:0> : <NUM_LIT:3> ] = m [ <NUM_LIT:2> : <NUM_LIT:5> ] <EOL> self . _check_contents ( tp , b , b"<STR_LIT>" ) <EOL> m [ : ] = tp ( b"<STR_LIT>" ) <EOL> m [ <NUM_LIT:2> : <NUM_LIT:5> ] = m [ <NUM_LIT:0> : <NUM_LIT:3> ] <EOL> self . _check_contents ( tp , b , b"<STR_LIT>" ) <EOL> def setitem ( key , value ) : <EOL> m [ key ] = tp ( value ) <EOL> self . assertRaises ( IndexError , setitem , <NUM_LIT:6> , b"<STR_LIT:a>" ) <EOL> self . assertRaises ( IndexError , setitem , - <NUM_LIT:7> , b"<STR_LIT:a>" ) <EOL> self . assertRaises ( IndexError , setitem , sys . maxsize , b"<STR_LIT:a>" ) <EOL> self . assertRaises ( IndexError , setitem , - sys . maxsize , b"<STR_LIT:a>" ) <EOL> self . assertRaises ( TypeError , setitem , <NUM_LIT:0.0> , b"<STR_LIT:a>" ) <EOL> self . assertRaises ( TypeError , setitem , ( <NUM_LIT:0> , ) , b"<STR_LIT:a>" ) <EOL> self . assertRaises ( TypeError , setitem , ( slice ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ) , <NUM_LIT:0> ) , b"<STR_LIT:a>" ) <EOL> self . assertRaises ( TypeError , setitem , ( <NUM_LIT:0> , slice ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ) ) , b"<STR_LIT:a>" ) <EOL> self . assertRaises ( TypeError , setitem , ( <NUM_LIT:0> , ) , b"<STR_LIT:a>" ) <EOL> self . assertRaises ( TypeError , setitem , "<STR_LIT:a>" , b"<STR_LIT:a>" ) <EOL> slices = ( slice ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ) , slice ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> self . assertRaises ( NotImplementedError , setitem , slices , b"<STR_LIT:a>" ) <EOL> exc = ValueError if m . format == '<STR_LIT:c>' else TypeError <EOL> self . assertRaises ( exc , setitem , <NUM_LIT:0> , b"<STR_LIT>" ) <EOL> self . assertRaises ( exc , setitem , <NUM_LIT:0> , b"<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , setitem , slice ( <NUM_LIT:1> , <NUM_LIT:1> ) , b"<STR_LIT:a>" ) <EOL> self . assertRaises ( ValueError , setitem , slice ( <NUM_LIT:0> , <NUM_LIT:2> ) , b"<STR_LIT:a>" ) <EOL> m = None <EOL> self . assertEqual ( sys . getrefcount ( b ) , oldrefcount ) <EOL> def test_delitem ( self ) : <EOL> for tp in self . _types : <EOL> b = tp ( self . _source ) <EOL> m = self . _view ( b ) <EOL> with self . assertRaises ( TypeError ) : <EOL> del m [ <NUM_LIT:1> ] <EOL> with self . assertRaises ( TypeError ) : <EOL> del m [ <NUM_LIT:1> : <NUM_LIT:4> ] <EOL> def test_tobytes ( self ) : <EOL> for tp in self . _types : <EOL> m = self . _view ( tp ( self . _source ) ) <EOL> b = m . tobytes ( ) <EOL> expected = b"<STR_LIT>" . join ( <EOL> self . getitem_type ( bytes ( [ c ] ) ) for c in b"<STR_LIT>" ) <EOL> self . assertEqual ( b , expected ) <EOL> self . assertIsInstance ( b , bytes ) <EOL> def test_tolist ( self ) : <EOL> for tp in self . _types : <EOL> m = self . _view ( tp ( self . _source ) ) <EOL> l = m . tolist ( ) <EOL> self . assertEqual ( l , list ( b"<STR_LIT>" ) ) <EOL> def test_compare ( self ) : <EOL> for tp in self . _types : <EOL> m = self . _view ( tp ( self . _source ) ) <EOL> for tp_comp in self . _types : <EOL> self . assertTrue ( m == tp_comp ( b"<STR_LIT>" ) ) <EOL> self . assertFalse ( m != tp_comp ( b"<STR_LIT>" ) ) <EOL> self . assertFalse ( m == tp_comp ( b"<STR_LIT>" ) ) <EOL> self . assertTrue ( m != tp_comp ( b"<STR_LIT>" ) ) <EOL> self . assertFalse ( m == tp_comp ( b"<STR_LIT>" ) ) <EOL> self . assertTrue ( m != tp_comp ( b"<STR_LIT>" ) ) <EOL> self . assertTrue ( m == m ) <EOL> self . assertTrue ( m == m [ : ] ) <EOL> self . assertTrue ( m [ <NUM_LIT:0> : <NUM_LIT:6> ] == m [ : ] ) <EOL> self . assertFalse ( m [ <NUM_LIT:0> : <NUM_LIT:5> ] == m ) <EOL> self . assertFalse ( m == "<STR_LIT>" ) <EOL> self . assertTrue ( m != "<STR_LIT>" ) <EOL> self . assertFalse ( "<STR_LIT>" == m ) <EOL> self . assertTrue ( "<STR_LIT>" != m ) <EOL> for c in ( m , b"<STR_LIT>" ) : <EOL> self . assertRaises ( TypeError , lambda : m < c ) <EOL> self . assertRaises ( TypeError , lambda : c <= m ) <EOL> self . assertRaises ( TypeError , lambda : m >= c ) <EOL> self . assertRaises ( TypeError , lambda : c > m ) <EOL> def check_attributes_with_type ( self , tp ) : <EOL> m = self . _view ( tp ( self . _source ) ) <EOL> self . assertEqual ( m . format , self . format ) <EOL> self . assertEqual ( m . itemsize , self . itemsize ) <EOL> self . assertEqual ( m . ndim , <NUM_LIT:1> ) <EOL> self . assertEqual ( m . shape , ( <NUM_LIT:6> , ) ) <EOL> self . assertEqual ( len ( m ) , <NUM_LIT:6> ) <EOL> self . assertEqual ( m . strides , ( self . itemsize , ) ) <EOL> self . assertEqual ( m . suboffsets , ( ) ) <EOL> return m <EOL> def test_attributes_readonly ( self ) : <EOL> if not self . ro_type : <EOL> return <EOL> m = self . check_attributes_with_type ( self . ro_type ) <EOL> self . assertEqual ( m . readonly , True ) <EOL> def test_attributes_writable ( self ) : <EOL> if not self . rw_type : <EOL> return <EOL> m = self . check_attributes_with_type ( self . rw_type ) <EOL> self . assertEqual ( m . readonly , False ) <EOL> def test_getbuffer ( self ) : <EOL> for tp in self . _types : <EOL> b = tp ( self . _source ) <EOL> oldrefcount = sys . getrefcount ( b ) <EOL> m = self . _view ( b ) <EOL> oldviewrefcount = sys . getrefcount ( m ) <EOL> s = str ( m , "<STR_LIT:utf-8>" ) <EOL> self . _check_contents ( tp , b , s . encode ( "<STR_LIT:utf-8>" ) ) <EOL> self . assertEqual ( sys . getrefcount ( m ) , oldviewrefcount ) <EOL> m = None <EOL> self . assertEqual ( sys . getrefcount ( b ) , oldrefcount ) <EOL> def test_gc ( self ) : <EOL> for tp in self . _types : <EOL> if not isinstance ( tp , type ) : <EOL> continue <EOL> class MyView ( ) : <EOL> def __init__ ( self , base ) : <EOL> self . m = memoryview ( base ) <EOL> class MySource ( tp ) : <EOL> pass <EOL> class MyObject : <EOL> pass <EOL> b = MySource ( tp ( b'<STR_LIT:abc>' ) ) <EOL> m = self . _view ( b ) <EOL> o = MyObject ( ) <EOL> b . m = m <EOL> b . o = o <EOL> wr = weakref . ref ( o ) <EOL> b = m = o = None <EOL> gc . collect ( ) <EOL> self . assertTrue ( wr ( ) is None , wr ( ) ) <EOL> m = MyView ( tp ( b'<STR_LIT:abc>' ) ) <EOL> o = MyObject ( ) <EOL> m . x = m <EOL> m . o = o <EOL> wr = weakref . ref ( o ) <EOL> m = o = None <EOL> gc . collect ( ) <EOL> self . assertTrue ( wr ( ) is None , wr ( ) ) <EOL> def _check_released ( self , m , tp ) : <EOL> check = self . assertRaisesRegex ( ValueError , "<STR_LIT>" ) <EOL> with check : bytes ( m ) <EOL> with check : m . tobytes ( ) <EOL> with check : m . tolist ( ) <EOL> with check : m [ <NUM_LIT:0> ] <EOL> with check : m [ <NUM_LIT:0> ] = b'<STR_LIT:x>' <EOL> with check : len ( m ) <EOL> with check : m . format <EOL> with check : m . itemsize <EOL> with check : m . ndim <EOL> with check : m . readonly <EOL> with check : m . shape <EOL> with check : m . strides <EOL> with check : <EOL> with m : <EOL> pass <EOL> self . assertIn ( "<STR_LIT>" , str ( m ) ) <EOL> self . assertIn ( "<STR_LIT>" , repr ( m ) ) <EOL> self . assertEqual ( m , m ) <EOL> self . assertNotEqual ( m , memoryview ( tp ( self . _source ) ) ) <EOL> self . assertNotEqual ( m , tp ( self . _source ) ) <EOL> def test_contextmanager ( self ) : <EOL> for tp in self . _types : <EOL> b = tp ( self . _source ) <EOL> m = self . _view ( b ) <EOL> with m as cm : <EOL> self . assertIs ( cm , m ) <EOL> self . _check_released ( m , tp ) <EOL> m = self . _view ( b ) <EOL> with m : <EOL> m . release ( ) <EOL> def test_release ( self ) : <EOL> for tp in self . _types : <EOL> b = tp ( self . _source ) <EOL> m = self . _view ( b ) <EOL> m . release ( ) <EOL> self . _check_released ( m , tp ) <EOL> m . release ( ) <EOL> self . _check_released ( m , tp ) <EOL> def test_writable_readonly ( self ) : <EOL> tp = self . ro_type <EOL> if tp is None : <EOL> return <EOL> b = tp ( self . _source ) <EOL> m = self . _view ( b ) <EOL> i = io . BytesIO ( b'<STR_LIT>' ) <EOL> self . assertRaises ( TypeError , i . readinto , m ) <EOL> def test_getbuf_fail ( self ) : <EOL> self . assertRaises ( TypeError , self . _view , { } ) <EOL> def test_hash ( self ) : <EOL> tp = self . ro_type <EOL> if tp is None : <EOL> self . skipTest ( "<STR_LIT>" ) <EOL> b = tp ( self . _source ) <EOL> m = self . _view ( b ) <EOL> self . assertEqual ( hash ( m ) , hash ( b"<STR_LIT>" ) ) <EOL> m . release ( ) <EOL> self . assertEqual ( hash ( m ) , hash ( b"<STR_LIT>" ) ) <EOL> m = self . _view ( b ) <EOL> m . release ( ) <EOL> self . assertRaises ( ValueError , hash , m ) <EOL> def test_hash_writable ( self ) : <EOL> tp = self . rw_type <EOL> if tp is None : <EOL> self . skipTest ( "<STR_LIT>" ) <EOL> b = tp ( self . _source ) <EOL> m = self . _view ( b ) <EOL> self . assertRaises ( ValueError , hash , m ) <EOL> def test_weakref ( self ) : <EOL> for tp in self . _types : <EOL> b = tp ( self . _source ) <EOL> m = self . _view ( b ) <EOL> L = [ ] <EOL> def callback ( wr , b = b ) : <EOL> L . append ( b ) <EOL> wr = weakref . ref ( m , callback ) <EOL> self . assertIs ( wr ( ) , m ) <EOL> del m <EOL> test . support . gc_collect ( ) <EOL> self . assertIs ( wr ( ) , None ) <EOL> self . assertIs ( L [ <NUM_LIT:0> ] , b ) <EOL> class BaseBytesMemoryTests ( AbstractMemoryTests ) : <EOL> ro_type = bytes <EOL> rw_type = bytearray <EOL> getitem_type = bytes <EOL> itemsize = <NUM_LIT:1> <EOL> format = '<STR_LIT:B>' <EOL> class BaseArrayMemoryTests ( AbstractMemoryTests ) : <EOL> ro_type = None <EOL> rw_type = lambda self , b : array . array ( '<STR_LIT:i>' , list ( b ) ) <EOL> getitem_type = lambda self , b : array . array ( '<STR_LIT:i>' , list ( b ) ) . tobytes ( ) <EOL> itemsize = array . array ( '<STR_LIT:i>' ) . itemsize <EOL> format = '<STR_LIT:i>' <EOL> def test_getbuffer ( self ) : <EOL> pass <EOL> def test_tolist ( self ) : <EOL> pass <EOL> class BaseMemoryviewTests : <EOL> def _view ( self , obj ) : <EOL> return memoryview ( obj ) <EOL> def _check_contents ( self , tp , obj , contents ) : <EOL> self . assertEqual ( obj , tp ( contents ) ) <EOL> class BaseMemorySliceTests : <EOL> source_bytes = b"<STR_LIT>" <EOL> def _view ( self , obj ) : <EOL> m = memoryview ( obj ) <EOL> return m [ <NUM_LIT:1> : <NUM_LIT:7> ] <EOL> def _check_contents ( self , tp , obj , contents ) : <EOL> self . assertEqual ( obj [ <NUM_LIT:1> : <NUM_LIT:7> ] , tp ( contents ) ) <EOL> def test_refs ( self ) : <EOL> for tp in self . _types : <EOL> m = memoryview ( tp ( self . _source ) ) <EOL> oldrefcount = sys . getrefcount ( m ) <EOL> m [ <NUM_LIT:1> : <NUM_LIT:2> ] <EOL> self . assertEqual ( sys . getrefcount ( m ) , oldrefcount ) <EOL> class BaseMemorySliceSliceTests : <EOL> source_bytes = b"<STR_LIT>" <EOL> def _view ( self , obj ) : <EOL> m = memoryview ( obj ) <EOL> return m [ : <NUM_LIT:7> ] [ <NUM_LIT:1> : ] <EOL> def _check_contents ( self , tp , obj , contents ) : <EOL> self . assertEqual ( obj [ <NUM_LIT:1> : <NUM_LIT:7> ] , tp ( contents ) ) <EOL> class BytesMemoryviewTest ( unittest . TestCase , <EOL> BaseMemoryviewTests , BaseBytesMemoryTests ) : <EOL> def test_constructor ( self ) : <EOL> for tp in self . _types : <EOL> ob = tp ( self . _source ) <EOL> self . assertTrue ( memoryview ( ob ) ) <EOL> self . assertTrue ( memoryview ( object = ob ) ) <EOL> self . assertRaises ( TypeError , memoryview ) <EOL> self . assertRaises ( TypeError , memoryview , ob , ob ) <EOL> self . assertRaises ( TypeError , memoryview , argument = ob ) <EOL> self . assertRaises ( TypeError , memoryview , ob , argument = True ) <EOL> class ArrayMemoryviewTest ( unittest . TestCase , <EOL> BaseMemoryviewTests , BaseArrayMemoryTests ) : <EOL> def test_array_assign ( self ) : <EOL> a = array . array ( '<STR_LIT:i>' , range ( <NUM_LIT:10> ) ) <EOL> m = memoryview ( a ) <EOL> new_a = array . array ( '<STR_LIT:i>' , range ( <NUM_LIT:9> , - <NUM_LIT:1> , - <NUM_LIT:1> ) ) <EOL> m [ : ] = new_a <EOL> self . assertEqual ( a , new_a ) <EOL> class BytesMemorySliceTest ( unittest . TestCase , <EOL> BaseMemorySliceTests , BaseBytesMemoryTests ) : <EOL> pass <EOL> class ArrayMemorySliceTest ( unittest . TestCase , <EOL> BaseMemorySliceTests , BaseArrayMemoryTests ) : <EOL> pass <EOL> class BytesMemorySliceSliceTest ( unittest . TestCase , <EOL> BaseMemorySliceSliceTests , BaseBytesMemoryTests ) : <EOL> pass <EOL> class ArrayMemorySliceSliceTest ( unittest . TestCase , <EOL> BaseMemorySliceSliceTests , BaseArrayMemoryTests ) : <EOL> pass <EOL> def test_main ( ) : <EOL> test . support . run_unittest ( __name__ ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
