def generate_attrib(attrib, encoding):
    """Generate unicode string from attribute."""
    if attrib is None:
        return None
    elif isinstance(attrib, basestring):
        return to_unicode(attrib, encoding)
    elif isinstance(attrib, ElementStream):
        text = []
        for ev, item in attrib:
            if ev == TEXT:
                text.append(to_unicode(item, encoding))
            else:
                raise TemplateAttrsError
        if text:
            return ''.join(text)
        else:
            return None
    elif hasattr(attrib, '__iter__'):
        # if we get any other iterable, join the strings together:
        text = []
        for item in attrib:
            if item is not None:
                item = generate_attrib(item, encoding)
                if item is not None:
                    text.append(item)
        if text:
            return ''.join(text)
        else:
            return None
    else:
        return to_unicode(attrib, encoding)
Exemple #2
0
def test_to_unicode():
    assert to_unicode(ustr, 'utf-8') == ustr
    assert to_unicode(astr, 'utf-8') == ustr

    class C(object):
        def __unicode__(self):
            return ustr

    assert to_unicode(C(), 'utf-8') == ustr
def test_to_unicode():
    assert to_unicode(ustr, 'utf-8') == ustr
    assert to_unicode(astr, 'utf-8') == ustr

    class C(object):
        def __unicode__(self):
            return ustr

    assert to_unicode(C(), 'utf-8') == ustr