Skip to content
Myr

deprecate markupsafe exports

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 1ee22e1 · Browse files · 8 files+41 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+41 added−20 removed

  • tests:+13 added−11 removed
  • other:+28 added−9 removed

8 files+41 added−20 removed

modified:docs/api.rst-4 removed
215 unmodified lines
216.. autofunction:: send_file217218.. autofunction:: send_from_directory219220.. autofunction:: escape220222.. autoclass:: Markup223 :members: escape, unescape, striptags224221Message Flashing222----------------223224.. autofunction:: flash
516 unmodified lines
modified:docs/security.rst+1 added-1 removed
21 unmodified lines
22in templates, but there are still other places where you have to be23careful:2425- generating HTML without the help of Jinja226- calling :class:`~flask.Markup` on data submitted by users26- calling :class:`~markupsafe.Markup` on data submitted by users27- sending out HTML from uploaded files, never do that, use the28 ``Content-Disposition: attachment`` header to prevent that problem.29- sending out textfiles from uploaded files. Some browsers are using30 content-type guessing based on the first few bytes so users could
244 unmodified lines
modified:docs/templating.rst+1 added-1 removed
113 unmodified lines
114markdown to HTML converter.115116There are three ways to accomplish that:117118- In the Python code, wrap the HTML string in a :class:`~flask.Markup`118- In the Python code, wrap the HTML string in a :class:`~markupsafe.Markup`119 object before passing it to the template. This is in general the120 recommended way.121- Inside the template, use the ``|safe`` filter to explicitly mark a122 string as safe HTML (``{{ myvariable|safe }}``)
107 unmodified lines
modified:src/flask/__init__.py+24 added-3 removed
1from markupsafe import escape2from markupsafe import Markup31from . import json as json2from .app import Flask as Flask3from .app import Request as Request4from .app import Response as Response
59 unmodified lines
64 stacklevel=2,65 )66 return __request_ctx_stack6768 if name == "escape":69 import warnings70 from markupsafe import escape7172 warnings.warn(73 "'flask.escape' is deprecated and will be removed in Flask 2.4. Import"74 " 'markupsafe.escape' instead.",75 DeprecationWarning,76 stacklevel=2,77 )78 return escape7980 if name == "escape":81 import warnings82 from markupsafe import Markup8384 warnings.warn(85 "'flask.Markup' is deprecated and will be removed in Flask 2.4. Import"86 " 'markupsafe.Markup' instead.",87 DeprecationWarning,88 stacklevel=2,89 )90 return Markup9192 raise AttributeError(name)
modified:tests/test_basic.py+9 added-8 removed
7 unmodified lines
8from platform import python_implementation910import pytest11import werkzeug.serving12from markupsafe import Markup13from werkzeug.exceptions import BadRequest14from werkzeug.exceptions import Forbidden15from werkzeug.exceptions import NotFound16from werkzeug.http import parse_date
455 unmodified lines
472 @app.route("/")473 def dump_session_contents():474 flask.session["t"] = (1, 2, 3)475 flask.session["b"] = b"\xff"475 flask.session["m"] = flask.Markup("<html>")476 flask.session["m"] = Markup("<html>")477 flask.session["u"] = the_uuid478 flask.session["d"] = now479 flask.session["t_tag"] = {" t": "not-a-tuple"}480 flask.session["di_t_tag"] = {" t__": "not-a-tuple"}
5 unmodified lines
486 s = flask.session487 assert s["t"] == (1, 2, 3)488 assert type(s["b"]) == bytes489 assert s["b"] == b"\xff"489 assert type(s["m"]) == flask.Markup490 assert s["m"] == flask.Markup("<html>")490 assert type(s["m"]) == Markup491 assert s["m"] == Markup("<html>")492 assert s["u"] == the_uuid493 assert s["d"] == now494 assert s["t_tag"] == {" t": "not-a-tuple"}495 assert s["di_t_tag"] == {" t__": "not-a-tuple"}
115 unmodified lines
611 @app.route("/")612 def index():613 flask.flash("Hello World")614 flask.flash("Hello World", "error")614 flask.flash(flask.Markup("<em>Testing</em>"), "warning")615 flask.flash(Markup("<em>Testing</em>"), "warning")616 return ""617618 @app.route("/test/")619 def test():620 messages = flask.get_flashed_messages()621 assert list(messages) == [622 "Hello World",623 "Hello World",623 flask.Markup("<em>Testing</em>"),624 Markup("<em>Testing</em>"),625 ]626 return ""627628 @app.route("/test_with_categories/")
2 unmodified lines
631 assert len(messages) == 3632 assert list(messages) == [633 ("message", "Hello World"),634 ("error", "Hello World"),634 ("warning", flask.Markup("<em>Testing</em>")),635 ("warning", Markup("<em>Testing</em>")),636 ]637 return ""638639 @app.route("/test_filter/")
10 unmodified lines
650 category_filter=["message", "warning"], with_categories=True651 )652 assert list(messages) == [653 ("message", "Hello World"),653 ("warning", flask.Markup("<em>Testing</em>")),654 ("warning", Markup("<em>Testing</em>")),655 ]656 return ""657658 @app.route("/test_filters_without_returning_categories/")659 def test_filters2():660 messages = flask.get_flashed_messages(category_filter=["message", "warning"])661 assert len(messages) == 2662 assert messages[0] == "Hello World"662 assert messages[1] == flask.Markup("<em>Testing</em>")663 assert messages[1] == Markup("<em>Testing</em>")664 return ""665666 # Create new test client on each test to clean flashed messages.667
1253 unmodified lines
modified:tests/test_json_tag.py+1 added-1 removed
1 unmodified line
2from datetime import timezone3from uuid import uuid445import pytest6from markupsafe import Markup77from flask import Markup8from flask.json.tag import JSONTag9from flask.json.tag import TaggedJSONSerializer1011
75 unmodified lines
modified:tests/test_templating.py+3 added-2 removed
1 unmodified line
23import pytest4import werkzeug.serving5from jinja2 import TemplateNotFound6from markupsafe import Markup78import flask910
62 unmodified lines
7374 @app.route("/")75 def index():76 return flask.render_template(76 "escaping_template.html", text=text, html=flask.Markup(text)77 "escaping_template.html", text=text, html=Markup(text)78 )7980 lines = client.get("/").data.splitlines()81 assert lines == [
11 unmodified lines
9394 @app.route("/")95 def index():96 return flask.render_template(96 "non_escaping_template.txt", text=text, html=flask.Markup(text)97 "non_escaping_template.txt", text=text, html=Markup(text)98 )99100 lines = client.get("/").data.splitlines()101 assert lines == [
350 unmodified lines
modified:CHANGES.rst+2 added
19 unmodified lines
20 - ``json_encoder`` and ``json_decoder`` attributes on app and blueprint, and the21 corresponding ``json.JSONEncoder`` and ``JSONDecoder`` classes, are removed.22 - The ``json.htmlsafe_dumps`` and ``htmlsafe_dump`` functions are removed.2324- Importing ``escape`` and ``Markup`` from ``flask`` is deprecated. Import them25 directly from ``markupsafe`` instead. :pr:`4996`26- Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``.27 :pr:`4947`28- Ensure subdomains are applied with nested blueprints. :issue:`4834`29
1418 unmodified lines