
    Ǆg                         d dl Z d dlZd dlZd dlZd dlmZmZmZmZ edef   Z	 ede	      Z
d Zd Z G d d	      Z G d
 de      Zy)    N)AnyCallableTypeVarcast.F)boundc                 F     t        j                         fd       }|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  |j                  d       }d d d        	 	 }        5  |j                  |      }d d d        '# 1 sw Y   1xY w# 1 sw Y   xY w# t        $ r.         5  |j                          d d d         # 1 sw Y    xY wt        $ rA         5   |j                  t        j                          }d d d        n# 1 sw Y   nxY wY w xY w# t        $ r}|j                  cY d }~S d }~ww xY wwN)	sendGeneratorExitcloseBaseExceptionthrowsysexc_infoStopIterationvalue)argskwargsgenresponserequestectx_factoryfuncs         _/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/utils/_contextlib.pygenerator_contextz*_wrap_generator.<locals>.generator_context   s	    D#F# 	 *88D>* 5$,nG % 5#&88G#45% * **5 5 % $ $		$$$ >$ >#,399clln#=> > >>  	 77N		s   	DC0 A	C0 A. C0 A"	C0 AC0 "A+'C0 .C->B	C-B!	C-3"C	C-C'	#C-*C0 ,C--C0 0	D9D
DD
DD)	functoolswraps)r   r   r   s   `` r   _wrap_generatorr!      s)     __T' 'R     c                 4    t               rt         d      rJ d  d       t               s fdn t        j                        rt	        d      t        j
                        rt              S t        j                        fd       }|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)c                       S r    )ctxs   r   r   z&context_decorator.<locals>.ctx_factory_   s    Jr"   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.c                  N            5   | i |cd d d        S # 1 sw Y   y xY wr   r&   )r   r   r   r   s     r   decorate_contextz+context_decorator.<locals>.decorate_contextq   s*    ] 	)((	) 	) 	)s   $)	callablehasattrinspectisclassRuntimeErrorisgeneratorfunctionr!   r   r    )r'   r   r)   r   s   `` @r   context_decoratorr0   F   s      '#{"; 
SE P 	P< C=	 t
 	
 ""4({D11__T) ) r"   c                   F    e Zd ZdZdedefdZddZdeded	eddfd
Zd Z	y)_DecoratorContextManagerz2Allow a context manager to be used as a decorator.	orig_funcreturnc                     t        j                        r0t        j                  dt        d       t        t        fd      }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 |S r   r&   )r   r   r3   s     r   <lambda>z3_DecoratorContextManager.__call__.<locals>.<lambda>   s    9d3Mf3M r"   )	r,   r-   warningswarnFutureWarningr   r   r0   clone)selfr3   r   s    ` r   __call__z!_DecoratorContextManager.__call__|   sV    ??9%MML  MNDDA(T:;;r"   Nc                     t         r   NotImplementedErrorr>   s    r   r$   z"_DecoratorContextManager.__enter__       !!r"   exc_type	exc_value	tracebackc                     t         r   rA   )r>   rE   rF   rG   s       r   __exit__z!_DecoratorContextManager.__exit__   rD   r"   c                 "    | j                         S r   	__class__rC   s    r   r=   z_DecoratorContextManager.clone   s    ~~r"   )r4   N)
__name__
__module____qualname____doc__r   r?   r$   r   rI   r=   r&   r"   r   r2   r2   y   sC    <<! < < "" " " " " r"   r2   c                   $     e Zd ZdZd fd	Z xZS )_NoParamDecoratorContextManagerzFAllow a context manager to be used as a decorator without parentheses.c                 @    |t         |   |       S   |        |      S r   )super__new__)clsr3   rL   s     r   rU   z'_NoParamDecoratorContextManager.__new__   s'    7?3''suYr"   r   )rM   rN   rO   rP   rU   __classcell__rK   s   @r   rR   rR      s    P   r"   rR   )r   r,   r:   r   typingr   r   r   r   FuncTyper   r!   r0   r2   rR   r&   r"   r   <module>rZ      sX   
    
 / /
 CHCx 1h0f   < &>  r"   