code: def coerce_to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
      
      PY2 = sys.version_info[0] == 2
      if PY2: # pragma: nocover
          if x is None:
              return None
          if isinstance(x, (bytes, bytearray, buffer)):
              return bytes(x)
          if isinstance(x, unicode):
              return x.encode(charset, errors)
          raise TypeError('Cannot coerce to bytes')
  
      else: # pragma: nocover
          if x is None:
              return None
          if isinstance(x, (bytes, bytearray, memoryview)):
              return bytes(x)
          if isinstance(x, str):
              return x.encode(charset, errors)
          raise TypeError('Cannot coerce to bytes')
docs: Coerce value to bytes
  
      >>> a = coerce_to_bytes('hello')
      >>> assert isinstance(a, bytes)
      >>> a = coerce_to_bytes(b'hello')
      >>> assert isinstance(a, bytes)
      >>> a = coerce_to_bytes(None)
      >>> assert a is None
      >>> coerce_to_bytes(object())
      Traceback (most recent call last):
      ...
      TypeError: Cannot coerce to bytes