
NXc           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l m	 Z	 d d l m
 Z
 m Z m Z m Z m Z m Z m Z m Z m Z d d l Z d d l m Z m Z d d l m Z m Z m Z m Z m Z d d l m Z m  Z  m! Z! e  r5d d l" m# Z# n d d l# m# Z# e j$ e%  Z& d	   Z' d
 e( f d     YZ) d e j* f d     YZ* d e j+ f d     YZ, d e j- f d     YZ- d e j. f d     YZ/ d e j. f d     YZ0 d e j1 f d     YZ2 e j3 d  Z4 d e j e( f d     YZ5 d e j6 f d     YZ6 d e j7 f d     YZ8 d e8 f d      YZ9 d S(!   s  Nose Plugin that supports IPython doctests.

Limitations:

- When generating examples for use as doctests, make sure that you have
  pretty-printing OFF.  This can be done either by setting the
  ``PlainTextFormatter.pprint`` option in your configuration file to  False, or
  by interactively disabling it with  %Pprint.  This is required so that IPython
  output matches that of normal Python, which is used by doctest for internal
  execution.

- Do not rely on specific prompt numbers for results (such as using
  '_34==True', for example).  For IPython tests run via an external process the
  prompt numbers may be different, and IPython tests run as normal python code
  won't even have these special _NN variables set at all.
iN(   t	   getmodule(	   t   REPORTING_FLAGSt   REPORT_ONLY_FIRST_FAILUREt   _unittest_reportflagst   DocTestRunnert   _extract_future_flagst   pdbt   _OutputRedirectingPdbt   _exception_tracebackt	   linecache(   t   doctestst   Plugin(   t   anypt
   getpackaget   test_addresst   resolve_namet   tolist(   t   builtin_modt   PY3t   getcwd(   t   StringIOc         C   s    t  j j |   d j   d k S(   s   Return whether the given filename is an extension module.

    This simply checks that the extension is either .so or .pyd.
    i   s   .sos   .pyd(   s   .sos   .pyd(   t   ost   patht   splitextt   lower(   t   filename(    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyt   is_extension_moduleB   s    t   DocTestSkipc           B   s&   e  Z d  Z d Z d   Z d   Z RS(   s*   Object wrapper for doctests to be skipped.s/   Doctest to skip.
    >>> 1 #doctest: +SKIP
    c         C   s   | |  _  d  S(   N(   t   obj(   t   selfR   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyt   __init__Q   s    c         C   s0   | d k r t  j St t j |  d  |  Sd  S(   Nt   __doc__R   (   R   t   ds_skipt   getattrt   objectt   __getattribute__(   R   t   key(    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR#   T   s    (   t   __name__t
   __module__R   R    R   R#   (    (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR   J   s   	t   DocTestFinderc           B   s   e  Z d    Z d   Z RS(   c         C   s  | d k r t St j |  r/ | j | j k St j |  rN | j | j k St j	 |  rm | j | j k St j
 |  r | j | j j j k St j |  d k	 r | t j |  k St | d  r | j | j k St | t  r t St j |  r t St d |   d S(   sY   
        Return true if the given object is defined in the given
        module.
        R&   s*   object must be a class or function, got %rN(   t   Nonet   Truet   inspectt
   isfunctiont   __dict__t   __globals__t	   isbuiltinR%   R&   t   isclasst   ismethodt   __self__t	   __class__R    t   hasattrt
   isinstancet   propertyt   ismethoddescriptort   Falset
   ValueError(   R   t   moduleR"   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyt   _from_module^   s&    c         C   s  t  | d  r t |  } n  t j j |  | | | | | | |  d d l m } m }	 m }
 t j |  r |  j	 r x| | j
 j   D]h \ } } d | | f } | |  s |	 |  r |  j | |  r |  j | | | | | | |  q q Wn  t j |  r|  j	 rx | j
 j   D] \ } } t | t  rKt | |  } n  t | t  rot | |  j } n  t j |  st j |  st j |  st | t  r|  j | |  rd | | f } |  j | | | | | | |  qqWn  d S(   sm   
        Find tests for the given object and any contained objects, and
        add them to `tests`.
        t   skip_doctesti(   t	   isroutineR/   t   ismodules   %s.%sN(   R3   R   t   doctestR'   t   _findR*   R<   R/   R=   t   _recurseR,   t   itemsR:   R4   t   staticmethodR!   t   classmethodt   __func__R+   R0   R5   (   R   t   testsR   t   nameR9   t   source_linest   globst   seenR<   R/   R=   t   valnamet   valt   valname1(    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR?   ~   s2    (   R%   R&   R:   R?   (    (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR'   \   s   	 t   IPDoctestOutputCheckerc           B   s&   e  Z d  Z e j d  Z d   Z RS(   s   Second-chance checker with support for random tests.

    If the default comparison doesn't pass, this checker looks in the expected
    output string for flags that tell us to ignore the output.
    s   #\s*random\s+c         C   s<   t  j j |  | | |  } | r8 |  j j |  r8 t S| S(   s   Check output, accepting special markers embedded in the output.

        If the output didn't pass the default validation but the special string
        '#random' is included, we accept it.(   R>   t   OutputCheckert   check_outputt	   random_ret   searchR)   (   R   t   wantt   gott   optionflagst   ret(    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyRO      s
    	(   R%   R&   R   t   ret   compileRP   RO   (    (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyRM      s   t   DocTestCasec           B   sD   e  Z d  Z d d d d d d d  Z d   Z d   Z d   Z RS(   sN  Proxy for DocTestCase: provides an address() method that
    returns the correct address for the doctest case. Otherwise
    acts as a proxy to the test case. To provide hints for address(),
    an obj may also be passed -- this will be used as the test object
    for purposes of determining the test address, if it is provided.
    i    t   _c   	      C   s   | |  _  t j j |  | d | d | d | d | | |  _ | |  _ | |  _ | j |  _ | |  _	 | |  _
 t d | d | d t  } | |  _ t j j | j  |  _ d  S(   NRT   t   setUpt   tearDownt   checkert   verbose(   t   _result_varR
   RX   R   t   _dt_optionflagst   _dt_checkert   _dt_testRH   t   _dt_test_globs_orit	   _dt_setUpt   _dt_tearDownt   IPDocTestRunnerR7   t
   _dt_runnerR   R   t   dirnameR   t   _ori_dir(	   R   t   testRT   RZ   R[   R\   R   t
   result_vart   runner(    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR      s    							c   	      C   s   |  j  } |  j } t j } t   } |  j } | t @sD | t O} n  zN t   } t	 j
 |  j  d d | _ | j | d | j d t \ } } Wd  | t _ t	 j
 |  X| r |  j |  j | j      n  d  S(   Nt   -iF   t   outt   clear_globs(   Ra   Rf   t   syst   stdoutR   R_   R   R   R   R   t   chdirRh   t   DIVIDERt   runt   writeR7   t   failureExceptiont   format_failuret   getvalue(	   R   Ri   Rk   t   oldt   newRT   t   curdirt   failurest   tries(    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyt   runTest   s"    					
		c         C   s   t  |  j j d t  r} i  |  _ |  j j t j  t j j |  j j  t j j	 d d  t t j d <t j |  j _ n  t t |   j   d S(   s5   Modified test setup that syncs with ipython namespacei    RY   t   __builtins__N(   R4   Ra   t   examplest	   IPExamplet   user_ns_origt   updatet   _ipt   user_nsRH   t   popR(   R   t   superRX   RZ   (   R   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyRZ     s    	c         C   s   t  |  j j d t  rK |  j |  j _ t j j   t j j	 |  j
  n  y t t |   j   Wn/ t k
 r } | j d |  j k r   q n Xd  S(   Ni    (   R4   Ra   R   R   Rb   RH   R   R   t   clearR   R   R   RX   R[   t   AttributeErrort   argsR^   (   R   t   exc(    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR[   &  s    N(   R%   R&   R   R(   R   R}   RZ   R[   (    (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyRX      s   			R   c           B   s   e  Z RS(    (   R%   R&   (    (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR   G  s    t   IPExternalExamplec           B   s#   e  Z d  Z d d d d d  Z RS(   s2   Doctest examples to be run in an external process.i    c         C   s5   t  j j |  | | | | | |  |  j d 7_ d  S(   Ns   
(   R>   t   ExampleR   t   source(   R   R   RR   t   exc_msgt   linenot   indentt   options(    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR   M  s    "N(   R%   R&   R   R(   R   (    (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR   J  s   	t   IPDocTestParserc           B   s   e  Z d  Z d Z d Z d Z d Z d Z e j	 e e e e e f e j
 e j B Z e j	 e e e e e f e j
 e j B Z e j	 d  Z e j	 d  Z d   Z d	 d
  Z e d  Z d   Z RS(   s   
    A class used to parse strings containing doctest examples.

    Note: This is a version modified to properly recognize IPython input and
    convert any IPython examples into valid Python ones.
    s   >>>s   \.\.\.s   In\ \[\d+\]:s   \ \ \ \.\.\.+:s]  
        # Source consists of a PS1 line followed by zero or more PS2 lines.
        (?P<source>
            (?:^(?P<indent> [ ]*) (?P<ps1> %s) .*)    # PS1 line
            (?:\n           [ ]*  (?P<ps2> %s) .*)*)  # PS2 lines
        \n? # a newline
        # Want consists of any non-blank lines that do not start with PS1.
        (?P<want> (?:(?![ ]*$)    # Not a blank line
                     (?![ ]*%s)   # Not a line starting with PS1
                     (?![ ]*%s)   # Not a line starting with PS2
                     .*$\n?       # But any other line
                  )*)
                  s   #\s*all-random\s+s   #\s*ipdoctest:\s*EXTERNALc         C   s?   t  j j |  } t | j    d k r7 t  j |  S| Sd S(   s/   Convert input IPython source into valid Python.i   N(   R   t   input_transformer_managert   transform_cellt   lent
   splitlinest	   prefilter(   R   R   t   block(    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyt   ip2py  s    s   <string>c         C   s/  | j    } |  j |  } | d k rY d j g  | j d  D] } | | ^ q=  } n  g  } d \ } } |  j j |  r d } n d } t }	 t |  j j	 |   }
 |
 r t
 j } n? t |  j j	 |   }
 |  j j |  r t } n t } t }	 x|
 D]} | j | | | j   ! | | j d | | j    7} |  j | | | |	  \ } } } } | | 7} | t k rt | t
 j <| d 7} n  |  j |  s| j | | | | d | d | t | j d   d |  n  | | j d | j   | j    7} | j   } qW| j | |  | S(	   s=  
        Divide the given string into examples and intervening text,
        and return them as a list of alternating Examples and strings.
        Line numbers for the Examples are 0-based.  The optional
        argument `name` is a name identifying this string, and is only
        used for error messages.
        i    s   
s	   
# randomt    R   R   R   (   i    i    (   t
   expandtabst   _min_indentt   joint   splitt   _RANDOM_TESTRQ   R7   t   listt   _EXAMPLE_RE_PYt   finditerR>   R   t   _EXAMPLE_RE_IPt   _EXTERNAL_IPR   R   R)   t   appendt   startt   countt   _parse_examplet   NORMALIZE_WHITESPACEt   _IS_BLANK_OR_COMMENTR   t   groupt   end(   R   t   stringRF   t
   min_indentt   lt   outputt   charnoR   t   random_markerR   t   termsR   t   mR   R   RR   R   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyt   parse  sF    2		$
%c         C   s  t  | j d   } | j d  j d  } | j d  } | j d  } t  |  }	 |  j | | | | |	  | r |  j | d d | | | |  n  d j g  | D] }
 |
 | |	 d ^ q  } | r |  j |  } n  | j d  } | j d  } t  |  d k r1t j d	 | d
  r1| d
 =n  |  j | d | | | t  |   t j	 d d | d  | d <d j g  | D] } | | ^ q } |  j
 j |  } | r| j d  } n d } |  j | | |  } | | | | f S(   s7  
        Given a regular expression match from `_EXAMPLE_RE` (`m`),
        return a pair `(source, want)`, where `source` is the matched
        example's source code (with prompts and indentation stripped);
        and `want` is the example's expected output (with indentation
        stripped).

        `name` is the string's name, and `lineno` is the line number
        where the example starts; both are used for error messages.

        Optional:
        `ip2py`: if true, filter the input via IPython to convert the syntax
        into valid python.
        R   R   s   
t   ps1t   ps2i   t    RR   s    *$is   Out\[\d+\]: \s*?\n?R   i    t   msgN(   R   R   R   t   _check_prompt_blankt   _check_prefixR   R   RV   t   matcht   subt   _EXCEPTION_RER(   t   _find_options(   R   R   RF   R   R   R   RG   R   R   t   ps1_lent   slR   RR   t
   want_linest   wlR   R   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR     s2    %.(
&c   
   	   C   s   | | } | d } xj t  |  D]\ \ } }	 t |	  | k r! |	 | d k r! t d | | d | |	 | | !|	 f   q! q! Wd S(   s  
        Given the lines of a source string (including prompts and
        leading indentation), check to make sure that every prompt is
        followed by a space character.  If any line is not followed by
        a space character, then raise ValueError.

        Note: IPython-modified version which takes the input prompt length as a
        parameter, so that prompts of variable length can be dealt with.
        i   R   s8   line %r of the docstring for %s lacks blank after %s: %rN(   t	   enumerateR   R8   (
   R   t   linesR   RF   R   R   t	   space_idxt   min_lent   it   line(    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR   &  s    


"(   R%   R&   R   t   _PS1_PYt   _PS2_PYt   _PS1_IPt   _PS2_IPt   _RE_TPLRV   RW   t	   MULTILINEt   VERBOSER   R   R   R   R   R   R7   R   R   (    (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR   V  s    	WAt   SKIPRe   c           B   s    e  Z d  Z d d e d  Z RS(   sK   Test runner that synchronizes the IPython namespace with test globals.
    c         C   s2   | j  j t j  t t |   j | | | |  S(   N(   RH   R   R   R   R   Re   Rs   (   R   Ri   t   compileflagsRm   Rn   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyRs   A  s    N(   R%   R&   R   R(   R)   Rs   (    (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyRe   =  s   t   DocFileCasec           B   s   e  Z d  Z d   Z RS(   s"   Overrides to provide filename
    c         C   s   |  j  j d  d  f S(   N(   Ra   R   R(   (   R   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyt   addressU  s    (   R%   R&   R   R   (    (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR   R  s   t   ExtensionDoctestc           B   sM   e  Z d  Z d Z e Z e j d  Z d   Z	 d   Z
 d   Z d   Z RS(   s=   Nose Plugin that supports doctests in extension modules.
    t
   extdoctestc      
   C   s   t  j |  | |  | j d d d d d d | j d t  d d	 | j d
 d d d d d d | j d  } | d  k	 r | j d t |   n  d  S(   Ns   --doctest-testst   actiont
   store_truet   destt   doctest_testst   defaultt   NOSE_DOCTEST_TESTSt   helps   Also look for doctests in test modules. Note that classes, methods and functions should have either doctests or non-doctest tests, not both. [NOSE_DOCTEST_TESTS]s   --doctest-extensionR   t   doctestExtensionsL   Also look for doctests in files with this extension [NOSE_DOCTEST_EXTENSION]t   NOSE_DOCTEST_EXTENSION(   R   R   t
   add_optiont   getR)   R(   t   set_defaultsR   (   R   t   parsert   envt   env_setting(    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR   _  s    c         C   s   t  j |  | |  g  | j j D] } | j d k r  | ^ q  | j _ | j |  _ t | j  |  _ t j	   |  _
 t   |  _ t   |  _ d  |  _ d  |  _ d  S(   NR>   (   R   t	   configuret   pluginsRF   R   R   R   t	   extensionR>   t   DocTestParserR   R'   t   finderRM   R\   R(   RH   t
   extraglobs(   R   R   t   configt   p(    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR   t  s    !	c         C   sx   t  j j |  \ } } t  j j |  d } z5 t j j |  t |  } t |  j |   } Wd  t j j	   X| S(   Ni    (
   R   R   R   R   Ro   R   t
   __import__R   t   loadTestsFromModuleR   (   R   R   t   bpatht   modt   modnameR9   RE   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyt   loadTestsFromExtensionModule  s    c         c   s   |  j  | j  s& t j d |  d  S|  j j | d |  j d |  j } | sT d  St j	 t j
 B} | j   | j } | d d
 k r | d  } n  xL | D]D } | j s q n  | j s | | _ n  t | d | d	 |  j Vq Wd  S(   Ns   Doctest doesn't want module %sRH   R   is   .pycs   .pyoiRT   R\   (   s   .pycs   .pyo(   t   matchesR%   t   logt   debugR   t   findRH   R   R>   R   t   ELLIPSISt   sortt   __file__R   R   RX   R\   (   R   R9   RE   RT   t   module_fileRi   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR     s(    
				c         c   s   t  |  r. x |  j |  D] } | Vq Wn |  j r t | j |  j  r t j j |  } t |  } z | j	   } Wd  | j
   X|  j j | d i | d 6d | d | d d } | j r t |  Vq t Vn  d  S(   NRH   R   RF   R   R   i    (   R   R   R   R   t   endswithR   R   t   basenamet   opent   readt   closeR   t   get_doctestR   R   R7   (   R   R   t   tRF   t   dht   docRi   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyt   loadTestsFromFile  s    		(   R%   R&   R   RF   R)   t   enabledR   t   environR   R   R   R   R  (    (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR   Y  s   			t   IPythonDoctestc           B   s;   e  Z d  Z d Z e Z d   Z e j d  Z	 d   Z
 RS(   s=   Nose Plugin that supports doctests in extension modules.
    t	   ipdoctestc      	   c   s   t  j t  j B} |  j j | d t |  } | r xL | D]A } t | j  d k r\ q; n  t | d | d | d |  j	 Vq; Wn  d S(   sb   Look for doctests in the given object, which will be a
        function, method or class.
        R9   i    R   RT   R\   N(
   R>   R   R   R   R   R    R   R   RX   R\   (   R   R   t   parentRT   R
   Ri   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyt   makeTest  s    c      
   C   s   t  j |  | |  | j d d d d d d | j d t  d d	 | j d
 d d d d d d | j d  } | d  k	 r | j d t |   n  d  S(   Ns   --ipdoctest-testsR   R   R   t   ipdoctest_testsR   t   NOSE_IPDOCTEST_TESTSR   s   Also look for doctests in test modules. Note that classes, methods and functions should have either doctests or non-doctest tests, not both. [NOSE_IPDOCTEST_TESTS]s   --ipdoctest-extensionR   t   ipdoctest_extensionsN   Also look for doctests in files with this extension [NOSE_IPDOCTEST_EXTENSION]t   NOSE_IPDOCTEST_EXTENSION(   R   R   R   R   R)   R(   R   R   (   R   R   R   R   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR     s    c         C   s   t  j |  | |  g  | j j D] } | j d k r  | ^ q  | j _ | j |  _ t | j  |  _ t	   |  _
 t d |  j
  |  _ t   |  _ d  |  _ d  |  _ d  S(   NR>   R   (   R   R   R   RF   R  R   R   R  R   R   R   R'   R   RM   R\   R(   RH   R   (   R   R   R   R   (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR     s    !	(   R%   R&   R   RF   R)   R  R  R   R  R   R   (    (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyR    s   	(:   R   R>   R*   t   loggingR   RV   Ro   t	   tracebackt   unittestR    R   R   R   R   R   R   R   R   R	   t	   nose.coret   noset   nose.pluginsR
   R   t	   nose.utilR   R   R   R   R   t   IPython.utils.py3compatR   R   R   t   ioR   t	   getLoggerR%   R   R   R"   R   R'   RN   RM   RX   R   R   R   R   R   t   register_optionflagR   Re   R   t   DoctestR   R  (    (    (    sX   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/plugin/ipdoctest.pyt   <module>   s>   @(	T}m