Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pystache_custom
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pystache_custom
Commits
b042d084
Commit
b042d084
authored
Apr 08, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More unit tests for Python 3: type-checking in Renderer.render() for byte strings.
parent
b06ea722
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
7 deletions
+20
-7
pystache/renderer.py
+14
-1
pystache/tests/test_renderer.py
+6
-6
No files found.
pystache/renderer.py
View file @
b042d084
...
...
@@ -5,6 +5,8 @@ This module provides a Renderer class to render templates.
"""
import
sys
from
pystache
import
defaults
from
pystache.context
import
Context
from
pystache.loader
import
Loader
...
...
@@ -13,6 +15,17 @@ from pystache.specloader import SpecLoader
from
pystache.template_spec
import
TemplateSpec
# TODO: come up with a better solution for this. One of the issues here
# is that in Python 3 there is no common base class for unicode strings
# and byte strings, and 2to3 seems to convert all of "str", "unicode",
# and "basestring" to Python 3's "str".
if
sys
.
version_info
<
(
3
,
):
_STRING_TYPES
=
basestring
else
:
# The latter evaluates to "bytes" in Python 3 -- even after conversion by 2to3.
_STRING_TYPES
=
(
unicode
,
type
(
u"a"
.
encode
(
'utf-8'
)))
class
Renderer
(
object
):
"""
...
...
@@ -336,7 +349,7 @@ class Renderer(object):
all items in the *context list.
"""
if
isinstance
(
template
,
basestring
):
if
isinstance
(
template
,
_STRING_TYPES
):
return
self
.
_render_string
(
template
,
*
context
,
**
kwargs
)
# Otherwise, we assume the template is an object.
...
...
pystache/tests/test_renderer.py
View file @
b042d084
...
...
@@ -180,13 +180,13 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
"""
renderer
=
Renderer
()
s
=
"é"
b
=
u"é"
.
encode
(
'utf-8'
)
renderer
.
string_encoding
=
"ascii"
self
.
assertRaises
(
UnicodeDecodeError
,
renderer
.
unicode
,
s
)
self
.
assertRaises
(
UnicodeDecodeError
,
renderer
.
unicode
,
b
)
renderer
.
string_encoding
=
"utf-8"
self
.
assertEqual
(
renderer
.
unicode
(
s
),
u"é"
)
self
.
assertEqual
(
renderer
.
unicode
(
b
),
u"é"
)
def
test_unicode__decode_errors
(
self
):
"""
...
...
@@ -195,14 +195,14 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
"""
renderer
=
Renderer
()
renderer
.
string_encoding
=
"ascii"
s
=
"déf"
b
=
u"déf"
.
encode
(
'utf-8'
)
renderer
.
decode_errors
=
"ignore"
self
.
assertEqual
(
renderer
.
unicode
(
s
),
"df"
)
self
.
assertEqual
(
renderer
.
unicode
(
b
),
"df"
)
renderer
.
decode_errors
=
"replace"
# U+FFFD is the official Unicode replacement character.
self
.
assertEqual
(
renderer
.
unicode
(
s
),
u'd
\ufffd\ufffd
f'
)
self
.
assertEqual
(
renderer
.
unicode
(
b
),
u'd
\ufffd\ufffd
f'
)
## Test the _make_loader() method.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment