Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mmap_mode option to allow writing to mmaped PLY files #79

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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={}):
dranjan marked this conversation as resolved.
Show resolved Hide resolved
"""
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,
dranjan marked this conversation as resolved.
Show resolved Hide resolved
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
Loading