| 399 | |
| 400 | |
| 401 | def hook_compressed(filename, mode, *, encoding=None, errors=None): |
| 402 | if encoding is None and "b" not in mode: # EncodingWarning is emitted in FileInput() already. |
| 403 | encoding = "locale" |
| 404 | ext = os.path.splitext(filename)[1] |
| 405 | if ext == '.gz': |
| 406 | import gzip |
| 407 | stream = gzip.open(filename, mode) |
| 408 | elif ext == '.bz2': |
| 409 | import bz2 |
| 410 | stream = bz2.BZ2File(filename, mode) |
| 411 | else: |
| 412 | return open(filename, mode, encoding=encoding, errors=errors) |
| 413 | |
| 414 | # gzip and bz2 are binary mode by default. |
| 415 | if "b" not in mode: |
| 416 | stream = io.TextIOWrapper(stream, encoding=encoding, errors=errors) |
| 417 | return stream |
| 418 | |
| 419 | |
| 420 | def hook_encoded(encoding, errors=None): |