attr.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. # event/attr.py
  2. # Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. """Attribute implementation for _Dispatch classes.
  8. The various listener targets for a particular event class are represented
  9. as attributes, which refer to collections of listeners to be fired off.
  10. These collections can exist at the class level as well as at the instance
  11. level. An event is fired off using code like this::
  12. some_object.dispatch.first_connect(arg1, arg2)
  13. Above, ``some_object.dispatch`` would be an instance of ``_Dispatch`` and
  14. ``first_connect`` is typically an instance of ``_ListenerCollection``
  15. if event listeners are present, or ``_EmptyListener`` if none are present.
  16. The attribute mechanics here spend effort trying to ensure listener functions
  17. are available with a minimum of function call overhead, that unnecessary
  18. objects aren't created (i.e. many empty per-instance listener collections),
  19. as well as that everything is garbage collectable when owning references are
  20. lost. Other features such as "propagation" of listener functions across
  21. many ``_Dispatch`` instances, "joining" of multiple ``_Dispatch`` instances,
  22. as well as support for subclass propagation (e.g. events assigned to
  23. ``Pool`` vs. ``QueuePool``) are all implemented here.
  24. """
  25. from __future__ import absolute_import
  26. from __future__ import with_statement
  27. import collections
  28. from itertools import chain
  29. import weakref
  30. from . import legacy
  31. from . import registry
  32. from .. import exc
  33. from .. import util
  34. from ..util import threading
  35. from ..util.concurrency import AsyncAdaptedLock
  36. class RefCollection(util.MemoizedSlots):
  37. __slots__ = ("ref",)
  38. def _memoized_attr_ref(self):
  39. return weakref.ref(self, registry._collection_gced)
  40. class _empty_collection(object):
  41. def append(self, element):
  42. pass
  43. def extend(self, other):
  44. pass
  45. def remove(self, element):
  46. pass
  47. def __iter__(self):
  48. return iter([])
  49. def clear(self):
  50. pass
  51. class _ClsLevelDispatch(RefCollection):
  52. """Class-level events on :class:`._Dispatch` classes."""
  53. __slots__ = (
  54. "clsname",
  55. "name",
  56. "arg_names",
  57. "has_kw",
  58. "legacy_signatures",
  59. "_clslevel",
  60. "__weakref__",
  61. )
  62. def __init__(self, parent_dispatch_cls, fn):
  63. self.name = fn.__name__
  64. self.clsname = parent_dispatch_cls.__name__
  65. argspec = util.inspect_getfullargspec(fn)
  66. self.arg_names = argspec.args[1:]
  67. self.has_kw = bool(argspec.varkw)
  68. self.legacy_signatures = list(
  69. reversed(
  70. sorted(
  71. getattr(fn, "_legacy_signatures", []), key=lambda s: s[0]
  72. )
  73. )
  74. )
  75. fn.__doc__ = legacy._augment_fn_docs(self, parent_dispatch_cls, fn)
  76. self._clslevel = weakref.WeakKeyDictionary()
  77. def _adjust_fn_spec(self, fn, named):
  78. if named:
  79. fn = self._wrap_fn_for_kw(fn)
  80. if self.legacy_signatures:
  81. try:
  82. argspec = util.get_callable_argspec(fn, no_self=True)
  83. except TypeError:
  84. pass
  85. else:
  86. fn = legacy._wrap_fn_for_legacy(self, fn, argspec)
  87. return fn
  88. def _wrap_fn_for_kw(self, fn):
  89. def wrap_kw(*args, **kw):
  90. argdict = dict(zip(self.arg_names, args))
  91. argdict.update(kw)
  92. return fn(**argdict)
  93. return wrap_kw
  94. def _do_insert_or_append(self, event_key, is_append):
  95. target = event_key.dispatch_target
  96. assert isinstance(
  97. target, type
  98. ), "Class-level Event targets must be classes."
  99. if not getattr(target, "_sa_propagate_class_events", True):
  100. raise exc.InvalidRequestError(
  101. "Can't assign an event directly to the %s class" % (target,)
  102. )
  103. for cls in util.walk_subclasses(target):
  104. if cls is not target and cls not in self._clslevel:
  105. self.update_subclass(cls)
  106. else:
  107. if cls not in self._clslevel:
  108. self.update_subclass(cls)
  109. if is_append:
  110. self._clslevel[cls].append(event_key._listen_fn)
  111. else:
  112. self._clslevel[cls].appendleft(event_key._listen_fn)
  113. registry._stored_in_collection(event_key, self)
  114. def insert(self, event_key, propagate):
  115. self._do_insert_or_append(event_key, is_append=False)
  116. def append(self, event_key, propagate):
  117. self._do_insert_or_append(event_key, is_append=True)
  118. def update_subclass(self, target):
  119. if target not in self._clslevel:
  120. if getattr(target, "_sa_propagate_class_events", True):
  121. self._clslevel[target] = collections.deque()
  122. else:
  123. self._clslevel[target] = _empty_collection()
  124. clslevel = self._clslevel[target]
  125. for cls in target.__mro__[1:]:
  126. if cls in self._clslevel:
  127. clslevel.extend(
  128. [fn for fn in self._clslevel[cls] if fn not in clslevel]
  129. )
  130. def remove(self, event_key):
  131. target = event_key.dispatch_target
  132. for cls in util.walk_subclasses(target):
  133. if cls in self._clslevel:
  134. self._clslevel[cls].remove(event_key._listen_fn)
  135. registry._removed_from_collection(event_key, self)
  136. def clear(self):
  137. """Clear all class level listeners"""
  138. to_clear = set()
  139. for dispatcher in self._clslevel.values():
  140. to_clear.update(dispatcher)
  141. dispatcher.clear()
  142. registry._clear(self, to_clear)
  143. def for_modify(self, obj):
  144. """Return an event collection which can be modified.
  145. For _ClsLevelDispatch at the class level of
  146. a dispatcher, this returns self.
  147. """
  148. return self
  149. class _InstanceLevelDispatch(RefCollection):
  150. __slots__ = ()
  151. def _adjust_fn_spec(self, fn, named):
  152. return self.parent._adjust_fn_spec(fn, named)
  153. class _EmptyListener(_InstanceLevelDispatch):
  154. """Serves as a proxy interface to the events
  155. served by a _ClsLevelDispatch, when there are no
  156. instance-level events present.
  157. Is replaced by _ListenerCollection when instance-level
  158. events are added.
  159. """
  160. propagate = frozenset()
  161. listeners = ()
  162. __slots__ = "parent", "parent_listeners", "name"
  163. def __init__(self, parent, target_cls):
  164. if target_cls not in parent._clslevel:
  165. parent.update_subclass(target_cls)
  166. self.parent = parent # _ClsLevelDispatch
  167. self.parent_listeners = parent._clslevel[target_cls]
  168. self.name = parent.name
  169. def for_modify(self, obj):
  170. """Return an event collection which can be modified.
  171. For _EmptyListener at the instance level of
  172. a dispatcher, this generates a new
  173. _ListenerCollection, applies it to the instance,
  174. and returns it.
  175. """
  176. result = _ListenerCollection(self.parent, obj._instance_cls)
  177. if getattr(obj, self.name) is self:
  178. setattr(obj, self.name, result)
  179. else:
  180. assert isinstance(getattr(obj, self.name), _JoinedListener)
  181. return result
  182. def _needs_modify(self, *args, **kw):
  183. raise NotImplementedError("need to call for_modify()")
  184. exec_once = (
  185. exec_once_unless_exception
  186. ) = insert = append = remove = clear = _needs_modify
  187. def __call__(self, *args, **kw):
  188. """Execute this event."""
  189. for fn in self.parent_listeners:
  190. fn(*args, **kw)
  191. def __len__(self):
  192. return len(self.parent_listeners)
  193. def __iter__(self):
  194. return iter(self.parent_listeners)
  195. def __bool__(self):
  196. return bool(self.parent_listeners)
  197. __nonzero__ = __bool__
  198. class _CompoundListener(_InstanceLevelDispatch):
  199. __slots__ = (
  200. "_exec_once_mutex",
  201. "_exec_once",
  202. "_exec_w_sync_once",
  203. "_is_asyncio",
  204. )
  205. def __init__(self, *arg, **kw):
  206. super(_CompoundListener, self).__init__(*arg, **kw)
  207. self._is_asyncio = False
  208. def _set_asyncio(self):
  209. self._is_asyncio = True
  210. def _memoized_attr__exec_once_mutex(self):
  211. if self._is_asyncio:
  212. return AsyncAdaptedLock()
  213. else:
  214. return threading.Lock()
  215. def _exec_once_impl(self, retry_on_exception, *args, **kw):
  216. with self._exec_once_mutex:
  217. if not self._exec_once:
  218. try:
  219. self(*args, **kw)
  220. exception = False
  221. except:
  222. exception = True
  223. raise
  224. finally:
  225. if not exception or not retry_on_exception:
  226. self._exec_once = True
  227. def exec_once(self, *args, **kw):
  228. """Execute this event, but only if it has not been
  229. executed already for this collection."""
  230. if not self._exec_once:
  231. self._exec_once_impl(False, *args, **kw)
  232. def exec_once_unless_exception(self, *args, **kw):
  233. """Execute this event, but only if it has not been
  234. executed already for this collection, or was called
  235. by a previous exec_once_unless_exception call and
  236. raised an exception.
  237. If exec_once was already called, then this method will never run
  238. the callable regardless of whether it raised or not.
  239. .. versionadded:: 1.3.8
  240. """
  241. if not self._exec_once:
  242. self._exec_once_impl(True, *args, **kw)
  243. def _exec_w_sync_on_first_run(self, *args, **kw):
  244. """Execute this event, and use a mutex if it has not been
  245. executed already for this collection, or was called
  246. by a previous _exec_w_sync_on_first_run call and
  247. raised an exception.
  248. If _exec_w_sync_on_first_run was already called and didn't raise an
  249. exception, then a mutex is not used.
  250. .. versionadded:: 1.4.11
  251. """
  252. if not self._exec_w_sync_once:
  253. with self._exec_once_mutex:
  254. try:
  255. self(*args, **kw)
  256. except:
  257. raise
  258. else:
  259. self._exec_w_sync_once = True
  260. else:
  261. self(*args, **kw)
  262. def __call__(self, *args, **kw):
  263. """Execute this event."""
  264. for fn in self.parent_listeners:
  265. fn(*args, **kw)
  266. for fn in self.listeners:
  267. fn(*args, **kw)
  268. def __len__(self):
  269. return len(self.parent_listeners) + len(self.listeners)
  270. def __iter__(self):
  271. return chain(self.parent_listeners, self.listeners)
  272. def __bool__(self):
  273. return bool(self.listeners or self.parent_listeners)
  274. __nonzero__ = __bool__
  275. class _ListenerCollection(_CompoundListener):
  276. """Instance-level attributes on instances of :class:`._Dispatch`.
  277. Represents a collection of listeners.
  278. As of 0.7.9, _ListenerCollection is only first
  279. created via the _EmptyListener.for_modify() method.
  280. """
  281. __slots__ = (
  282. "parent_listeners",
  283. "parent",
  284. "name",
  285. "listeners",
  286. "propagate",
  287. "__weakref__",
  288. )
  289. def __init__(self, parent, target_cls):
  290. super(_ListenerCollection, self).__init__()
  291. if target_cls not in parent._clslevel:
  292. parent.update_subclass(target_cls)
  293. self._exec_once = False
  294. self._exec_w_sync_once = False
  295. self.parent_listeners = parent._clslevel[target_cls]
  296. self.parent = parent
  297. self.name = parent.name
  298. self.listeners = collections.deque()
  299. self.propagate = set()
  300. def for_modify(self, obj):
  301. """Return an event collection which can be modified.
  302. For _ListenerCollection at the instance level of
  303. a dispatcher, this returns self.
  304. """
  305. return self
  306. def _update(self, other, only_propagate=True):
  307. """Populate from the listeners in another :class:`_Dispatch`
  308. object."""
  309. existing_listeners = self.listeners
  310. existing_listener_set = set(existing_listeners)
  311. self.propagate.update(other.propagate)
  312. other_listeners = [
  313. l
  314. for l in other.listeners
  315. if l not in existing_listener_set
  316. and not only_propagate
  317. or l in self.propagate
  318. ]
  319. existing_listeners.extend(other_listeners)
  320. if other._is_asyncio:
  321. self._set_asyncio()
  322. to_associate = other.propagate.union(other_listeners)
  323. registry._stored_in_collection_multi(self, other, to_associate)
  324. def insert(self, event_key, propagate):
  325. if event_key.prepend_to_list(self, self.listeners):
  326. if propagate:
  327. self.propagate.add(event_key._listen_fn)
  328. def append(self, event_key, propagate):
  329. if event_key.append_to_list(self, self.listeners):
  330. if propagate:
  331. self.propagate.add(event_key._listen_fn)
  332. def remove(self, event_key):
  333. self.listeners.remove(event_key._listen_fn)
  334. self.propagate.discard(event_key._listen_fn)
  335. registry._removed_from_collection(event_key, self)
  336. def clear(self):
  337. registry._clear(self, self.listeners)
  338. self.propagate.clear()
  339. self.listeners.clear()
  340. class _JoinedListener(_CompoundListener):
  341. __slots__ = "parent", "name", "local", "parent_listeners"
  342. def __init__(self, parent, name, local):
  343. self._exec_once = False
  344. self.parent = parent
  345. self.name = name
  346. self.local = local
  347. self.parent_listeners = self.local
  348. @property
  349. def listeners(self):
  350. return getattr(self.parent, self.name)
  351. def _adjust_fn_spec(self, fn, named):
  352. return self.local._adjust_fn_spec(fn, named)
  353. def for_modify(self, obj):
  354. self.local = self.parent_listeners = self.local.for_modify(obj)
  355. return self
  356. def insert(self, event_key, propagate):
  357. self.local.insert(event_key, propagate)
  358. def append(self, event_key, propagate):
  359. self.local.append(event_key, propagate)
  360. def remove(self, event_key):
  361. self.local.remove(event_key)
  362. def clear(self):
  363. raise NotImplementedError()