Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #498 (closed defect: fixed)

Opened 13 years ago

Last modified 12 years ago

DataGrid doesn't work with Unicode strings

Reported by: Jorge Godoy <jgodoy@…> Owned by: anonymous
Priority: normal Milestone: 0.9
Component: TG Widgets Version:
Severity: major Keywords:
Cc: jgodoy@…

Description

When trying to use an unicode string as the title for a datagrid column the program is aborted with an UnicodeEncodeError? exception. Example code:

grid_clientes = datawidgets.DataGrid(fields = [
    (lazy_gettext('Cliente'), 'nomeAbreviado'),
    (lazy_gettext('Contrato assinado em'), grid_contrato_assinatura),
    (lazy_gettext(u'Contrato válido até'), grid_contrato_vencimento),
    (lazy_gettext(u'Está ativo?'), 'isAtivo'),
    ])

If I change it to:

grid_clientes = datawidgets.DataGrid(fields = [
    (lazy_gettext('Cliente'), 'nomeAbreviado'),
    (lazy_gettext('Contrato assinado em'), grid_contrato_assinatura),
    (lazy_gettext('Contrato valido ate'), grid_contrato_vencimento),
    (lazy_gettext('Esta ativo?'), 'isAtivo'),
    ])

Then the datagrid works as expected.

The traceback for the Unicode situation is:

Page handler: <bound method Clientes.default of <site_amostras.clientes.Clientes instance at 0x40a3cc2c>>
Traceback (most recent call last):
  File "/usr/lib/python2.4/site-packages/CherryPy-2.2.0beta-py2.4.egg/cherrypy/_cphttptools.py", line 99, in _run
    self.main()
  File "/usr/lib/python2.4/site-packages/CherryPy-2.2.0beta-py2.4.egg/cherrypy/_cphttptools.py", line 247, in main
    body = page_handler(*virtual_path, **self.params)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/controllers.py", line 241, in newfunc
    html, fragment, *args, **kw)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/database.py", line 189, in run_with_transaction
    retval = func(*args, **kw)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/controllers.py", line 258, in _execute_func
    output = func(self, *args, **kw)
  File "/home/godoy/empresa/clientes/latam/site_amostras/site_amostras/clientes.py", line 33, in default
    return self.clientes(id)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/controllers.py", line 244, in newfunc
    tg_format, html, fragment, *args, **kw)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/controllers.py", line 263, in _execute_func
    return _process_output(tg_format, output, html, fragment)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/controllers.py", line 63, in _process_output
    output = view.render(output, tg_format, template=template,fragment=fragment)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/view.py", line 60, in render
    return engine.render(info, format, fragment, template)
  File "/usr/lib/python2.4/site-packages/TurboKid-0.9.0-py2.4.egg/turbokid/kidsupport.py", line 136, in render
    return t.serialize(encoding=self.defaultencoding, output=format, fragment=fragment)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/__init__.py", line 236, in serialize
    return serializer.serialize(self, encoding, fragment)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/serialization.py", line 51, in serialize
    text = list(self.generate(stream, encoding, fragment))
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/serialization.py", line 327, in generate
    for ev, item in self.apply_filters(stream):
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/serialization.py", line 84, in balancing_filter
    for ev, item in stream:
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/pull.py", line 206, in _coalesce
    for ev, item in stream:
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/filter.py", line 21, in transform_filter
    for ev, item in apply_matches(stream, template, templates, apply_func):
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/filter.py", line 31, in apply_matches
    item = stream.expand()
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/pull.py", line 95, in expand
    for ev, item in self._iter:
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/pull.py", line 164, in _track
    for p in stream:
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/pull.py", line 206, in _coalesce
    for ev, item in stream:
  File "/home/godoy/empresa/clientes/latam/site_amostras/site_amostras/templates/clientes.py", line 220, in _pull
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/widgets/base.py", line 136, in insert
    convert=convert, **kw),
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/widgets/base.py", line 113, in create_dict
    self.update_dict(d)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/fastdata/datawidgets.py", line 93, in update_dict
    key = '%s-%s' % (title, n)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/i18n/tg_gettext.py", line 100, in __str__
    return str(self.eval())
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 10: ordinal not in range(128)

Change History

comment:1 Changed 13 years ago by anonymous

  • Cc jgodoy@… added

comment:2 Changed 13 years ago by anonymous

  • Status changed from new to closed
  • Resolution set to fixed

I think I fixed it in r620. Please try it and reopen the ticket if it still fails.

comment:3 Changed 13 years ago by Jorge Godoy <jgodoy@…>

  • Status changed from closed to reopened
  • Resolution fixed deleted

The error is different now, but it still doesn't work:

Traceback (most recent call last):
  File "/usr/lib/python2.4/site-packages/CherryPy-2.2.0beta-py2.4.egg/cherrypy/_cphttptools.py", line 99, in _run
    self.main()
  File "/usr/lib/python2.4/site-packages/CherryPy-2.2.0beta-py2.4.egg/cherrypy/_cphttptools.py", line 247, in main
    body = page_handler(*virtual_path, **self.params)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/controllers.py", line 241, in newfunc
    html, fragment, *args, **kw)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/database.py", line 193, in run_with_transaction
    retval = func(*args, **kw)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/controllers.py", line 258, in _execute_func
    output = func(self, *args, **kw)
  File "/home/godoy/empresa/clientes/latam/site_amostras/site_amostras/clientes.py", line 35, in index
    return self.clientes(cliente_id)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/controllers.py", line 244, in newfunc
    tg_format, html, fragment, *args, **kw)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/controllers.py", line 263, in _execute_func
    return _process_output(tg_format, output, html, fragment)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/controllers.py", line 63, in _process_output
    output = view.render(output, tg_format, template=template,fragment=fragment)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/view.py", line 60, in render
    return engine.render(info, format, fragment, template)
  File "/usr/lib/python2.4/site-packages/TurboKid-0.9.0-py2.4.egg/turbokid/kidsupport.py", line 136, in render
    return t.serialize(encoding=self.defaultencoding, output=format, fragment=fragment)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/__init__.py", line 236, in serialize
    return serializer.serialize(self, encoding, fragment)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/serialization.py", line 51, in serialize
    text = list(self.generate(stream, encoding, fragment))
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/serialization.py", line 327, in generate
    for ev, item in self.apply_filters(stream):
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/serialization.py", line 84, in balancing_filter
    for ev, item in stream:
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/pull.py", line 206, in _coalesce
    for ev, item in stream:
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/filter.py", line 21, in transform_filter
    for ev, item in apply_matches(stream, template, templates, apply_func):
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/filter.py", line 31, in apply_matches
    item = stream.expand()
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/pull.py", line 95, in expand
    for ev, item in self._iter:
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/pull.py", line 164, in _track
    for p in stream:
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/thirdparty/kid/kid/pull.py", line 206, in _coalesce
    for ev, item in stream:
  File "/home/godoy/empresa/clientes/latam/site_amostras/site_amostras/templates/clientes.py", line 113, in _pull
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/widgets/base.py", line 136, in insert
    convert=convert, **kw),
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/widgets/base.py", line 113, in create_dict
    self.update_dict(d)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/fastdata/datawidgets.py", line 93, in update_dict
    key = title + '-' + str(n) # avoid %s to avoid str() on title
TypeError: unsupported operand type(s) for +: 'lazystring' and 'str'

Maybe using the famous "-".join([title, n]) should solve the problem... (I'd also avoid using 'str' here due to #499 where it will probably clash with Unicode...)

comment:4 Changed 13 years ago by Jorge Godoy <jgodoy@…>

  • Status changed from reopened to closed
  • Resolution set to fixed

As of SVN r.645 this is completely solved: I'm using Unicode with DataGrid from fastdata and it is behaving as expected. The TypeError? message is also gone.

Note: See TracTickets for help on using tickets.