@@ -899,35 +899,69 @@ def test_madvise(self):
899899 self .assertEqual (m .madvise (mmap .MADV_NORMAL , 0 , 2 ), None )
900900 self .assertEqual (m .madvise (mmap .MADV_NORMAL , 0 , size ), None )
901901
902- @unittest .skipUnless (os .name == 'nt' , 'requires Windows' )
903- def test_resize_up_when_mapped_to_pagefile (self ):
902+ def test_resize_up_anonymous_mapping (self ):
904903 """If the mmap is backed by the pagefile ensure a resize up can happen
905904 and that the original data is still in place
906905 """
907906 start_size = PAGESIZE
908907 new_size = 2 * start_size
909- data = bytes ( random .getrandbits ( 8 ) for _ in range ( start_size ) )
908+ data = random .randbytes ( start_size )
910909
911- m = mmap .mmap (- 1 , start_size )
912- m [:] = data
913- m .resize (new_size )
914- self .assertEqual (len (m ), new_size )
915- self .assertEqual (m [:start_size ], data [:start_size ])
910+ with mmap .mmap (- 1 , start_size ) as m :
911+ m [:] = data
912+ if sys .platform .startswith (('linux' , 'android' )):
913+ # Can't expand a shared anonymous mapping on Linux.
914+ # See https://bugzilla.kernel.org/show_bug.cgi?id=8691
915+ with self .assertRaises (ValueError ):
916+ m .resize (new_size )
917+ else :
918+ try :
919+ m .resize (new_size )
920+ except SystemError :
921+ pass
922+ else :
923+ self .assertEqual (len (m ), new_size )
924+ self .assertEqual (m [:start_size ], data )
925+ self .assertEqual (m [start_size :], b'\0 ' * (new_size - start_size ))
916926
917- @unittest .skipUnless (os .name == 'nt' , 'requires Windows' )
918- def test_resize_down_when_mapped_to_pagefile (self ):
927+ @unittest .skipUnless (os .name == 'posix' , 'requires Posix' )
928+ def test_resize_up_private_anonymous_mapping (self ):
929+ start_size = PAGESIZE
930+ new_size = 2 * start_size
931+ data = random .randbytes (start_size )
932+
933+ with mmap .mmap (- 1 , start_size , flags = mmap .MAP_PRIVATE ) as m :
934+ m [:] = data
935+ try :
936+ m .resize (new_size )
937+ except SystemError :
938+ pass
939+ else :
940+ self .assertEqual (len (m ), new_size )
941+ self .assertEqual (m [:start_size ], data )
942+ self .assertEqual (m [start_size :], b'\0 ' * (new_size - start_size ))
943+
944+ def test_resize_down_anonymous_mapping (self ):
919945 """If the mmap is backed by the pagefile ensure a resize down up can happen
920946 and that a truncated form of the original data is still in place
921947 """
922- start_size = PAGESIZE
948+ start_size = 2 * PAGESIZE
923949 new_size = start_size // 2
924- data = bytes ( random .getrandbits ( 8 ) for _ in range ( start_size ) )
950+ data = random .randbytes ( start_size )
925951
926- m = mmap .mmap (- 1 , start_size )
927- m [:] = data
928- m .resize (new_size )
929- self .assertEqual (len (m ), new_size )
930- self .assertEqual (m [:new_size ], data [:new_size ])
952+ with mmap .mmap (- 1 , start_size ) as m :
953+ m [:] = data
954+ try :
955+ m .resize (new_size )
956+ except SystemError :
957+ pass
958+ else :
959+ self .assertEqual (len (m ), new_size )
960+ self .assertEqual (m [:], data [:new_size ])
961+ if sys .platform .startswith (('linux' , 'android' )):
962+ # Can't expand to its original size.
963+ with self .assertRaises (ValueError ):
964+ m .resize (start_size )
931965
932966 @unittest .skipUnless (os .name == 'nt' , 'requires Windows' )
933967 def test_resize_fails_if_mapping_held_elsewhere (self ):
0 commit comments