L iddlZddlZddlZddlZddlmZmZmZmZedefZ ede Z dZ dZ Gdd Z Gd d e Zy) N)AnyCallablecastTypeVar.F)boundcFtjfd}|S)z Wrap each generator invocation with the context manager factory. The input should be a function that returns a context manager, not a context manager itself, to handle one-shot context managers. c?(K|i|} 5|jd}ddd }5|j|}ddd'#1swY1xYw#1swYxYw#t$r.5|jddd#1swYxYwt$rA5|jt j }dddn #1swYnxYwYwxYw#t$r}|jcYd}~Sd}~wwxYwwN) send GeneratorExitclose BaseExceptionthrowsysexc_info StopIterationvalue)argskwargsgenresponserequeste ctx_factoryfuncs ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/utils/_contextlib.pygenerator_contextz*_wrap_generator..generator_contexts D#F#  *88D> *5$,nG%5#&88G#45% * **55%$$ $$$>$>#,399clln#=>>>> 77N  s DC0A C0A.C0A" C0AC0"A+'C0.C->B C-B! C-3"C C-C' #C-*C0,C--C00 D9 D DD DD) functoolswraps)rrrs`` r_wrap_generatorr!s)__T''R c4trtdr Jddtsfdntjr t dtj r t Stjfd}|S)a Like contextlib.ContextDecorator. But with the following differences: 1. Is done by wrapping, rather than inheritance, so it works with context managers that are implemented from C and thus cannot easily inherit from Python classes 2. Wraps generators in the intuitive way (c.f. https://bugs.python.org/issue37743) 3. Errors out if you try to wrap a class, because it is ambiguous whether or not you intended to wrap only the constructor The input argument can either be a context manager (in which case it must be a multi-shot context manager that can be directly invoked multiple times) or a callable that produces a context manager. __enter__z Passed in aB is both callable and also a valid context manager (has __enter__), making it ambiguous which interface to use. If you intended to pass a context manager factory, rewrite your call as context_decorator(lambda: ctx()); if you intended to pass a context manager directly, rewrite your call as context_decorator(lambda: ctx)cSr )ctxsrrz&context_decorator..ctx_factorybsJr"a>Cannot decorate classes; it is ambiguous whether or not only the constructor or all methods should have the context manager applied; additionally, decorating a class at definition-site will prevent use of the identifier as a conventional type. To specify which methods to decorate, decorate each of them individually.cN5|i|cdddS#1swYyxYwr r&)rrrrs rdecorate_contextz+context_decorator..decorate_contextus* ] )(( ) ) )s$) callablehasattrinspectisclass RuntimeErrorisgeneratorfunctionr!rr )r'rr)rs`` @rcontext_decoratorr0Hs  '#{"; SEP P < C=  t   ""4({D11__T)) r"cFeZdZdZdedefdZd dZdeded eddfd Zd Z y) _DecoratorContextManagerz2Allow a context manager to be used as a decorator. orig_funcreturnctjr0tjdtdt t fd}n}t t t|j|S)NzDecorating classes is deprecated and will be disabled in future versions. You should only decorate functions or methods. To preserve the current behavior of class decoration, you can directly decorate the `__init__` method and nothing else.) stacklevelc|i|Sr r&)rrr3s rz3_DecoratorContextManager.__call__..s9d3Mf3Mr") r,r-warningswarn FutureWarningrrr0clone)selfr3rs ` r__call__z!_DecoratorContextManager.__call__sV ??9 % MML  MNDDA(T:;;r"Nctr NotImplementedErrorr>s rr$z"_DecoratorContextManager.__enter__!!r"exc_type exc_value tracebackctr rA)r>rErFrGs r__exit__z!_DecoratorContextManager.__exit__rDr"c"|jSr  __class__rCs rr=z_DecoratorContextManager.clones~~r")r4N) __name__ __module__ __qualname____doc__rr?r$rrIr=r&r"rr2r2}sC<rZsX  // CH  Cx 2j2j  < &> r"