aboutsummaryrefslogtreecommitdiffstats
path: root/mako
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 /mako
parentdefd4668e5da1d2fbef704ffb8676f2a0236218b (diff)
downloadexternal_python_mako-2f022a271212b220519305649e6a8490cf4399ff.tar.gz
external_python_mako-2f022a271212b220519305649e6a8490cf4399ff.tar.bz2
external_python_mako-2f022a271212b220519305649e6a8490cf4399ff.zip
- [bug] Fixed some Py3K resource warnings due
to filehandles being implicitly closed. [ticket:182]
Diffstat (limited to 'mako')
-rw-r--r--mako/__init__.py2
-rw-r--r--mako/template.py23
-rw-r--r--mako/util.py16
3 files changed, 30 insertions, 11 deletions
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()