Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: fix the method for checking local files for 1.24.x #23739

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

Merged
Merged
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
11 changes: 11 additions & 0 deletions 11 numpy/compat/py3k.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ def asstr(s):
def isfileobj(f):
return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))

def _isfileobj(f):
if not isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter)):
return False
try:
# BufferedReader/Writer may raise OSError when
# fetching `fileno()` (e.g. when wrapping BytesIO).
f.fileno()
return True
except OSError:
return False

def open_latin1(filename, mode='r'):
return open(filename, mode=mode, encoding='iso-8859-1')

Expand Down
18 changes: 17 additions & 1 deletion 18 numpy/compat/tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from os.path import join
from io import BufferedReader, BytesIO

from numpy.compat import isfileobj
from numpy.compat.py3k import isfileobj, _isfileobj
from numpy.testing import assert_
from numpy.testing import tempdir

Expand All @@ -17,3 +18,18 @@ def test_isfileobj():

with open(filename, 'rb') as f:
assert_(isfileobj(f))

def test__isfileobj():
with tempdir(prefix="numpy_test_compat_") as folder:
filename = join(folder, 'a.bin')

with open(filename, 'wb') as f:
assert_(_isfileobj(f))

with open(filename, 'ab') as f:
assert_(_isfileobj(f))

with open(filename, 'rb') as f:
assert_(_isfileobj(f))

assert_(_isfileobj(BufferedReader(BytesIO())) is False)
11 changes: 6 additions & 5 deletions 11 numpy/lib/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@
import warnings
from numpy.lib.utils import safe_eval
from numpy.compat import (
isfileobj, os_fspath, pickle
os_fspath, pickle
)
from numpy.compat.py3k import _isfileobj


__all__ = []
Expand Down Expand Up @@ -710,15 +711,15 @@ def write_array(fp, array, version=None, allow_pickle=True, pickle_kwargs=None):
pickle_kwargs = {}
pickle.dump(array, fp, protocol=3, **pickle_kwargs)
elif array.flags.f_contiguous and not array.flags.c_contiguous:
if isfileobj(fp):
if _isfileobj(fp):
array.T.tofile(fp)
else:
for chunk in numpy.nditer(
array, flags=['external_loop', 'buffered', 'zerosize_ok'],
buffersize=buffersize, order='F'):
fp.write(chunk.tobytes('C'))
else:
if isfileobj(fp):
if _isfileobj(fp):
array.tofile(fp)
else:
for chunk in numpy.nditer(
Expand Down Expand Up @@ -796,7 +797,7 @@ def read_array(fp, allow_pickle=False, pickle_kwargs=None, *,
"You may need to pass the encoding= option "
"to numpy.load" % (err,)) from err
else:
if isfileobj(fp):
if _isfileobj(fp):
# We can use the fast fromfile() function.
array = numpy.fromfile(fp, dtype=dtype, count=count)
else:
Expand Down Expand Up @@ -888,7 +889,7 @@ def open_memmap(filename, mode='r+', dtype=None, shape=None,
numpy.memmap

"""
if isfileobj(filename):
if _isfileobj(filename):
raise ValueError("Filename must be a string or a path-like object."
" Memmap cannot use existing file handles.")

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.