U
    d                     @   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
   9/tmp/pip-unpacked-wheel-ua33x9lu/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td| j | j |ks.td S )Nr   zContext %s is empty.)lenr   AssertionErrorr   popr   r
   r
   r   exit   s    z_ContextInfo.exitTc                 C   sB   t | jdkr8|sd S | js*td| j | |   | jd S )Nr   z*Context %s is required but none is active.)r   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 kr:t|ts"td|t|t|td| j |< | j | S )Nz'must be a context managed class, got {})r   )r!   
issubclassManagedr   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 k	r6t|| s2td| t|f |S |j|dS )Nz)Wrong context type. Expected: %s, got %s.)r   )r)   r'   
isinstancer   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               
      s&     | |W  5 Q R  S Q R X d S r   r
   )r4   kwargsfuncr	   r
   r   wrapper^   s    z!Managed.__call__.<locals>.wrapper)	functoolswraps)r	   r8   r9   r
   r7   r   __call__]   s    zManaged.__call__)NT)	r   r   r   __doc__classmethodr2   r3   r5   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   r:   objectr   r    r(   r)   r/   r#   r%   r
   r
   r
   r   <module>   s   $