Skip to content

Commit

Permalink
Add mmap_mode option to allow writing to mmaped PLY files.
Browse files Browse the repository at this point in the history
  • Loading branch information
chpatrick authored and nh2 committed Jun 16, 2024
1 parent f9c1546 commit 618b02e
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions plyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def _parse_header(stream):
)

@staticmethod
def read(stream, mmap=True, known_list_len={}):
def read(stream, mmap=True, mmap_mode="c", known_list_len={}):
"""
Read PLY data from a readable file-like object or filename.
Expand Down Expand Up @@ -171,6 +171,7 @@ def read(stream, mmap=True, known_list_len={}):
data_stream = stream
for elt in data:
elt._read(data_stream, data.text, data.byte_order, mmap,
mmap_mode=mmap_mode,
known_list_len=known_list_len.get(elt.name, {}))
finally:
if must_close:
Expand Down Expand Up @@ -497,7 +498,7 @@ def describe(data, name, len_types={}, val_types={},

return elt

def _read(self, stream, text, byte_order, mmap,
def _read(self, stream, text, byte_order, mmap, mmap_mode,
known_list_len={}):
"""
Read the actual data from a PLY file.
Expand All @@ -519,7 +520,7 @@ def _read(self, stream, text, byte_order, mmap,
if mmap and _can_mmap(stream) and can_mmap_lists:
# Loading the data is straightforward. We will memory
# map the file in copy-on-write mode.
self._read_mmap(stream, byte_order, known_list_len)
self._read_mmap(stream, byte_order, mmap_mode, known_list_len)
else:
# A simple load is impossible.
self._read_bin(stream, byte_order)
Expand Down Expand Up @@ -549,7 +550,7 @@ def _write(self, stream, text, byte_order):
stream.write(self.data.astype(self.dtype(byte_order),
copy=False).data)

def _read_mmap(self, stream, byte_order, known_list_len):
def _read_mmap(self, stream, byte_order, mmap_mode, known_list_len):
"""
Memory-map an input file as `self.data`.
Expand Down Expand Up @@ -581,7 +582,7 @@ def _read_mmap(self, stream, byte_order, known_list_len):
if max_bytes < num_bytes:
raise PlyElementParseError("early end-of-file", self,
max_bytes // dtype.itemsize)
self._data = _np.memmap(stream, dtype, 'c', offset, self.count)
self._data = _np.memmap(stream, dtype, mmap_mode, offset, self.count)
# Fix stream position
stream.seek(offset + self.count * dtype.itemsize)
# remove any extra properties added
Expand Down

0 comments on commit 618b02e

Please sign in to comment.