diff --git a/examples/wsgi/htdocs/index.html b/examples/wsgi/htdocs/index.html
index 79a5af2d69bdd84ccd168e9288dbc7dfd834ca55..0f0ce9deabfe8ad10150537ff7dd76cb0a6632ef 100644
--- a/examples/wsgi/htdocs/index.html
+++ b/examples/wsgi/htdocs/index.html
@@ -1,3 +1,6 @@
+<%
+%>
+
 <%inherit file="root.html"/>
 
 This is index.html
diff --git a/lib/mako/codegen.py b/lib/mako/codegen.py
index 71f815235e348a1a19667b40f9de9c50d01e4c97..a06ef2607d610988c09b330bb639e786fde6b896 100644
--- a/lib/mako/codegen.py
+++ b/lib/mako/codegen.py
@@ -66,6 +66,8 @@ class _GenerateRenderMethod(object):
         namespaces = {}
         module_code = []
         pagetag = [None]
+        encoding =[None]
+
         class FindTopLevel(object):
             def visitInheritTag(s, node):
                 inherit.append(node)
@@ -76,6 +78,7 @@ class _GenerateRenderMethod(object):
             def visitCode(self, node):
                 if node.ismodule:
                     module_code.append(node)
+            
         f = FindTopLevel()
         for n in self.node.nodes:
             n.accept_visitor(f)
diff --git a/lib/mako/lexer.py b/lib/mako/lexer.py
index 921d20600dbc4b70a0695efbff9a0297b542fe25..d032b5ae0866092f7dff9c0da4eedb5b0a0a129d 100644
--- a/lib/mako/lexer.py
+++ b/lib/mako/lexer.py
@@ -88,7 +88,12 @@ class Lexer(object):
                 raise exceptions.SyntaxException("Keyword '%s' not a legal ternary for keyword '%s'" % (node.keyword, self.control_line[-1].keyword), self.matched_lineno, self.matched_charpos, self.filename)
 
     def parse(self):
+        encoding = self.match_encoding()
+        if encoding:
+            self.text = self.text.decode(encoding)
+
         length = len(self.text)
+            
         while (True):
             if self.match_position > length: 
                 break
@@ -116,6 +121,13 @@ class Lexer(object):
             raise exceptions.SyntaxException("Unclosed tag: <%%%s>" % self.tag[-1].keyword, self.matched_lineno, self.matched_charpos, self.filename)
         return self.template
 
+    def match_encoding(self):
+        match = self.match(r'#\s*-\*- encoding: (.+?) -\*-\n')
+        if match:
+            return match.group(1)
+        else:
+            return None
+            
     def match_tag_start(self):
         match = self.match(r'''
             \<%     # opening tag
diff --git a/test/lexer.py b/test/lexer.py
index 8b7fe67a9fb7f7b91f34d40352856be002668224..db9d4c81669ad3decb4bf19d02ac191d87d7d352 100644
--- a/test/lexer.py
+++ b/test/lexer.py
@@ -14,6 +14,7 @@ class LexerTest(unittest.TestCase):
         and some more text.
 """
         node = Lexer(template).parse()
+        print repr(node)
         assert repr(node) == r"""TemplateNode({}, [Text('\n<b>Hello world</b>\n        ', (1, 1)), DefTag('def', {'name': 'foo'}, (3, 9), ["Text('\\n                this is a def.\\n        ', (3, 26))"]), Text('\n        \n        and some more text.\n', (5, 16))])"""
 
     def test_unclosed_tag(self):
diff --git a/test/lookup.py b/test/lookup.py
index 25d54e376e49dfeaa5a02bb3bd4e116f2b6a76b7..5986f64f907cc5a6c7f9190da4166314376d3b7a 100644
--- a/test/lookup.py
+++ b/test/lookup.py
@@ -7,16 +7,18 @@ import os
 
 if not os.access('./test_htdocs', os.F_OK):
     os.mkdir('./test_htdocs')
-    file('./test_htdocs/index.html', 'w').write("this is index")
-    file('./test_htdocs/incl.html', 'w').write("this is include 1")
+file('./test_htdocs/index.html', 'w').write("this is index")
+file('./test_htdocs/incl.html', 'w').write("this is include 1")
+if not os.access('./test_htdocs/subdir', os.F_OK):
     os.mkdir('./test_htdocs/subdir')
-    file('./test_htdocs/subdir/incl.html', 'w').write("""
-        this is include 2
-    """)
-    file('./test_htdocs/subdir/index.html', 'w').write("""
-        this is sub index
-        <%include file="incl.html"/>
-    """)
+file('./test_htdocs/subdir/incl.html', 'w').write("""
+    this is include 2
+""")
+file('./test_htdocs/subdir/index.html', 'w').write("""
+    this is sub index
+    <%include file="incl.html"/>
+""")
+
 tl = lookup.TemplateLookup(directories=['./test_htdocs'])
 class LookupTest(unittest.TestCase):
     def test_basic(self):
diff --git a/test/template.py b/test/template.py
index 6c9c4ef24f94936e25cfee8c6e52495ffc6323f4..5700d768181b17a386141c44afe6c4b5a7a312db 100644
--- a/test/template.py
+++ b/test/template.py
@@ -1,9 +1,14 @@
 # -*- encoding: utf-8 -*-
 
 from mako.template import Template
-import unittest, re
+import unittest, re, os
 from util import flatten_result, result_lines
 
+if not os.access('./test_htdocs', os.F_OK):
+    os.mkdir('./test_htdocs')
+file('./test_htdocs/unicode.html', 'w').write("""# -*- encoding: utf-8 -*-
+Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""")
+
 class EncodingTest(unittest.TestCase):
     def test_unicode(self):
         template = Template(u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""")
@@ -14,6 +19,16 @@ class EncodingTest(unittest.TestCase):
         template = Template("${val}")
         assert template.render_unicode(val=val) == u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""
 
+    def test_unicode_file(self):
+        template = Template(filename='./test_htdocs/unicode.html', module_directory='./test_htdocs')
+        assert template.render_unicode() == u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""
+
+    def test_unicode_memory(self):
+        val = u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""
+        val = "# -*- encoding: utf-8 -*-\n" + val.encode('utf-8')
+        template = Template(val)
+        assert template.render_unicode() == u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""
+        
 class ControlTest(unittest.TestCase):
     def test_control(self):
         t = Template("""