ó
 \c           @   s   d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d d d g Z d d d  Z e d    Z	 e d	    Z
 d S(
   i’’’’(   t   contextmanagerNt   all_warningst   expected_warningst   warni   c         C   s?   | d k	 r( t j |  d | d | n t j |  d | d S(   sA   A version of `warnings.warn` with a default stacklevel of 2.
    t   categoryt
   stacklevelN(   t   Nonet   warningsR   (   t   messageR   R   (    (    s8   lib/python2.7/site-packages/skimage/_shared/_warnings.pyR   
   s    c          c   sŃ   t  j   }  |  r@ x+ t  j |   D] } i  | d j d <q" Wn  ~  xZ t t j j    D]C \ } } d | k rw qY n  y | j j	   WqY t
 k
 r qY XqY Wt j d t   } t j d  | VWd QXd S(   sĀ  
    Context for use in testing to ensure that all warnings are raised.

    Examples
    --------
    >>> import warnings
    >>> def foo():
    ...     warnings.warn(RuntimeWarning("bar"))

    We raise the warning once, while the warning filter is set to "once".
    Hereafter, the warning is invisible, even with custom filters:

    >>> with warnings.catch_warnings():
    ...     warnings.simplefilter('once')
    ...     foo()

    We can now run ``foo()`` without a warning being raised:

    >>> from numpy.testing import assert_warns
    >>> foo()

    To catch the warning, we call in the help of ``all_warnings``:

    >>> with all_warnings():
    ...     assert_warns(RuntimeWarning, foo)
    i    t   __warningregistry__s	   six.movest   recordt   alwaysN(   t   inspectt   currentframet   getouterframest   f_localst   listt   syst   modulest   itemsR	   t   cleart   AttributeErrorR   t   catch_warningst   Truet   simplefilter(   t   framet   ft   mod_namet   modt   w(    (    s8   lib/python2.7/site-packages/skimage/_shared/_warnings.pyR      s    &"c      
   c   sK  t  |  t  r t d   n  t   } | Vx d |  k rN |  j d  q2 Wg  |  D]! } d | j d  k rV | ^ qV } x | D] } t } xT |  D]L } t j	 | t | j
   d k	 r t } | | k rć | j |  qć q q W| s t d t | j
    q q Wt |  d k rAd d j |  } t |   n  Wd QXd S(	   s  Context for use in testing to catch known warnings matching regexes

    Parameters
    ----------
    matching : list of strings or compiled regexes
        Regexes for the desired warning to catch

    Examples
    --------
    >>> from skimage import data, img_as_ubyte, img_as_float
    >>> with expected_warnings(['precision loss']):
    ...     d = img_as_ubyte(img_as_float(data.coins()))

    Notes
    -----
    Uses `all_warnings` to ensure all warnings are raised.
    Upon exiting, it checks the recorded warnings for the desired matching
    pattern(s).
    Raises a ValueError if any match was not found or an unexpected
    warning was raised.
    Allows for three types of behaviors: `and`, `or`, and `optional` matches.
    This is done to accomodate different build enviroments or loop conditions
    that may produce different warnings.  The behaviors can be combined.
    If you pass multiple patterns, you get an orderless `and`, where all of the
    warnings must be raised.
    If you use the `|` operator in a pattern, you can catch one of several
    warnings.
    Finally, you can use `|\A\Z` in a pattern to signify it as optional.

    sA   ``matching`` should be a list of strings and not a string itself.s   \A\Zt   |s   Unexpected warning: %si    s   No warning raised matching:
%ss   
N(   t
   isinstancet   strt
   ValueErrorR   R   t   removet   splitt   Falset   ret   searchR   R   t   lent   join(   t   matchingR   t   mt	   remainingR   t   foundt   matcht   msg(    (    s8   lib/python2.7/site-packages/skimage/_shared/_warnings.pyR   L   s&     .! (   t
   contextlibR    R   R   R   R%   t   __all__R   R   R   R   (    (    (    s8   lib/python2.7/site-packages/skimage/_shared/_warnings.pyt   <module>   s   	9