six u() breaks for existing unicode strings for Python 2

Issue #45 new
James Cleveland
created an issue


import six; six.u(u'bawbag')


TypeError: decoding Unicode is not supported

I would suggest adding this in the u(s) function:

    if isinstance(s, unicode):
        return s

So that it behaves like the unicode() function, IE: liberal in what you accept, conservative in what you produce.

Comments (13)

  1. James Cleveland reporter

    If you're using third party libraries, it's kind of a pain due to having to fork them and whatnot. Would this change actually break anything?

    It's kind of interesting that this works fine:


    Whereas this breaks:

    unicode(u'hi', 'unicode_escape')

    Any ideas why?

  2. Vladimir Protasov

    @Benjamin Peterson, It's common thing in Python 2 that function result in some conditions is str and on the other is unicode instance, so the call

    result = six.u(json.dumps(data, **json_params))

    may fail with

    TypeError: decoding Unicode is not supported

    if someone will pass ensure_ascii=False in json_params.

  3. bfschott

    I'm hitting this is a third party library:

            return six.u('')

    Just realized, of course, that the file is utf-8:

    # -*- coding: utf-8 -*-

    Not exactly sure what the proper portable way is to return a unicode literal in a return is with python. I can't do u'', just ''?

  4. Log in to comment