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

4380Re: [Cheetahtemplate-discuss] [PATCH] Properly handle dealing with executing unicode strings as well as writing them out to a generated .py file

Expand Messages
  • R. Tyler Ballance
    Apr 3, 2009
    • 0 Attachment
      On Fri, Apr 03, 2009 at 02:47:45PM -0700, R. Tyler Ballance wrote:
      > This should resolve the issue reported by jbq@ reflected in src/Tests/Unicode,py:JBQ_UTF8_Test7

      FWIW this has been pushed up to the unicode branch and is ready for
      testing :)


      >
      > With this commit the #encoding directive will do what you might expect it to do in terms of encoding
      > the resulting .py file (adding in the "-*- coding: " directive) or encoding the unicode string when
      > executing a dynamic template
      >
      > Signed-off-by: R. Tyler Ballance <tyler@...>
      > ---
      > src/CheetahWrapper.py | 2 +-
      > src/Compiler.py | 3 ---
      > src/Template.py | 12 ++++++++++--
      > 3 files changed, 11 insertions(+), 6 deletions(-)
      >
      > diff --git a/src/CheetahWrapper.py b/src/CheetahWrapper.py
      > index 776a394..5a37f5e 100644
      > --- a/src/CheetahWrapper.py
      > +++ b/src/CheetahWrapper.py
      > @@ -587,7 +587,7 @@ be named according to the same rules as Python modules.""" % tup)
      > sys.stdout.write(output)
      > else:
      > f = open(dst, 'w')
      > - f.write(output.encode('utf8'))
      > + f.write(output)
      > f.close()
      >
      >
      > diff --git a/src/Compiler.py b/src/Compiler.py
      > index 5d3968c..34bc994 100644
      > --- a/src/Compiler.py
      > +++ b/src/Compiler.py
      > @@ -1813,10 +1813,7 @@ class ModuleCompiler(SettingsManager, GenUtils):
      > self._moduleShBang = shBang
      >
      > def setModuleEncoding(self, encoding):
      > - #print ('setModuleEncoding', locals())
      > - return
      > self._moduleEncoding = encoding
      > - self._moduleEncodingStr = '# -*- coding: %s -*-' %encoding
      >
      > def getModuleEncoding(self):
      > return self._moduleEncoding
      > diff --git a/src/Template.py b/src/Template.py
      > index a4d07e0..4958e9f 100644
      > --- a/src/Template.py
      > +++ b/src/Template.py
      > @@ -719,6 +719,7 @@ class Template(Servlet):
      > except:
      > #@@TR: should add some logging to this
      > pass
      > + outputEncoding = 'ascii'
      > if useCache and cacheHash and cacheHash in klass._CHEETAH_compileCache:
      > cacheItem = klass._CHEETAH_compileCache[cacheHash]
      > generatedModuleCode = cacheItem.code
      > @@ -733,9 +734,16 @@ class Template(Servlet):
      > compiler.setShBang(commandlineopts.shbang)
      > compiler.compile()
      > generatedModuleCode = compiler.getModuleCode()
      > + outputEncoding = compiler.getModuleEncoding()
      >
      > if not returnAClass:
      > - return generatedModuleCode
      > + # This is a bit of a hackish solution to make sure we're setting the proper
      > + # encoding on generated code that is destined to be written to a file
      > + if not outputEncoding == 'ascii':
      > + generatedModuleCode = generatedModuleCode.split('\n')
      > + generatedModuleCode.insert(1, '# -*- coding: %s -*-' % outputEncoding)
      > + generatedModuleCode = '\n'.join(generatedModuleCode)
      > + return generatedModuleCode.encode(outputEncoding)
      > else:
      > if cacheItem:
      > cacheItem.lastCheckoutTime = time.time()
      > @@ -768,7 +776,7 @@ class Template(Servlet):
      > setattr(mod, baseclassName, baseclassValue)
      > ##
      > try:
      > - co = compile(generatedModuleCode, __file__, 'exec')
      > + co = compile(generatedModuleCode.encode(outputEncoding), __file__, 'exec')
      > exec co in mod.__dict__
      > except SyntaxError, e:
      > try:
      > --
      >
      >

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