code: def python_bytes_to_unicode(
      source, encoding="utf-8", errors="strict"
  ):
      """
      Checks for unicode BOMs and PEP 263 encoding declarations. Then returns a
      unicode object like in :py:meth:`bytes.decode`.
  
      :param encoding: See :py:meth:`bytes.decode` documentation.
      :param errors: See :py:meth:`bytes.decode` documentation. ``errors`` can be
          ``'strict'``, ``'replace'`` or ``'ignore'``.
      """
  
      def detect_encoding():
          """
          For the implementation of encoding definitions in Python, look at:
          - http://www.python.org/dev/peps/pep-0263/
          - http://docs.python.org/2/reference/lexical_analysis.html#encoding-declarations
          """
          byte_mark = literal_eval(r"b'\xef\xbb\xbf'")
          if source.startswith(byte_mark):
              # UTF-8 byte-order mark
              return "utf-8"
  
          first_two_lines = re.match(
              rb"(?:[^\n]*\n){0,2}", source
          ).group(0)
          possible_encoding = re.search(
              rb"coding[=:]\s*([-\w.]+)", first_two_lines
          )
          if possible_encoding:
              return possible_encoding.group(1)
          else:
              # the default if nothing else has been set -> PEP 263
              return encoding
  
      if isinstance(source, unicode):
          # only cast str/bytes
          return source
  
      encoding = detect_encoding()
      if not isinstance(encoding, unicode):
          encoding = unicode(encoding, "utf-8", "replace")
  
      try:
          # Cast to unicode
          return unicode(source, encoding, errors)
      except LookupError:
          if errors == "replace":
              # This is a weird case that can happen if the given encoding is not
              # a valid encoding. This usually shouldn't happen with provided
              # encodings, but can happen if somebody uses encoding declarations
              # like `# coding: foo-8`.
              return unicode(source, "utf-8", errors)
          raise
  
file_path: catboost/contrib/python/parso/py2/parso/utils.py
imports: from collections import namedtuple
  import re
  import sys
  from ast import literal_eval
  from functools import total_ordering
  from parso._compatibility import unicode
label_desc: Use image node_id 2 for calling a global function with example usage: python_bytes_to_unicode(source, encoding, errors) and returns: source, unicode, str, possible_encoding, encoding, unicode