Skip to content

Commit

Permalink
fixed encoding (always str in py2 and unicode/str in py3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdipierro committed Feb 18, 2019
1 parent 8775e10 commit 58045dd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
8 changes: 3 additions & 5 deletions yatl/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cgi
from . sanitizer import sanitize, xmlescape
from . sanitizer import sanitize, xmlescape, PY2

try:
# python 2
Expand Down Expand Up @@ -170,11 +170,9 @@ def __init__(

if sanitize:
text = sanitize(text, permitted_tags, allowed_attributes)
if isinstance(text, unicode):
if PY2 and isinstance(text, unicode):
text = text.encode('utf8', 'xmlcharrefreplace')
elif not isinstance(text, str):
text = str(text)
self.text = text
self.text = unicode(text)

def xml(self):
return self.text
Expand Down
30 changes: 12 additions & 18 deletions yatl/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import sys
from functools import wraps
from re import compile, sub, escape, DOTALL
from . helpers import xmlescape

PY2 = sys.version_info[0] == 2

if PY2:
from cgi import escape as html_escape
from cStringIO import StringIO
basestring = basestring
unicodeT = unicode
Expand All @@ -27,8 +27,7 @@ def to_native(obj, charset='utf8', errors='strict'):
return obj if isinstance(obj, str) else obj.encode(charset, errors)

else:
from html import escape as html_escape
from io import StringIO, BytesIO
from io import StringIO
basestring = str
unicodeT = str

Expand Down Expand Up @@ -819,24 +818,18 @@ def __init__(self):

def write(self, data, escape=True):
if not escape:
self.body.write(str(data))
data = str(data)
elif hasattr(data, 'xml') and callable(data.xml):
if PY2:
self.body.write(data.xml())
else:
self.body.write(str(data))
data = data.xml()
else:
# make it a string
if PY2:
if not isinstance(data, (str, unicodeT)):
data = str(data)
elif isinstance(data, unicodeT):
data = data.encode('utf8', 'xmlcharrefreplace')
if PY2 and isinstance(data, unicodeT):
# in python2 we always encode unicode
data = data.encode('utf8', 'xmlcharrefreplace')
else:
if not isinstance(data, str):
data = str(data)
data = html_escape(data, True).replace("'", "'")
self.body.write(data)
# in python3 we always use unicode
data = str(data)
data = xmlescape(data)
self.body.write(str(data))


class NOESCAPE():
Expand Down Expand Up @@ -1005,3 +998,4 @@ def wrapper(*a, **b):
else:
return context
return wrapper

0 comments on commit 58045dd

Please sign in to comment.