Skip to content
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
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Version History
Unreleased
----------

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

v0.5.0
------
Expand Down
2 changes: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This package offers the following functions:
encoding=None, errors=None, newline=None)

Open the given object and return a file object. Unlike the built-in
``open()`` function, this function calls the object's
``open()`` function, this function additionally calls the object's
:meth:`~ReadablePath.__open_reader__`,
:meth:`~WritablePath.__open_writer__` or :meth:`!__open_updater__` method,
as appropriate for the given mode.
Expand Down
15 changes: 0 additions & 15 deletions pathlib_abc/_glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,6 @@ def _glob0(dirname, basename, dir_fd, dironly, include_hidden=False):
return [basename]
return []

_deprecated_function_message = (
"{name} is deprecated and will be removed in Python {remove}. Use "
"glob.glob and pass a directory to its root_dir argument instead."
)

def glob0(dirname, pattern):
import warnings
warnings._deprecated("glob.glob0", _deprecated_function_message, remove=(3, 15))
return _glob0(dirname, pattern, None, False)

def glob1(dirname, pattern):
import warnings
warnings._deprecated("glob.glob1", _deprecated_function_message, remove=(3, 15))
return _glob1(dirname, pattern, None, False)

# This helper function recursively yields relative pathnames inside a literal
# directory.

Expand Down
25 changes: 15 additions & 10 deletions pathlib_abc/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def vfsopen(obj, mode='r', buffering=-1, encoding=None, errors=None,
Open the file pointed to by this path and return a file object, as
the built-in open() function does.

Unlike the built-in open() function, this function accepts 'openable'
objects, which are objects with any of these special methods:
Unlike the built-in open() function, this function additionally accepts
'openable' objects, which are objects with any of these special methods:

__open_reader__()
__open_writer__(mode)
Expand All @@ -221,19 +221,24 @@ def vfsopen(obj, mode='r', buffering=-1, encoding=None, errors=None,
and 'x' modes; and '__open_updater__' for 'r+' and 'w+' modes. If text
mode is requested, the result is wrapped in an io.TextIOWrapper object.
"""
text = 'b' not in mode
if buffering != -1:
raise ValueError("buffer size can't be customized")
elif text:
text = 'b' not in mode
if text:
# Call io.text_encoding() here to ensure any warning is raised at an
# appropriate stack level.
encoding = text_encoding(encoding)
elif encoding is not None:
raise ValueError("binary mode doesn't take an encoding argument")
elif errors is not None:
raise ValueError("binary mode doesn't take an errors argument")
elif newline is not None:
raise ValueError("binary mode doesn't take a newline argument")
try:
return open(obj, mode, buffering, encoding, errors, newline)
except TypeError:
pass
if not text:
if encoding is not None:
raise ValueError("binary mode doesn't take an encoding argument")
if errors is not None:
raise ValueError("binary mode doesn't take an errors argument")
if newline is not None:
raise ValueError("binary mode doesn't take a newline argument")
mode = ''.join(sorted(c for c in mode if c not in 'bt'))
if mode == 'r':
stream = _open_reader(obj)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def test_open_r(self):
self.assertIsInstance(f, io.TextIOBase)
self.assertEqual(f.read(), 'this is file A\n')

def test_open_r_buffering_error(self):
p = self.root / 'fileA'
self.assertRaises(ValueError, vfsopen, p, 'r', buffering=0)
self.assertRaises(ValueError, vfsopen, p, 'r', buffering=1)
self.assertRaises(ValueError, vfsopen, p, 'r', buffering=1024)

@unittest.skipIf(
not getattr(sys.flags, 'warn_default_encoding', 0),
"Requires warn_default_encoding",
Expand Down
6 changes: 6 additions & 0 deletions tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def test_open_w(self):
f.write('this is file A\n')
self.assertEqual(self.ground.readtext(p), 'this is file A\n')

def test_open_w_buffering_error(self):
p = self.root / 'fileA'
self.assertRaises(ValueError, vfsopen, p, 'w', buffering=0)
self.assertRaises(ValueError, vfsopen, p, 'w', buffering=1)
self.assertRaises(ValueError, vfsopen, p, 'w', buffering=1024)

@unittest.skipIf(
not getattr(sys.flags, 'warn_default_encoding', 0),
"Requires warn_default_encoding",
Expand Down