Skip to content

Commit 452e67b

Browse files
committed
Restore call to built-in open()
1 parent c128965 commit 452e67b

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

Lib/pathlib/__init__.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -754,27 +754,6 @@ def samefile(self, other_path):
754754
return (st.st_ino == other_st.st_ino and
755755
st.st_dev == other_st.st_dev)
756756

757-
def __open_reader__(self):
758-
"""
759-
Open the file pointed to by this path for reading in binary mode and
760-
return a file object.
761-
"""
762-
return io.open(self, 'rb')
763-
764-
def __open_writer__(self, mode):
765-
"""
766-
Open the file pointed to by this path for writing in binary mode and
767-
return a file object.
768-
"""
769-
return io.open(self, f'{mode}b')
770-
771-
def __open_updater__(self, mode):
772-
"""
773-
Open the file pointed to by this path for updating in binary mode and
774-
return a file object.
775-
"""
776-
return io.open(self, f'{mode}+b')
777-
778757
def open(self, mode='r', buffering=-1, encoding=None,
779758
errors=None, newline=None):
780759
"""

Lib/pathlib/_os.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ def vfsopen(obj, mode='r', buffering=-1, encoding=None, errors=None,
205205
Open the file pointed to by this path and return a file object, as
206206
the built-in open() function does.
207207
208-
Unlike the built-in open() function, this function accepts 'openable'
209-
objects, which are objects with any of these special methods:
208+
Unlike the built-in open() function, this function additionally accepts
209+
'openable' objects, which are objects with any of these special methods:
210210
211211
__open_reader__()
212212
__open_writer__(mode)
@@ -216,19 +216,24 @@ def vfsopen(obj, mode='r', buffering=-1, encoding=None, errors=None,
216216
and 'x' modes; and '__open_updater__' for 'r+' and 'w+' modes. If text
217217
mode is requested, the result is wrapped in an io.TextIOWrapper object.
218218
"""
219-
text = 'b' not in mode
220219
if buffering != -1:
221220
raise ValueError("buffer size can't be customized")
221+
text = 'b' not in mode
222222
if text:
223223
# Call io.text_encoding() here to ensure any warning is raised at an
224224
# appropriate stack level.
225225
encoding = text_encoding(encoding)
226-
elif encoding is not None:
227-
raise ValueError("binary mode doesn't take an encoding argument")
228-
elif errors is not None:
229-
raise ValueError("binary mode doesn't take an errors argument")
230-
elif newline is not None:
231-
raise ValueError("binary mode doesn't take a newline argument")
226+
try:
227+
return open(obj, mode, buffering, encoding, errors, newline)
228+
except TypeError:
229+
pass
230+
if not text:
231+
if encoding is not None:
232+
raise ValueError("binary mode doesn't take an encoding argument")
233+
if errors is not None:
234+
raise ValueError("binary mode doesn't take an errors argument")
235+
if newline is not None:
236+
raise ValueError("binary mode doesn't take a newline argument")
232237
mode = ''.join(sorted(c for c in mode if c not in 'bt'))
233238
if mode == 'r':
234239
stream = _open_reader(obj)

0 commit comments

Comments
 (0)