Skip to content
Myr

Merge pull request #4662 from pallets/fix-run-skip

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

re-add flag to skip unguarded app.run from CLI

authored by David Lord · committed by GitHub · parents ab36542, 5544d09 · Browse files · 3 files+9 added−7 removed
This commit

State

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

Diff stats

3 files+9 added−7 removed

  • tests:+2 added−5 removed
  • other:+7 added−2 removed

3 files+9 added−7 removed

modified:src/flask/app.py+2 added-2 removed
914 unmodified lines
915 # the 'flask run' command is used.916 if os.environ.get("FLASK_RUN_FROM_CLI") == "true":917 if not is_running_from_reloader():918 click.secho(919 " * Ignoring a call to 'app.run()', the server is"920 " already being run with the 'flask run' command.\n"919 " * Ignoring a call to 'app.run()' that would block"920 " the current 'flask' CLI command.\n"921 " Only call 'app.run()' in an 'if __name__ =="922 ' "__main__"\' guard.',923 fg="red",924 )
1389 unmodified lines
modified:src/flask/cli.py+5 added
713 unmodified lines
714 args: list[str],715 parent: click.Context | None = None,716 **extra: t.Any,717 ) -> click.Context:718 # Set a flag to tell app.run to become a no-op. If app.run was719 # not in a __name__ == __main__ guard, it would start the server720 # when importing, blocking whatever command is being called.721 os.environ["FLASK_RUN_FROM_CLI"] = "true"722723 # Attempt to load .env and .flask env files. The --env-file724 # option can cause another file to be loaded.725 if get_load_dotenv(self.load_dotenv):726 load_dotenv()
434 unmodified lines
modified:tests/test_cli.py+2 added-5 removed
11 unmodified lines
12import pytest13from _pytest.monkeypatch import notset14from click.testing import CliRunner1516from flask import _app_ctx_stack16from flask import Blueprint17from flask import current_app18from flask import Flask19from flask.cli import AppGroup
300 unmodified lines
320 def check(value):321 app = click.get_current_context().obj.load_app()322 # the loaded app should be the same as current_app323 same_app = current_app._get_current_object() is app325 # only one app context should be pushed326 stack_size = len(_app_ctx_stack._local.stack)327 return same_app, stack_size, value324 return same_app, value325326 cli = FlaskGroup(create_app=lambda: app)327 result = runner.invoke(cli, ["check", "x"], standalone_mode=False)331 assert result.return_value == (True, 1, True)328 assert result.return_value == (True, True)329330331def test_with_appcontext(runner):332 @click.command()
369 unmodified lines