def ensure_bytes(s):
 if isinstance(s, bytes):
  return s
 if hasattr(s, 'encode'):
  return s.encode()
 msg = 'Object %s is neither a bytes object nor has an encode method'
 raise TypeError((msg % s))

def ensure_bytes ( s ) : if isinstance ( s , bytes ) : return s if hasattr ( s , 'encode' ) : return s . encode ( ) msg = 'Object %s is neither a bytes object nor has an encode method' raise TypeError ( ( msg % s ) )
turn string or bytes to bytes .