From aafe94d97a75ffd82f8d38c0a9b5d7e6d250e3e7 Mon Sep 17 00:00:00 2001 From: David Lord Date: Sat, 10 Apr 2021 10:22:24 -0700 Subject: Markup and escape should be imported from markupsafe --- docs/api.rst | 18 ------------------ src/jinja2/__init__.py | 5 ++--- src/jinja2/utils.py | 38 ++++++++++++++++++++++++++++++-------- tests/test_asyncfilters.py | 2 +- tests/test_filters.py | 2 +- tests/test_regression.py | 2 +- tests/test_security.py | 2 +- tests/test_tests.py | 2 +- 8 files changed, 37 insertions(+), 34 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index ce3a26e..79efd87 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -609,28 +609,10 @@ functions to a Jinja environment. .. autofunction:: jinja2.environmentfunction -.. function:: escape(s) - - Convert the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in string `s` - to HTML-safe sequences. Use this if you need to display text that might - contain such characters in HTML. This function will not escaped objects - that do have an HTML representation such as already escaped data. - - The return value is a :class:`Markup` string. - .. autofunction:: jinja2.clear_caches .. autofunction:: jinja2.is_undefined -.. autoclass:: jinja2.Markup([string]) - :members: escape, unescape, striptags - -.. admonition:: Note - - The Jinja :class:`Markup` class is compatible with at least Pylons and - Genshi. It's expected that more template engines and framework will pick - up the `__html__` concept soon. - Exceptions ---------- diff --git a/src/jinja2/__init__.py b/src/jinja2/__init__.py index 2682304..bdb49c5 100644 --- a/src/jinja2/__init__.py +++ b/src/jinja2/__init__.py @@ -2,9 +2,6 @@ non-XML syntax that supports inline expressions and an optional sandboxed environment. """ -from markupsafe import escape -from markupsafe import Markup - from .bccache import BytecodeCache from .bccache import FileSystemBytecodeCache from .bccache import MemcachedBytecodeCache @@ -36,8 +33,10 @@ from .runtime import Undefined from .utils import clear_caches from .utils import contextfunction from .utils import environmentfunction +from .utils import escape from .utils import evalcontextfunction from .utils import is_undefined +from .utils import Markup from .utils import pass_context from .utils import pass_environment from .utils import pass_eval_context diff --git a/src/jinja2/utils.py b/src/jinja2/utils.py index 6150578..80769a7 100644 --- a/src/jinja2/utils.py +++ b/src/jinja2/utils.py @@ -12,8 +12,7 @@ from threading import Lock from types import CodeType from urllib.parse import quote_from_bytes -from markupsafe import escape -from markupsafe import Markup +import markupsafe if t.TYPE_CHECKING: F = t.TypeVar("F", bound=t.Callable[..., t.Any]) @@ -332,9 +331,9 @@ def urlize( def trim_url(x): return x - words = re.split(r"(\s+)", str(escape(text))) - rel_attr = f' rel="{escape(rel)}"' if rel else "" - target_attr = f' target="{escape(target)}"' if target else "" + words = re.split(r"(\s+)", str(markupsafe.escape(text))) + rel_attr = f' rel="{markupsafe.escape(rel)}"' if rel else "" + target_attr = f' target="{markupsafe.escape(target)}"' if target else "" for i, word in enumerate(words): head, middle, tail = "", word, "" @@ -448,7 +447,9 @@ def generate_lorem_ipsum(n=5, html=True, min=20, max=100): if not html: return "\n\n".join(result) - return Markup("\n".join(f"

{escape(x)}

" for x in result)) + return markupsafe.Markup( + "\n".join(f"

{markupsafe.escape(x)}

" for x in result) + ) def url_quote(obj: t.Any, charset: str = "utf-8", for_qs: bool = False) -> str: @@ -701,7 +702,7 @@ def select_autoescape( def htmlsafe_json_dumps( obj: t.Any, dumps: t.Optional[t.Callable[..., str]] = None, **kwargs: t.Any -) -> Markup: +) -> markupsafe.Markup: """Serialize an object to a string of JSON with :func:`json.dumps`, then replace HTML-unsafe characters with Unicode escapes and mark the result safe with :class:`~markupsafe.Markup`. @@ -730,7 +731,7 @@ def htmlsafe_json_dumps( if dumps is None: dumps = json.dumps - return Markup( + return markupsafe.Markup( dumps(obj, **kwargs) .replace("<", "\\u003c") .replace(">", "\\u003e") @@ -837,3 +838,24 @@ try: have_async_gen = True except SyntaxError: have_async_gen = False + + +class Markup(markupsafe.Markup): + def __init__(self, *args, **kwargs): + warnings.warn( + "'jinja2.Markup' is deprecated and will be removed in Jinja" + " 3.1. Import 'markupsafe.Markup' instead.", + DeprecationWarning, + stacklevel=2, + ) + super().__init__(*args, **kwargs) + + +def escape(s): + warnings.warn( + "'jinja2.escape' is deprecated and will be removed in Jinja" + " 3.1. Import 'markupsafe.escape' instead.", + DeprecationWarning, + stacklevel=2, + ) + return markupsafe.escape(s) diff --git a/tests/test_asyncfilters.py b/tests/test_asyncfilters.py index b6025ac..f5fcbf2 100644 --- a/tests/test_asyncfilters.py +++ b/tests/test_asyncfilters.py @@ -1,10 +1,10 @@ from collections import namedtuple import pytest +from markupsafe import Markup from jinja2 import Environment from jinja2.asyncsupport import auto_aiter -from jinja2.utils import Markup async def make_aiter(iter): diff --git a/tests/test_filters.py b/tests/test_filters.py index 0843246..2195157 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -2,9 +2,9 @@ import random from collections import namedtuple import pytest +from markupsafe import Markup from jinja2 import Environment -from jinja2 import Markup from jinja2 import StrictUndefined from jinja2 import TemplateRuntimeError from jinja2 import UndefinedError diff --git a/tests/test_regression.py b/tests/test_regression.py index 8e86e41..5d7a7fb 100644 --- a/tests/test_regression.py +++ b/tests/test_regression.py @@ -613,7 +613,7 @@ class TestBug: assert tmpl.render(values=[]) == "0" def test_markup_and_chainable_undefined(self): - from jinja2 import Markup + from markupsafe import Markup from jinja2.runtime import ChainableUndefined assert str(Markup(ChainableUndefined())) == "" diff --git a/tests/test_security.py b/tests/test_security.py index 1b64cd3..0e8dc5c 100644 --- a/tests/test_security.py +++ b/tests/test_security.py @@ -1,7 +1,7 @@ import pytest +from markupsafe import escape from jinja2 import Environment -from jinja2 import escape from jinja2.exceptions import SecurityError from jinja2.exceptions import TemplateRuntimeError from jinja2.exceptions import TemplateSyntaxError diff --git a/tests/test_tests.py b/tests/test_tests.py index 4d56a15..75178d6 100644 --- a/tests/test_tests.py +++ b/tests/test_tests.py @@ -1,7 +1,7 @@ import pytest +from markupsafe import Markup from jinja2 import Environment -from jinja2 import Markup from jinja2 import TemplateAssertionError from jinja2 import TemplateRuntimeError -- cgit v1.2.1