mercurial/util.py
changeset 51926 bc9ed92d4753
parent 51885 cbd01bf33802
child 51927 2d51b0cf707c
--- a/mercurial/util.py	Fri Sep 27 12:30:37 2024 -0400
+++ b/mercurial/util.py	Tue Oct 01 15:00:39 2024 -0400
@@ -485,15 +485,24 @@
     elif size is None:
         size = 0
     fd = getattr(fp, 'fileno', lambda: fp)()
-    flags = mmap.MAP_PRIVATE
-    bg_populate = hasattr(osutil, "background_mmap_populate")
-    if pre_populate and not bg_populate:
-        flags |= getattr(mmap, 'MAP_POPULATE', 0)
+
+    if pycompat.iswindows:
+        _mmap = lambda fd, size: mmap.mmap(fd, size, access=mmap.ACCESS_READ)
+    else:
+        flags = mmap.MAP_PRIVATE
+        bg_populate = hasattr(osutil, "background_mmap_populate")
+
+        if pre_populate and not bg_populate:
+            flags |= getattr(mmap, 'MAP_POPULATE', 0)
+
+        def _mmap(fd, size) -> mmap.mmap:
+            m = mmap.mmap(fd, size, flags=flags, prot=mmap.PROT_READ)
+            if pre_populate and bg_populate:
+                osutil.background_mmap_populate(m)
+            return m
+
     try:
-        m = mmap.mmap(fd, size, flags=flags, prot=mmap.PROT_READ)
-        if pre_populate and bg_populate:
-            osutil.background_mmap_populate(m)
-        return m
+        return _mmap(fd, size)
     except ValueError:
         # Empty files cannot be mmapped, but mmapread should still work.  Check
         # if the file is empty, and if so, return an empty buffer.