instrumentation.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. # ext/instrumentation.py
  2. # Copyright (C) 2005-2025 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. # mypy: ignore-errors
  8. """Extensible class instrumentation.
  9. The :mod:`sqlalchemy.ext.instrumentation` package provides for alternate
  10. systems of class instrumentation within the ORM. Class instrumentation
  11. refers to how the ORM places attributes on the class which maintain
  12. data and track changes to that data, as well as event hooks installed
  13. on the class.
  14. .. note::
  15. The extension package is provided for the benefit of integration
  16. with other object management packages, which already perform
  17. their own instrumentation. It is not intended for general use.
  18. For examples of how the instrumentation extension is used,
  19. see the example :ref:`examples_instrumentation`.
  20. """
  21. import weakref
  22. from .. import util
  23. from ..orm import attributes
  24. from ..orm import base as orm_base
  25. from ..orm import collections
  26. from ..orm import exc as orm_exc
  27. from ..orm import instrumentation as orm_instrumentation
  28. from ..orm import util as orm_util
  29. from ..orm.instrumentation import _default_dict_getter
  30. from ..orm.instrumentation import _default_manager_getter
  31. from ..orm.instrumentation import _default_opt_manager_getter
  32. from ..orm.instrumentation import _default_state_getter
  33. from ..orm.instrumentation import ClassManager
  34. from ..orm.instrumentation import InstrumentationFactory
  35. INSTRUMENTATION_MANAGER = "__sa_instrumentation_manager__"
  36. """Attribute, elects custom instrumentation when present on a mapped class.
  37. Allows a class to specify a slightly or wildly different technique for
  38. tracking changes made to mapped attributes and collections.
  39. Only one instrumentation implementation is allowed in a given object
  40. inheritance hierarchy.
  41. The value of this attribute must be a callable and will be passed a class
  42. object. The callable must return one of:
  43. - An instance of an :class:`.InstrumentationManager` or subclass
  44. - An object implementing all or some of InstrumentationManager (TODO)
  45. - A dictionary of callables, implementing all or some of the above (TODO)
  46. - An instance of a :class:`.ClassManager` or subclass
  47. This attribute is consulted by SQLAlchemy instrumentation
  48. resolution, once the :mod:`sqlalchemy.ext.instrumentation` module
  49. has been imported. If custom finders are installed in the global
  50. instrumentation_finders list, they may or may not choose to honor this
  51. attribute.
  52. """
  53. def find_native_user_instrumentation_hook(cls):
  54. """Find user-specified instrumentation management for a class."""
  55. return getattr(cls, INSTRUMENTATION_MANAGER, None)
  56. instrumentation_finders = [find_native_user_instrumentation_hook]
  57. """An extensible sequence of callables which return instrumentation
  58. implementations
  59. When a class is registered, each callable will be passed a class object.
  60. If None is returned, the
  61. next finder in the sequence is consulted. Otherwise the return must be an
  62. instrumentation factory that follows the same guidelines as
  63. sqlalchemy.ext.instrumentation.INSTRUMENTATION_MANAGER.
  64. By default, the only finder is find_native_user_instrumentation_hook, which
  65. searches for INSTRUMENTATION_MANAGER. If all finders return None, standard
  66. ClassManager instrumentation is used.
  67. """
  68. class ExtendedInstrumentationRegistry(InstrumentationFactory):
  69. """Extends :class:`.InstrumentationFactory` with additional
  70. bookkeeping, to accommodate multiple types of
  71. class managers.
  72. """
  73. _manager_finders = weakref.WeakKeyDictionary()
  74. _state_finders = weakref.WeakKeyDictionary()
  75. _dict_finders = weakref.WeakKeyDictionary()
  76. _extended = False
  77. def _locate_extended_factory(self, class_):
  78. for finder in instrumentation_finders:
  79. factory = finder(class_)
  80. if factory is not None:
  81. manager = self._extended_class_manager(class_, factory)
  82. return manager, factory
  83. else:
  84. return None, None
  85. def _check_conflicts(self, class_, factory):
  86. existing_factories = self._collect_management_factories_for(
  87. class_
  88. ).difference([factory])
  89. if existing_factories:
  90. raise TypeError(
  91. "multiple instrumentation implementations specified "
  92. "in %s inheritance hierarchy: %r"
  93. % (class_.__name__, list(existing_factories))
  94. )
  95. def _extended_class_manager(self, class_, factory):
  96. manager = factory(class_)
  97. if not isinstance(manager, ClassManager):
  98. manager = _ClassInstrumentationAdapter(class_, manager)
  99. if factory != ClassManager and not self._extended:
  100. # somebody invoked a custom ClassManager.
  101. # reinstall global "getter" functions with the more
  102. # expensive ones.
  103. self._extended = True
  104. _install_instrumented_lookups()
  105. self._manager_finders[class_] = manager.manager_getter()
  106. self._state_finders[class_] = manager.state_getter()
  107. self._dict_finders[class_] = manager.dict_getter()
  108. return manager
  109. def _collect_management_factories_for(self, cls):
  110. """Return a collection of factories in play or specified for a
  111. hierarchy.
  112. Traverses the entire inheritance graph of a cls and returns a
  113. collection of instrumentation factories for those classes. Factories
  114. are extracted from active ClassManagers, if available, otherwise
  115. instrumentation_finders is consulted.
  116. """
  117. hierarchy = util.class_hierarchy(cls)
  118. factories = set()
  119. for member in hierarchy:
  120. manager = self.opt_manager_of_class(member)
  121. if manager is not None:
  122. factories.add(manager.factory)
  123. else:
  124. for finder in instrumentation_finders:
  125. factory = finder(member)
  126. if factory is not None:
  127. break
  128. else:
  129. factory = None
  130. factories.add(factory)
  131. factories.discard(None)
  132. return factories
  133. def unregister(self, class_):
  134. super().unregister(class_)
  135. if class_ in self._manager_finders:
  136. del self._manager_finders[class_]
  137. del self._state_finders[class_]
  138. del self._dict_finders[class_]
  139. def opt_manager_of_class(self, cls):
  140. try:
  141. finder = self._manager_finders.get(
  142. cls, _default_opt_manager_getter
  143. )
  144. except TypeError:
  145. # due to weakref lookup on invalid object
  146. return None
  147. else:
  148. return finder(cls)
  149. def manager_of_class(self, cls):
  150. try:
  151. finder = self._manager_finders.get(cls, _default_manager_getter)
  152. except TypeError:
  153. # due to weakref lookup on invalid object
  154. raise orm_exc.UnmappedClassError(
  155. cls, f"Can't locate an instrumentation manager for class {cls}"
  156. )
  157. else:
  158. manager = finder(cls)
  159. if manager is None:
  160. raise orm_exc.UnmappedClassError(
  161. cls,
  162. f"Can't locate an instrumentation manager for class {cls}",
  163. )
  164. return manager
  165. def state_of(self, instance):
  166. if instance is None:
  167. raise AttributeError("None has no persistent state.")
  168. return self._state_finders.get(
  169. instance.__class__, _default_state_getter
  170. )(instance)
  171. def dict_of(self, instance):
  172. if instance is None:
  173. raise AttributeError("None has no persistent state.")
  174. return self._dict_finders.get(
  175. instance.__class__, _default_dict_getter
  176. )(instance)
  177. orm_instrumentation._instrumentation_factory = _instrumentation_factory = (
  178. ExtendedInstrumentationRegistry()
  179. )
  180. orm_instrumentation.instrumentation_finders = instrumentation_finders
  181. class InstrumentationManager:
  182. """User-defined class instrumentation extension.
  183. :class:`.InstrumentationManager` can be subclassed in order
  184. to change
  185. how class instrumentation proceeds. This class exists for
  186. the purposes of integration with other object management
  187. frameworks which would like to entirely modify the
  188. instrumentation methodology of the ORM, and is not intended
  189. for regular usage. For interception of class instrumentation
  190. events, see :class:`.InstrumentationEvents`.
  191. The API for this class should be considered as semi-stable,
  192. and may change slightly with new releases.
  193. """
  194. # r4361 added a mandatory (cls) constructor to this interface.
  195. # given that, perhaps class_ should be dropped from all of these
  196. # signatures.
  197. def __init__(self, class_):
  198. pass
  199. def manage(self, class_, manager):
  200. setattr(class_, "_default_class_manager", manager)
  201. def unregister(self, class_, manager):
  202. delattr(class_, "_default_class_manager")
  203. def manager_getter(self, class_):
  204. def get(cls):
  205. return cls._default_class_manager
  206. return get
  207. def instrument_attribute(self, class_, key, inst):
  208. pass
  209. def post_configure_attribute(self, class_, key, inst):
  210. pass
  211. def install_descriptor(self, class_, key, inst):
  212. setattr(class_, key, inst)
  213. def uninstall_descriptor(self, class_, key):
  214. delattr(class_, key)
  215. def install_member(self, class_, key, implementation):
  216. setattr(class_, key, implementation)
  217. def uninstall_member(self, class_, key):
  218. delattr(class_, key)
  219. def instrument_collection_class(self, class_, key, collection_class):
  220. return collections.prepare_instrumentation(collection_class)
  221. def get_instance_dict(self, class_, instance):
  222. return instance.__dict__
  223. def initialize_instance_dict(self, class_, instance):
  224. pass
  225. def install_state(self, class_, instance, state):
  226. setattr(instance, "_default_state", state)
  227. def remove_state(self, class_, instance):
  228. delattr(instance, "_default_state")
  229. def state_getter(self, class_):
  230. return lambda instance: getattr(instance, "_default_state")
  231. def dict_getter(self, class_):
  232. return lambda inst: self.get_instance_dict(class_, inst)
  233. class _ClassInstrumentationAdapter(ClassManager):
  234. """Adapts a user-defined InstrumentationManager to a ClassManager."""
  235. def __init__(self, class_, override):
  236. self._adapted = override
  237. self._get_state = self._adapted.state_getter(class_)
  238. self._get_dict = self._adapted.dict_getter(class_)
  239. ClassManager.__init__(self, class_)
  240. def manage(self):
  241. self._adapted.manage(self.class_, self)
  242. def unregister(self):
  243. self._adapted.unregister(self.class_, self)
  244. def manager_getter(self):
  245. return self._adapted.manager_getter(self.class_)
  246. def instrument_attribute(self, key, inst, propagated=False):
  247. ClassManager.instrument_attribute(self, key, inst, propagated)
  248. if not propagated:
  249. self._adapted.instrument_attribute(self.class_, key, inst)
  250. def post_configure_attribute(self, key):
  251. super().post_configure_attribute(key)
  252. self._adapted.post_configure_attribute(self.class_, key, self[key])
  253. def install_descriptor(self, key, inst):
  254. self._adapted.install_descriptor(self.class_, key, inst)
  255. def uninstall_descriptor(self, key):
  256. self._adapted.uninstall_descriptor(self.class_, key)
  257. def install_member(self, key, implementation):
  258. self._adapted.install_member(self.class_, key, implementation)
  259. def uninstall_member(self, key):
  260. self._adapted.uninstall_member(self.class_, key)
  261. def instrument_collection_class(self, key, collection_class):
  262. return self._adapted.instrument_collection_class(
  263. self.class_, key, collection_class
  264. )
  265. def initialize_collection(self, key, state, factory):
  266. delegate = getattr(self._adapted, "initialize_collection", None)
  267. if delegate:
  268. return delegate(key, state, factory)
  269. else:
  270. return ClassManager.initialize_collection(
  271. self, key, state, factory
  272. )
  273. def new_instance(self, state=None):
  274. instance = self.class_.__new__(self.class_)
  275. self.setup_instance(instance, state)
  276. return instance
  277. def _new_state_if_none(self, instance):
  278. """Install a default InstanceState if none is present.
  279. A private convenience method used by the __init__ decorator.
  280. """
  281. if self.has_state(instance):
  282. return False
  283. else:
  284. return self.setup_instance(instance)
  285. def setup_instance(self, instance, state=None):
  286. self._adapted.initialize_instance_dict(self.class_, instance)
  287. if state is None:
  288. state = self._state_constructor(instance, self)
  289. # the given instance is assumed to have no state
  290. self._adapted.install_state(self.class_, instance, state)
  291. return state
  292. def teardown_instance(self, instance):
  293. self._adapted.remove_state(self.class_, instance)
  294. def has_state(self, instance):
  295. try:
  296. self._get_state(instance)
  297. except orm_exc.NO_STATE:
  298. return False
  299. else:
  300. return True
  301. def state_getter(self):
  302. return self._get_state
  303. def dict_getter(self):
  304. return self._get_dict
  305. def _install_instrumented_lookups():
  306. """Replace global class/object management functions
  307. with ExtendedInstrumentationRegistry implementations, which
  308. allow multiple types of class managers to be present,
  309. at the cost of performance.
  310. This function is called only by ExtendedInstrumentationRegistry
  311. and unit tests specific to this behavior.
  312. The _reinstall_default_lookups() function can be called
  313. after this one to re-establish the default functions.
  314. """
  315. _install_lookups(
  316. dict(
  317. instance_state=_instrumentation_factory.state_of,
  318. instance_dict=_instrumentation_factory.dict_of,
  319. manager_of_class=_instrumentation_factory.manager_of_class,
  320. opt_manager_of_class=_instrumentation_factory.opt_manager_of_class,
  321. )
  322. )
  323. def _reinstall_default_lookups():
  324. """Restore simplified lookups."""
  325. _install_lookups(
  326. dict(
  327. instance_state=_default_state_getter,
  328. instance_dict=_default_dict_getter,
  329. manager_of_class=_default_manager_getter,
  330. opt_manager_of_class=_default_opt_manager_getter,
  331. )
  332. )
  333. _instrumentation_factory._extended = False
  334. def _install_lookups(lookups):
  335. global instance_state, instance_dict
  336. global manager_of_class, opt_manager_of_class
  337. instance_state = lookups["instance_state"]
  338. instance_dict = lookups["instance_dict"]
  339. manager_of_class = lookups["manager_of_class"]
  340. opt_manager_of_class = lookups["opt_manager_of_class"]
  341. orm_base.instance_state = attributes.instance_state = (
  342. orm_instrumentation.instance_state
  343. ) = instance_state
  344. orm_base.instance_dict = attributes.instance_dict = (
  345. orm_instrumentation.instance_dict
  346. ) = instance_dict
  347. orm_base.manager_of_class = attributes.manager_of_class = (
  348. orm_instrumentation.manager_of_class
  349. ) = manager_of_class
  350. orm_base.opt_manager_of_class = orm_util.opt_manager_of_class = (
  351. attributes.opt_manager_of_class
  352. ) = orm_instrumentation.opt_manager_of_class = opt_manager_of_class