- [bug] Fixed some Py3K resource warnings due
  to filehandles being implicitly closed.
  [ticket:182]
diff --git a/.hgignore b/.hgignore
index 400cc47..9662617 100755
--- a/.hgignore
+++ b/.hgignore
@@ -5,3 +5,4 @@
 .orig$
 Mako.egg-info
 easy-install.pth
+env.*/
diff --git a/CHANGES b/CHANGES
index cab28ad..4783699 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,8 @@
+0.6.3
+- [bug] Fixed some Py3K resource warnings due
+  to filehandles being implicitly closed.
+  [ticket:182]
+
 0.6.2
 - [bug] The ${{"foo":"bar"}} parsing issue is fixed!!
   The legendary Eevee has slain the dragon!
diff --git a/mako/__init__.py b/mako/__init__.py
index 40033ee..4121e6b 100644
--- a/mako/__init__.py
+++ b/mako/__init__.py
@@ -5,5 +5,5 @@
 # the MIT License: http://www.opensource.org/licenses/mit-license.php
 
 
-__version__ = '0.6.2'
+__version__ = '0.6.3'
 
diff --git a/mako/template.py b/mako/template.py
index dcfc9f1..f38a055 100644
--- a/mako/template.py
+++ b/mako/template.py
@@ -9,7 +9,7 @@
 
 from mako.lexer import Lexer
 from mako import runtime, util, exceptions, codegen, cache
-import imp, os, re, shutil, stat, sys, tempfile, time, types, weakref
+import os, re, shutil, stat, sys, tempfile, types, weakref
 
  
 class Template(object):
@@ -307,30 +307,33 @@
             filemtime = os.stat(filename)[stat.ST_MTIME]
             if not os.path.exists(path) or \
                         os.stat(path)[stat.ST_MTIME] < filemtime:
+                data = util.read_file(filename)
                 _compile_module_file(
                             self, 
-                            open(filename, 'rb').read(), 
+                            data,
                             filename, 
                             path,
                             self.module_writer)
-            module = imp.load_source(self.module_id, path, open(path, 'rb'))
+            module = util.load_module(self.module_id, path)
             del sys.modules[self.module_id]
             if module._magic_number != codegen.MAGIC_NUMBER:
+                data = util.read_file(filename)
                 _compile_module_file(
                             self, 
-                            open(filename, 'rb').read(), 
+                            data, 
                             filename, 
                             path,
                             self.module_writer)
-                module = imp.load_source(self.module_id, path, open(path, 'rb'))
+                module = util.load_module(self.module_id, path)
                 del sys.modules[self.module_id]
             ModuleInfo(module, path, self, filename, None, None)
         else:
             # template filename and no module directory, compile code
             # in memory
+            data = util.read_file(filename)
             code, module = _compile_text(
                                 self, 
-                                open(filename, 'rb').read(), 
+                                data, 
                                 filename)
             self._source = None
             self._code = code
@@ -534,7 +537,7 @@
         if self.module_source is not None:
             return self.module_source
         else:
-            return open(self.module_filename).read()
+            return util.read_file(self.module_filename)
  
     @property
     def source(self):
@@ -546,11 +549,11 @@
             else:
                 return self.template_source
         else:
+            data = util.read_file(self.template_filename)
             if self.module._source_encoding:
-                return open(self.template_filename, 'rb').read().\
-                                decode(self.module._source_encoding)
+                return data.decode(self.module._source_encoding)
             else:
-                return open(self.template_filename).read()
+                return data
  
 def _compile_text(template, text, filename):
     identifier = template.module_id
diff --git a/mako/util.py b/mako/util.py
index dab5584..408dd3d 100644
--- a/mako/util.py
+++ b/mako/util.py
@@ -4,6 +4,7 @@
 # This module is part of Mako and is released under
 # the MIT License: http://www.opensource.org/licenses/mit-license.php
 
+import imp
 import sys
 
 
@@ -378,3 +379,18 @@
     import inspect
     def inspect_func_args(fn):
         return inspect.getargspec(fn)
+
+def read_file(path, mode='rb'):
+    fp = open(path, mode)
+    try:
+        data = fp.read()
+        return data
+    finally:
+        fp.close()
+
+def load_module(module_id, path):
+    fp = open(path, 'rb')
+    try:
+        return imp.load_source(module_id, path, fp)
+    finally:
+        fp.close()
diff --git a/test/test_util.py b/test/test_util.py
index 8f826a0..ad0de03 100644
--- a/test/test_util.py
+++ b/test/test_util.py
@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
 
+import os
 import unittest
 from mako import util
 from test import eq_
@@ -26,4 +27,15 @@
         buf.write(s[0:10])
         buf.write(s[10:])
         q = buf.getvalue()
-        eq_(buf.getvalue(), s.encode('utf-8'))
\ No newline at end of file
+        eq_(buf.getvalue(), s.encode('utf-8'))
+
+    def test_read_file(self):
+        fn = os.path.join(os.path.dirname(__file__), 'test_util.py')
+        data = util.read_file(fn, 'rb')
+        self.failUnless('test_util' in data)
+
+    def test_load_module(self):
+        fn = os.path.join(os.path.dirname(__file__), 'test_util.py')
+        module = util.load_module('mako.template', fn)
+        import mako.template
+        self.assertEqual(module, mako.template)