jinja2: migrate to soft_str (#1)

This commit is contained in:
Phillip Stephens 2022-03-23 21:06:48 -07:00 committed by GitHub
parent 5772c54b99
commit 64ffca228b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 14 deletions

View File

@ -10,7 +10,7 @@ from itertools import groupby
from markupsafe import escape from markupsafe import escape
from markupsafe import Markup from markupsafe import Markup
from markupsafe import soft_unicode from markupsafe import soft_str
from ._compat import abc from ._compat import abc
from ._compat import imap from ._compat import imap
@ -191,18 +191,18 @@ def do_replace(eval_ctx, s, old, new, count=None):
): ):
s = escape(s) s = escape(s)
else: else:
s = soft_unicode(s) s = soft_str(s)
return s.replace(soft_unicode(old), soft_unicode(new), count) return s.replace(soft_str(old), soft_str(new), count)
def do_upper(s): def do_upper(s):
"""Convert a value to uppercase.""" """Convert a value to uppercase."""
return soft_unicode(s).upper() return soft_str(s).upper()
def do_lower(s): def do_lower(s):
"""Convert a value to lowercase.""" """Convert a value to lowercase."""
return soft_unicode(s).lower() return soft_str(s).lower()
@evalcontextfilter @evalcontextfilter
@ -245,7 +245,7 @@ def do_capitalize(s):
"""Capitalize a value. The first character will be uppercase, all others """Capitalize a value. The first character will be uppercase, all others
lowercase. lowercase.
""" """
return soft_unicode(s).capitalize() return soft_str(s).capitalize()
def do_title(s): def do_title(s):
@ -255,7 +255,7 @@ def do_title(s):
return "".join( return "".join(
[ [
item[0].upper() + item[1:].lower() item[0].upper() + item[1:].lower()
for item in _word_beginning_split_re.split(soft_unicode(s)) for item in _word_beginning_split_re.split(soft_str(s))
if item if item
] ]
) )
@ -495,7 +495,7 @@ def do_join(eval_ctx, value, d=u"", attribute=None):
return d.join(value) return d.join(value)
# no html involved, to normal joining # no html involved, to normal joining
return soft_unicode(d).join(imap(soft_unicode, value)) return soft_str(d).join(imap(soft_str, value))
def do_center(value, width=80): def do_center(value, width=80):
@ -761,7 +761,7 @@ def do_wordwrap(
def do_wordcount(s): def do_wordcount(s):
"""Count the words in that string.""" """Count the words in that string."""
return len(_word_re.findall(soft_unicode(s))) return len(_word_re.findall(soft_str(s)))
def do_int(value, default=0, base=10): def do_int(value, default=0, base=10):
@ -820,12 +820,12 @@ def do_format(value, *args, **kwargs):
raise FilterArgumentError( raise FilterArgumentError(
"can't handle positional and keyword arguments at the same time" "can't handle positional and keyword arguments at the same time"
) )
return soft_unicode(value) % (kwargs or args) return soft_str(value) % (kwargs or args)
def do_trim(value, chars=None): def do_trim(value, chars=None):
"""Strip leading and trailing characters, by default whitespace.""" """Strip leading and trailing characters, by default whitespace."""
return soft_unicode(value).strip(chars) return soft_str(value).strip(chars)
def do_striptags(value): def do_striptags(value):
@ -1365,7 +1365,7 @@ FILTERS = {
"selectattr": do_selectattr, "selectattr": do_selectattr,
"slice": do_slice, "slice": do_slice,
"sort": do_sort, "sort": do_sort,
"string": soft_unicode, "string": soft_str,
"striptags": do_striptags, "striptags": do_striptags,
"sum": do_sum, "sum": do_sum,
"title": do_title, "title": do_title,

View File

@ -6,7 +6,7 @@ from types import MethodType
from markupsafe import escape # noqa: F401 from markupsafe import escape # noqa: F401
from markupsafe import Markup from markupsafe import Markup
from markupsafe import soft_unicode from markupsafe import soft_str
from ._compat import abc from ._compat import abc
from ._compat import imap from ._compat import imap
@ -62,7 +62,7 @@ def identity(x):
def markup_join(seq): def markup_join(seq):
"""Concatenation that escapes if necessary and converts to unicode.""" """Concatenation that escapes if necessary and converts to unicode."""
buf = [] buf = []
iterator = imap(soft_unicode, seq) iterator = imap(soft_str, seq)
for arg in iterator: for arg in iterator:
buf.append(arg) buf.append(arg)
if hasattr(arg, "__html__"): if hasattr(arg, "__html__"):