changeset 51888:f5c46c3518a5

util: make buffer readonly There is no use of writable buffers anywhere in the code, and this helps us make sure we don't get into unsound territory when sharing memory with Rust. This `toreadonly` method was not available in Python 3.6, but we dropped the support for it earlier that week, so no need for any compatibility code.
author Raphaël Gomès <rgomes@octobus.net>
date Tue, 26 Mar 2024 15:51:31 +0000
parents 4dc1fc2b2f3a
children 0dbf6a5ccf5f
files mercurial/util.py
diffstat 1 files changed, 4 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Thu Sep 05 17:12:52 2024 -0400
+++ b/mercurial/util.py	Tue Mar 26 15:51:31 2024 +0000
@@ -333,8 +333,10 @@
 
     def buffer(sliceable, offset=0, length=None):
         if length is not None:
-            return memoryview(sliceable)[offset : offset + length]
-        return memoryview(sliceable)[offset:]
+            view = memoryview(sliceable)[offset : offset + length]
+        else:
+            view = memoryview(sliceable)[offset:]
+        return view.toreadonly()
 
 
 _chunksize = 4096