Commit graph

538 commits

Author SHA1 Message Date
David Lord
2e3276e3dd blueprint name may not contain a dot 2021-05-13 14:31:50 -07:00
pgjones
8c9c931f28 Remove the async helper method
It is better to encourage users to utilise the app ensure_sync method
(or the newely added async_to_sync method) so that any extensions that
alter these methods take affect throughout the users code.

With the helper method users code fix parts of their code to the
asgiref async_to_sync ignoring any extension changes.
2021-05-03 06:18:41 -07:00
pgjones
271eede7ad Simplify the async handling code
Firstly `run_sync` was a misleading name as it didn't run anything,
instead I think `async_to_sync` is much clearer as it converts a
coroutine function to a function. (Name stolen from asgiref).

Secondly trying to run the ensure_sync during registration made the
code more complex and brittle, e.g. the _flask_async_wrapper
usage. This was done to pay any setup costs during registration rather
than runtime, however this only saved a iscoroutne check. It allows
the weirdness of the Blueprint and Scaffold ensure_sync methods to be
removed.

Switching to runtime ensure_sync usage provides a method for
extensions to also support async, as now documented.
2021-05-03 06:15:39 -07:00
Adrian Moennich
d235ab0faa Allow using Click 7 with a DeprecationWarning
As long as popular libraries (e.g. Celery) require click 7, depending
on Click 8 in Flask makes it hard to test the latest version (and its
other dependencies) in existing applications.
2021-04-27 16:45:06 +02:00
pgjones
2fe5f3d65c Fix wrapped view function comparison
Wrapped functions are not comparable, see
https://bugs.python.org/issue3564, therefore a marker is used to note
when the function has been sync wrapped to allow comparison with the
wrapped function instead.

This ensures that multiple route decorators work without raising
exceptions i.e.,

    @app.route("/")
    @app.route("/a")
    async def index():
        ...

works.
2021-04-16 12:34:51 +01:00
pgjones
257c7c5c2a Nested blueprints
This allows blueprints to be nested within blueprints via a new
Blueprint.register_blueprint method. This should provide a use case
that has been desired for the past ~10 years.

This works by setting the endpoint name to be the blueprint names,
from parent to child delimeted by "." and then iterating over the
blueprint names in reverse order in the app (from most specific to
most general). This means that the expectation of nesting a blueprint
within a nested blueprint is met.
2021-04-14 09:25:42 -07:00
David Lord
bb02c14740 skip async tests if asgiref isn't installed 2021-04-06 15:33:06 -07:00
pgjones
d65f574bff Alter ensure_sync implementation to support extensions
This allows extensions to override the Flask.ensure_sync method and
have the change apply to blueprints as well. Without this change it is
possible for differing blueprints to have differing ensure_sync
approaches depending on the extension used - which would likely result
in event-loop blocking issues.

This also allows blueprints to have a custom ensure_sync, although
this is a by product rather than an expected use case.
2021-04-06 15:33:06 -07:00
pgjones
47e0f2aab8 Raise a runtime error if run_async is called without real ContextVars
Werkzeug offers a ContextVar replacement for Python < 3.7, however it
doesn't work across asyncio tasks, hence it makes sense to error out
rather than find there are odd bugs.

Note the docs build requires the latest (dev) Werkzeug due to this
change (to import ContextVar from werkzeug.local).
2021-04-06 09:35:10 -07:00
pgjones
c9f774d650 Add async support
This allows for async functions to be passed to the Flask class
instance, for example as a view function,

    @app.route("/")
    async def index():
        return "Async hello"

this comes with a cost though of poorer performance than using the
sync equivalent.

asgiref is the standard way to run async code within a sync context,
and is used in Django making it a safe and sane choice for this.
2021-04-06 09:35:10 -07:00
Grey Li
5d11a3d105 Set default encoding to UTF-8 for load_dotenv 2021-03-10 21:40:29 +08:00
pgjones
26932dad46 Add syntatic sugar for route registration
This takes a popular API whereby instead of passing the HTTP method as
an argument to route it is instead used as the method name i.e.

    @app.route("/", methods=["POST"])

is now writeable as,

    @app.post("/")

This is simply syntatic sugar, it doesn't do anything else, but makes
it slightly easier for users.

I've included all the methods that are relevant and aren't auto
generated i.e. not connect, head, options, and trace.
2021-03-08 08:55:14 -08:00
Grey Li
2d8caf350e update json.dumps for http_date changes 2021-02-25 09:45:55 -08:00
David Lord
1d8ade59d2 remove test relying on Werkzeug Local internals 2021-02-08 18:04:02 -08:00
David Lord
39d978f42b use Jinja's tojson filter 2021-02-01 22:48:09 -08:00
Matthew Preble
5aa2518a34 Ensure session_interface.open_session is called after URL matching (#3776) 2021-02-01 20:41:17 -08:00
Grey Li
92b39077a5 Silence pytest warnings for exception propagation test 2021-01-03 13:57:45 +08:00
David Lord
26bb6b1c56 move send_file and send_from_directory to Werkzeug
The implementations were moved to Werkzeug, Flask's functions become
wrappers around Werkzeug to pass some Flask-specific values.

cache_timeout is renamed to max_age. SEND_FILE_MAX_AGE_DEFAULT,
app.send_file_max_age_default, and app.get_send_file_max_age defaults
to None. This tells the browser to use conditional requests rather than
a 12 hour cache.

attachment_filename is renamed to download_name, and is always sent if
a name is known.

Deprecate helpers.safe_join in favor of werkzeug.utils.safe_join.

Removed most of the send_file tests, they're tested in Werkzeug.

In the file upload example, renamed the uploaded_file view to
download_file to avoid a common source of confusion.
2020-11-05 09:27:52 -08:00
Mathurshan Vimalesvaran
8819687447 include samesite and secure when removing session cookie (#3726) 2020-11-04 18:16:05 -08:00
David Lord
eecaf84333 update tests for new werkzeug client (#3827)
Flask's client.open mirrors Werkzeug's for processing an existing
environ.

Always test with latest code for other Pallets projects. This will
be changed back once the new versions are released.
2020-11-04 18:00:21 -08:00
David Lord
8817c77eb0 update requirements (#3823) 2020-11-01 05:30:02 -08:00
David Lord
ea4b52e85f test json.dumps for object with __html__ method 2020-10-31 20:18:25 -07:00
David Lord
91e1437ba6 move json tests to separate file 2020-10-31 20:16:24 -07:00
Paul Sanders
106f6ba7e5 parametrize some tests (#3786) 2020-10-11 19:16:17 -07:00
Bogdan Opanchuk
849cf2c2a9 Break reference cycle created by default in Flask instances.
Flask instances with static folders were creating a reference cycle
via their "static" view function (which held a strong reference back
to the Flask instance to call its `send_static_file` method). This
prevented CPython from freeing the memory for a Flask instance
when all external references to it were released.

Now use a weakref for the back reference to avoid this.

Co-authored-by: Joshua Bronson <jab@users.noreply.github.com>
2020-10-03 10:05:05 -04:00
David Lord
8b9128ee06 cleaner message when CLI can't load app
When loading the app fails for the --help command, only the error
message is shown, then the help text. The full traceback is shown for
other exceptions. Also show the message when loading fails while
getting a command, instead of only "command not found". The error
message goes to stderr to match other error behavior, and is in red
with an extra newline to make it more obvious next to the help text.

Also fixes an issue with the test_apps fixture that caused an imported
app to still be importable after the test was over and the path was
reset. Now the module cache is reset as well.
2020-07-30 18:36:55 -07:00
David Lord
ee7d80bb55 Merge pull request #3699 from MartinThoma/style
remove unnecessary docstrings from tests
2020-07-28 19:44:23 -07:00
Martin Thoma
090c9e9a81 DOC: Remove unnecessary docstrings 2020-07-26 15:47:12 +02:00
Christopher Nguyen
95653896a4 change make_response to use headers.update 2020-07-23 16:53:39 -07:00
Joshua Bronson
06f7f34d76 Merge pull request #3560 from greyli/fix-env-chdir
Stop change CWD to .env/.flaskenv location
2020-07-18 13:14:09 -04:00
Joshua Bronson
f81846c68b Restore support for using pathlib.Path for static_folder.
* No longer causes AttributeError: 'PosixPath' object has no
  attribute 'rstrip'.

* This was broken by e6178fe489
  which was released in 1.1.2.

* Add a regression test that now passes.

See #3557.
2020-07-06 08:55:19 -04:00
Grey Li
62c582e19b Stop change CWD to .env/.flaskenv location 2020-07-05 09:37:03 +08:00
jackwardell
148c89bccd add assert message for errorhandler exception type 2020-04-16 10:49:42 -07:00
David Lord
042754ba4a use ast to parse FLASK_APP
enables keyword arguments to factory functions
2020-04-07 17:54:51 -07:00
David Lord
f2a8087cf5 deprecate passing script_info to factory 2020-04-07 15:54:36 -07:00
David Lord
1039d7834b deprecate JSON encoding options
make consistent with built-in json module
2020-04-07 12:32:27 -07:00
David Lord
187cfe90b8 remove simplejson
- remove encoding detection backport, json.loads supports it directly
- use str.translate instead of multiple str.replace
2020-04-07 09:55:39 -07:00
David Lord
296ef35943 remove unused module docstrings 2020-04-04 12:28:08 -07:00
David Lord
f5038c4c39 f-strings everywhere 2020-04-04 12:10:00 -07:00
David Lord
67bb43b738 apply pyupgrade 2020-04-04 12:10:00 -07:00
David Lord
1e5e3aae8a remove more compat code 2020-04-04 12:10:00 -07:00
David Lord
24723fa6e8 remove _compat module 2020-04-04 12:10:00 -07:00
David Lord
8cf6480f91 remove deprecated code 2020-04-04 12:10:00 -07:00
David Lord
8433de53c9 drop support for Python 2.7 and 3.5 2020-04-04 12:09:55 -07:00
David Lord
376a64137c Merge remote-tracking branch 'origin/1.1.x' 2020-02-15 10:40:32 -08:00
frostming
c4de6a0bfc strip the ending slash for static_url_path 2020-02-10 18:19:25 -08:00
David Lord
d1ffe44185 cli checks for cryptography library 2020-02-10 17:34:19 -08:00
Marc Hernandez Cabot
ed93b738ed fix docstring and remove redundant parentheses 2020-02-10 17:03:52 -08:00
raymond-devries
5ffacfc010 Feature request #3445. 2020-02-10 13:09:53 -08:00
David Lord
c5366e7e30 Merge branch '1.1.x' 2020-02-10 10:16:20 -08:00