
    fi.                     H   d Z ddlZddlZddlZddlmZ  ej                  e      Z G d dej                        Z	 G d dej                        Z G d d	ej                        Z G d
 dej                        Z ej                   d      Z G d dej$                  e      Zy)a  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.
    N)modified_envc                        e Zd Z fdZ xZS )DocTestFinderc                     t         |   |||||      }t        t        |dd            r0|.|j                  D ]  }d|j
                  t        j                  <   ! |S )N__skip_doctest__FT)super	_get_testboolgetattrexamplesoptionsdoctestSKIP)	selfobjnamemoduleglobssource_linestestexample	__class__s	           [/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/testing/plugin/ipdoctest.pyr	   zDocTestFinder._get_test)   s]    w dFE<H/78T=M== 504-5     )__name__
__module____qualname__r	   __classcell__r   s   @r   r   r   (   s     r   r   c                   :    e Zd ZdZ ej
                  d      Zd Zy)IPDoctestOutputCheckerzSecond-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.
    z#\s*random\s+c                     t         j                  j                  | |||      }|s| j                  j	                  |      ry|S )zCheck 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.T)r   OutputCheckercheck_output	random_research)r   wantgotoptionflagsrets        r   r$   z#IPDoctestOutputChecker.check_output<   s?     ##00tS1<>t~~,,T2
r   N)r   r   r   __doc__recompiler%   r$    r   r   r!   r!   3   s     

+,Ir   r!   c                       e Zd Zy)	IPExampleN)r   r   r   r.   r   r   r0   r0   O   s    r   r0   c                   (   e Zd ZdZdZdZdZdZdZ e	j                  eeeeefz  e	j                  e	j                  z        Z e	j                  eeeeefz  e	j                  e	j                  z        Z e	j                  d      Zd Zdd	Zdd
Zd Zy)IPDocTestParserz
    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.
    z>>>z\.\.\.zIn\ \[\d+\]:z\ \ \ \.\.\.+:a]  
        # 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
                  )*)
                  z#\s*all-random\s+c                     t         j                  j                  |      }t        |j	                               dk(  rt         j                  |      S |S )z/Convert input IPython source into valid Python.   )_ipinput_transformer_managertransform_celllen
splitlines	prefilter)r   sourceblocks      r   ip2pyzIPDocTestParser.ip2py   sB    --<<VDu!"a'==''Lr   c                    |j                         }| j                  |      }|dkD  r1dj                  |j                  d      D cg c]  }||d 	 c}      }g }d\  }}| j                  j                  |      rd}nd}d}	t        | j                  j                  |            }
|
rt        j                  }n,t        | j                  j                  |            }
t        }d}	|
D ]  }|j                  |||j                                 ||j                  d||j                               z  }| j!                  ||||	      \  }}}}||z  }| j#                  |      s8|j                   ||||||t%        |j'                  d	            z   |
             ||j                  d|j                         |j)                               z  }|j)                         } |j                  ||d        |S c c}w )a=  
        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.
        r   
N)r   r   z	
# random FTindent)linenorA   r   )
expandtabs_min_indentjoinsplit_RANDOM_TESTr&   list_EXAMPLE_RE_PYfinditerr   Example_EXAMPLE_RE_IPr0   appendstartcount_parse_example_IS_BLANK_OR_COMMENTr8   groupend)r   stringr   
min_indentloutputcharnorB   random_markerr=   termsrK   mr;   r   r'   exc_msgs                    r   parsezIPDocTestParser.parse   s    ""$%%f-
>YYT8JK1*+KLF ##F+(MM  T((11&9:ooG ,,55f=>EGE 	AMM&	23fll4;;F ((D&? -VWdG
 M!D ,,V4gfdG-3-7AGGH<M8N-N.57 8
 fll4AEEG<<FUUWF-	0 	fVWo&g  Ls   G0c           	         t        |j                  d            }|j                  d      j                  d      }|j                  d      }|j                  d      }t        |      }	| j                  |||||	       |r| j	                  |dd d|z  |z   ||       dj                  |D 
cg c]  }
|
||	z   dz   d  c}
      }|r| j                  |      }|j                  d	      }|j                  d      }t        |      dkD  rt        j                  d
|d         r|d= | j	                  |d|z  ||t        |      z          t        j                  dd|d         |d<   dj                  |D cg c]  }||d 	 c}      }| j                  j                  |      }|r|j                  d      }nd}| j                  |||      }||||fS c c}
w c c}w )a7  
        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.
        rA   r;   r?   ps1ps2r4   N r'   z *$zOut\[\d+\]: \s*?\n?r@   r   msg)r8   rR   rF   _check_prompt_blank_check_prefixrE   r=   r,   matchsub_EXCEPTION_RE_find_options)r   r[   r   rB   r=   rA   r   r_   r`   ps1_lenslr;   r'   
want_lineswlr\   r   s                    r   rP   zIPDocTestParser._parse_example   s   " QWWX&' wwx(..t4 ggenggenc(  vtVGL|AB/Vc1A4PLIbBvg~a/01IJZZ'F
 wwvZZ%
z?Q288FJrN#C2:s6z4!C$55	7 5bAG
1yy
;""VW+;< $$T*ggenGG $$VT6:wg--? J& <s   +G2Gc                     ||z   }|dz   }t        |      D ]<  \  }}	t        |	      |k\  s|	|   dk7  st        d||z   dz   d|d|	|| d|	       y)a  
        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.
        r4   ra   zline z of the docstring for z lacks blank after z: N)	enumerater8   
ValueError)
r   linesrA   r   rB   rj   	space_idxmin_lenilines
             r   rd   z#IPDocTestParser._check_prompt_blank  su     7N	A+ ' 	AGAt4yW$iC)? "((1*d"&vi"8$"@ A A	Ar   N)z<string>)F)r   r   r   r+   _PS1_PY_PS2_PY_PS1_IP_PS2_IP_RE_TPLr,   r-   	MULTILINEVERBOSErI   rL   rG   r=   r]   rP   rd   r.   r   r   r2   r2   R   s     GGGGG  RZZGGGG+L!L!#

!:<N  RZZGGGG+L!L!#

!:<N 2::23LBH?.BAr   r2   r   c                   $     e Zd ZdZd fd	Z xZS )IPDocTestRunnerzKTest runner that synchronizes the IPython namespace with test globals.
    c                 x    t        ddd      5  t        t        |   ||||      cd d d        S # 1 sw Y   y xY w)N8024)COLUMNSLINES)r   r   r~   run)r   r   compileflagsoutclear_globsr   s        r   r   zIPDocTestRunner.run'  sB    dT:; 	Q243?KQ	Q 	Q 	Qs   09)NNT)r   r   r   r+   r   r   r   s   @r   r~   r~   #  s    Q Qr   r~   )r+   r   loggingr,   testpathr   	getLoggerr   logr   r#   r!   rK   r0   DocTestParserr2   register_optionflagr   DocTestRunnerobjectr~   r.   r   r   <module>r      s   *   	 ! g!G)) W22 8 ' &KAg++ KA\ #w""6*Qg++F Qr   