Path: blob/master/venv/Lib/site-packages/pip/_internal/utils/encoding.py
811 views
# The following comment should be removed at some point in the future.1# mypy: strict-optional=False23import codecs4import locale5import re6import sys78from pip._internal.utils.typing import MYPY_CHECK_RUNNING910if MYPY_CHECK_RUNNING:11from typing import List, Tuple, Text1213BOMS = [14(codecs.BOM_UTF8, 'utf-8'),15(codecs.BOM_UTF16, 'utf-16'),16(codecs.BOM_UTF16_BE, 'utf-16-be'),17(codecs.BOM_UTF16_LE, 'utf-16-le'),18(codecs.BOM_UTF32, 'utf-32'),19(codecs.BOM_UTF32_BE, 'utf-32-be'),20(codecs.BOM_UTF32_LE, 'utf-32-le'),21] # type: List[Tuple[bytes, Text]]2223ENCODING_RE = re.compile(br'coding[:=]\s*([-\w.]+)')242526def auto_decode(data):27# type: (bytes) -> Text28"""Check a bytes string for a BOM to correctly detect the encoding2930Fallback to locale.getpreferredencoding(False) like open() on Python3"""31for bom, encoding in BOMS:32if data.startswith(bom):33return data[len(bom):].decode(encoding)34# Lets check the first two lines as in PEP26335for line in data.split(b'\n')[:2]:36if line[0:1] == b'#' and ENCODING_RE.search(line):37encoding = ENCODING_RE.search(line).groups()[0].decode('ascii')38return data.decode(encoding)39return data.decode(40locale.getpreferredencoding(False) or sys.getdefaultencoding(),41)424344