From 07dd7a6c60fe8c3fed004a66afd12fa2bf866ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= Date: Tue, 21 Jul 2026 16:30:52 +0000 Subject: [PATCH] Use mmap_mode for probing. Fixes plyfile incorrectly falling back to non-mmap read even when mmap is supported. Until now, for example, when the user wanted a read-only ("r") mmap and that would succeed, plyfile's `_can_mmap()` check would not use "r" but "c" for the check, which would fail for large files. --- plyfile.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/plyfile.py b/plyfile.py index 4573a16..a2ac909 100644 --- a/plyfile.py +++ b/plyfile.py @@ -515,10 +515,10 @@ def _read(self, stream, text, byte_order, mmap, list_prop_names = set(p.name for p in self.properties if isinstance(p, PlyListProperty)) can_mmap_lists = list_prop_names <= set(known_list_len) - if mmap and _can_mmap(stream) and can_mmap_lists: + mmap_mode = mmap if isinstance(mmap, str) else 'c' + if mmap and _can_mmap(stream, mmap_mode) and can_mmap_lists: # Loading the data is straightforward. We will memory - # map the file in copy-on-write mode. - mmap_mode = mmap if isinstance(mmap, str) else 'c' + # map the file in the requested mode. self._read_mmap(stream, byte_order, mmap_mode, known_list_len) else: @@ -1434,7 +1434,7 @@ def _write_array(stream, array): stream.write(array.tobytes()) -def _can_mmap(stream): +def _can_mmap(stream, mmap_mode='c'): """ Determine if a readable stream can be memory-mapped, using some good heuristics. @@ -1442,6 +1442,7 @@ def _can_mmap(stream): Parameters ---------- stream : open binary file + mmap_mode : {'c', 'r', 'r+'} Returns ------- @@ -1450,7 +1451,7 @@ def _can_mmap(stream): try: pos = stream.tell() try: - _np.memmap(stream, 'u1', 'c') + _np.memmap(stream, 'u1', mmap_mode) stream.seek(pos) return True except Exception: