
    Mhd                         d Z ddlZddlZddlZddlZddlmZmZmZ ddl	m
Z
 d Z G d d          Z G d d	          Zd
 Z e            Z e            ZdS )a  Experimental code for cleaner support of IPython syntax with unittest.

In IPython up until 0.10, we've used very hacked up nose machinery for running
tests with IPython special syntax, and this has proved to be extremely slow.
This module provides decorators to try a different approach, stemming from a
conversation Brian and I (FP) had about this problem Sept/09.

The goal is to be able to easily write simple functions that can be seen by
unittest as tests, and ultimately for these to support doctests with full
IPython syntax.  Nose already offers this based on naming conventions and our
hackish plugins, but we are seeking to move away from nose dependencies if
possible.

This module follows a different approach, based on decorators.

- A decorator called @ipdoctest can mark any function as having a docstring
  that should be viewed as a doctest, but after syntax conversion.

Authors
-------

- Fernando Perez <Fernando.Perez@berkeley.edu>
    N)DocTestFinderDocTestRunnerTestResults)InteractiveShellc                     t           j        dk     r#d | j                                        D             S d | j                                        D             S )zmCount number of failures in a doctest runner.

    Code modeled after the summarize() method in doctest.
    )      c                 @    g | ]\  }}|d k    t          ||          S r   r   ).0fts      Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/IPython/testing/ipunittest.py
<listcomp>z"count_failures.<locals>.<listcomp>7   s+    PPPda!a%%Aq!!%%%    c                 B    g | ]\  }}}|d k    t          ||          S r   r   )r   failuretry_skips       r   r   z"count_failures.<locals>.<listcomp>9   s9     
 
 
#t{{ &&{{r   )sysversion_info_name2ftvalues_stats)runners    r   count_failuresr   1   sc    
 '!!PPfo.D.D.F.FPPPP
 
'-}';';'='=
 
 
 	
r   c                       e Zd ZdZd Zd ZdS )IPython2PythonConverterzConvert IPython 'syntax' to valid Python.

    Eventually this code may grow to be the full IPython syntax conversion
    implementation, but for now it only does prompt conversion.c                 <   t          j        d          | _        t          j        d          | _        t          j        d          | _        d| _        d| _        t          j        d| j        z            | _        t          j        d| j        z            | _        d S )NzIn\ \[\d+\]: z\ \ \ \.\.\.+: zOut\[\d+\]: \s*?\n?>>> ... z(\s*%s)(.*)$)	recompilerps1rps2routpyps1pyps2rpyps1rpyps2)selfs    r   __init__z IPython2PythonConverter.__init__F   sz    J/00	J122	J566	

j/DJ">??j/DJ">??r   c                    ddl m} d}d}d}|}| j                            ||          }| j                            ||          }| j                            ||          }t          j                    }g }|j        }	|	                                D ]}
| j
                            |
          }|:|                                \  }} |	||                    |d          z              X| j                            |
          }|:|                                \  }} |	||                    |d          z               |	|
            |	d           d	                    |          S )
z3Convert IPython prompts to python ones in a string.   )globalipappr!   r"    NFT
)r1   r0   r%   subr&   r'   r   instanceappend
splitlinesr*   matchgroups	prefilterr+   join)r,   dsr0   r(   r)   pyoutdnewipoutnewlinelinemps1prompttextmps2s                  r   __call__z IPython2PythonConverter.__call__O   sl   !!!!!!y}}UD))y}}UD))y}}UD))&(( *OO%% 	 	D;$$T**D#{{}}r||D%888999;$$T**D#{{}}r||D$777888GDMMMMyy~~r   N__name__
__module____qualname____doc__r-   rF    r   r   r   r   @   sA        C C
@ @ @" " " " "r   r   c                        e Zd ZdZddZd ZdS )Doc2UnitTesterzClass whose instances act as a decorator for docstring testing.

    In practice we're only likely to need one instance ever, made below (though
    no attempt is made at turning it into a singleton, there is no need for
    that).
    Fc                 @    || _         t          |d          | _        dS )zNew decorator.

        Parameters
        ----------

        verbose : boolean, optional (False)
          Passed to the doctest finder and runner to control verbosity.
        F)verboserecurseN)rP   r   finder)r,   rP   s     r   r-   zDoc2UnitTester.__init__}   s#     #GUCCCr   c                     | j         t          j                   _          G fddt          j                  }j        |_        |S )zUse as a decorator: doctest a function's docstring as a unittest.
        
        This version runs normal doctests, but the idea is to make it later run
        ipython syntax instead.Nc                       e Zd Z fdZdS )'Doc2UnitTester.__call__.<locals>.Testerc                 l   t          j                  }j                            j                  D ]}|                    |           t          |          }|rTt          |          dk    rd|z  }t          |          | 	                    dt          |d                   z             d S d S )N)rP   r/   z"Invalid number of test results: %szfailed doctests: %sr   )r   rP   rR   findrH   runr   len
ValueErrorfailstr)r,   r   the_testfailederrd2ufuncs        r   testz,Doc2UnitTester.__call__.<locals>.Tester.test   s    &s{;;; #
dm D D ) )HJJx(((('// F 6{{QBVK(oo-II3c&)nnDEEEEEF Fr   N)rH   rI   rJ   rb   )r`   ra   s   r   TesterrU      s;        F F F F F F F Fr   rc   )rK   ip2pyunittestTestCaserH   )r,   ra   rc   r`   s    ` @r   rF   zDoc2UnitTester.__call__   s}      <# ..DL	F 	F 	F 	F 	F 	F 	F 	FX& 	F 	F 	F$ -r   N)FrG   rL   r   r   rN   rN   v   sE         D D D D$ $ $ $ $r   rN   c                 F    | j         t          | j                   | _         | S )z-Change the function docstring via ip2py.
    )rK   rd   )ra   s    r   ipdocstringrh      s"     |T\**Kr   )rK   r#   r   re   builtinsdoctestr   r   r   !IPython.terminal.interactiveshellr   r   r   rN   rh   	ipdoctestrd   rL   r   r   <module>rm      s    J 
			 



   = = = = = = = = = = > > > > > >
 
 
1 1 1 1 1 1 1 1l8 8 8 8 8 8 8 8v   N	!!r   