Merge pull request #4169 from Rohan-Salwan/dev
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fix raising error during cli lazy loading
authored by David Lord · committed by GitHub · parents 66d9d4f, 2305b05 · Browse files · 3 files·+29 added−8 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+29 added−8 removed
- tests:+18 added−0 removed
- other:+11 added−8 removed
Provenance
- Commit
c3f923d- Tree
8296c6f- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
66d9d4f- Parent 2
2305b05
3 files changed+29 added−8 removed
modified:src/flask/cli.py+9 added-8 removed
310 unmodified lines311 def __init__(self, loader, use_eager_loading=None):312 self.loader = loader313 self._app = None314 self._lock = Lock()315- self._bg_loading_exc_info = None315+ self._bg_loading_exc = None316 317 if use_eager_loading is None:318 use_eager_loading = os.environ.get("WERKZEUG_RUN_MAIN") != "true"319 7 unmodified lines327 __traceback_hide__ = True # noqa: F841328 with self._lock:329 try:330 self._load_unlocked()331- except Exception:332- self._bg_loading_exc_info = sys.exc_info()331+ except Exception as e:332+ self._bg_loading_exc = e333 334 t = Thread(target=_load_app, args=())335 t.start()336 337 def _flush_bg_loading_exception(self):338 __traceback_hide__ = True # noqa: F841339- exc_info = self._bg_loading_exc_info340- if exc_info is not None:341- self._bg_loading_exc_info = None342- raise exc_info339+ exc = self._bg_loading_exc340 341+ if exc is not None:342+ self._bg_loading_exc = None343+ raise exc344+345 def _load_unlocked(self):346 __traceback_hide__ = True # noqa: F841347 self._app = rv = self.loader()347- self._bg_loading_exc_info = None348+ self._bg_loading_exc = None349 return rv350 351 def __call__(self, environ, start_response):352 __traceback_hide__ = True # noqa: F841643 unmodified linesmodified:tests/test_cli.py+18 added
15 unmodified lines16 from flask import Blueprint17 from flask import current_app18 from flask import Flask19 from flask.cli import AppGroup20+from flask.cli import DispatchingApp21 from flask.cli import dotenv22 from flask.cli import find_best_app23 from flask.cli import FlaskGroup24 from flask.cli import get_version285 unmodified lines310 app = obj.load_app()311 assert app.name == "testapp"312 313 314+def test_lazy_load_error(monkeypatch):315+ """When using lazy loading, the correct exception should be316+ re-raised.317+ """318+319+ class BadExc(Exception):320+ pass321+322+ def bad_load():323+ raise BadExc324+325+ lazy = DispatchingApp(bad_load, use_eager_loading=False)326+327+ with pytest.raises(BadExc):328+ lazy._flush_bg_loading_exception()329+330+331 def test_with_appcontext(runner):332 @click.command()333 @with_appcontext334 def testcmd():345 unmodified linesmodified:CHANGES.rst+2 added
15 unmodified lines16 - Fix the type of ``static_folder`` to accept ``pathlib.Path``.17 :issue:`4150`18 - ``jsonify`` handles ``decimal.Decimal`` by encoding to ``str``.19 :issue:`4157`20+- Correctly handle raising deferred errors in CLI lazy loading.21+ :issue:`4096`22 23 24 Version 2.0.125 -------------1154 unmodified lines