zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
U
¸ý°dybã
@s¸dZddlmZddlmZddlmZddlmZddlmZddlmZddlm    Z    dd    lm
Z
dd
lm Z dd lm Z dd lm Z dd lmZddlmZddlmZddlmZddlmZddlZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddl"m#Z#dd l"m$Z$erÚdd!lm%Z%dd"lm&Z&dd#lm'Z'dd$lm(Z(dd%lm)Z)dd&l*m+Z+dd'l,m-Z-dd(l.m/Z/dd)lm0Z0dd*lm1Z1ed+ed,Z2e 3d-¡Z4Gd.d/„d/e$ƒZ5Gd0d1„d1e$ƒZ6Gd2d3„d3e!ee7d4fe    ee ƒZ8Gd5d6„d6ƒZ9Gd7d8„d8e ƒZ:e:ƒZ;ej<Z<Z=ej>Z>Z?ej@Z@ZAejBZBZCdKd:d;d<d=d>d?d@dAdBœdCdD„ZDdEdF„ZEdGdH„ZFdIdJ„ZGdS)LaœDefines SQLAlchemy's system of class instrumentation.
 
This module is usually not directly visible to user applications, but
defines a large part of the ORM's interactivity.
 
instrumentation.py deals with registration of end-user classes
for state tracking.   It interacts closely with state.py
and attributes.py which establish per-instance and per-class-attribute
instrumentation, respectively.
 
The class instrumentation system can be customized on a per-class
or global basis using the :mod:`sqlalchemy.ext.instrumentation`
module, which provides the means to build and specify
alternate instrumentation forms.
 
.. versionchanged: 0.8
   The instrumentation extension system was moved out of the
   ORM and into the external :mod:`sqlalchemy.ext.instrumentation`
   package.  When that package is imported, it installs
   itself within sqlalchemy.orm so that its more comprehensive
   resolution mechanics take effect.
 
é)Ú annotations)ÚAny)ÚCallable)Úcast)Ú
Collection)ÚDict)ÚGeneric)ÚIterable)ÚList)ÚOptional)ÚSet)ÚTuple)ÚType)Ú TYPE_CHECKING)ÚTypeVar)ÚUnionNé)Úbase)Ú collections)Úexc)Ú
interfaces)Ústate)Ú_O)Ú_is_collection_attribute_implé)Úutil)Ú EventTarget)Ú HasMemoized)ÚLiteral)ÚProtocol)Ú _RegistryType)Ú AttributeImpl)ÚQueryableAttribute)Ú_AdaptedCollectionProtocol)Ú_CollectionFactoryType)Ú _MapperConfig)ÚInstanceEvents)ÚMapper)Ú InstanceState)Ú
dispatcherÚ_T)ÚboundÚDEL_ATTRc@s eZdZdddddœdd„ZdS)    Ú_ExpiredAttributeLoaderProtoústate.InstanceState[Any]zSet[str]zbase.PassiveFlagÚNone)rÚtoloadÚpassiveÚreturncCsdS©N©)Úselfrr0r1r4r4úUd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/orm/instrumentation.pyÚ__call__Ssz%_ExpiredAttributeLoaderProto.__call__N©Ú__name__Ú
__module__Ú __qualname__r7r4r4r4r6r-Rsr-c@seZdZdddœdd„ZdS)Ú_ManagerFactoryúType[_O]úClassManager[_O]©Úclass_r2cCsdSr3r4©r5r@r4r4r6r7]sz_ManagerFactory.__call__Nr8r4r4r4r6r<\sr<c    @sbeZdZUdZded<ejZejZ    e
e   e    ¡ƒZ ded<ded<dZded    <d
ed <dZd ed <ded<espdZded<ded<ee jddddd„ƒƒZeje jddddd„ƒƒZdd„Zdœdddd d!dd"d#œd$d%„Zd"d&œd'd(„Zd)d&œd*d+„Zd,dd-œd.d/„Zedd&œd0d1„ƒZejd2d3„ƒZejd4d5„ƒZejd6d7„ƒZ ejd8d9„ƒZ!e j"d:d&œd;d<„ƒZ#dd=d>„Z$džd?d@„Z%dAddBœdCdD„Z&dEdFdGœdHdI„Z'dJdK„Z(e j"dLd&œdMdN„ƒZ)dOdP„Z*e j+dQdR„ƒZ,e j+dSdT„ƒZ-e j+dUdV„ƒZ.dŸdAdWdd"dXœdYdZ„Z/d[d\„Z0d]d^„Z1d d_d`„Z2d"d&œdadb„Z3dAdWd"dcœddde„Z4dAd"dBœdfdg„Z5dAd,d"dhœdidj„Z6dAd"dBœdkdl„Z7dAdmdndoœdpdq„Z8dAdrdndsdtœdudv„Z9d¡dAdddwœdxdy„Z:dAdzdBœd{d|„Z;ed}d&œd~d„ƒZ<d¢d€dd‚œdƒd„„Z=d£dd€d"d…œd†d‡„Z>dd"dˆœd‰dŠ„Z?drd‹dŒdœdŽd„Z@dddˆœd‘d’„ZAdddˆœd“d”„ZBd¤drdAddd•œd–d—„ZCdd&œd˜d™„ZDdAd&œdšd›„ZEdS)¥Ú ClassManagerz,Tracks state information at the class level.zdispatcher[ClassManager[_O]]Údispatchr-Úexpired_attribute_loaderú$Optional[Callable[(Ellipsis, None)]]Ú init_methodNÚ original_initzOptional[_ManagerFactory]Úfactoryz$Optional[weakref.ref[_MapperConfig]]Údeclarative_scanr Úregistryr=r@zList[ClassManager[Any]]Ú_basesz1.4zWThe ClassManager.deferred_scalar_loader attribute is now named expired_attribute_loader)ÚmessagecCs|jSr3©rD©r5r4r4r6Údeferred_scalar_loader„sz#ClassManager.deferred_scalar_loadercCs
||_dSr3rM)r5Úobjr4r4r6rOscCsÎ||_i|_d|_i|_i|_d|_d|_d|_dd„tddd„|jj    DƒƒDƒ|_
|j
D]}|  |¡qZtd|j j ƒ ||¡|jD]"}t|ƒ}|dk    r†|j  |j ¡q†| ¡d|jkrÊt d|¡dS)    NFcSsg|]}|dk    r|‘qSr3r4)Ú.0Úmgrr4r4r6Ú
<listcomp> s÷z)ClassManager.__init__.<locals>.<listcomp>z!List[Optional[ClassManager[Any]]]cSsg|]}t|tƒrt|ƒ‘qSr4)Ú
isinstanceÚtypeÚopt_manager_of_class)rQrr4r4r6rS¤s
þr&Ú__del__z¦__del__() method on class %s will cause unreachable cycles and memory leaks, as SQLAlchemy instrumentation often creates reference cycles.  Please remove this method.)r@ÚinfoÚnew_initÚ local_attrsÚ    originalsÚ
_finalizedrHrFrÚ    __bases__rKÚupdaterCZ_eventsZ_new_classmanager_instanceÚ__mro__rVÚ_updateÚmanageÚ__dict__rÚwarn)r5r@Zbase_ZbaseclsrRr4r4r6Ú__init__–sHþþþ
 ÿþ
 
ýÿzClassManager.__init__FÚboolúOptional[Mapper[_O]]úOptional[_RegistryType]úOptional[_MapperConfig]ú&Optional[_ExpiredAttributeLoaderProto]r/)ÚfinalizeÚmapperrJrIrDrFr2cCs’|r
||_|r| |¡|r(t |¡|_|r2||_|rN|jrHtd|ƒ‚||_|js||jdk    rr|j    j
t j
krr|jn|j    j
|_ |rŽ|jsŽ|  ¡dS)Nz>class is already instrumented, init_method %s can't be applied)rkZ _add_managerÚweakrefÚrefrIrDr\ÚAssertionErrorrFr@rdÚobjectrGÚ    _finalize)r5rjrkrJrIrDrFr4r4r6Ú _update_stateÃs0 
 ÿÿÿ þü
zClassManager._update_state)r2cCs*|jr
dSd|_| ¡tj |j¡dS©NT)r\Ú_instrument_initÚ_instrumentation_factoryrCZclass_instrumentr@rNr4r4r6rpês
zClassManager._finalizeÚintcCst|ƒSr3)ÚidrNr4r4r6Ú__hash__ószClassManager.__hash__r)Úotherr2cCs||kSr3r4)r5rxr4r4r6Ú__eq__öszClassManager.__eq__cCs
d|jkS)Nrk)rbrNr4r4r6Ú    is_mappedùszClassManager.is_mappedcCst|ƒSr3)Ú    frozensetrNr4r4r6Ú _all_key_setýszClassManager._all_key_setcCstdd„| ¡DƒƒS)NcSsg|]}|jjr|j‘qSr4)ÚimplZ
collectionÚkey©rQÚattrr4r4r6rSsz6ClassManager._collection_impl_keys.<locals>.<listcomp>©r{ÚvaluesrNr4r4r6Ú_collection_impl_keyssÿz"ClassManager._collection_impl_keyscCstdd„| ¡DƒƒS)NcSsg|]}|jjr|j‘qSr4)r}Zaccepts_scalar_loaderrr4r4r6rS
sþz5ClassManager._scalar_loader_impls.<locals>.<listcomp>rrNr4r4r6Ú_scalar_loader_implss
þÿz!ClassManager._scalar_loader_implscCstdd„| ¡DƒƒS)NcSsg|]
}|j‘qSr4©r}rr4r4r6rSsz.ClassManager._loader_impls.<locals>.<listcomp>rrNr4r4r6Ú _loader_implsszClassManager._loader_implsz
Mapper[_O]cCst |j¡‚dSr3)rZUnmappedClassErrorr@rNr4r4r6rkszClassManager.mapperccsvi}|jjdd…D]\}|jd}|jD]F}| ||¡||jkrDq(||j|}t|tjƒr(|jr(||fVq(qdS)areturn an iterator of all classbound attributes that are
        implement :class:`.InspectionAttr`.
 
        This includes :class:`.QueryableAttribute` as well as extension
        types such as :class:`.hybrid_property` and
        :class:`.AssociationProxy`.
 
        réÿÿÿÿrN)r@r_rbÚ
setdefaultrTrZInspectionAttrZ is_attribute)r5ÚexcludeÚfoundÚsuperclsZinheritsr~Úvalr4r4r6Ú_all_sqla_attributess
 
 
 
 
 
ÿþz!ClassManager._all_sqla_attributescCs*|jjD]}||jkr|j|Sq|S)z5return an attribute on the class without tripping it.N)r@r_rb)r5r~Údefaultr‹r4r4r6Ú_get_class_attr_mro;s 
z ClassManager._get_class_attr_mroÚstr)r~r2cCs||ko||jdk    S)z\Return True if the given attribute is fully initialized.
 
        i.e. has an impl.
        Nr…©r5r~r4r4r6Ú_attr_has_implDszClassManager._attr_has_implzType[_T]zClassManager[_T])Úclsr2cCs t|ddS)aÏCreate a new ClassManager for a subclass of this ClassManager's
        class.
 
        This is called automatically when attributes are instrumented so that
        the attributes can be propagated to subclasses against their own
        class-local manager, without the need for mappers etc. to have already
        pre-configured managers for the full class hierarchy.   Mappers
        can post-configure the auto-generated ClassManager when needed.
 
        F)rj)Úregister_class)r5r“r4r4r6Ú_subclass_managerLs zClassManager._subclass_managercCs$t|j||jƒ|_| d|j¡dS)Nrd)Ú_generate_initr@rGrYÚinstall_memberrNr4r4r6rsYszClassManager._instrument_initzType[state.InstanceState[_O]]cCs|j ||j¡tjSr3)rCZ
first_initr@rr(rNr4r4r6Ú_state_constructor]szClassManager._state_constructorcCst|j|j|ƒdS)z0Mark this instance as the manager for its class.N)Úsetattrr@Ú MANAGER_ATTRrNr4r4r6rabszClassManager.managecCstSr3)Ú_default_manager_getterrNr4r4r6Úmanager_gettergszClassManager.manager_gettercCstS)zÍReturn a (instance) -> InstanceState callable.
 
        "state getter" callables should raise either KeyError or
        AttributeError if no InstanceState could be found for the
        instance.
        )Ú_default_state_getterrNr4r4r6Ú state_getterks    zClassManager.state_gettercCstSr3)Ú_default_dict_getterrNr4r4r6Ú dict_gettervszClassManager.dict_getterúQueryableAttribute[Any])r~ÚinstÚ
propagatedr2cCsf|r||jkr*dSn||j|<| ||¡| ¡|||<|j ¡D]}| |¡}| ||d¡qDdSrr)rZÚinstall_descriptorÚ_reset_memoizationsr@Ú__subclasses__r•Úinstrument_attribute)r5r~r¢r£r“Úmanagerr4r4r6r§zs
 
 
z!ClassManager.instrument_attributeccsF|j ¡D]6}t|ƒ}|dk    r
||k    r
|V|r
| d¡EdHq
dSrr)r@r¦rVÚsubclass_managers)r5Ú    recursiver“rRr4r4r6r©s zClassManager.subclass_managerscCstj |j|||¡dSr3)rtrCZattribute_instrumentr@r‘r4r4r6Úpost_configure_attribute•s
ÿz%ClassManager.post_configure_attributecCsl||kr dS|r ||jkr2dSn|j|=| |¡| ¡||=|j ¡D]}t|ƒ}|rJ| |d¡qJdSrr)rZÚuninstall_descriptorr¥r@r¦rVÚuninstrument_attribute)r5r~r£r“r¨r4r4r6r­šs
 
z#ClassManager.uninstrument_attributecCsxt|jƒD]}| |¡q
d|_d|_d|_|j ¡t|ƒD]}||jkr>|     |¡q>|j
|j j krtt |j |j
ƒdS)z<remove all instrumentation established by this ClassManager.N)Úlistr[Úuninstall_memberrkrCrYrXÚclearrZr­ršr@rbÚdelattrr‘r4r4r6Ú
unregisterªs 
 
 zClassManager.unregister)r~r¢r2cCs.||j|jfkrtd|ƒ‚t|j||ƒdS©NzW%r: requested attribute name conflicts with instrumentation attribute of the same name.)Ú
STATE_ATTRršÚKeyErrorr™r@)r5r~r¢r4r4r6r¤¼sÿÿzClassManager.install_descriptorcCst|j|ƒdSr3)r±r@r‘r4r4r6r¬Æsz!ClassManager.uninstall_descriptor)r~Úimplementationr2cCsH||j|jfkrtd|ƒ‚|j ||jj |t¡¡t    |j||ƒdSr³)
r´ršrµr[rˆr@rbÚgetr,r™)r5r~r¶r4r4r6r—ÉsÿÿzClassManager.install_membercCs6|j |d¡}|tk    r&t|j||ƒn t|j|ƒdSr3)r[Úpopr,r™r@r±)r5r~Úoriginalr4r4r6r¯ÒszClassManager.uninstall_memberzType[Collection[Any]]r$)r~Úcollection_classr2cCs
t |¡Sr3)rZprepare_instrumentation)r5r~rºr4r4r6Úinstrument_collection_classÙsz(ClassManager.instrument_collection_classzInstanceState[_O]z@Tuple[collections.CollectionAdapter, _AdaptedCollectionProtocol])r~rrHr2cCs2|ƒ}| |¡}t|ƒst‚t |||¡}||fSr3)Úget_implrrnrZCollectionAdapter)r5r~rrHÚ    user_datar}Úadapterr4r4r6Úinitialize_collectionÞs
 
 z"ClassManager.initialize_collection)r~Úsearchr2cCs|r ||kS||jkSdSr3)rZ)r5r~rÀr4r4r6Úis_instrumentedêszClassManager.is_instrumentedr!cCs
||jSr3r…r‘r4r4r6r¼ðszClassManager.get_implz Iterable[Any]cCs t| ¡ƒSr3)Úiterr‚rNr4r4r6Ú
attributesószClassManager.attributeszOptional[InstanceState[_O]]r)rr2cCs2|j |j¡}|dkr"| ||¡}| ||¡|Sr3)r@Ú__new__r˜Ú _state_setter)r5rÚinstancer4r4r6Ú new_instanceùs
  zClassManager.new_instance)rÆrr2cCs$|dkr| ||¡}| ||¡dSr3)r˜rÅ©r5rÆrr4r4r6Úsetup_instances zClassManager.setup_instance)rÆr2cCst||jƒdSr3)r±r´©r5rÆr4r4r6Úteardown_instance szClassManager.teardown_instanceúDict[str, Any]Ú_SerializeManager)rÚ
state_dictr2cCs
t||ƒSr3)rÍ)r5rrÎr4r4r6Ú
_serializeszClassManager._serializez(Union[Literal[False], InstanceState[_O]]cCsTt||jƒrdS|j|jk    r4|jr4| |j¡ |¡S| ||¡}| ||¡|SdS)zƒInstall a default InstanceState if none is present.
 
        A private convenience method used by the __init__ decorator.
 
        FN)    Úhasattrr´r@Ú    __class__rzr•Ú_new_state_if_noner˜rÅrÈr4r4r6rÒs ÿþ  zClassManager._new_state_if_nonecCs t||jƒSr3)rÐr´rÊr4r4r6Ú    has_state+szClassManager.has_state)rr~Ú
optimisticr2cCs| |¡j||dS)ZTODO)rÔ)r¼Z    hasparent)r5rr~rÔr4r4r6Ú
has_parent.szClassManager.has_parentcCsdS)z=All ClassManagers are non-zero regardless of attribute state.Tr4rNr4r4r6Ú__bool__4szClassManager.__bool__cCsd|jj|jt|ƒfS)Nz<%s of %r at %x>)rÑr9r@rvrNr4r4r6Ú__repr__8s
ýzClassManager.__repr__)FNNNNN)N)N)F)F)F)N)N)F)Fr9r:r;Ú__doc__Ú__annotations__rZDEFAULT_MANAGER_ATTRršZDEFAULT_STATE_ATTRr´Ú staticmethodrZ
attrsetterrÅrGrIrrJÚpropertyÚ
deprecatedrOÚsetterrdrqrprwryrzrZmemoized_attributer|rƒr„r†Zmemoized_propertyrkrrr’r•rsr˜raZ hybridmethodrœržr r§r©r«r­r²r¤r¬r—r¯r»r¿rÁr¼rÃrÇrÉrËrÏrÒrÓrÕrÖr×r4r4r4r6rBas¶
  þ þ /÷'    
 
 
    
 
!
     
 
 
 
ü
 
      ÿÿrBr¡c@s(eZdZdZdddœdd„Zdd„Zd    S)
rÍz¡Provide serialization of a :class:`.ClassManager`.
 
    The :class:`.InstanceState` uses ``__init__()`` on serialize
    and ``__call__()`` on deserialize.
 
    r.rÌ)rÚdcCs |j|_|j}|j ||¡dSr3)r@r¨rCÚpickle)r5rrÞr¨r4r4r6rdHsz_SerializeManager.__init__cCsjt|jƒ|_}|dkr,t |d|j¡‚n|jrD|jjsD|j ¡|dk    rX|     ||¡|j
  ||¡dS)NzxCannot deserialize object of type %r - no mapper() has been configured for this class within the current Python process!) rVr@r¨rZUnmappedInstanceErrorrzrkZ
configuredZ_check_configurerÉrCZunpickle)r5rr¢rÎr¨r4r4r6r7Msýþ
 z_SerializeManager.__call__N)r9r:r;rØrdr7r4r4r4r6rÍ@srÍc@s\eZdZUdZded<dddœdd„Zdd    dœd
d „Zdd d dœdd„Zdd dœdd„ZdS)ÚInstrumentationFactoryz'Factory for new ClassManager instances.z"dispatcher[InstrumentationFactory]rCr=r>r?cCsb|dk    s t‚t|ƒdkst‚| |¡\}}|dkr@t}t|ƒ}n |dk    sLt‚| ||¡||_|Sr3)rnrVÚ_locate_extended_factoryrBÚ_check_conflictsrH)r5r@r¨rHr4r4r6Úcreate_manager_for_clsgs 
  z-InstrumentationFactory.create_manager_for_clsz<Tuple[Optional[ClassManager[_O]], Optional[_ManagerFactory]]cCsdS)z2Overridden by a subclass to do an extended lookup.)NNr4rAr4r4r6rá{sz/InstrumentationFactory._locate_extended_factoryz&Callable[[Type[_O]], ClassManager[_O]]r/)r@rHr2cCsdS)z;Overridden by a subclass to test for conflicting factories.Nr4)r5r@rHr4r4r6râsz'InstrumentationFactory._check_conflictscCs t|ƒ}| ¡|j |¡dSr3)Úmanager_of_classr²rCZclass_uninstrument)r5r@r¨r4r4r6r²†sz!InstrumentationFactory.unregisterN)    r9r:r;rØrÙrãrárâr²r4r4r4r6ràbs
ràTr=rerfrgrhrirEr>)r@rjrkrJrIrDrFr2cCs4t|ƒ}|dkrt |¡}|j||||||d|S)z_Register class instrumentation.
 
    Returns the existing or newly created class manager.
 
    N)rkrJrIrDrFrj)rVrtrãrq)r@rjrkrJrIrDrFr¨r4r4r6r”›s
ú    r”cCst |¡dS)z!Unregister class instrumentation.N)rtr²)r@r4r4r6Úunregister_class¹sråcCst|jƒj|ddS)zïReturn True if the given attribute on the given instance is
    instrumented by the attributes package.
 
    This function may be used regardless of instrumentation
    applied directly to the class, i.e. no descriptors are required.
 
    T)rÀ)rärÑrÁ)rÆr~r4r4r6rÁ¿s
ÿrÁc
CsŠ|dkr|j}d}tj|dd}||}t|ddƒ}t|ddƒ}tƒ ¡}t|d<t||ƒ|d}    |j|    _||    _    |r|||    _
|r†||    _ |    S)    z>Build an __init__ decorator that triggers ClassManager events.Nzßdef __init__(%(apply_pos)s):
    new_state = class_manager._new_state_if_none(%(self_arg)s)
    if new_state:
        return new_state._initialize_instance(%(apply_kw)s)
    else:
        return original_init(%(apply_kw)s)
F)ZgroupedÚ __defaults__Ú__kwdefaults__r9rd) rdrZformat_argspec_initÚgetattrÚlocalsÚcopyr9ÚexecrØZ_sa_original_initrærç)
r@Z class_managerrGZ    func_bodyZ    func_varsZ    func_textZ func_defaultsZfunc_kw_defaultsÚenvrdr4r4r6r–Ìs$  
 
r–)TNNNNN)HrØÚ
__future__rÚtypingrrrrrrr    r
r r r rrrrrlÚrrrrrÚ_typingrrÃrrÚeventrrZ util.typingrrr r!r"r#r$Z    decl_baser%Úeventsr&rkr'r(r)r*Úsymbolr,r-r<rrBrÍràrtZinstance_staterÚ instance_dictrŸrär›rVZ_default_opt_manager_getterr”rårÁr–r4r4r4r6Ú<module>    sŒ                                       
 
 
 
üb",
 
 
 
ù