[PATCH 5 of 6] kallithea_config: properly handle Unicode characters in .ini template

Thomas De Schampheleire patrickdepinguin at gmail.com
Sun Aug 2 20:51:39 UTC 2015


# HG changeset patch
# User Thomas De Schampheleire <thomas.de.schampheleire at gmail.com>
# Date 1438547875 -7200
#      Sun Aug 02 22:37:55 2015 +0200
# Node ID b2edf3d7ad971e48214632bba5187c490161716d
# Parent  c758464b9b91693580cb6927a1b37a02f2bf334b
kallithea_config: properly handle Unicode characters in .ini template

The kallithea_config.py script did not allow Unicode characters in the
configuration file template template.ini.mako. Following error would be
given:
    Traceback (most recent call last):
      File "/home/tdescham/repo/contrib/kallithea-fixes/kallithea/bin/kallithea_config.py", line 141, in _run
        f.write(tmpl)
    UnicodeEncodeError: 'ascii' codec can't encode character u'\u2002' in position 2087: ordinal not in range(128)

Instead of using plain open, use codecs.open, as per the Unicode HOWTO:
https://docs.python.org/2/howto/unicode.html#reading-and-writing-unicode-data

diff --git a/kallithea/bin/kallithea_config.py b/kallithea/bin/kallithea_config.py
--- a/kallithea/bin/kallithea_config.py
+++ b/kallithea/bin/kallithea_config.py
@@ -29,6 +29,7 @@ Original author and date, and relevant c
 
 
 from __future__ import with_statement
+import codecs
 import os
 import sys
 import uuid
@@ -131,13 +132,13 @@ def _run(argv):
         if args.template:
             tmpl_file = args.template
 
-        with open(tmpl_file, 'rb') as f:
+        with codecs.open(tmpl_file, 'rb', encoding='utf-8') as f:
             tmpl_data = f.read()
             if args.raw:
                 tmpl = tmpl_data
             else:
                 tmpl = Template(tmpl_data).render(**tmpl_stored_args)
-        with open(args.filename, 'wb') as f:
+        with codecs.open(args.filename, 'wb', encoding='utf-8') as f:
             f.write(tmpl)
         print 'Wrote new config file in %s' % (os.path.abspath(args.filename))
 


More information about the kallithea-general mailing list