

def source_encoding(source):
    'Determine the encoding for `source` (a string), according to PEP 263.\n\n    Returns a string, the name of the encoding.\n\n    '
    assert (sys.version_info < (3, 0))
    cookie_re = re.compile('coding[:=]\\s*([-\\w.]+)')
    readline = iter(source.splitlines(True)).next

    def _get_normal_name(orig_enc):
        'Imitates get_normal_name in tokenizer.c.'
        enc = orig_enc[:12].lower().replace('_', '-')
        if re.match('^utf-8($|-)', enc):
            return 'utf-8'
        if re.match('^(latin-1|iso-8859-1|iso-latin-1)($|-)', enc):
            return 'iso-8859-1'
        return orig_enc
    if (sys.version_info <= (2, 4)):
        default = 'iso-8859-1'
    else:
        default = 'ascii'
    bom_found = False
    encoding = None

    def read_or_stop():
        "Get the next source line, or ''."
        try:
            return readline()
        except StopIteration:
            return ''

    def find_cookie(line):
        'Find an encoding cookie in `line`.'
        try:
            line_string = line.decode('ascii')
        except UnicodeDecodeError:
            return None
        matches = cookie_re.findall(line_string)
        if (not matches):
            return None
        encoding = _get_normal_name(matches[0])
        try:
            codec = codecs.lookup(encoding)
        except LookupError:
            raise SyntaxError(('unknown encoding: ' + encoding))
        if bom_found:
            codec_name = getattr(codec, 'name', encoding)
            if (codec_name != 'utf-8'):
                raise SyntaxError('encoding problem: utf-8')
            encoding += '-sig'
        return encoding
    first = read_or_stop()
    if first.startswith(codecs.BOM_UTF8):
        bom_found = True
        first = first[3:]
        default = 'utf-8-sig'
    if (not first):
        return default
    encoding = find_cookie(first)
    if encoding:
        return encoding
    second = read_or_stop()
    if (not second):
        return default
    encoding = find_cookie(second)
    if encoding:
        return encoding
    return default

dataset/ETHPy150Open RoseOu/flasky/venv/lib/python2.7/site-packages/coverage/phystokens.py source_encoding/original