Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
GH-128520: pathlib ABCs: improve protocol for 'openable' objects#134101
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -166,48 +166,78 @@ def copyfileobj(source_f, target_f): | ||||||||||||||||||||||||||
write_target(buf) | ||||||||||||||||||||||||||
def _open_reader(obj): | ||||||||||||||||||||||||||
cls = type(obj) | ||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||
return cls.__open_reader__(obj) | ||||||||||||||||||||||||||
except AttributeError: | ||||||||||||||||||||||||||
if hasattr(cls, '__open_reader__'): | ||||||||||||||||||||||||||
raise | ||||||||||||||||||||||||||
raise TypeError(f"{cls.__name__} can't be opened for reading") | ||||||||||||||||||||||||||
Comment on lines +171 to +176 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
Suggested change
| ||||||||||||||||||||||||||
def _open_writer(obj, mode): | ||||||||||||||||||||||||||
cls = type(obj) | ||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||
return cls.__open_writer__(obj, mode) | ||||||||||||||||||||||||||
except AttributeError: | ||||||||||||||||||||||||||
if hasattr(cls, '__open_writer__'): | ||||||||||||||||||||||||||
raise | ||||||||||||||||||||||||||
raise TypeError(f"{cls.__name__} can't be opened for writing") | ||||||||||||||||||||||||||
def _open_updater(obj, mode): | ||||||||||||||||||||||||||
cls = type(obj) | ||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||
return cls.__open_updater__(obj, mode) | ||||||||||||||||||||||||||
except AttributeError: | ||||||||||||||||||||||||||
if hasattr(cls, '__open_updater__'): | ||||||||||||||||||||||||||
raise | ||||||||||||||||||||||||||
raise TypeError(f"{cls.__name__} can't be opened for updating") | ||||||||||||||||||||||||||
def vfsopen(obj, mode='r', buffering=-1, encoding=None, errors=None, | ||||||||||||||||||||||||||
newline=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 magic methods: | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
Seethe glossary. | ||||||||||||||||||||||||||
__open_reader__() | ||||||||||||||||||||||||||
__open_writer__(mode) | ||||||||||||||||||||||||||
__open_updater__(mode) | ||||||||||||||||||||||||||
'__open_reader__' is called for 'r' mode; '__open_writer__' for 'a', 'w' | ||||||||||||||||||||||||||
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: | ||||||||||||||||||||||||||
# 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") | ||||||||||||||||||||||||||
mode = ''.join(sorted(c for c in mode if c not in 'bt')) | ||||||||||||||||||||||||||
if mode == 'r': | ||||||||||||||||||||||||||
stream = _open_reader(obj) | ||||||||||||||||||||||||||
elif mode in ('a', 'w', 'x'): | ||||||||||||||||||||||||||
stream = _open_writer(obj, mode) | ||||||||||||||||||||||||||
elif mode in ('+r', '+w'): | ||||||||||||||||||||||||||
stream = _open_updater(obj, mode[1]) | ||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||
raise ValueError(f'invalid mode: {mode}') | ||||||||||||||||||||||||||
if text: | ||||||||||||||||||||||||||
stream = TextIOWrapper(stream, encoding, errors, newline) | ||||||||||||||||||||||||||
return stream | ||||||||||||||||||||||||||
def vfspath(path): | ||||||||||||||||||||||||||
Uh oh!
There was an error while loading.Please reload this page.