Skip to content

Commit eb68d78

Browse files
committed
Sync from upstream
1 parent 0f1f154 commit eb68d78

File tree

6 files changed

+30
-28
lines changed

6 files changed

+30
-28
lines changed

CHANGES.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Version History
44
Unreleased
55
----------
66

7-
- Nothing yet
7+
- Make ``vfsopen()`` try to call ``open()``, like it did before 0.5.0.
88

99
v0.5.0
1010
------

docs/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This package offers the following functions:
2020
encoding=None, errors=None, newline=None)
2121

2222
Open the given object and return a file object. Unlike the built-in
23-
``open()`` function, this function calls the object's
23+
``open()`` function, this function additionally calls the object's
2424
:meth:`~ReadablePath.__open_reader__`,
2525
:meth:`~WritablePath.__open_writer__` or :meth:`!__open_updater__` method,
2626
as appropriate for the given mode.

pathlib_abc/_glob.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,6 @@ def _glob0(dirname, basename, dir_fd, dironly, include_hidden=False):
122122
return [basename]
123123
return []
124124

125-
_deprecated_function_message = (
126-
"{name} is deprecated and will be removed in Python {remove}. Use "
127-
"glob.glob and pass a directory to its root_dir argument instead."
128-
)
129-
130-
def glob0(dirname, pattern):
131-
import warnings
132-
warnings._deprecated("glob.glob0", _deprecated_function_message, remove=(3, 15))
133-
return _glob0(dirname, pattern, None, False)
134-
135-
def glob1(dirname, pattern):
136-
import warnings
137-
warnings._deprecated("glob.glob1", _deprecated_function_message, remove=(3, 15))
138-
return _glob1(dirname, pattern, None, False)
139-
140125
# This helper function recursively yields relative pathnames inside a literal
141126
# directory.
142127

pathlib_abc/_os.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ def vfsopen(obj, mode='r', buffering=-1, encoding=None, errors=None,
210210
Open the file pointed to by this path and return a file object, as
211211
the built-in open() function does.
212212
213-
Unlike the built-in open() function, this function accepts 'openable'
214-
objects, which are objects with any of these special methods:
213+
Unlike the built-in open() function, this function additionally accepts
214+
'openable' objects, which are objects with any of these special methods:
215215
216216
__open_reader__()
217217
__open_writer__(mode)
@@ -222,18 +222,23 @@ def vfsopen(obj, mode='r', buffering=-1, encoding=None, errors=None,
222222
mode is requested, the result is wrapped in an io.TextIOWrapper object.
223223
"""
224224
text = 'b' not in mode
225-
if buffering != -1:
226-
raise ValueError("buffer size can't be customized")
227-
elif text:
225+
if text:
228226
# Call io.text_encoding() here to ensure any warning is raised at an
229227
# appropriate stack level.
230228
encoding = text_encoding(encoding)
231-
elif encoding is not None:
232-
raise ValueError("binary mode doesn't take an encoding argument")
233-
elif errors is not None:
234-
raise ValueError("binary mode doesn't take an errors argument")
235-
elif newline is not None:
236-
raise ValueError("binary mode doesn't take a newline argument")
229+
try:
230+
return open(obj, mode, buffering, encoding, errors, newline)
231+
except TypeError:
232+
pass
233+
if buffering != -1:
234+
raise ValueError("buffer size can't be customized")
235+
if not text:
236+
if encoding is not None:
237+
raise ValueError("binary mode doesn't take an encoding argument")
238+
if errors is not None:
239+
raise ValueError("binary mode doesn't take an errors argument")
240+
if newline is not None:
241+
raise ValueError("binary mode doesn't take a newline argument")
237242
mode = ''.join(sorted(c for c in mode if c not in 'bt'))
238243
if mode == 'r':
239244
stream = _open_reader(obj)

tests/test_read.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ def test_open_r(self):
3636
self.assertIsInstance(f, io.TextIOBase)
3737
self.assertEqual(f.read(), 'this is file A\n')
3838

39+
def test_open_r_buffering_error(self):
40+
p = self.root / 'fileA'
41+
self.assertRaises(ValueError, vfsopen, p, 'r', buffering=0)
42+
self.assertRaises(ValueError, vfsopen, p, 'r', buffering=1)
43+
self.assertRaises(ValueError, vfsopen, p, 'r', buffering=1024)
44+
3945
@unittest.skipIf(
4046
not getattr(sys.flags, 'warn_default_encoding', 0),
4147
"Requires warn_default_encoding",

tests/test_write.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ def test_open_w(self):
3636
f.write('this is file A\n')
3737
self.assertEqual(self.ground.readtext(p), 'this is file A\n')
3838

39+
def test_open_w_buffering_error(self):
40+
p = self.root / 'fileA'
41+
self.assertRaises(ValueError, vfsopen, p, 'w', buffering=0)
42+
self.assertRaises(ValueError, vfsopen, p, 'w', buffering=1)
43+
self.assertRaises(ValueError, vfsopen, p, 'w', buffering=1024)
44+
3945
@unittest.skipIf(
4046
not getattr(sys.flags, 'warn_default_encoding', 0),
4147
"Requires warn_default_encoding",

0 commit comments

Comments
 (0)