On 08/02/2015 10:51 PM, Thomas De Schampheleire wrote:
# HG changeset patch
# User Thomas De Schampheleire <[email protected]>
# 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()

In this case, when all the file content is read with a single .read(), why not just keep it simple and use

        with open(tmpl_file, 'rb') as f:
            tmpl_data = f.read().decode('utf-8')

?

/Mads

_______________________________________________
kallithea-general mailing list
[email protected]
http://lists.sfconservancy.org/mailman/listinfo/kallithea-general

Reply via email to