@@ -500,82 +500,12 @@ def test_anonymous(self):
500500 for x in range (PAGESIZE ):
501501 self .assertEqual (m [x ], 0 ,
502502 "anonymously mmap'ed contents should be zero" )
503- with self .assertRaises (IndexError ):
504- m [PAGESIZE ]
505503
506504 for x in range (PAGESIZE ):
507505 b = x & 0xff
508506 m [x ] = b
509507 self .assertEqual (m [x ], b )
510508
511- if sys .platform .startswith (('linux' , 'android' )):
512- # Can't expand a shared anonymous mapping on Linux.
513- # See https://bugzilla.kernel.org/show_bug.cgi?id=8691
514- with self .assertRaises (ValueError ):
515- m .resize (2 * PAGESIZE )
516- else :
517- try :
518- m .resize (2 * PAGESIZE )
519- except SystemError :
520- pass
521- else :
522- for x in range (PAGESIZE ):
523- self .assertEqual (m [x ], x & 0xff )
524- for x in range (PAGESIZE , 2 * PAGESIZE ):
525- self .assertEqual (m [x ], 0 )
526- with self .assertRaises (IndexError ):
527- m [2 * PAGESIZE ]
528-
529- for x in range (PAGESIZE , 2 * PAGESIZE ):
530- b = x & 0xff
531- m [x ] = b
532- self .assertEqual (m [x ], b )
533-
534- try :
535- m .resize (PAGESIZE // 2 )
536- except SystemError :
537- pass
538- else :
539- for x in range (PAGESIZE // 2 ):
540- self .assertEqual (m [x ], x & 0xff )
541- with self .assertRaises (IndexError ):
542- m [PAGESIZE // 2 ]
543-
544- if sys .platform .startswith (('linux' , 'android' )):
545- # Can't expand to its original size.
546- with self .assertRaises (ValueError ):
547- m .resize (PAGESIZE )
548-
549- @unittest .skipUnless (os .name == 'posix' , 'requires Posix' )
550- def test_private_anonymous (self ):
551- m = mmap .mmap (- 1 , PAGESIZE , flags = mmap .MAP_PRIVATE )
552- for x in range (PAGESIZE ):
553- self .assertEqual (m [x ], 0 )
554- with self .assertRaises (IndexError ):
555- m [PAGESIZE ]
556-
557- for x in range (PAGESIZE ):
558- b = x & 0xff
559- m [x ] = b
560- self .assertEqual (m [x ], b )
561-
562- try :
563- m .resize (2 * PAGESIZE )
564- except SystemError :
565- pass
566- else :
567- for x in range (PAGESIZE ):
568- self .assertEqual (m [x ], x & 0xff )
569- for x in range (PAGESIZE , 2 * PAGESIZE ):
570- self .assertEqual (m [x ], 0 )
571- with self .assertRaises (IndexError ):
572- m [2 * PAGESIZE ]
573-
574- for x in range (PAGESIZE , 2 * PAGESIZE ):
575- b = x & 0xff
576- m [x ] = b
577- self .assertEqual (m [x ], b )
578-
579509 def test_read_all (self ):
580510 m = mmap .mmap (- 1 , 16 )
581511 self .addCleanup (m .close )
@@ -971,35 +901,69 @@ def test_madvise(self):
971901 self .assertEqual (m .madvise (mmap .MADV_NORMAL , 0 , 2 ), None )
972902 self .assertEqual (m .madvise (mmap .MADV_NORMAL , 0 , size ), None )
973903
974- @unittest .skipUnless (os .name == 'nt' , 'requires Windows' )
975- def test_resize_up_when_mapped_to_pagefile (self ):
904+ def test_resize_up_anonymous_mapping (self ):
976905 """If the mmap is backed by the pagefile ensure a resize up can happen
977906 and that the original data is still in place
978907 """
979908 start_size = PAGESIZE
980909 new_size = 2 * start_size
981- data = bytes ( random .getrandbits ( 8 ) for _ in range ( start_size ) )
910+ data = random .randbytes ( start_size )
982911
983- m = mmap .mmap (- 1 , start_size )
984- m [:] = data
985- m .resize (new_size )
986- self .assertEqual (len (m ), new_size )
987- self .assertEqual (m [:start_size ], data [:start_size ])
912+ with mmap .mmap (- 1 , start_size ) as m :
913+ m [:] = data
914+ if sys .platform .startswith (('linux' , 'android' )):
915+ # Can't expand a shared anonymous mapping on Linux.
916+ # See https://bugzilla.kernel.org/show_bug.cgi?id=8691
917+ with self .assertRaises (ValueError ):
918+ m .resize (new_size )
919+ else :
920+ try :
921+ m .resize (new_size )
922+ except SystemError :
923+ pass
924+ else :
925+ self .assertEqual (len (m ), new_size )
926+ self .assertEqual (m [:start_size ], data )
927+ self .assertEqual (m [start_size :], b'\0 ' * (new_size - start_size ))
988928
989- @unittest .skipUnless (os .name == 'nt' , 'requires Windows' )
990- def test_resize_down_when_mapped_to_pagefile (self ):
929+ @unittest .skipUnless (os .name == 'posix' , 'requires Posix' )
930+ def test_resize_up_private_anonymous_mapping (self ):
931+ start_size = PAGESIZE
932+ new_size = 2 * start_size
933+ data = random .randbytes (start_size )
934+
935+ with mmap .mmap (- 1 , start_size , flags = mmap .MAP_PRIVATE ) as m :
936+ m [:] = data
937+ try :
938+ m .resize (new_size )
939+ except SystemError :
940+ pass
941+ else :
942+ self .assertEqual (len (m ), new_size )
943+ self .assertEqual (m [:start_size ], data )
944+ self .assertEqual (m [start_size :], b'\0 ' * (new_size - start_size ))
945+
946+ def test_resize_down_anonymous_mapping (self ):
991947 """If the mmap is backed by the pagefile ensure a resize down up can happen
992948 and that a truncated form of the original data is still in place
993949 """
994- start_size = PAGESIZE
950+ start_size = 2 * PAGESIZE
995951 new_size = start_size // 2
996- data = bytes ( random .getrandbits ( 8 ) for _ in range ( start_size ) )
952+ data = random .randbytes ( start_size )
997953
998- m = mmap .mmap (- 1 , start_size )
999- m [:] = data
1000- m .resize (new_size )
1001- self .assertEqual (len (m ), new_size )
1002- self .assertEqual (m [:new_size ], data [:new_size ])
954+ with mmap .mmap (- 1 , start_size ) as m :
955+ m [:] = data
956+ try :
957+ m .resize (new_size )
958+ except SystemError :
959+ pass
960+ else :
961+ self .assertEqual (len (m ), new_size )
962+ self .assertEqual (m [:], data [:new_size ])
963+ if sys .platform .startswith (('linux' , 'android' )):
964+ # Can't expand to its original size.
965+ with self .assertRaises (ValueError ):
966+ m .resize (start_size )
1003967
1004968 @unittest .skipUnless (os .name == 'nt' , 'requires Windows' )
1005969 def test_resize_fails_if_mapping_held_elsewhere (self ):
0 commit comments