Format: application/x-python-code
⬇ Download File (8.4 KB)
   1  o
   2  
   3  ��hp.�@s�dZddlZddlZddlZddlmZe�e�ZGdd�dej	�Z	Gdd�dej
   4  �ZGdd	�d	ej�Z
   5  Gd
   6  d�dej�Ze�d�ZGd
   7  d�deje�ZdS)a�Nose Plugin that supports IPython doctests.
   8  
   9  Limitations:
  10  
  11  - When generating examples for use as doctests, make sure that you have
  12    pretty-printing OFF.  This can be done either by setting the
  13    ``PlainTextFormatter.pprint`` option in your configuration file to  False, or
  14    by interactively disabling it with  %Pprint.  This is required so that IPython
  15    output matches that of normal Python, which is used by doctest for internal
  16    execution.
  17  
  18  - Do not rely on specific prompt numbers for results (such as using
  19    '_34==True', for example).  For IPython tests run via an external process the
  20    prompt numbers may be different, and IPython tests run as normal python code
  21    won't even have these special _NN variables set at all.
  22  �N)�modified_envcseZdZ�fdd�Z�ZS)�
  23  DocTestFindercsHt��|||||�}tt|dd��r"|dur"|jD]}d|jtj<q|S)NZ__skip_doctest__FT)�super�	_get_test�bool�getattrZexamples�options�doctest�SKIP)�self�obj�name�moduleZglobs�source_lines�testZexample��	__class__��Vc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/testing/plugin/ipdoctest.pyr)s
  24  
  25  zDocTestFinder._get_test)�__name__�
  26  __module__�__qualname__r�
  27  __classcell__rrrrr(src@s"eZdZdZe�d�Zdd�ZdS)�IPDoctestOutputCheckerz�Second-chance checker with support for random tests.
  28  
  29      If the default comparison doesn't pass, this checker looks in the expected
  30      output string for flags that tell us to ignore the output.
  31      z
  32  #\s*random\s+cCs*tj�||||�}|s|j�|�rdS|S)z�Check output, accepting special markers embedded in the output.
  33  
  34          If the output didn't pass the default validation but the special string
  35          '#random' is included, we accept it.T)r	�
  36  OutputChecker�check_output�	random_re�search)r�want�gotZoptionflags�retrrrr<s�z#IPDoctestOutputChecker.check_outputN)rrr�__doc__�re�compilerrrrrrr3s
  37  rc@seZdZdS)�	IPExampleN)rrrrrrrr$Osr$c@s�eZdZdZdZdZdZdZdZe	�
  38  eeeeefe	je	jB�Z
  39  e	�
  40  eeeeefe	je	jB�Ze	�
  41  d�Zdd	�Zddd�Zddd�Zdd�ZdS)�IPDocTestParserz�
  42      A class used to parse strings containing doctest examples.
  43  
  44      Note: This is a version modified to properly recognize IPython input and
  45      convert any IPython examples into valid Python ones.
  46      z>>>z\.\.\.zIn\ \[\d+\]:z\ \ \ \.\.\.+:a]
  47          # Source consists of a PS1 line followed by zero or more PS2 lines.
  48          (?P<source>
  49              (?:^(?P<indent> [ ]*) (?P<ps1> %s) .*)    # PS1 line
  50              (?:\n           [ ]*  (?P<ps2> %s) .*)*)  # PS2 lines
  51          \n? # a newline
  52          # Want consists of any non-blank lines that do not start with PS1.
  53          (?P<want> (?:(?![ ]*$)    # Not a blank line
  54                       (?![ ]*%s)   # Not a line starting with PS1
  55                       (?![ ]*%s)   # Not a line starting with PS2
  56                       .*$\n?       # But any other line
  57                    )*)
  58                    z#\s*all-random\s+cCs*tj�|�}t|���dkrt�|�S|S)z/Convert input IPython source into valid Python.�)�_ipZinput_transformer_managerZtransform_cell�len�
  59  splitlinesZ	prefilter)r�source�blockrrr�ip2pys
  60  zIPDocTestParser.ip2py�<string>c
  61  sR|��}|�|���dkrd��fdd�|�d�D��}g}d\}}|j�|�r+d}nd}d}t|j�|��}|r=t	j
  62  }	nt|j�|��}t}	d	}|D]R}
  63  |�
  64  |||
  65  ����||�d||
  66  ���7}|�|
  67  |||�\}}}
  68  }|
  69  |7}
  70  |�|�s�|�
  71  |	||
  72  ||�t|
  73  �d
  74  ��|d��||�d|
  75  ��|
  76  ���7}|
  77  ��}qK|�
  78  ||d��|S)
  79  a=
  80          Divide the given string into examples and intervening text,
  81          and return them as a list of alternating Examples and strings.
  82          Line numbers for the Examples are 0-based.  The optional
  83          argument `name` is a name identifying this string, and is only
  84          used for error messages.
  85          r�
  86  c�g|]}|�d��qS�Nr)�.0�l�Z
  87  min_indentrr�
  88  <listcomp>��z)IPDocTestParser.parse.<locals>.<listcomp>)rrz	
  89  # random�FT�indent)�linenor7rN)�
  90  expandtabsZ_min_indent�join�split�_RANDOM_TESTr�list�_EXAMPLE_RE_PY�finditerr	�Example�_EXAMPLE_RE_IPr$�append�start�count�_parse_exampleZ_IS_BLANK_OR_COMMENTr(�group�end)r�stringr
  91  �outputZcharnor8Z
  92  random_markerr,Ztermsr@�mr*rr�exc_msgrr3r�parse�s@
  93  
  94  �
  95  �
  96  zIPDocTestParser.parseFc
  97  sRt|�d���|�d��d�}|�d�}|�d�}t|��|�|�||��|r8|�|dd�d�|||�d���fd	d
  98  �|D��}|rL|�|�}|�d�}	|	�d�}
  99  t|
 100  �dkrgt�d|
 101  d
 102  �rg|
 103  d
 104  =|�|
 105  d�||t|��t�	dd|
 106  d�|
 107  d<d��fdd
 108  �|
 109  D��}	|j
 110  �|	�}|r�|�d�}nd}|�|||�}|||	|fS)a7
 111          Given a regular expression match from `_EXAMPLE_RE` (`m`),
 112          return a pair `(source, want)`, where `source` is the matched
 113          example's source code (with prompts and indentation stripped);
 114          and `want` is the example's expected output (with indentation
 115          stripped).
 116  
 117          `name` is the string's name, and `lineno` is the line number
 118          where the example starts; both are used for error messages.
 119  
 120          Optional:
 121          `ip2py`: if true, filter the input via IPython to convert the syntax
 122          into valid python.
 123          r7r*r.�ps1�ps2r&N� cs g|]}|��dd��qS)r&Nr)r1�sl�r7�ps1_lenrrr4�s z2IPDocTestParser._parse_example.<locals>.<listcomp>rz *$�����zOut\[\d+\]: \s*?\n?r6rcr/r0r)r1Zwl)r7rrr4�r5�msg)r(rFr;�_check_prompt_blankZ
 124  _check_prefixr:r,r"�match�subZ
 125  _EXCEPTION_REZ
 126  _find_options)
 127  rrJr
 128  r8r,rrMrNr*rZ
 129  want_linesrKrrrQrrE�s4
 130  
 131   
 132  
 133  
 134  
 135  �zIPDocTestParser._parse_examplec
 136  Csb||}|d}t|�D]"\}}	t|	�|kr.|	|dkr.td||d||	||�|	f��qdS)a�
 137          Given the lines of a source string (including prompts and
 138          leading indentation), check to make sure that every prompt is
 139          followed by a space character.  If any line is not followed by
 140          a space character, then raise ValueError.
 141  
 142          Note: IPython-modified version which takes the input prompt length as a
 143          parameter, so that prompts of variable length can be dealt with.
 144          r&rOz8line %r of the docstring for %s lacks blank after %s: %rN)�	enumerater(�
 145  ValueError)
 146  r�linesr7r
 147  r8rRZ	space_idxZmin_len�i�linerrrrUs
 148  ����z#IPDocTestParser._check_prompt_blankN)r-)F)rrrr!Z_PS1_PYZ_PS2_PYZ_PS1_IPZ_PS2_IPZ_RE_TPLr"r#�	MULTILINE�VERBOSEr>rAr<r,rLrErUrrrrr%Rs$
 149  
 150  �
 151  �
 152  
 153  
 154  DAr%r
 155  cs"eZdZdZd�fdd�	Z�ZS)�IPDocTestRunnerzKTest runner that synchronizes the IPython namespace with test globals.
 156      NTcsHtddd���tt|��||||�Wd�S1swYdS)N�80Z24)�COLUMNS�LINES)rrr_�run)rrZcompileflags�outZclear_globsrrrrc's
 157  �$�zIPDocTestRunner.run)NNT)rrrr!rcrrrrrr_#sr_)r!r	�loggingr"Ztestpathr�	getLoggerr�logrrrr@r$Z
 158  DocTestParserr%Zregister_optionflagr
 159  Z
 160  DocTestRunner�objectr_rrrr�<module>s
 161  
 162  O