forked from orbit-oss/flask
blinker is required, signals are always available
This commit is contained in:
parent
e1e4e82096
commit
9cb1a7a52d
18 changed files with 77 additions and 163 deletions
|
|
@ -32,7 +32,6 @@ from .signals import message_flashed as message_flashed
|
|||
from .signals import request_finished as request_finished
|
||||
from .signals import request_started as request_started
|
||||
from .signals import request_tearing_down as request_tearing_down
|
||||
from .signals import signals_available as signals_available
|
||||
from .signals import template_rendered as template_rendered
|
||||
from .templating import render_template as render_template
|
||||
from .templating import render_template_string as render_template_string
|
||||
|
|
@ -89,4 +88,15 @@ def __getattr__(name):
|
|||
)
|
||||
return Markup
|
||||
|
||||
if name == "signals_available":
|
||||
import warnings
|
||||
|
||||
warnings.warn(
|
||||
"'signals_available' is deprecated and will be removed in Flask 2.4."
|
||||
" Signals are always available",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
return True
|
||||
|
||||
raise AttributeError(name)
|
||||
|
|
|
|||
|
|
@ -1,49 +1,13 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import typing as t
|
||||
import warnings
|
||||
|
||||
try:
|
||||
from blinker import Namespace
|
||||
from blinker import Namespace
|
||||
|
||||
signals_available = True
|
||||
except ImportError:
|
||||
signals_available = False
|
||||
|
||||
class Namespace: # type: ignore
|
||||
def signal(self, name: str, doc: t.Optional[str] = None) -> "_FakeSignal":
|
||||
return _FakeSignal(name, doc)
|
||||
|
||||
class _FakeSignal:
|
||||
"""If blinker is unavailable, create a fake class with the same
|
||||
interface that allows sending of signals but will fail with an
|
||||
error on anything else. Instead of doing anything on send, it
|
||||
will just ignore the arguments and do nothing instead.
|
||||
"""
|
||||
|
||||
def __init__(self, name: str, doc: t.Optional[str] = None) -> None:
|
||||
self.name = name
|
||||
self.__doc__ = doc
|
||||
|
||||
def send(self, *args: t.Any, **kwargs: t.Any) -> t.Any:
|
||||
pass
|
||||
|
||||
def _fail(self, *args: t.Any, **kwargs: t.Any) -> t.Any:
|
||||
raise RuntimeError(
|
||||
"Signalling support is unavailable because the blinker"
|
||||
" library is not installed."
|
||||
) from None
|
||||
|
||||
connect = connect_via = connected_to = temporarily_connected_to = _fail
|
||||
disconnect = _fail
|
||||
has_receivers_for = receivers_for = _fail
|
||||
del _fail
|
||||
|
||||
|
||||
# The namespace for code signals. If you are not Flask code, do
|
||||
# not put signals in here. Create your own namespace instead.
|
||||
# This namespace is only for signals provided by Flask itself.
|
||||
_signals = Namespace()
|
||||
|
||||
|
||||
# Core signals. For usage examples grep the source code or consult
|
||||
# the API documentation in docs/api.rst as well as docs/signals.rst
|
||||
template_rendered = _signals.signal("template-rendered")
|
||||
before_render_template = _signals.signal("before-render-template")
|
||||
request_started = _signals.signal("request-started")
|
||||
|
|
@ -54,3 +18,16 @@ appcontext_tearing_down = _signals.signal("appcontext-tearing-down")
|
|||
appcontext_pushed = _signals.signal("appcontext-pushed")
|
||||
appcontext_popped = _signals.signal("appcontext-popped")
|
||||
message_flashed = _signals.signal("message-flashed")
|
||||
|
||||
|
||||
def __getattr__(name: str) -> t.Any:
|
||||
if name == "signals_available":
|
||||
warnings.warn(
|
||||
"The 'signals_available' attribute is deprecated and will be removed in"
|
||||
" Flask 2.4. Signals are always available.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
return True
|
||||
|
||||
raise AttributeError(name)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue