Return a wrapped version of file which provides transparent encoding translation. Data written to the wrapped file is decoded according to the given data_encoding and then encoded to the underlying file using file_encoding. The intermediate data type will us
(file, data_encoding, file_encoding=None, errors='strict')
| 935 | raise |
| 936 | |
| 937 | def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'): |
| 938 | |
| 939 | """ Return a wrapped version of file which provides transparent |
| 940 | encoding translation. |
| 941 | |
| 942 | Data written to the wrapped file is decoded according |
| 943 | to the given data_encoding and then encoded to the underlying |
| 944 | file using file_encoding. The intermediate data type |
| 945 | will usually be Unicode but depends on the specified codecs. |
| 946 | |
| 947 | Bytes read from the file are decoded using file_encoding and then |
| 948 | passed back to the caller encoded using data_encoding. |
| 949 | |
| 950 | If file_encoding is not given, it defaults to data_encoding. |
| 951 | |
| 952 | errors may be given to define the error handling. It defaults |
| 953 | to 'strict' which causes ValueErrors to be raised in case an |
| 954 | encoding error occurs. |
| 955 | |
| 956 | The returned wrapped file object provides two extra attributes |
| 957 | .data_encoding and .file_encoding which reflect the given |
| 958 | parameters of the same name. The attributes can be used for |
| 959 | introspection by Python programs. |
| 960 | |
| 961 | """ |
| 962 | if file_encoding is None: |
| 963 | file_encoding = data_encoding |
| 964 | data_info = lookup(data_encoding) |
| 965 | file_info = lookup(file_encoding) |
| 966 | sr = StreamRecoder(file, data_info.encode, data_info.decode, |
| 967 | file_info.streamreader, file_info.streamwriter, errors) |
| 968 | # Add attributes to simplify introspection |
| 969 | sr.data_encoding = data_encoding |
| 970 | sr.file_encoding = file_encoding |
| 971 | return sr |
| 972 | |
| 973 | ### Helpers for codec lookup |
| 974 |
nothing calls this directly
no test coverage detected
searching dependent graphs…