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

Commit 512bd17

Browse filesBrowse files
authored
Merge pull request #23739 from LoveEatCandy/1.24.x/check_local_file
BUG: fix the method for checking local files for 1.24.x
2 parents 87921fe + bc442be commit 512bd17
Copy full SHA for 512bd17

File tree

Expand file treeCollapse file tree

3 files changed

+34
-6
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+34
-6
lines changed

‎numpy/compat/py3k.py

Copy file name to clipboardExpand all lines: numpy/compat/py3k.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ def asstr(s):
4949
def isfileobj(f):
5050
return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))
5151

52+
def _isfileobj(f):
53+
if not isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter)):
54+
return False
55+
try:
56+
# BufferedReader/Writer may raise OSError when
57+
# fetching `fileno()` (e.g. when wrapping BytesIO).
58+
f.fileno()
59+
return True
60+
except OSError:
61+
return False
62+
5263
def open_latin1(filename, mode='r'):
5364
return open(filename, mode=mode, encoding='iso-8859-1')
5465

‎numpy/compat/tests/test_compat.py

Copy file name to clipboardExpand all lines: numpy/compat/tests/test_compat.py
+17-1Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from os.path import join
2+
from io import BufferedReader, BytesIO
23

3-
from numpy.compat import isfileobj
4+
from numpy.compat.py3k import isfileobj, _isfileobj
45
from numpy.testing import assert_
56
from numpy.testing import tempdir
67

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

1819
with open(filename, 'rb') as f:
1920
assert_(isfileobj(f))
21+
22+
def test__isfileobj():
23+
with tempdir(prefix="numpy_test_compat_") as folder:
24+
filename = join(folder, 'a.bin')
25+
26+
with open(filename, 'wb') as f:
27+
assert_(_isfileobj(f))
28+
29+
with open(filename, 'ab') as f:
30+
assert_(_isfileobj(f))
31+
32+
with open(filename, 'rb') as f:
33+
assert_(_isfileobj(f))
34+
35+
assert_(_isfileobj(BufferedReader(BytesIO())) is False)

‎numpy/lib/format.py

Copy file name to clipboardExpand all lines: numpy/lib/format.py
+6-5Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@
165165
import warnings
166166
from numpy.lib.utils import safe_eval
167167
from numpy.compat import (
168-
isfileobj, os_fspath, pickle
168+
os_fspath, pickle
169169
)
170+
from numpy.compat.py3k import _isfileobj
170171

171172

172173
__all__ = []
@@ -710,15 +711,15 @@ def write_array(fp, array, version=None, allow_pickle=True, pickle_kwargs=None):
710711
pickle_kwargs = {}
711712
pickle.dump(array, fp, protocol=3, **pickle_kwargs)
712713
elif array.flags.f_contiguous and not array.flags.c_contiguous:
713-
if isfileobj(fp):
714+
if _isfileobj(fp):
714715
array.T.tofile(fp)
715716
else:
716717
for chunk in numpy.nditer(
717718
array, flags=['external_loop', 'buffered', 'zerosize_ok'],
718719
buffersize=buffersize, order='F'):
719720
fp.write(chunk.tobytes('C'))
720721
else:
721-
if isfileobj(fp):
722+
if _isfileobj(fp):
722723
array.tofile(fp)
723724
else:
724725
for chunk in numpy.nditer(
@@ -796,7 +797,7 @@ def read_array(fp, allow_pickle=False, pickle_kwargs=None, *,
796797
"You may need to pass the encoding= option "
797798
"to numpy.load" % (err,)) from err
798799
else:
799-
if isfileobj(fp):
800+
if _isfileobj(fp):
800801
# We can use the fast fromfile() function.
801802
array = numpy.fromfile(fp, dtype=dtype, count=count)
802803
else:
@@ -888,7 +889,7 @@ def open_memmap(filename, mode='r+', dtype=None, shape=None,
888889
numpy.memmap
889890
890891
"""
891-
if isfileobj(filename):
892+
if _isfileobj(filename):
892893
raise ValueError("Filename must be a string or a path-like object."
893894
" Memmap cannot use existing file handles.")
894895

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.