[arvados] updated: 2.7.0-6603-gd022c4d9db

git repository hosting git at public.arvados.org
Mon May 20 21:01:11 UTC 2024


Summary of changes:
 sdk/python/arvados/arvfile.py    | 19 ++++++++++---------
 sdk/python/arvados/collection.py |  9 ++++++++-
 2 files changed, 18 insertions(+), 10 deletions(-)

       via  d022c4d9dbd94ada1f9d0449cd7c9494b90bb518 (commit)
      from  82b47fbaeb66a790b10f830ede3119462e2e402e (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit d022c4d9dbd94ada1f9d0449cd7c9494b90bb518
Author: Peter Amstutz <peter.amstutz at curii.com>
Date:   Mon May 20 16:59:23 2024 -0400

    21718: Add return_memoryview flag
    
    Added to ArvFile.readfrom(), ArvadosFileReaderBase and
    RichCollectionBase.open().
    
    Arvados-DCO-1.1-Signed-off-by: Peter Amstutz <peter.amstutz at curii.com>

diff --git a/sdk/python/arvados/arvfile.py b/sdk/python/arvados/arvfile.py
index 2fc690d864..fb3e5da72b 100644
--- a/sdk/python/arvados/arvfile.py
+++ b/sdk/python/arvados/arvfile.py
@@ -79,11 +79,12 @@ class _FileLikeObjectBase(object):
 
 
 class ArvadosFileReaderBase(_FileLikeObjectBase):
-    def __init__(self, name, mode, num_retries=None):
+    def __init__(self, name, mode, num_retries=None, return_memoryview=False):
         super(ArvadosFileReaderBase, self).__init__(name, mode)
         self._filepos = 0
         self.num_retries = num_retries
         self._readline_cache = (None, None)
+        self._return_memoryview = return_memoryview
 
     def __iter__(self):
         while True:
@@ -1254,8 +1255,8 @@ class ArvadosFileReader(ArvadosFileReaderBase):
 
     """
 
-    def __init__(self, arvadosfile, mode="r", num_retries=None):
-        super(ArvadosFileReader, self).__init__(arvadosfile.name, mode=mode, num_retries=num_retries)
+    def __init__(self, arvadosfile, mode="r", num_retries=None, return_memoryview=False):
+        super(ArvadosFileReader, self).__init__(arvadosfile.name, mode=mode, num_retries=num_retries, return_memoryview=return_memoryview)
         self.arvadosfile = arvadosfile
 
     def size(self):
@@ -1271,7 +1272,7 @@ class ArvadosFileReader(ArvadosFileReaderBase):
 
     @_FileLikeObjectBase._before_close
     @retry_method
-    def read(self, size=None, num_retries=None, return_memoryview=False):
+    def read(self, size=None, num_retries=None):
         """Read up to `size` bytes from the file and return the result.
 
         Starts at the current file position.  If `size` is None, read the
@@ -1290,18 +1291,18 @@ class ArvadosFileReader(ArvadosFileReaderBase):
                 rd = self.arvadosfile.readfrom(self._filepos, config.KEEP_BLOCK_SIZE, num_retries, exact=False, return_memoryview=True)
             return b''.join(data)
         else:
-            data = self.arvadosfile.readfrom(self._filepos, size, num_retries, exact=True, return_memoryview=return_memoryview)
+            data = self.arvadosfile.readfrom(self._filepos, size, num_retries, exact=True, return_memoryview=self._return_memoryview)
             self._filepos += len(data)
             return data
 
     @_FileLikeObjectBase._before_close
     @retry_method
-    def readfrom(self, offset, size, num_retries=None, return_memoryview=False):
+    def readfrom(self, offset, size, num_retries=None):
         """Read up to `size` bytes from the stream, starting at the specified file offset.
 
         This method does not change the file position.
         """
-        return self.arvadosfile.readfrom(offset, size, num_retries, return_memoryview=return_memoryview)
+        return self.arvadosfile.readfrom(offset, size, num_retries, return_memoryview=self._return_memoryview)
 
     def flush(self):
         pass
@@ -1315,8 +1316,8 @@ class ArvadosFileWriter(ArvadosFileReader):
 
     """
 
-    def __init__(self, arvadosfile, mode, num_retries=None):
-        super(ArvadosFileWriter, self).__init__(arvadosfile, mode=mode, num_retries=num_retries)
+    def __init__(self, arvadosfile, mode, num_retries=None, return_memoryview=False):
+        super(ArvadosFileWriter, self).__init__(arvadosfile, mode=mode, num_retries=num_retries, return_memoryview=return_memoryview)
         self.arvadosfile.add_writer(self)
 
     def writable(self):
diff --git a/sdk/python/arvados/collection.py b/sdk/python/arvados/collection.py
index 1050d4c093..8ae53db5f8 100644
--- a/sdk/python/arvados/collection.py
+++ b/sdk/python/arvados/collection.py
@@ -337,6 +337,7 @@ class RichCollectionBase(CollectionBase):
             path: str,
             mode: str="r",
             encoding: Optional[str]=None,
+            return_memoryview: bool=False
     ) -> IO:
         """Open a file-like object within the collection
 
@@ -356,6 +357,12 @@ class RichCollectionBase(CollectionBase):
         * encoding: str | None --- The text encoding of the file. Only used
           when the file is opened in text mode. The default is
           platform-dependent.
+
+        * return_memoryview: bool -- If True and the file is opened in
+          binary mode, may return either `bytes` or a zero-copy
+          `memoryview` object (more efficient, but may confuse code
+          expecting a `bytes` object).
+
         """
         if not re.search(r'^[rwa][bt]?\+?$', mode):
             raise errors.ArgumentError("Invalid mode {!r}".format(mode))
@@ -378,7 +385,7 @@ class RichCollectionBase(CollectionBase):
             arvfile.truncate(0)
 
         binmode = mode[0] + 'b' + re.sub('[bt]', '', mode[1:])
-        f = fclass(arvfile, mode=binmode, num_retries=self.num_retries)
+        f = fclass(arvfile, mode=binmode, num_retries=self.num_retries, return_memoryview=return_memoryview)
         if 'b' not in mode:
             bufferclass = io.BufferedRandom if f.writable() else io.BufferedReader
             f = io.TextIOWrapper(bufferclass(WrappableFile(f)), encoding=encoding)

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list