Skip to content
Browse files

make it possible to force file encoding via the command line

  • Loading branch information...
1 parent e835293 commit 1f3e4ed66a343f21f6171e42b7222b5189a1fdce Evan Klitzke committed with rtyler
Showing with 10 additions and 1 deletion.
  1. +1 −0 cheetah/
  2. +9 −1 cheetah/
1 cheetah/
@@ -173,6 +173,7 @@ def parseOpts(self, args):
pao("--templateAPIClass", action="store", dest="templateClassName", default=None, help='Name of a subclass of Cheetah.Template.Template to use for compilation, e.g. MyTemplateClass')
pao("--parallel", action="store", type="int", dest="parallel", default=1, help='Compile/fill templates in parallel, e.g. --parallel=4')
pao('--shbang', dest='shbang', default='#!/usr/bin/env python', help='Specify the shbang to place at the top of compiled templates, e.g. --shbang="#!/usr/bin/python2.6"')
+ pao('--encoding', dest='encoding', default=None, help='Specify the encoding of source files (e.g. \'utf-8\' to force input files to be interpreted as UTF-8)')
opts, files = self.parser.parse_args(args)
self.opts = opts
10 cheetah/
@@ -18,6 +18,7 @@
import random
import warnings
import copy
+import codecs
from Cheetah.Version import Version, VersionTuple
from Cheetah.SettingsManager import SettingsManager
@@ -99,6 +100,7 @@ class Error(Exception): pass
('allowEmptySingleLineMethods', False, ''),
('allowNestedDefScopes', True, ''),
('allowPlaceholderFilterArgs', True, ''),
+ ('encoding', None, 'The encoding to read input files as (or None for ASCII)'),
@@ -1530,7 +1532,13 @@ def __init__(self, source=None, file=None,
if source and file:
raise TypeError("Cannot compile from a source string AND file.")
elif isinstance(file, basestring): # it's a filename.
- f = open(file) # Raises IOError.
+ encoding = settings.get('encoding')
+ if encoding:
+ f =, 'r', encoding=encoding)
+ else:
+ f = open(file, 'r') # if no encoding is specified, use the
+ # builtin open function, which will
+ # effectively read data as a bytestream
source =
self._filePath = file

0 comments on commit 1f3e4ed

Please sign in to comment.
Something went wrong with that request. Please try again.