code: def unicode_to_str(text, encoding=None, errors='strict'):
      
  
      if encoding is None:
          encoding = 'utf-8'
      if isinstance(text, unicode):
          return text.encode(encoding, errors)
      elif isinstance(text, str):
          return text
      else:
          raise TypeError('unicode_to_str must receive a unicode or str object, got %s' % type(text).__name__)
docs: Return the str representation of text in the given encoding. Unlike
      .encode(encoding) this function can be applied directly to a str
      object without the risk of double-decoding problems (which can happen if
      you don't use the default 'ascii' encoding)
  
      This function has been copied from here:
      https://github.com/scrapy/scrapy/blob/master/scrapy/utils/python.py