Ticket #13592 (confirmed Bug)

Opened 3 years ago

Last modified 3 years ago

SimpleTerm and UnicodeEncodeError

Reported by: waldopat Owned by:
Priority: minor Milestone: 4.x
Component: General Version: 4.2
Keywords: SimpleTerm, UnicodeEncodeError, Vocabulary Cc:

Description

I was creating a Form View in Plone 4.2.4 with a pull down menu with country choices:

...
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
import pycountry
...
def vocabulary_maker(l):
    vocab_list = []
    for row in l:
        entry = SimpleTerm(value=row, title=_(row))
        vocab_list.append(entry)
    return SimpleVocabulary(vocab_list)
...    
countries = vocabulary_maker([country.name for country in pycountry.countries])

country = schema.Choice(title=_(u"COUNTRY"),
                                description=_(u"Please select the countries you would like to filter."),    
                                required=False,
                                vocabulary=countries,)
...

The country names include unicode characters, such as u'\xc5land Islands'. However in zope.schema.vocabulary it tries to convert the tokens to string (Line 41):

class SimpleTerm(object):
    """Simple tokenized term used by SimpleVocabulary."""

    def __init__(self, value, token=None, title=None):
        """Create a term for value and token. If token is omitted,
        str(value) is used for the token.  If title is provided, 
        term implements ITitledTokenizedTerm.
        """
        self.value = value
        if token is None:
            token = value
        self.token = str(token)
        self.title = title
        if title is not None:
            directlyProvides(self, ITitledTokenizedTerm)

As a result, I got a Unicode Error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xc5' in position 0: ordinal not in range(128)

By changing self.token = str(token) to self.token = token, it fixed the bug.

Change History

comment:1 Changed 3 years ago by kleist

  • Status changed from new to confirmed
  • Component changed from Unknown to General
Note: See TracTickets for help on using tickets.