returns the declared encoding or None

    This function is a replacement for :
    >>> py_encoding = re.compile(r"coding[:=]\s*([-\w.]+)")
    >>> py_encoding.search(comment)
342656
def match_encoding_declaration(comment):
    

    # the coding line must be ascii
    try:
        comment = comment.decode('ascii')
    except UnicodeDecodeError:
        return None

    index = comment.find('coding')
    if index < 0:
        return None
    next_char = comment[index + 6]
    if next_char not in ':=':
        return None
    end_of_decl = comment[index + 7:]
    index = 0
    for char in end_of_decl:
        if char not in WHITESPACES:
            break
        index += 1
    else:
        return None
    encoding = ''
    for char in end_of_decl[index:]:
        if char in EXTENDED_ALNUMCHARS:
            encoding += char
        else:
            break
    if encoding != '':
        return encoding
    return None
851649
def encoding(self):
        
        if hasattr(self, '_encoding'):
            return self._encoding

        # content is unicode
        if isinstance(self.content, six.text_type):
            return 'unicode'

        # Try charset from content-type or content
        encoding = get_encoding(self.headers, self.content)

        # Fallback to auto-detected encoding.
        if not encoding and chardet is not None:
            encoding = chardet.detect(self.content[:600])['encoding']

        if encoding and encoding.lower() == 'gb2312':
            encoding = 'gb18030'

        self._encoding = encoding or 'utf-8'
        return self._encoding
477928
def compile(self, code):
        
        is_plain_text = True
        compiled_regex = r""
        for chunk in self.delimiter_regex().split(code):
            if is_plain_text:
                compiled_regex = compiled_regex + simex_escape(chunk, flexible_whitespace=self._flexible_whitespace)
            else:
                stripped_chunk = chunk.strip()
                if stripped_chunk in self._regexes.keys():
                    compiled_regex = u"{0}{1}".format(
                        compiled_regex,
                        self._regexes[stripped_chunk]
                    )
                else:
                    raise KeyNotFound("'{0}' not found in keys")
            is_plain_text = not is_plain_text
        if self._exact:
            compiled_regex = r"^" + compiled_regex + r"$"
        return compile(compiled_regex)
717694
def _ensure_decoded(s):
    
    if isinstance(s, np.bytes_):
        s = s.decode('UTF-8')
    return s
980663
def getunicode(self, name, default=None, encoding=None):
        '''  '''
        try:
            return self._fix(self[name], encoding)
        except (UnicodeError, KeyError):
            return default
1015261
def _get_codepage_char_map(self, encoding):
        
        # Skip things that were loaded previously
        if encoding in self.available_characters:
            return self.available_characters[encoding]
        codepage_char_list = self._get_codepage_char_list(encoding)
        codepage_char_map = dict((utf8, i + 128) for (i, utf8) in enumerate(codepage_char_list))
        self.available_characters[encoding] = codepage_char_map
        return codepage_char_map
829668
def quick_detect_encoding(string):
    
    assert isinstance(string, bytes)
    try:
        detected = cchardet.detect(string)
        if detected:
            return detected.get('encoding') or detect_encoding(string)
    except Exception as e:
        pass
    return detect_encoding(string)
129203
def _ensure_decoded(s):
    
    if isinstance(s, (np.bytes_, bytes)):
        s = s.decode(pd.get_option('display.encoding'))
    return s
102405
def _verify_encoding(self, enc):
        """"""

        enc = PYTHON_ENCODING_NAMES.get(enc, enc)
        try:
            codecs.getencoder(enc)
            encoding = enc
        except LookupError:
            encoding = None
        return encoding
472612
def unicode_compatible(cls):
    
    if PY3:
        cls.__str__ = cls.__unicode__
        cls.__bytes__ = lambda self: self.__str__().encode("utf-8")
    else:
        cls.__str__ = lambda self: self.__unicode__().encode("utf-8")

    return cls
772415
def _get_encoding(encoding_or_label):
    
    if hasattr(encoding_or_label, 'codec_info'):
        return encoding_or_label

    encoding = lookup(encoding_or_label)
    if encoding is None:
        raise LookupError('Unknown encoding label: %r' % encoding_or_label)
    return encoding
822622
def detect_encoding(data, encoding=None, fallback='latin1', is_html=False):
    '''
    '''
    if encoding:
        encoding = normalize_codec_name(encoding)

    bs4_detector = EncodingDetector(
        data,
        override_encodings=(encoding,) if encoding else (),
        is_html=is_html
    )
    candidates = itertools.chain(bs4_detector.encodings, (fallback,))

    for candidate in candidates:
        if not candidate:
            continue

        candidate = normalize_codec_name(candidate)

        if not candidate:
            continue

        if candidate == 'ascii' and fallback != 'ascii':
            # it's never ascii :)
            # Falling back on UTF-8/CP-1252/Latin-1 reduces chance of
            # failure
            continue

        if try_decoding(data, candidate):
            return candidate

    raise ValueError('Unable to detect encoding.')
815365
def _normalize_encoding(encoding):
    
    if encoding is None:
        return None
    # lower() + '_' / '-' conversion
    encoding = encoding.replace('_', '-').lower()
    if encoding == 'utf-8' or encoding.startswith('utf-8-'):
        return 'utf-8'
    for variant in ['latin-1', 'iso-latin-1', 'iso-8859-1']:
        if (encoding == variant or
            encoding.startswith(variant + '-')):
            return 'iso-8859-1'
    return encoding
650324
def b(s):
	

	return s if isinstance(s, bytes) else s.encode(locale.getpreferredencoding())
300497
def detect_encoding(value):
    """"""
    # https://tools.ietf.org/html/rfc4627#section-3
    if six.PY2:
        null_pattern = tuple(bool(ord(char)) for char in value[:4])
    else:
        null_pattern = tuple(bool(char) for char in value[:4])

    encodings = {
        # Zero is a null-byte, 1 is anything else.
        (0, 0, 0, 1): 'utf-32-be',
        (0, 1, 0, 1): 'utf-16-be',
        (1, 0, 0, 0): 'utf-32-le',
        (1, 0, 1, 0): 'utf-16-le',
    }

    return encodings.get(null_pattern, 'utf-8')
1250716
def getpreferredencoding():
    """"""

    # Borrowed from Invoke
    # (see https://github.com/pyinvoke/invoke/blob/93af29d/invoke/runners.py#L881)
    _encoding = locale.getpreferredencoding(False)
    if six.PY2 and not sys.platform == "win32":
        _default_encoding = locale.getdefaultlocale()[1]
        if _default_encoding is not None:
            _encoding = _default_encoding
    return _encoding
618828
def _is_valid_language(self):
        

        comp_str = self._encoded_value.lower()
        lang_rxc = re.compile(CPEComponentSimple._LANGTAG_PATTERN)
        return lang_rxc.match(comp_str) is not None