Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mmapfile_demo.py 2.8KB

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