Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 9DCD8200CC3 for ; Sat, 15 Jul 2017 13:31:52 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 931AD16A722; Sat, 15 Jul 2017 11:31:52 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (unknown [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id D7EB516A720 for ; Sat, 15 Jul 2017 13:31:51 +0200 (CEST) Received: (qmail 52511 invoked by uid 500); 15 Jul 2017 11:31:50 -0000 Mailing-List: contact commits-help@arrow.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@arrow.apache.org Delivered-To: mailing list commits@arrow.apache.org Received: (qmail 52501 invoked by uid 99); 15 Jul 2017 11:31:50 -0000 Received: from Unknown (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 15 Jul 2017 11:31:50 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 0CB6CE024D; Sat, 15 Jul 2017 11:31:49 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: uwe@apache.org To: commits@arrow.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: arrow git commit: ARROW-1100: [Python] Add mode property to NativeFile Date: Sat, 15 Jul 2017 11:31:49 +0000 (UTC) archived-at: Sat, 15 Jul 2017 11:31:52 -0000 Repository: arrow Updated Branches: refs/heads/master bfe395906 -> f62db836a ARROW-1100: [Python] Add mode property to NativeFile Author: Wes McKinney 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 Authored: Sat Jul 15 13:30:54 2017 +0200 Committer: Uwe L. Korn 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+'