Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions python/pyarrow/io-hdfs.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,5 @@ cdef class HdfsFile(NativeFile):
object mode
object parent

cdef object __weakref__

def __dealloc__(self):
self.parent = None
14 changes: 13 additions & 1 deletion python/pyarrow/io.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,21 @@ cdef class PythonFile(NativeFile):
cdef:
object handle

def __cinit__(self, handle, mode='w'):
def __cinit__(self, handle, mode=None):
self.handle = handle

if mode is None:
try:
mode = handle.mode
except AttributeError:
# Not all file-like objects have a mode attribute
# (e.g. BytesIO)
try:
mode = 'w' if handle.writable() else 'r'
except AttributeError:
raise ValueError("could not infer open mode for file-like "
"object %r, please pass it explicitly"
% (handle,))
if mode.startswith('w'):
self.wr_file.reset(new PyOutputStream(handle))
self.is_writable = True
Expand Down
1 change: 1 addition & 0 deletions python/pyarrow/lib.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ cdef class NativeFile:
bint is_writable
readonly bint closed
bint own_file
object __weakref__

# By implementing these "virtual" functions (all functions in Cython
# extension classes are technically virtual in the C++ sense) we can expose
Expand Down
39 changes: 39 additions & 0 deletions python/pyarrow/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os
import pytest
import sys
import weakref

import numpy as np

Expand Down Expand Up @@ -124,6 +125,44 @@ def get_buffer():
assert buf.to_pybytes() == b'sample'
assert buf.parent is not None


def test_python_file_implicit_mode(tmpdir):
path = os.path.join(str(tmpdir), 'foo.txt')
with open(path, 'wb') as f:
pf = pa.PythonFile(f)
assert pf.writable()
assert not pf.readable()
assert not pf.seekable() # PyOutputStream isn't seekable
f.write(b'foobar\n')

with open(path, 'rb') as f:
pf = pa.PythonFile(f)
assert pf.readable()
assert not pf.writable()
assert pf.seekable()
assert pf.read() == b'foobar\n'

bio = BytesIO()
pf = pa.PythonFile(bio)
assert pf.writable()
assert not pf.readable()
assert not pf.seekable()
pf.write(b'foobar\n')
assert bio.getvalue() == b'foobar\n'


def test_python_file_closing():
bio = BytesIO()
pf = pa.PythonFile(bio)
wr = weakref.ref(pf)
del pf
assert wr() is None # object was destroyed
assert not bio.closed
pf = pa.PythonFile(bio)
pf.close()
assert bio.closed


# ----------------------------------------------------------------------
# Buffers

Expand Down