def maybe_read_encoded_stream(reader, encoding=None, compression=None): """read an encoded stream from the reader and transform the bytes to unicode if required based on the encoding Parameters ---------- reader : a streamable file-like object encoding : optional, the encoding to attempt to read Returns ------- a tuple of (a stream of decoded bytes, the encoding which was used) """ if compat.PY3 or encoding is not None: # pragma: no cover if encoding: errors = 'strict' else: errors = 'replace' encoding = 'utf-8' if compression == 'gzip': reader = BytesIO(reader.read()) else: reader = StringIO(reader.read().decode(encoding, errors)) else: if compression == 'gzip': reader = BytesIO(reader.read()) encoding = None return reader, encoding