Skip to content

Commit

Permalink
- [bug] Cleaned up all the various deprecation/
Browse files Browse the repository at this point in the history
  file warnings when running the tests under
  various Pythons with warnings turned on.
  [ticket:213]
  • Loading branch information
zzzeek committed Apr 14, 2013
1 parent 6c5e273 commit c16198a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
- [bug] Fixed bug where mako-render script wasn't
compatible with Py3k. [ticket:212]

- [bug] Cleaned up all the various deprecation/
file warnings when running the tests under
various Pythons with warnings turned on.
[ticket:213]

0.8.0
- [feature] Performance improvement to the
"legacy" HTML escape feature, used for XML
Expand Down
20 changes: 17 additions & 3 deletions mako/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time

py3k = sys.version_info >= (3, 0)
py3kwarning = getattr(sys, 'py3kwarning', False) or py3k
py33 = sys.version_info >= (3, 3)
py26 = sys.version_info >= (2, 6)
py25 = sys.version_info >= (2, 5)
jython = sys.platform.startswith('java')
Expand Down Expand Up @@ -42,6 +42,21 @@ def u(s):
def octal(lit):
return eval("0" + lit)


if py33:
from importlib import machinery
def load_module(module_id, path):
return machinery.SourceFileLoader(module_id, path).load_module()
else:
import imp
def load_module(module_id, path):
fp = open(path, 'rb')
try:
return imp.load_source(module_id, path, fp)
finally:
fp.close()


def exception_as():
return sys.exc_info()[1]

Expand Down Expand Up @@ -120,7 +135,7 @@ def inspect_func_args(fn):
def inspect_func_args(fn):
return inspect.getargspec(fn)

if py3kwarning:
if py3k:
def callable(fn):
return hasattr(fn, '__call__')
else:
Expand All @@ -135,4 +150,3 @@ def with_metaclass(meta, base=object):
return meta("%sBase" % meta.__name__, (base,), {})
################################################


4 changes: 2 additions & 2 deletions mako/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def _compile_from_file(self, path, filename):
filename,
path,
self.module_writer)
module = util.load_module(self.module_id, path)
module = compat.load_module(self.module_id, path)
del sys.modules[self.module_id]
if module._magic_number != codegen.MAGIC_NUMBER:
data = util.read_file(filename)
Expand All @@ -386,7 +386,7 @@ def _compile_from_file(self, path, filename):
filename,
path,
self.module_writer)
module = util.load_module(self.module_id, path)
module = compat.load_module(self.module_id, path)
del sys.modules[self.module_id]
ModuleInfo(module, path, self, filename, None, None)
else:
Expand Down
7 changes: 0 additions & 7 deletions mako/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

import imp
import re
import collections
import codecs
Expand Down Expand Up @@ -364,9 +363,3 @@ def read_python_file(path):
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()
2 changes: 1 addition & 1 deletion test/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def test_comment_after_statement(self):
ControlLine('if', 'endif #end', True, (6, 1))]))

def test_crlf(self):
template = open(self._file_path("crlf.html"), 'rb').read()
template = util.read_file(self._file_path("crlf.html"))
nodes = Lexer(template).parse()
self._compare(
nodes,
Expand Down
12 changes: 6 additions & 6 deletions test/test_lru.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def testlru(self):
l[id] = item(id)

# first couple of items should be gone
self.assert_(1 not in l)
self.assert_(2 not in l)
assert 1 not in l
assert 2 not in l

# next batch over the threshold of 10 should be present
for id in range(11,20):
self.assert_(id in l)
assert id in l

l[12]
l[15]
Expand All @@ -38,11 +38,11 @@ def testlru(self):
l[26] = item(26)
l[27] = item(27)

self.assert_(11 not in l)
self.assert_(13 not in l)
assert 11 not in l
assert 13 not in l

for id in (25, 24, 23, 14, 12, 19, 18, 17, 16, 15):
self.assert_(id in l)
assert id in l

def _disabled_test_threaded(self):
size = 100
Expand Down
9 changes: 5 additions & 4 deletions test/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from mako.ext.preprocessors import convert_comments
from mako import exceptions, runtime
from mako import compat
from mako import util
import os
from test.util import flatten_result, result_lines
from mako.compat import u
Expand Down Expand Up @@ -219,7 +220,7 @@ def test_unicode_literal_in_tag(self):
)

self._do_memory_test(
open(self._file_path("unicode_arguments.html"), 'rb').read(),
util.read_file(self._file_path("unicode_arguments.html")),
[
u('x is: drôle de petite voix m’a réveillé'),
u('x is: drôle de petite voix m’a réveillé'),
Expand Down Expand Up @@ -1017,16 +1018,16 @@ def _do_test_traceback(self, utf8, memory, syntax):
source = source.encode('utf-8')
else:
source = source
templateargs = {'text':source}
templateargs = {'text': source}
else:
if syntax:
filename = 'unicode_syntax_error.html'
else:
filename = 'unicode_runtime_error.html'
source = open(self._file_path(filename), 'rb').read()
source = util.read_file(self._file_path(filename), 'rb')
if not utf8:
source = source.decode('utf-8')
templateargs = {'filename':self._file_path(filename)}
templateargs = {'filename': self._file_path(filename)}
try:
template = Template(**templateargs)
if not syntax:
Expand Down
4 changes: 2 additions & 2 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def test_fast_buffer_encoded(self):
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 str(data)) # str() for py3k
assert 'test_util' in str(data) # str() for py3k

@skip_if(lambda: compat.pypy, "Pypy does this differently")
def test_load_module(self):
fn = os.path.join(os.path.dirname(__file__), 'test_util.py')
module = util.load_module('mako.template', fn)
module = compat.load_module('mako.template', fn)
import mako.template
self.assertEqual(module, mako.template)

Expand Down

0 comments on commit c16198a

Please sign in to comment.