deprecate before_first_request
This commit is contained in:
parent
7a2d5fb6df
commit
96c97dec09
6 changed files with 66 additions and 33 deletions
|
|
@ -451,6 +451,10 @@ class Flask(Scaffold):
|
|||
#: first request to this instance. To register a function, use the
|
||||
#: :meth:`before_first_request` decorator.
|
||||
#:
|
||||
#: .. deprecated:: 2.2
|
||||
#: Will be removed in Flask 2.3. Run setup code when
|
||||
#: creating the application instead.
|
||||
#:
|
||||
#: .. versionadded:: 0.8
|
||||
self.before_first_request_funcs: t.List[ft.BeforeFirstRequestCallable] = []
|
||||
|
||||
|
|
@ -1255,8 +1259,21 @@ class Flask(Scaffold):
|
|||
The function will be called without any arguments and its return
|
||||
value is ignored.
|
||||
|
||||
.. deprecated:: 2.2
|
||||
Will be removed in Flask 2.3. Run setup code when creating
|
||||
the application instead.
|
||||
|
||||
.. versionadded:: 0.8
|
||||
"""
|
||||
import warnings
|
||||
|
||||
warnings.warn(
|
||||
"'before_first_request' is deprecated and will be removed"
|
||||
" in Flask 2.3. Run setup code while creating the"
|
||||
" application instead.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
self.before_first_request_funcs.append(f)
|
||||
return f
|
||||
|
||||
|
|
@ -1552,7 +1569,17 @@ class Flask(Scaffold):
|
|||
|
||||
.. versionadded:: 0.7
|
||||
"""
|
||||
self.try_trigger_before_first_request_functions()
|
||||
# Run before_first_request functions if this is the thread's first request.
|
||||
# Inlined to avoid a method call on subsequent requests.
|
||||
# This is deprecated, will be removed in Flask 2.3.
|
||||
if not self._got_first_request:
|
||||
with self._before_request_lock:
|
||||
if not self._got_first_request:
|
||||
for func in self.before_first_request_funcs:
|
||||
self.ensure_sync(func)()
|
||||
|
||||
self._got_first_request = True
|
||||
|
||||
try:
|
||||
request_started.send(self)
|
||||
rv = self.preprocess_request()
|
||||
|
|
@ -1591,22 +1618,6 @@ class Flask(Scaffold):
|
|||
)
|
||||
return response
|
||||
|
||||
def try_trigger_before_first_request_functions(self) -> None:
|
||||
"""Called before each request and will ensure that it triggers
|
||||
the :attr:`before_first_request_funcs` and only exactly once per
|
||||
application instance (which means process usually).
|
||||
|
||||
:internal:
|
||||
"""
|
||||
if self._got_first_request:
|
||||
return
|
||||
with self._before_request_lock:
|
||||
if self._got_first_request:
|
||||
return
|
||||
for func in self.before_first_request_funcs:
|
||||
self.ensure_sync(func)()
|
||||
self._got_first_request = True
|
||||
|
||||
def make_default_options_response(self) -> Response:
|
||||
"""This method is called to create the default ``OPTIONS`` response.
|
||||
This can be changed through subclassing to change the default
|
||||
|
|
|
|||
|
|
@ -543,7 +543,20 @@ class Blueprint(Scaffold):
|
|||
) -> ft.BeforeFirstRequestCallable:
|
||||
"""Like :meth:`Flask.before_first_request`. Such a function is
|
||||
executed before the first request to the application.
|
||||
|
||||
.. deprecated:: 2.2
|
||||
Will be removed in Flask 2.3. Run setup code when creating
|
||||
the application instead.
|
||||
"""
|
||||
import warnings
|
||||
|
||||
warnings.warn(
|
||||
"'before_app_first_request' is deprecated and will be"
|
||||
" removed in Flask 2.3. Use 'record_once' instead to run"
|
||||
" setup code when registering the blueprint.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
self.record_once(lambda s: s.app.before_first_request_funcs.append(f))
|
||||
return f
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue