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)
| 199 | |
| 200 | |
| 201 | def 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 | |
| 250 | def vfspath(obj): |
searching dependent graphs…