Skip to content

Commit 34ff7d7

Browse files
authored
Merge pull request pallets#4283 from pallets/except-chain
use exception chaining
2 parents ca0033c + 6a4bf9e commit 34ff7d7

File tree

5 files changed

+21
-20
lines changed

5 files changed

+21
-20
lines changed

src/flask/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,7 @@ def async_to_sync(
16211621
except ImportError:
16221622
raise RuntimeError(
16231623
"Install Flask with the 'async' extra in order to use async views."
1624-
)
1624+
) from None
16251625

16261626
# Check that Werkzeug isn't using its fallback ContextVar class.
16271627
if ContextVar.__module__ == "werkzeug.local":
@@ -1727,7 +1727,7 @@ def make_response(self, rv: ResponseReturnValue) -> Response:
17271727
" response. The return type must be a string,"
17281728
" dict, tuple, Response instance, or WSGI"
17291729
f" callable, but it was a {type(rv).__name__}."
1730-
).with_traceback(sys.exc_info()[2])
1730+
).with_traceback(sys.exc_info()[2]) from None
17311731
else:
17321732
raise TypeError(
17331733
"The view function did not return a valid"

src/flask/cli.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,16 @@ def find_best_app(script_info, module):
6969

7070
if isinstance(app, Flask):
7171
return app
72-
except TypeError:
72+
except TypeError as e:
7373
if not _called_with_wrong_args(app_factory):
7474
raise
75+
7576
raise NoAppException(
7677
f"Detected factory {attr_name!r} in module {module.__name__!r},"
7778
" but could not call it without arguments. Use"
7879
f" \"FLASK_APP='{module.__name__}:{attr_name}(args)'\""
7980
" to specify arguments."
80-
)
81+
) from e
8182

8283
raise NoAppException(
8384
"Failed to find Flask application or factory in module"
@@ -161,7 +162,7 @@ def find_app_by_string(script_info, module, app_name):
161162
except SyntaxError:
162163
raise NoAppException(
163164
f"Failed to parse {app_name!r} as an attribute name or function call."
164-
)
165+
) from None
165166

166167
if isinstance(expr, ast.Name):
167168
name = expr.id
@@ -184,33 +185,33 @@ def find_app_by_string(script_info, module, app_name):
184185
# message with the full expression instead.
185186
raise NoAppException(
186187
f"Failed to parse arguments as literal values: {app_name!r}."
187-
)
188+
) from None
188189
else:
189190
raise NoAppException(
190191
f"Failed to parse {app_name!r} as an attribute name or function call."
191192
)
192193

193194
try:
194195
attr = getattr(module, name)
195-
except AttributeError:
196+
except AttributeError as e:
196197
raise NoAppException(
197198
f"Failed to find attribute {name!r} in {module.__name__!r}."
198-
)
199+
) from e
199200

200201
# If the attribute is a function, call it with any args and kwargs
201202
# to get the real application.
202203
if inspect.isfunction(attr):
203204
try:
204205
app = call_factory(script_info, attr, args, kwargs)
205-
except TypeError:
206+
except TypeError as e:
206207
if not _called_with_wrong_args(attr):
207208
raise
208209

209210
raise NoAppException(
210211
f"The factory {app_name!r} in module"
211212
f" {module.__name__!r} could not be called with the"
212213
" specified arguments."
213-
)
214+
) from e
214215
else:
215216
app = attr
216217

@@ -257,16 +258,15 @@ def locate_app(script_info, module_name, app_name, raise_if_not_found=True):
257258

258259
try:
259260
__import__(module_name)
260-
except ImportError:
261+
except ImportError as e:
261262
# Reraise the ImportError if it occurred within the imported module.
262263
# Determine this by checking whether the trace has a depth > 1.
263264
if sys.exc_info()[2].tb_next:
264265
raise NoAppException(
265-
f"While importing {module_name!r}, an ImportError was"
266-
f" raised:\n\n{traceback.format_exc()}"
267-
)
266+
f"While importing {module_name!r}, an ImportError was raised."
267+
) from e
268268
elif raise_if_not_found:
269-
raise NoAppException(f"Could not import {module_name!r}.")
269+
raise NoAppException(f"Could not import {module_name!r}.") from e
270270
else:
271271
return
272272

@@ -725,7 +725,7 @@ def convert(self, value, param, ctx):
725725
"Using ad-hoc certificates requires the cryptography library.",
726726
ctx,
727727
param,
728-
)
728+
) from None
729729

730730
return value
731731

src/flask/debughelpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ class newcls(oldcls):
8383
def __getitem__(self, key):
8484
try:
8585
return oldcls.__getitem__(self, key)
86-
except KeyError:
86+
except KeyError as e:
8787
if key not in request.form:
8888
raise
89-
raise DebugFilesKeyError(request, key)
89+
90+
raise DebugFilesKeyError(request, key) from e
9091

9192
newcls.__name__ = oldcls.__name__
9293
newcls.__module__ = oldcls.__module__

src/flask/scaffold.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ def register_error_handler(
715715
f"'{code_or_exception}' is not a recognized HTTP error"
716716
" code. Use a subclass of HTTPException with that code"
717717
" instead."
718-
)
718+
) from None
719719

720720
self.error_handler_spec[None][code][exc_class] = t.cast(
721721
"ErrorHandlerCallable[Exception]", f

src/flask/signals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _fail(self, *args: t.Any, **kwargs: t.Any) -> t.Any:
2929
raise RuntimeError(
3030
"Signalling support is unavailable because the blinker"
3131
" library is not installed."
32-
)
32+
) from None
3333

3434
connect = connect_via = connected_to = temporarily_connected_to = _fail
3535
disconnect = _fail

0 commit comments

Comments
 (0)