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
If Python 2.x, return decorated function encoding unicode return value
dataset/ETHPy150Open jmcarp/robobrowser/robobrowser/compat.py/encode_if_py2