mmapfile_demo.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import win32api, mmapfile
  2. import winerror
  3. import tempfile, os
  4. from pywin32_testutil import str2bytes
  5. system_info=win32api.GetSystemInfo()
  6. page_size=system_info[1]
  7. alloc_size=system_info[7]
  8. fname=tempfile.mktemp()
  9. mapping_name=os.path.split(fname)[1]
  10. fsize=8*page_size
  11. print(fname, fsize, mapping_name)
  12. m1=mmapfile.mmapfile(File=fname, Name=mapping_name, MaximumSize=fsize)
  13. m1.seek(100)
  14. m1.write_byte(str2bytes('?'))
  15. m1.seek(-1,1)
  16. assert m1.read_byte()==str2bytes('?')
  17. ## A reopened named mapping should have exact same size as original mapping
  18. m2=mmapfile.mmapfile(Name=mapping_name, File=None, MaximumSize=fsize*2)
  19. assert m2.size()==m1.size()
  20. m1.seek(0,0)
  21. m1.write(fsize*str2bytes('s'))
  22. assert m2.read(fsize)==fsize*str2bytes('s')
  23. move_src=100
  24. move_dest=500
  25. move_size=150
  26. m2.seek(move_src,0)
  27. assert m2.tell()==move_src
  28. m2.write(str2bytes('m')*move_size)
  29. m2.move(move_dest, move_src, move_size)
  30. m2.seek(move_dest, 0)
  31. assert m2.read(move_size) == str2bytes('m') * move_size
  32. ## m2.write('x'* (fsize+1))
  33. m2.close()
  34. m1.resize(fsize*2)
  35. assert m1.size()==fsize * 2
  36. m1.seek(fsize)
  37. m1.write(str2bytes('w') * fsize)
  38. m1.flush()
  39. m1.close()
  40. os.remove(fname)
  41. ## Test a file with size larger than 32 bits
  42. ## need 10 GB free on drive where your temp folder lives
  43. fname_large=tempfile.mktemp()
  44. mapping_name='Pywin32_large_mmap'
  45. offsetdata=str2bytes('This is start of offset')
  46. ## Deliberately use odd numbers to test rounding logic
  47. fsize = (1024*1024*1024*10) + 333
  48. offset = (1024*1024*32) + 42
  49. view_size = (1024*1024*16) + 111
  50. ## round mapping size and view size up to multiple of system page size
  51. if fsize%page_size:
  52. fsize += page_size - (fsize%page_size)
  53. if view_size%page_size:
  54. view_size += page_size - (view_size%page_size)
  55. ## round offset down to multiple of allocation granularity
  56. offset -= offset%alloc_size
  57. m1=None
  58. m2=None
  59. try:
  60. try:
  61. m1=mmapfile.mmapfile(fname_large, mapping_name, fsize, 0, offset*2)
  62. except mmapfile.error as exc:
  63. # if we don't have enough disk-space, that's OK.
  64. if exc.winerror!=winerror.ERROR_DISK_FULL:
  65. raise
  66. print("skipping large file test - need", fsize, "available bytes.")
  67. else:
  68. m1.seek(offset)
  69. m1.write(offsetdata)
  70. ## When reopening an existing mapping without passing a file handle, you have
  71. ## to specify a positive size even though it's ignored
  72. m2=mmapfile.mmapfile(File=None, Name=mapping_name, MaximumSize=1,
  73. FileOffset=offset, NumberOfBytesToMap=view_size)
  74. assert m2.read(len(offsetdata))==offsetdata
  75. finally:
  76. if m1 is not None:
  77. m1.close()
  78. if m2 is not None:
  79. m2.close()
  80. if os.path.exists(fname_large):
  81. os.remove(fname_large)