MCPcopy Index your code
hub / github.com/python/cpython / vfsopen

Function vfsopen

Lib/pathlib/_os.py:201–247  ·  view source on GitHub ↗

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 additionally accepts 'openable' objects, which are objects with any of these special methods: __open_reader__()

(obj, mode='r', buffering=-1, encoding=None, errors=None,
            newline=None)

Source from the content-addressed store, hash-verified

199
200
201def vfsopen(obj, mode='r', buffering=-1, encoding=None, errors=None,
202 newline=None):
203 """
204 Open the file pointed to by this path and return a file object, as
205 the built-in open() function does.
206
207 Unlike the built-in open() function, this function additionally accepts
208 'openable' objects, which are objects with any of these special methods:
209
210 __open_reader__()
211 __open_writer__(mode)
212 __open_updater__(mode)
213
214 '__open_reader__' is called for 'r' mode; '__open_writer__' for 'a', 'w'
215 and 'x' modes; and '__open_updater__' for 'r+' and 'w+' modes. If text
216 mode is requested, the result is wrapped in an io.TextIOWrapper object.
217 """
218 if buffering != -1:
219 raise ValueError("buffer size can't be customized")
220 text = 'b' not in mode
221 if text:
222 # Call io.text_encoding() here to ensure any warning is raised at an
223 # appropriate stack level.
224 encoding = text_encoding(encoding)
225 try:
226 return open(obj, mode, buffering, encoding, errors, newline)
227 except TypeError:
228 pass
229 if not text:
230 if encoding is not None:
231 raise ValueError("binary mode doesn't take an encoding argument")
232 if errors is not None:
233 raise ValueError("binary mode doesn't take an errors argument")
234 if newline is not None:
235 raise ValueError("binary mode doesn't take a newline argument")
236 mode = ''.join(sorted(c for c in mode if c not in 'bt'))
237 if mode == 'r':
238 stream = _open_reader(obj)
239 elif mode in ('a', 'w', 'x'):
240 stream = _open_writer(obj, mode)
241 elif mode in ('+r', '+w'):
242 stream = _open_updater(obj, mode[1])
243 else:
244 raise ValueError(f'invalid mode: {mode}')
245 if text:
246 stream = TextIOWrapper(stream, encoding, errors, newline)
247 return stream
248
249
250def vfspath(obj):

Callers 12

_copy_from_fileMethod · 0.90
read_bytesMethod · 0.90
read_textMethod · 0.90
write_bytesMethod · 0.90
write_textMethod · 0.90
_copy_fromMethod · 0.90
test_open_rMethod · 0.90
test_open_rbMethod · 0.90
test_open_wMethod · 0.90
test_open_wbMethod · 0.90

Calls 7

text_encodingFunction · 0.90
TextIOWrapperClass · 0.90
_open_readerFunction · 0.85
_open_writerFunction · 0.85
_open_updaterFunction · 0.85
openFunction · 0.50
joinMethod · 0.45

Tested by 6

test_open_rMethod · 0.72
test_open_rbMethod · 0.72
test_open_wMethod · 0.72
test_open_wbMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…