summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/instrumentation.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy/orm/instrumentation.py')
-rw-r--r--lib/sqlalchemy/orm/instrumentation.py105
1 files changed, 62 insertions, 43 deletions
diff --git a/lib/sqlalchemy/orm/instrumentation.py b/lib/sqlalchemy/orm/instrumentation.py
index d34326e0f..fa29c3233 100644
--- a/lib/sqlalchemy/orm/instrumentation.py
+++ b/lib/sqlalchemy/orm/instrumentation.py
@@ -59,11 +59,15 @@ class ClassManager(dict):
self.local_attrs = {}
self.originals = {}
- self._bases = [mgr for mgr in [
- manager_of_class(base)
- for base in self.class_.__bases__
- if isinstance(base, type)
- ] if mgr is not None]
+ self._bases = [
+ mgr
+ for mgr in [
+ manager_of_class(base)
+ for base in self.class_.__bases__
+ if isinstance(base, type)
+ ]
+ if mgr is not None
+ ]
for base in self._bases:
self.update(base)
@@ -78,12 +82,13 @@ class ClassManager(dict):
self.manage()
self._instrument_init()
- if '__del__' in class_.__dict__:
- util.warn("__del__() method on class %s will "
- "cause unreachable cycles and memory leaks, "
- "as SQLAlchemy instrumentation often creates "
- "reference cycles. Please remove this method." %
- class_)
+ if "__del__" in class_.__dict__:
+ util.warn(
+ "__del__() method on class %s will "
+ "cause unreachable cycles and memory leaks, "
+ "as SQLAlchemy instrumentation often creates "
+ "reference cycles. Please remove this method." % class_
+ )
def __hash__(self):
return id(self)
@@ -93,7 +98,7 @@ class ClassManager(dict):
@property
def is_mapped(self):
- return 'mapper' in self.__dict__
+ return "mapper" in self.__dict__
@_memoized_key_collection
def _all_key_set(self):
@@ -101,14 +106,19 @@ class ClassManager(dict):
@_memoized_key_collection
def _collection_impl_keys(self):
- return frozenset([
- attr.key for attr in self.values() if attr.impl.collection])
+ return frozenset(
+ [attr.key for attr in self.values() if attr.impl.collection]
+ )
@_memoized_key_collection
def _scalar_loader_impls(self):
- return frozenset([
- attr.impl for attr in
- self.values() if attr.impl.accepts_scalar_loader])
+ return frozenset(
+ [
+ attr.impl
+ for attr in self.values()
+ if attr.impl.accepts_scalar_loader
+ ]
+ )
@util.memoized_property
def mapper(self):
@@ -174,11 +184,11 @@ class ClassManager(dict):
# of such, since this adds method overhead.
self.original_init = self.class_.__init__
self.new_init = _generate_init(self.class_, self)
- self.install_member('__init__', self.new_init)
+ self.install_member("__init__", self.new_init)
def _uninstrument_init(self):
if self.new_init:
- self.uninstall_member('__init__')
+ self.uninstall_member("__init__")
self.new_init = None
@util.memoized_property
@@ -239,8 +249,9 @@ class ClassManager(dict):
yield m
def post_configure_attribute(self, key):
- _instrumentation_factory.dispatch.\
- attribute_instrument(self.class_, key, self[key])
+ _instrumentation_factory.dispatch.attribute_instrument(
+ self.class_, key, self[key]
+ )
def uninstrument_attribute(self, key, propagated=False):
if key not in self:
@@ -272,9 +283,10 @@ class ClassManager(dict):
def install_descriptor(self, key, inst):
if key in (self.STATE_ATTR, self.MANAGER_ATTR):
- raise KeyError("%r: requested attribute name conflicts with "
- "instrumentation attribute of the same name." %
- key)
+ raise KeyError(
+ "%r: requested attribute name conflicts with "
+ "instrumentation attribute of the same name." % key
+ )
setattr(self.class_, key, inst)
def uninstall_descriptor(self, key):
@@ -282,9 +294,10 @@ class ClassManager(dict):
def install_member(self, key, implementation):
if key in (self.STATE_ATTR, self.MANAGER_ATTR):
- raise KeyError("%r: requested attribute name conflicts with "
- "instrumentation attribute of the same name." %
- key)
+ raise KeyError(
+ "%r: requested attribute name conflicts with "
+ "instrumentation attribute of the same name." % key
+ )
self.originals.setdefault(key, getattr(self.class_, key, None))
setattr(self.class_, key, implementation)
@@ -299,7 +312,8 @@ class ClassManager(dict):
def initialize_collection(self, key, state, factory):
user_data = factory()
adapter = collections.CollectionAdapter(
- self.get_impl(key), state, user_data)
+ self.get_impl(key), state, user_data
+ )
return adapter, user_data
def is_instrumented(self, key, search=False):
@@ -343,15 +357,15 @@ class ClassManager(dict):
"""
if hasattr(instance, self.STATE_ATTR):
return False
- elif self.class_ is not instance.__class__ and \
- self.is_mapped:
+ elif self.class_ is not instance.__class__ and self.is_mapped:
# this will create a new ClassManager for the
# subclass, without a mapper. This is likely a
# user error situation but allow the object
# to be constructed, so that it is usable
# in a non-ORM context at least.
- return self._subclass_manager(instance.__class__).\
- _new_state_if_none(instance)
+ return self._subclass_manager(
+ instance.__class__
+ )._new_state_if_none(instance)
else:
state = self._state_constructor(instance, self)
self._state_setter(instance, state)
@@ -371,8 +385,11 @@ class ClassManager(dict):
__nonzero__ = __bool__
def __repr__(self):
- return '<%s of %r at %x>' % (
- self.__class__.__name__, self.class_, id(self))
+ return "<%s of %r at %x>" % (
+ self.__class__.__name__,
+ self.class_,
+ id(self),
+ )
class _SerializeManager(object):
@@ -396,8 +413,8 @@ class _SerializeManager(object):
"Cannot deserialize object of type %r - "
"no mapper() has "
"been configured for this class within the current "
- "Python process!" %
- self.class_)
+ "Python process!" % self.class_,
+ )
elif manager.is_mapped and not manager.mapper.configured:
manager.mapper._configure_all()
@@ -447,6 +464,7 @@ class InstrumentationFactory(object):
if ClassManager.MANAGER_ATTR in class_.__dict__:
delattr(class_, ClassManager.MANAGER_ATTR)
+
# this attribute is replaced by sqlalchemy.ext.instrumentation
# when importred.
_instrumentation_factory = InstrumentationFactory()
@@ -488,8 +506,9 @@ def is_instrumented(instance, key):
applied directly to the class, i.e. no descriptors are required.
"""
- return manager_of_class(instance.__class__).\
- is_instrumented(key, search=True)
+ return manager_of_class(instance.__class__).is_instrumented(
+ key, search=True
+ )
def _generate_init(class_, class_manager):
@@ -518,15 +537,15 @@ def __init__(%(apply_pos)s):
func_text = func_body % func_vars
if util.py2k:
- func = getattr(original__init__, 'im_func', original__init__)
- func_defaults = getattr(func, 'func_defaults', None)
+ func = getattr(original__init__, "im_func", original__init__)
+ func_defaults = getattr(func, "func_defaults", None)
else:
- func_defaults = getattr(original__init__, '__defaults__', None)
- func_kw_defaults = getattr(original__init__, '__kwdefaults__', None)
+ func_defaults = getattr(original__init__, "__defaults__", None)
+ func_kw_defaults = getattr(original__init__, "__kwdefaults__", None)
env = locals().copy()
exec(func_text, env)
- __init__ = env['__init__']
+ __init__ = env["__init__"]
__init__.__doc__ = original__init__.__doc__
__init__._sa_original_init = original__init__