Convert Flask.run into a noop when run from the CLI
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 42fbbb4 · Browse files · 4 files·+30 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+30 added−0 removed
- other:+30 added−0 removed
Provenance
- Commit
c9b33d0- Tree
862f97d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
42fbbb4
4 files changed+30 added−0 removed
modified:flask/app.py+7 added
822 unmodified lines823 Werkzeug server. See824 :func:`werkzeug.serving.run_simple` for more825 information.826 """827+ # Change this into a no-op if the server is invoked from the828+ # command line. Have a look at cli.py for more information.829+ if os.environ.get('FLASK_RUN_FROM_CLI_SERVER') == '1':830+ from .debughelpers import explain_ignored_app_run831+ explain_ignored_app_run()832+ return833+834 from werkzeug.serving import run_simple835 _host = '127.0.0.1'836 _port = 5000837 server_name = self.config.get("SERVER_NAME")1170 unmodified linesmodified:flask/cli.py+7 added
410 unmodified lines411 Flask is enabled and disabled otherwise.412 """413 from werkzeug.serving import run_simple414 415+ # Set a global flag that indicates that we were invoked from the416+ # command line interface provided server command. This is detected417+ # by Flask.run to make the call into a no-op. This is necessary to418+ # avoid ugly errors when the script that is loaded here also attempts419+ # to start a server.420+ os.environ['FLASK_RUN_FROM_CLI_SERVER'] = '1'421+422 debug = get_debug_flag()423 if reload is None:424 reload = bool(debug)425 if debugger is None:99 unmodified linesmodified:flask/debughelpers.py+12 added
6 unmodified lines7 8 :copyright: (c) 2015 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11+import os12+from warnings import warn13+14 from ._compat import implements_to_string, text_type15 from .app import Flask16 from .blueprints import Blueprint17 from .globals import _request_ctx_stack137 unmodified lines155 info.append(' Maybe you did not place a template in the right folder?')156 info.append(' See http://flask.pocoo.org/docs/blueprints/#templates')157 158 app.logger.info('\n'.join(info))159+160+161+def explain_ignored_app_run():162+ if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':163+ warn(Warning('Silently ignoring app.run() because the '164+ 'application is run from the flask command line '165+ 'executable. Consider putting app.run() behind an '166+ 'if __name__ == "__main__" guard to silence this '167+ 'warning.'), stacklevel=3)modified:CHANGES+4 added
6 unmodified lines7 ------------8 9 Major release, unreleased10 11+- Make `app.run()` into a noop if a Flask application is run from the12+ development server on the command line. This avoids some behavior that13+ was confusing to debug for newcomers.14+15 Version 0.12.116 --------------17 18 Bugfix release, unreleased604 unmodified lines