Loading ...
Sorry, an error occurred while loading the content.

4378Re: [Cheetahtemplate-discuss] [PATCH] Convert unicode compiled template to an utf8 char buffer when writing to a file

Expand Messages
  • R. Tyler Ballance
    Apr 3, 2009
    • 0 Attachment
      If you wouldn't mind jumping onto #cheetah on Freenode today, I'd like
      to discuss this a bit more with you.

      I'm at an impasse as to which changes to roll into next and which to
      keep in unicode for a v2.2 release

      On Fri, Apr 03, 2009 at 04:49:57PM +0200, Jean-Baptiste Quenot wrote:
      > 2009/4/3 R. Tyler Ballance <tyler@...>:
      > > On Thu, Apr 02, 2009 at 12:33:10PM +0200, Jean-Baptiste Quenot wrote:
      > >
      > > What basically was occuring is some interesting behavior IMHO in Python
      > > with regards to "file encodings".
      > >
      > > I prematurely return out of the setModuleEncoding() function in
      > > Compiler.py because the internal representation of "source" is now a
      > > unicode() object instead of a regular old str() object. When dealing with a
      > > dynamically compiled template, the code is exec'd (Template.py:772) and
      > > for some reason the call would fail to execute a unicode string that had
      > > an "coding" directive in them. When writing the compiled template out to
      > > a file it's obvious that we'd want to have the "coding" directive there
      > > however.
      > >
      > > I'm still on the fence on how I want to solve this (in a manner that's
      > > not hackish), so suggestions/patches welcome ;)
      >
      > If the issue we're facing is this one:
      >
      > http://bugs.python.org/issue4626
      > http://svn.python.org/view?view=rev&revision=70113
      >
      > Then we'll have to wait for the next Python 3.0.x for an official fix.
      >
      > Or we can work around this Python bug by re-encoding the Unicode
      > string to a char buffer. See attached patch. With this patch, the
      > unit tests pass for me now.
      > --
      > Jean-Baptiste Quenot
      > http://jbq.caraldi.com/

      > From 583423d470e538133d604edec8c444faead25d50 Mon Sep 17 00:00:00 2001
      > From: Jean-Baptiste Quenot <jbq@...>
      > Date: Fri, 3 Apr 2009 16:46:05 +0200
      > Subject: [PATCH] Work around Python bug #4626 regarding compile() and exec() with coding cookie
      >
      > ---
      > src/Compiler.py | 2 --
      > src/Template.py | 2 +-
      > 2 files changed, 1 insertions(+), 3 deletions(-)
      >
      > diff --git a/src/Compiler.py b/src/Compiler.py
      > index 5d3968c..8377478 100644
      > --- a/src/Compiler.py
      > +++ b/src/Compiler.py
      > @@ -1813,8 +1813,6 @@ class ModuleCompiler(SettingsManager, GenUtils):
      > self._moduleShBang = shBang
      >
      > def setModuleEncoding(self, encoding):
      > - #print ('setModuleEncoding', locals())
      > - return
      > self._moduleEncoding = encoding
      > self._moduleEncodingStr = '# -*- coding: %s -*-' %encoding
      >
      > diff --git a/src/Template.py b/src/Template.py
      > index a4d07e0..a2bf0b0 100644
      > --- a/src/Template.py
      > +++ b/src/Template.py
      > @@ -768,7 +768,7 @@ class Template(Servlet):
      > setattr(mod, baseclassName, baseclassValue)
      > ##
      > try:
      > - co = compile(generatedModuleCode, __file__, 'exec')
      > + co = compile(generatedModuleCode.encode('utf8'), __file__, 'exec')
      > exec co in mod.__dict__
      > except SyntaxError, e:
      > try:
      > --
      > 1.6.0.4
      >

      > ------------------------------------------------------------------------------

      > _______________________________________________
      > Cheetahtemplate-discuss mailing list
      > Cheetahtemplate-discuss@...
      > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss


      --
      -R. Tyler Ballance
      Slide, Inc.
    • Show all 16 messages in this topic