@classmethod
def encode_source(cls, source):
    """Encode source from unicode.

        :param source: source string (may already be encoded).

        If the ``source`` string contains a coding specification
        within the first two lines, the specified coding is used,
        otherwise, ``UTF-8`` is applied.
        """
    if not source:
        return ''.encode('utf-8')
    if isinstance(source, cls.uc_type) and hasattr(source, 'encode'):
        source = source.encode(cls.check_coding(source))
    return source
