web.form inputs fails when value parameter contains unicode data

Bug #180653 reported by Łukasz Topa
2
Affects Status Importance Assigned to Milestone
web.py
Fix Released
High
Anand Chitipothu

Bug Description

I'm getting this error (web.py 0.23 + Mako templates)

Traceback (most recent call last):
  File "/home/lukasz/Praca/Python/WebPyEnv/lib/python2.5/site-packages/web/form.py", line 33, in render
    out += "<td>"+i.pre+i.render()+i.post+"</td>"
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 48: ordinal not in range(128)

sample code:
...
address = list(web.select('sh_address',
            where='client_id = $client_id AND id = $id',
            vars={
                'client_id': session['client']['id']
                }
            ))

address_form = web.form.Form(
    web.form.Textbox('first_name', maxlength='255', description=u'Imię:',
        value=address[0]['first_name']),
...

Workaround:

edit form.py
line 33: out += "<td>"+i.pre+i.render()+i.post+"</td>"
change to:
line 33: out += "<td>"+i.pre+i.render().decode('utf-8')+i.post+"</td>"

Revision history for this message
Anand Chitipothu (anandology) wrote : Re: [Bug 180653] web.form inputs fails when value parameter contains unicode data

> Workaround:
>
> edit form.py
> line 33: out += "<td>"+i.pre+i.render()+i.post+"</td>"
> change to:
> line 33: out += "<td>"+i.pre+i.render().decode('utf-8')+i.post+"</td>"

I think, the problem is not there. Problem is in adding the
description, which not websafe'd. The same problem may come, if you
have unicode error messages.
Here is the fix.

file 'web/form.py'
--- web/form.py 2007-06-21 00:58:41 +0000
+++ web/form.py 2008-01-06 01:18:31 +0000
@@ -29,14 +29,14 @@
         out += self.rendernote(self.note)
         out += '<table>\n'
         for i in self.inputs:
- out += ' <tr><th><label for="%s">%s</label></th>' %
(i.id, i.description)
+ out += ' <tr><th><label for="%s">%s</label></th>' %
(i.id, net.websafe(i.description))
             out += "<td>"+i.pre+i.render()+i.post+"</td>"
             out += '<td id="note_%s">%s</td></tr>\n' % (i.id,
self.rendernote(i.note))
         out += "</table>"
         return out

     def rendernote(self, note):
- if note: return '<strong class="wrong">%s</strong>' % note
+ if note: return '<strong class="wrong">%s</strong>' % web.netsafe(note)
         else: return ""

     def validates(self, source=None, _validate=True, **kw):

Changed in webpy:
assignee: nobody → anandology
importance: Undecided → High
milestone: none → 0.23
status: New → Confirmed
Changed in webpy:
status: Confirmed → Fix Committed
Revision history for this message
Łukasz Topa (lukasz-topa) wrote :

Hi, problem still exists.
form.py properly rendered descriptions and error messages in unicode,
I had problems only with value parameter, now it's raising errors also on net.websafe().

Revision history for this message
Anand Chitipothu (anandology) wrote : Re: [Bug 180653] Re: web.form inputs fails when value parameter contains unicode data

On Jan 6, 2008 4:42 PM, Łukasz Topa <email address hidden> wrote:
> Hi, problem still exists.
> form.py properly rendered descriptions and error messages in unicode,
> I had problems only with value parameter, now it's raising errors also on net.websafe().

I have a test, which has unicode value and it passes.

    def testUnicode(self):
        t = form.Textbox('first_name', maxlength='255',
description=u'\u1234', value=u'\u1234')
        f = form.Form(t)
        f.render() # this fails if unicode is not supported.

        t = form.Textbox('first_name', maxlength='255',
description=u'\u1234', value=u'\u1234')
        t.note = u'\u1234'
        f = form.Form(t)
        f.render() # this fails if unicode is not supported.

try running python test/fom.py in webpy-0.23 directory.
See if you can add a failing test with your input.

Revision history for this message
Łukasz Topa (lukasz-topa) wrote :

Hello again.
This test passed, I rewrote my example and it also passed.
Probably I missed something in configuration of Mako templates.
Anyway, thank you for help and fixing it by net.websafe().

Revision history for this message
Łukasz Topa (lukasz-topa) wrote :

hmm.. still fails..
example attached

Revision history for this message
Anand Chitipothu (anandology) wrote :

On Jan 6, 2008 6:20 PM, Łukasz Topa <email address hidden> wrote:
> hmm.. still fails..
> example attached

I tried the same script and it works for me.

Revision history for this message
Łukasz Topa (lukasz-topa) wrote :

oops.. you're right, sorry for making chaos
I've tested it on another environment with earlier web.py version.
Thanks for help :)

Changed in webpy:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.