code: def to_bytes(value):
      
      vtype = type(value)
  
      if vtype == bytes or vtype == type(None):
          return value
  
      try:
          return vtype.encode(value)
      except UnicodeEncodeError:
          pass
      return value
docs: str to bytes (py3k)