summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2012-02-21 09:49:40 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2012-02-21 09:49:40 -0500
commit2f022a271212b220519305649e6a8490cf4399ff (patch)
treea01d3798bddc96aa3c10462f72345220b0769e33
parentdefd4668e5da1d2fbef704ffb8676f2a0236218b (diff)
downloadmako-2f022a271212b220519305649e6a8490cf4399ff.tar.gz
- [bug] Fixed some Py3K resource warnings due
to filehandles being implicitly closed. [ticket:182]
-rwxr-xr-x.hgignore1
-rw-r--r--CHANGES5
-rw-r--r--mako/__init__.py2
-rw-r--r--mako/template.py23
-rw-r--r--mako/util.py16
-rw-r--r--test/test_util.py14
6 files changed, 49 insertions, 12 deletions
diff --git a/.hgignore b/.hgignore
index 400cc47..9662617 100755
--- a/.hgignore
+++ b/.hgignore
@@ -5,3 +5,4 @@ syntax:regexp
.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 @@ template strings, as well as template runtime operations."""
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 @@ class Template(object):
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 @@ class ModuleInfo(object):
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 @@ class ModuleInfo(object):
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 @@ except ImportError:
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 @@ class UtilTest(unittest.TestCase):
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)