a
    ==ic                     @   sr   d dl Z d dlZd dlZG dd deZG dd deZe add Zdd	 ZG d
d deZ	G dd de	Z
dS )    Nc                   @   s:   e Zd Zdd Zedd Zdd Zdd Zdd
dZdS )_ContextInfoc                 C   s   || _ || _t | _d S N)clsallow_default	threadinglocal_local_stack)selfr   r    r
   f/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/caffe2/python/context.py__init__
   s    z_ContextInfo.__init__c                 C   s   t | jdsg | j_| jjS )Nobj)hasattrr   r   r	   r
   r
   r   _stack   s    z_ContextInfo._stackc                 C   s   | j | d S r   )r   appendr	   valuer
   r
   r   enter   s    z_ContextInfo.enterc                 C   s2   t | jdksJ d| j | j |ks.J d S )Nr   zContext %s is empty.)lenr   r   popr   r
   r
   r   exit   s    z_ContextInfo.exitTc                 C   sB   t | jdkr8|sd S | js*J d| j | |   | jd S )Nr   z*Context %s is required but none is active.)r   r   r   r   r   )r	   requiredr
   r
   r   
get_active   s    z_ContextInfo.get_activeN)T)	__name__
__module____qualname__r   propertyr   r   r   r   r
   r
   r
   r   r   	   s   
r   c                   @   s   e Zd Zdd Zdd ZdS )_ContextRegistryc                 C   s
   i | _ d S r   )_ctxsr   r
   r
   r   r   '   s    z_ContextRegistry.__init__c                 C   sD   || j vr:t|ts"J d|t|t|td| j |< | j | S )Nz'must be a context managed class, got {})r   )r    
issubclassManagedformatr   DefaultManagedr	   r   r
   r
   r   get*   s    
z_ContextRegistry.getN)r   r   r   r   r&   r
   r
   r
   r   r   &   s   r   c                   C   s   t S r   )_CONTEXT_REGISTRYr
   r
   r
   r   _context_registry4   s    r(   c                 C   s   dd t | jD S )Nc                 S   s*   g | ]"}t |tr|tkr|tkr|qS r
   )r!   r"   r$   ).0r   r
   r
   r   
<listcomp>:   s   z(_get_managed_classes.<locals>.<listcomp>)inspectgetmro	__class__)r   r
   r
   r   _get_managed_classes9   s    
r.   c                   @   s6   e Zd ZdZedddZdd Zdd	 Zd
d ZdS )r"   z
    Managed makes the inheritted class a context managed class.

        class Foo(Managed): ...

        with Foo() as f:
            assert f == Foo.current()
    NTc                 C   sB   t  | }|d ur6t|| s2J d| t|f |S |j|dS )Nz)Wrong context type. Expected: %s, got %s.)r   )r(   r&   
isinstancetyper   )r   r   r   Zctx_infor
   r
   r   currentK   s    zManaged.currentc                 C   s$   t | D ]}t ||  q| S r   )r.   r(   r&   r   r%   r
   r
   r   	__enter__T   s    zManaged.__enter__c                 G   s$   t | D ]}t ||  qd S r   )r.   r(   r&   r   )r	   argsr   r
   r
   r   __exit__Y   s    zManaged.__exit__c                    s   t   fdd}|S )Nc                     s6     | i |W  d    S 1 s(0    Y  d S r   r
   )r3   kwargsfuncr	   r
   r   wrapper^   s    z!Managed.__call__.<locals>.wrapper)	functoolswraps)r	   r7   r8   r
   r6   r   __call__]   s    zManaged.__call__)NT)	r   r   r   __doc__classmethodr1   r2   r4   r;   r
   r
   r
   r   r"   A   s   	r"   c                   @   s   e Zd ZdZdS )r$   zy
    DefaultManaged is similar to Managed but if there is no parent when
    current() is called it makes a new one.
    N)r   r   r   r<   r
   r
   r
   r   r$   e   s   r$   )r+   r   r9   objectr   r   r'   r(   r.   r"   r$   r
   r
   r
   r   <module>   s   $