U kf@sdZddlmZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZdd lmZddlmZddlmZddlmZededefdZGdddeZGdddeZGdddZdS)z9State tracking utilities used by :class:`_orm.Session`. ) annotationsN)Enum)Any)Callable)cast)Iterator)NoReturn)Optional)Tuple)TypeVar)Union)exc)util)Literal_F.)boundc@s eZdZdS)_StateChangeStateN)__name__ __module__ __qualname__rrL/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/orm/state_changes.pyr!src@seZdZdZdZdZdS)_StateChangeStatesr N)rrrANY NO_CHANGECHANGE_IN_PROGRESSrrrrr%src@sxeZdZUdZejZded<ejZ ded<dZ ded<ddd d d d Z e d dddddZ ejdddddZdS) _StateChangea!Supplies state assertion decorators. The current use case is for the :class:`_orm.SessionTransaction` class. The :class:`_StateChange` class itself is agnostic of the :class:`_orm.SessionTransaction` class so could in theory be generalized for other systems as well. r _next_state_stateNzOptional[Callable[..., Any]] _current_fnstrr)operation_namestatereturncCstjd|d|dddS)NzCan't run operation 'z()' when Session is in state iscecode)sa_excIllegalStateChangeError)selfr$r%rrr_raise_for_prerequisite_state9sz*_StateChange._raise_for_prerequisite_statezEUnion[Literal[_StateChangeStates.ANY], Tuple[_StateChangeState, ...]]zCallable[[_F], _F])prerequisite_statesmoves_tor&csT|s td|tjk td|tjk tjddddddfdd }|S)aMethod decorator declaring valid states. :param prerequisite_states: sequence of acceptable prerequisite states. Can be the single constant _State.ANY to indicate no prerequisite state :param moves_to: the expected state at the end of the method, assuming no exceptions raised. Can be the constant _State.NO_CHANGE to indicate state should not change at the end of the method. zno prequisite states sentzTuple[_StateChangeState, ...]rr)fnr,argkwr&c s~|j}r |kr ||j||j}|j}r4n|}|tjk rr||k r|r|tjtjfkrt j d|jd|jdddn"t j d|jdd|dd||_tj|_zz||f||}WnYnX|j|kr|W~S|j|krt j d|jd d ddnL|rJt j d |jd |jd |jddnt j d|jd |jddW5||_||_XdS)NzMethod 'z"()' can't be called here; method 'zN()' is already in progress and this would cause an unexpected state change to r'r(zCant run operation 'z()' here; will move to state z where we are expecting z()' failed to change state to z as expectedzWhile method 'z()' was running, method 'z)()' caused an unexpected state change to ) r!r-rr r"rrrrr*r+) r0r,r1r2Z current_stateZ next_stateZ existing_fnZ expect_stateZ ret_valueZexpect_state_changeZhas_prerequisite_statesr/Zprerequisite_state_collectionrr_go_sf     z(_StateChange.declare_states.._go)AssertionErrorrrrrr decorator)clsr.r/r4rr3rdeclare_statesBs  "Mz_StateChange.declare_statesz Iterator[Any])expectedr&ccsh|jtjkstd||_z>z dVWnYn"X|j|k rVtjd|jddW5tj|_XdS)zxcalled within a method that changes states. method must also use the ``@declare_states()`` decorator. zAUnexpected call to _expect_state outside of state-changing methodNzUnexpected state change to r'r()r rrr5r!r*r+)r,r9rrr _expect_states    z_StateChange._expect_state)rrr__doc__rrr __annotations__rr!r"r- classmethodr8 contextlibcontextmanagerr:rrrrr+s    lr)r; __future__rr>enumrtypingrrrrrr r r r rr*rZ util.typingrrrrrrrrrs&