util: make buffer readonly
authorRaphaël Gomès <rgomes@octobus.net>
Tue, 26 Mar 2024 15:51:31 +0000
changeset 51850 f5c46c3518a5
parent 51849 4dc1fc2b2f3a
child 51851 0dbf6a5ccf5f
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.
mercurial/util.py
--- 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