Commit 63d6dd49 by Chris Jerdonek

Python 3 support: use html module instead of cgi.escape().

Python 3.2 deprecates cgi.escape() and introduces the html module in its place.
parent 93a58fc0
...@@ -8,7 +8,12 @@ does not otherwise specify a value. ...@@ -8,7 +8,12 @@ does not otherwise specify a value.
""" """
import cgi try:
# Python 3.2 deprecates cgi.escape() and adds the html module as a replacement.
import html
except ImportError:
import cgi as html
import os import os
import sys import sys
...@@ -42,9 +47,10 @@ SEARCH_DIRS = [os.curdir] # i.e. ['.'] ...@@ -42,9 +47,10 @@ SEARCH_DIRS = [os.curdir] # i.e. ['.']
# The quote=True argument causes double quotes to be escaped, # The quote=True argument causes double quotes to be escaped,
# but not single quotes: # but not single quotes:
# #
# http://docs.python.org/dev/library/html.html#html.escape
# http://docs.python.org/library/cgi.html#cgi.escape # http://docs.python.org/library/cgi.html#cgi.escape
# #
TAG_ESCAPE = lambda u: cgi.escape(u, quote=True) TAG_ESCAPE = lambda u: html.escape(u, quote=True)
# The default template extension. # The default template extension.
TEMPLATE_EXTENSION = 'mustache' TEMPLATE_EXTENSION = 'mustache'
...@@ -64,10 +64,10 @@ class Renderer(object): ...@@ -64,10 +64,10 @@ class Renderer(object):
this class will only pass it unicode strings. The constructor this class will only pass it unicode strings. The constructor
assigns this function to the constructed instance's escape() assigns this function to the constructed instance's escape()
method. method.
The argument defaults to `cgi.escape(s, quote=True)`. To To disable escaping entirely, one can pass `lambda u: u`
disable escaping entirely, one can pass `lambda u: u` as the as the escape function, for example. One may also wish to
escape function, for example. One may also wish to consider consider using markupsafe's escape function: markupsafe.escape().
using markupsafe's escape function: markupsafe.escape(). This argument defaults to the package default.
file_encoding: the name of the default encoding to use when reading file_encoding: the name of the default encoding to use when reading
template files. All templates are converted to unicode prior template files. All templates are converted to unicode prior
......
...@@ -5,10 +5,10 @@ Unit tests of renderengine.py. ...@@ -5,10 +5,10 @@ Unit tests of renderengine.py.
""" """
import cgi
import unittest import unittest
from pystache.context import Context from pystache.context import Context
from pystache import defaults
from pystache.parser import ParsingError from pystache.parser import ParsingError
from pystache.renderengine import RenderEngine from pystache.renderengine import RenderEngine
from pystache.tests.common import AssertStringMixin from pystache.tests.common import AssertStringMixin
...@@ -47,7 +47,7 @@ class RenderTests(unittest.TestCase, AssertStringMixin): ...@@ -47,7 +47,7 @@ class RenderTests(unittest.TestCase, AssertStringMixin):
Create and return a default RenderEngine for testing. Create and return a default RenderEngine for testing.
""" """
escape = lambda s: unicode(cgi.escape(s)) escape = defaults.TAG_ESCAPE
engine = RenderEngine(literal=unicode, escape=escape, load_partial=None) engine = RenderEngine(literal=unicode, escape=escape, load_partial=None)
return engine return engine
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment