code: def encode_if_py2(func):
      
      if not PY2:
          return func
      def wrapped(*args, **kwargs):
          ret = func(*args, **kwargs)
          if not isinstance(ret, unicode):
              raise TypeError('Wrapped function must return `unicode`')
          return ret.encode('utf-8', 'ignore')
      return wrapped
docs: If Python 2.x, return decorated function encoding unicode return value
      to UTF-8; else noop.