Repository: arrow
Updated Branches:
  refs/heads/master bfe395906 -> f62db836a


ARROW-1100: [Python] Add mode property to NativeFile

Author: Wes McKinney <wes.mckin...@twosigma.com>

Closes #842 from wesm/ARROW-1100 and squashes the following commits:

ac245e6 [Wes McKinney] Add docstring
64831de [Wes McKinney] Add mode property to pyarrow.NativeFile


Project: http://git-wip-us.apache.org/repos/asf/arrow/repo
Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/f62db836
Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/f62db836
Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/f62db836

Branch: refs/heads/master
Commit: f62db836a0eb606b1e92d3e29fef8012fc9d000f
Parents: bfe3959
Author: Wes McKinney <wes.mckin...@twosigma.com>
Authored: Sat Jul 15 13:30:54 2017 +0200
Committer: Uwe L. Korn <uw...@xhochy.com>
Committed: Sat Jul 15 13:30:54 2017 +0200

----------------------------------------------------------------------
 python/pyarrow/io.pxi           | 26 +++++++++++++++++++++++---
 python/pyarrow/tests/test_io.py | 34 ++++++++++++++++++++++++++++++++--
 2 files changed, 55 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/arrow/blob/f62db836/python/pyarrow/io.pxi
----------------------------------------------------------------------
diff --git a/python/pyarrow/io.pxi b/python/pyarrow/io.pxi
index 64cce03..c15be5e 100644
--- a/python/pyarrow/io.pxi
+++ b/python/pyarrow/io.pxi
@@ -54,6 +54,26 @@ cdef class NativeFile:
     def __exit__(self, exc_type, exc_value, tb):
         self.close()
 
+    property mode:
+        """
+        The file mode. Currently instances of NativeFile may support:
+
+        * rb: binary read
+        * wb: binary write
+        * rb+: binary read and write
+        """
+
+        def __get__(self):
+            # Emulate built-in file modes
+            if self.is_readable and self.is_writeable:
+                return 'rb+'
+            elif self.is_readable:
+                return 'rb'
+            elif self.is_writeable:
+                return 'wb'
+            else:
+                raise ValueError('File object is malformed, has no mode')
+
     def close(self):
         if self.is_open:
             with nogil:
@@ -346,7 +366,7 @@ cdef class MemoryMappedFile(NativeFile):
 
         return result
 
-    def open(self, path, mode='r'):
+    def _open(self, path, mode='r'):
         self.path = path
 
         cdef:
@@ -360,7 +380,7 @@ cdef class MemoryMappedFile(NativeFile):
         elif mode in ('w', 'wb'):
             c_mode = FileMode_WRITE
             self.is_writeable = 1
-        elif mode == 'r+w':
+        elif mode in ('r+', 'r+b', 'rb+'):
             c_mode = FileMode_READWRITE
             self.is_readable = 1
             self.is_writeable = 1
@@ -388,7 +408,7 @@ def memory_map(path, mode='r'):
     mmap : MemoryMappedFile
     """
     cdef MemoryMappedFile mmap = MemoryMappedFile()
-    mmap.open(path, mode)
+    mmap._open(path, mode)
     return mmap
 
 

http://git-wip-us.apache.org/repos/asf/arrow/blob/f62db836/python/pyarrow/tests/test_io.py
----------------------------------------------------------------------
diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py
index cadf786..6258f6d 100644
--- a/python/pyarrow/tests/test_io.py
+++ b/python/pyarrow/tests/test_io.py
@@ -354,7 +354,7 @@ def test_memory_map_writer(tmpdir):
     with open(path, 'wb') as f:
         f.write(data)
 
-    f = pa.memory_map(path, mode='r+w')
+    f = pa.memory_map(path, mode='r+b')
 
     f.seek(10)
     f.write('peekaboo')
@@ -363,7 +363,7 @@ def test_memory_map_writer(tmpdir):
     f.seek(10)
     assert f.read(8) == b'peekaboo'
 
-    f2 = pa.memory_map(path, mode='r+w')
+    f2 = pa.memory_map(path, mode='r+b')
 
     f2.seek(10)
     f2.write(b'booapeak')
@@ -404,3 +404,33 @@ def test_os_file_writer(tmpdir):
 
     with pytest.raises(IOError):
         f2.read(5)
+
+
+def test_native_file_modes(tmpdir):
+    path = os.path.join(str(tmpdir), guid())
+    with open(path, 'wb') as f:
+        f.write(b'foooo')
+
+    with pa.OSFile(path, mode='r') as f:
+        assert f.mode == 'rb'
+
+    with pa.OSFile(path, mode='rb') as f:
+        assert f.mode == 'rb'
+
+    with pa.OSFile(path, mode='w') as f:
+        assert f.mode == 'wb'
+
+    with pa.OSFile(path, mode='wb') as f:
+        assert f.mode == 'wb'
+
+    with open(path, 'wb') as f:
+        f.write(b'foooo')
+
+    with pa.memory_map(path, 'r') as f:
+        assert f.mode == 'rb'
+
+    with pa.memory_map(path, 'r+') as f:
+        assert f.mode == 'rb+'
+
+    with pa.memory_map(path, 'r+b') as f:
+        assert f.mode == 'rb+'

Reply via email to