Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Apply directive in templates and UnicodeDecode Errors

Received: by 10.50.153.230 with SMTP id vj6mr4203259igb.3.1349374670911;
        Thu, 04 Oct 2012 11:17:50 -0700 (PDT)
X-BeenThere: python-tornado@googlegroups.com
Received: by 10.50.51.234 with SMTP id n10ls16022159igo.3.canary; Thu, 04 Oct
 2012 11:17:48 -0700 (PDT)
Received: by 10.50.182.132 with SMTP id ee4mr3655286igc.5.1349374668109;
        Thu, 04 Oct 2012 11:17:48 -0700 (PDT)
Received: by 10.50.182.132 with SMTP id ee4mr3655285igc.5.1349374668090;
        Thu, 04 Oct 2012 11:17:48 -0700 (PDT)
Return-Path: <dmit...@gmail.com>
Received: from mail-oa0-f44.google.com (mail-oa0-f44.google.com [209.85.219.44])
        by gmr-mx.google.com with ESMTPS id hr6si2242240igc.0.2012.10.04.11.17.47
        (version=TLSv1/SSLv3 cipher=OTHER);
        Thu, 04 Oct 2012 11:17:48 -0700 (PDT)
Received-SPF: pass (google.com: domain of dmit...@gmail.com designates 209.85.219.44 as permitted sender) client-ip=209.85.219.44;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of dmit...@gmail.com designates 209.85.219.44 as permitted sender) smtp.mail=dmit...@gmail.com; dkim=pass header...@gmail.com
Received: by mail-oa0-f44.google.com with SMTP id n5so790602oag.3
        for <python-tornado@googlegroups.com>; Thu, 04 Oct 2012 11:17:47 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=mime-version:in-reply-to:references:from:date:message-id:subject:to
         :content-type;
        bh=/skyMrRzcdkAIyYIAQDRwp0CDrCkfo3CBr9Ld9veCU0=;
        b=UZVcfXSAs1/QgYb8gECtGx3EjTDNy2shwlwoWpiihIPJNHyzY5iwXcc+4YMwMcVeBr
         mFjEeqg4lme9DofMTZNtWeqxvv3W4K1eT8pzn7MqawfSu3/KQdnbPWNJTBEkzwffpDFj
         n2WwbCCfeYrx+0fEnthze1tkt6j6kdduR8pD0Wg92p91ww3RAVJKVS3z4Kta3tuedB55
         K63y/bUTBtU8jpWdyHCrAf6xeoe+WNZKbLf40o7pR3nwzlXv3dVfHoxtJiL6l6tZrF0P
         +2bR4yGyNEjSNg6WjkZkrxTMK/RayPVm1BlksYGse/BwvXMm/GnIBPd5Tss9CWF3g1+M
         EN9w==
Received: by 10.60.25.193 with SMTP id e1mr4992680oeg.87.1349374667873; Thu,
 04 Oct 2012 11:17:47 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.60.95.34 with HTTP; Thu, 4 Oct 2012 11:17:27 -0700 (PDT)
In-Reply-To: <CAFkYKJ5=dTpjA0zQdckVD4PbvAOOErY6zTd_crW7uAoGYyh...@mail.gmail.com>
References: <f2da7ba4-326e-4829-aa7d-a694d2455fa2@googlegroups.com> <CAFkYKJ5=dTpjA0zQdckVD4PbvAOOErY6zTd_crW7uAoGYyh...@mail.gmail.com>
From: Dmitri Cherniak <dmit...@gmail.com>
Date: Thu, 4 Oct 2012 14:17:27 -0400
Message-ID: <CAG07S98c9_GGNmJywQm_MWU6pCg+k+s4Bb4W+DyePgmvN+9...@mail.gmail.com>
Subject: Re: [tornado] Apply directive in templates and UnicodeDecode Errors
To: python-tornado@googlegroups.com
Content-Type: multipart/alternative; boundary=e89a8fb1f84cbf5c1d04cb3fc501

--e89a8fb1f84cbf5c1d04cb3fc501
Content-Type: text/plain; charset=ISO-8859-1

Yes that did work. Thanks Ben!


I've put in a pull request https://github.com/facebook/tornado/pull/606 which
solved my problem.

On Thu, Oct 4, 2012 at 1:25 AM, Ben Darnell <b...@bendarnell.com> wrote:

> This looks like a bug in the template system.  Does it work if you
> return utf8(_LINK_RE.sub())?  The problem looks like the template
> system generally turns unicode strings into utf8 for you, but not for
> the output of apply blocks.  It'll work as long as you either
> pre-encode your strings or everything happens to be ascii-only, but it
> will blow up if you get any non-ascii characters in a unicode string.
>
> -Ben
>
> On Wed, Oct 3, 2012 at 10:02 AM, DLC <dmit...@gmail.com> wrote:
> > I'm trying to create a template function that turns @mentions or hash
> tags
> > into their corresponding href links (sample code here:
> > https://gist.github.com/3828280#file_p.py), but I've run into a snag.
> >
> > It works most of the time, but when I use my own {% apply *function* %}
> {%
> > end %} I sometimes run into an error like:
> >
> > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0:
> > ordinal not in range(128)
> >
> > Here is a traceback: https://gist.github.com/3828280#file_gistfile1.pytb
> > And here's the general form of the template:
> > https://gist.github.com/3828280#file_t.html
> >
> > If I remove that apply function, I no longer get the error. I'm not quite
> > what causes the error though. I tried using tornado.escape.linkify in
> it's
> > place, but I still get the error, and so believe I may be doing something
> > wrong, or it may be a bug in the template rendering code.
> >
> > I noted in the documentation
> > http://www.tornadoweb.org/documentation/template.html#syntax-referenceit
> > cautions using apply blocks in for loops with breaks or continues,
> however I
> > do not use them. I do extend from a base template.
> >
> > If anyone has any insight, would love to hear!
> > D
> >
> >
>

--e89a8fb1f84cbf5c1d04cb3fc501
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable









<p class=3D"p1">Yes that did work. Thanks Ben!</p>
<p class=3D"p2"><br></p>
<p class=3D"p1">I&#39;ve put in a pull request=A0<a href=3D"https://github.=
com/facebook/tornado/pull/606"><span class=3D"s1">https://github.com/facebo=
ok/tornado/pull/606</span></a>=A0which solved my problem.</p><br><div class=
=3D"gmail_quote">

On Thu, Oct 4, 2012 at 1:25 AM, Ben Darnell <span dir=3D"ltr">&lt;<a href=
=3D"mailto:b...@bendarnell.com" target=3D"_blank">b...@bendarnell.com</a>&gt;=
</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .=
8ex;border-left:1px #ccc solid;padding-left:1ex">

This looks like a bug in the template system. =A0Does it work if you<br>
return utf8(_LINK_RE.sub())? =A0The problem looks like the template<br>
system generally turns unicode strings into utf8 for you, but not for<br>
the output of apply blocks. =A0It&#39;ll work as long as you either<br>
pre-encode your strings or everything happens to be ascii-only, but it<br>
will blow up if you get any non-ascii characters in a unicode string.<br>
<span class=3D"HOEnZb"><font color=3D"#888888"><br>
-Ben<br>
</font></span><div class=3D"HOEnZb"><div class=3D"h5"><br>
On Wed, Oct 3, 2012 at 10:02 AM, DLC &lt;<a href=3D"mailto:dmit...@gmail.co=
m">dmit...@gmail.com</a>&gt; wrote:<br>
&gt; I&#39;m trying to create a template function that turns @mentions or h=
ash tags<br>
&gt; into their corresponding href links (sample code here:<br>
&gt; <a href=3D"https://gist.github.com/3828280#file_p.py" target=3D"_blank=
">https://gist.github.com/3828280#file_p.py</a>), but I&#39;ve run into a s=
nag.<br>
&gt;<br>
&gt; It works most of the time, but when I use my own {% apply *function* %=
} {%<br>
&gt; end %} I sometimes run into an error like:<br>
&gt;<br>
&gt; UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte 0xe2 i=
n position 0:<br>
&gt; ordinal not in range(128)<br>
&gt;<br>
&gt; Here is a traceback: <a href=3D"https://gist.github.com/3828280#file_g=
istfile1.pytb" target=3D"_blank">https://gist.github.com/3828280#file_gistf=
ile1.pytb</a><br>
&gt; And here&#39;s the general form of the template:<br>
&gt; <a href=3D"https://gist.github.com/3828280#file_t.html" target=3D"_bla=
nk">https://gist.github.com/3828280#file_t.html</a><br>
&gt;<br>
&gt; If I remove that apply function, I no longer get the error. I&#39;m no=
t quite<br>
&gt; what causes the error though. I tried using tornado.escape.linkify in =
it&#39;s<br>
&gt; place, but I still get the error, and so believe I may be doing someth=
ing<br>
&gt; wrong, or it may be a bug in the template rendering code.<br>
&gt;<br>
&gt; I noted in the documentation<br>
&gt; <a href=3D"http://www.tornadoweb.org/documentation/template.html#synta=
x-reference" target=3D"_blank">http://www.tornadoweb.org/documentation/temp=
late.html#syntax-reference</a> it<br>
&gt; cautions using apply blocks in for loops with breaks or continues, how=
ever I<br>
&gt; do not use them. I do extend from a base template.<br>
&gt;<br>
&gt; If anyone has any insight, would love to hear!<br>
&gt; D<br>
&gt;<br>
&gt;<br>
</div></div></blockquote></div><br>

--e89a8fb1f84cbf5c1d04cb3fc501--