From b8e3f87464b12e67141dc9827c11935b5c2d2982 Mon Sep 17 00:00:00 2001 From: Bruno Romero de Azevedo Date: Mon, 9 Dec 2019 11:29:32 +0100 Subject: [PATCH 1/2] Improve python3 support --- pystache/common.py | 2 ++ pystache/parser.py | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pystache/common.py b/pystache/common.py index fb266dd8..44ab680f 100644 --- a/pystache/common.py +++ b/pystache/common.py @@ -14,6 +14,8 @@ def _get_string_types(): # and "basestring" to Python 3's "str". if version_info < (3, ): return basestring + else: + unicode = str # The latter evaluates to "bytes" in Python 3 -- even after conversion by 2to3. return (unicode, type(u"a".encode('utf-8'))) diff --git a/pystache/parser.py b/pystache/parser.py index 9a4fba23..5a6b3eb9 100644 --- a/pystache/parser.py +++ b/pystache/parser.py @@ -6,13 +6,17 @@ """ import re +import sys + +if sys.version_info.major >= 3: + unicode = str from pystache import defaults from pystache.parsed import ParsedTemplate END_OF_LINE_CHARACTERS = [u'\r', u'\n'] -NON_BLANK_RE = re.compile(ur'^(.)', re.M) +NON_BLANK_RE = re.compile(u'^(.)', re.M) # TODO: add some unit tests for this. @@ -146,8 +150,9 @@ def __repr__(self): def render(self, engine, context): template = engine.resolve_partial(self.key) + # Indent before rendering. - template = re.sub(NON_BLANK_RE, self.indent + ur'\1', template) + template = re.sub(NON_BLANK_RE, self.indent + u'\1', template) return engine.render(template, context) From da3138078509c0a8544bfbbf015a2f9d5591785c Mon Sep 17 00:00:00 2001 From: Bruno Romero de Azevedo Date: Mon, 9 Dec 2019 20:45:38 +0100 Subject: [PATCH 2/2] Add missing slash after removing prefix r --- pystache/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pystache/parser.py b/pystache/parser.py index 5a6b3eb9..bd62c17b 100644 --- a/pystache/parser.py +++ b/pystache/parser.py @@ -152,7 +152,7 @@ def render(self, engine, context): template = engine.resolve_partial(self.key) # Indent before rendering. - template = re.sub(NON_BLANK_RE, self.indent + u'\1', template) + template = re.sub(NON_BLANK_RE, self.indent + u'\\1', template) return engine.render(template, context)