
    Mhi.                     ,   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                  ZdS )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                    r ||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/html/test/jupyter/venv/lib/python3.11/site-packages/IPython/testing/plugin/ipdoctest.pyr	   zDocTestFinder._get_test)   sk    ww  dFE<HH/7788 	5T=M= 5 504--    )__name__
__module____qualname__r	   __classcell__r   s   @r   r   r   (   s8                r   r   c                   8    e Zd ZdZ ej        d          Zd ZdS )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                            | |||          }|s| j                            |          rdS |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<   sN     #00tS1<> > 	t~,,T22 	4
r   N)r   r   r   __doc__recompiler%   r$    r   r   r!   r!   3   sA          
+,,I    r   r!   c                       e Zd ZdS )	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dS )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                            |          }t          |                                          dk    rt                               |          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   sN    -<<VDDu!!""a''=='''Lr   <string>c                    |                                 }|                     |          dk    r4d                    fd|                    d          D                       }g }d\  }}| j                            |          rd}nd}d}t          | j                            |                    }|rt          j
        }	n0t          | j                            |                    }t          }	d}|D ].}
|                    |||
                                                    ||                    d||
                                          z  }|                     |
|||          \  }}}}||z  }|                     |          sG|                     |	||||t%          |
                    d	                    z   |
                     ||                    d|
                                |
                                          z  }|
                                }0|                    ||d                    |S )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   
c                 $    g | ]}|d          S Nr.   ).0l
min_indents     r   
<listcomp>z)IPDocTestParser.parse.<locals>.<listcomp>   s!    KKK1*++KKKr   )r   r   z	
# random FTindent)linenorH   r   N)
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   outputcharnorI   random_markerr=   termsrR   mr;   r   r'   exc_msgrE   s                  @r   parsezIPDocTestParser.parse   sE    ""$$%%f--
>>YYKKKKT8J8JKKKLLF ##F++ 	(MMM  T(11&99:: 	oGG ,55f==>>EGE 	 	AMM&		!12333fll4;;;F ((D&?? -VWdG
 M!D ,,V44 8ggfdG-3-7AGGH<M<M8N8N-N.57 7 7 8 8 8
 fll4AEEGG<<<FUUWWFFfVWWo&&&r   Fc           	      .   t          |                    d                    |                    d                              d          }|                    d          }|                    d          }t          |          |                     |||           |r&|                     |dd         dz  |z   ||           d                    fd	|D                       }|r|                     |          }|                    d
          }	|	                    d          }
t          |
          dk    rt          j        d|
d                   r|
d= |                     |
dz  ||t          |          z              t          j	        dd|
d                   |
d<   d                    fd|
D                       }	| j
                            |	          }|r|                    d          }nd}|                     |||          }|||	|fS )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.
        rH   r;   r@   ps1ps2r4   N c                 0    g | ]}|z   d z   d         S )r4   Nr.   )rC   slrH   ps1_lens     r   rF   z2IPDocTestParser._parse_example.<locals>.<listcomp>   s-    IIIbBvg~a/001IIIr   r'   z *$zOut\[\d+\]: \s*?\n?rG   r   c                 $    g | ]}|d          S rB   r.   )rC   wlrH   s     r   rF   z2IPDocTestParser._parse_example.<locals>.<listcomp>   s!    ;;;""VWW+;;;r   msg)r8   rY   rM   _check_prompt_blank_check_prefixrL   r=   r,   matchsub_EXCEPTION_RE_find_options)r   r`   r   rI   r=   r   rd   re   r;   r'   
want_linesra   r   rH   ri   s                @@r   rW   zIPDocTestParser._parse_example   s!   " QWWX&&'' wwx((..t44 ggennggennc((  vtVGLLL 	Q|ABB/Vc1A4PPPIIIIILIIIJJ 	(ZZ''F
 wwvZZ%%
z??Q28FJrN#C#C2:s6z4!C$5$55	7 	7 	7 5bAGG
1yy;;;;
;;;<< $$T** 	ggennGGG $$VT6::wg--r   c                     ||z   }|dz   }t          |          D ]M\  }}	t          |	          |k    r5|	|         dk    r)t          d||z   dz   d|d|	||         d|	          NdS )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   rf   zline z of the docstring for z lacks blank after z: N)	enumerater8   
ValueError)
r   linesrH   r   rI   ri   	space_idxmin_lenilines
             r   rn   z#IPDocTestParser._check_prompt_blank  s     7N	A+ '' 	A 	AGAt4yyW$$iC)?)? j"((1***ddd"&vi'7"8"8"8$$"@ A A A	A 	Ar   N)r>   )F)r   r   r   r+   _PS1_PY_PS2_PY_PS1_IP_PS2_IP_RE_TPLr,   r-   	MULTILINEVERBOSErP   rS   rN   r=   rb   rW   rn   r.   r   r   r2   r2   R   s         GGGGG  RZGGGG+L!L!#
!:< <N  RZGGGG+L!L!#
!:< <N 2:233L  B B B BH?. ?. ?. ?.BA A A A 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.
    NTc                     t          ddd          5  t          t          |                               ||||          cd d d            S # 1 swxY w Y   d S )N8024)COLUMNSLINES)r   r   r   run)r   r   compileflagsoutclear_globsr   s        r   r   zIPDocTestRunner.run'  s    dT::;; 	Q 	Q..2243?KQ Q	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Qs   +AAA)NNT)r   r   r   r+   r   r   r   s   @r   r   r   #  sQ         Q Q Q Q Q Q Q Q Q Qr   r   )r+   r   loggingr,   testpathr   	getLoggerr   logr   r#   r!   rR   r0   DocTestParserr2   register_optionflagr   DocTestRunnerr   r.   r   r   <module>r      s]   *   				 ! ! ! ! ! ! g!!    G)       W2   8 ' & & & & & & &KA KA KA KA KAg+ KA KA KA\ #w"6**Q Q Q Q Qg+ Q Q Q Q Qr   