Open an encoded file using the given mode and return a wrapped version providing transparent encoding/decoding. Note: The wrapped version will only accept the object format defined by the codecs, i.e. Unicode objects for most builtin codecs. Output is also code
(filename, mode='r', encoding=None, errors='strict', buffering=-1)
| 881 | ### Shortcuts |
| 882 | |
| 883 | def open(filename, mode='r', encoding=None, errors='strict', buffering=-1): |
| 884 | |
| 885 | """ Open an encoded file using the given mode and return |
| 886 | a wrapped version providing transparent encoding/decoding. |
| 887 | |
| 888 | Note: The wrapped version will only accept the object format |
| 889 | defined by the codecs, i.e. Unicode objects for most builtin |
| 890 | codecs. Output is also codec dependent and will usually be |
| 891 | Unicode as well. |
| 892 | |
| 893 | If encoding is not None, then the |
| 894 | underlying encoded files are always opened in binary mode. |
| 895 | The default file mode is 'r', meaning to open the file in read mode. |
| 896 | |
| 897 | encoding specifies the encoding which is to be used for the |
| 898 | file. |
| 899 | |
| 900 | errors may be given to define the error handling. It defaults |
| 901 | to 'strict' which causes ValueErrors to be raised in case an |
| 902 | encoding error occurs. |
| 903 | |
| 904 | buffering has the same meaning as for the builtin open() API. |
| 905 | It defaults to -1 which means that the default buffer size will |
| 906 | be used. |
| 907 | |
| 908 | The returned wrapped file object provides an extra attribute |
| 909 | .encoding which allows querying the used encoding. This |
| 910 | attribute is only available if an encoding was specified as |
| 911 | parameter. |
| 912 | |
| 913 | """ |
| 914 | if encoding is not None and \ |
| 915 | 'b' not in mode: |
| 916 | # Force opening of the file in binary mode |
| 917 | mode = mode + 'b' |
| 918 | file = builtins.open(filename, mode, buffering) |
| 919 | if encoding is None: |
| 920 | return file |
| 921 | |
| 922 | try: |
| 923 | info = lookup(encoding) |
| 924 | srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors) |
| 925 | # Add attributes to simplify introspection |
| 926 | srw.encoding = encoding |
| 927 | return srw |
| 928 | except: |
| 929 | file.close() |
| 930 | raise |
| 931 | |
| 932 | def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'): |
| 933 |
nothing calls this directly
no test coverage detected