wL iddZddlZddlZddlZddlZddlmZmZmZddl m Z dZ GddZ Gdd Z d Ze Ze Zy) aExperimental 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 N) DocTestFinder DocTestRunner TestResults)InteractiveShellc :tjdkr=|jjDcgc]\}}|dkDs t ||c}}S|j jDcgc]\}}}|dkDr t ||c}}}Scc}}wcc}}}w)zmCount number of failures in a doctest runner. Code modeled after the summarize() method in doctest. ) r)sys version_info_name2ftvaluesr_stats)runnerftfailuretry_skips `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/IPython/testing/ipunittest.pycount_failuresr1s  '!.4oo.D.D.FPda!a% Aq!PP(.}}';';'=  #t{  &  Q s BB/BceZdZdZdZdZy)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.cXtjd|_tjd|_tjd|_d|_d|_tjd|j z|_tjd|j z|_y)Nz In\ \[\d+\]: z\ \ \ \.\.\.+: zOut\[\d+\]: \s*?\n?>>> ... z (\s*%s)(.*)$) recompilerps1rps2routpyps1pyps2rpyps1rpyps2)selfs r__init__z IPython2PythonConverter.__init__FsuJJ/0 JJ12 JJ56   jj/DJJ">? jj/DJJ">? cddlm}d}d}d}|}|jj||}|jj||}|j j||}t j}g}|j} |jD]} |jj| } | /| j\} } | | |j| dzO|jj| }|/|j\} } | | |j| dz| | | ddj|S) z3Convert IPython prompts to python ones in a string.) globalipapprrFT )r+r*rsubrr rinstanceappend splitlinesr#matchgroups prefilterr$join)r%dsr*r!r"pyoutdnewipoutnewlinelinemps1prompttextmps2s r__call__z IPython2PythonConverter.__call__Os/!yy}}UD)yy}}UD)yy}}UD)  & & (**OO% D;;$$T*D#{{} r||D%889;;$$T*D#{{} r||D$778 DM   yy~r'N__name__ __module__ __qualname____doc__r&r@r'rrr@sC @"r'rceZdZdZddZdZy)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). c6||_t|d|_y)zNew decorator. Parameters ---------- verbose : boolean, optional (False) Passed to the doctest finder and runner to control verbosity. F)verboserecurseN)rJrfinder)r%rJs rr&zDoc2UnitTester.__init__}s #GUC r'c|jtj_Gfddtj}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.ceZdZfdZy)'Doc2UnitTester.__call__..TestercBtj}jjjD]}|j |t |}|r?t|dkDrd|z}t||jdt|dzyy)N)rJr)z"Invalid number of test results: %szfailed doctests: %sr) rrJrLfindrBrunrlen ValueErrorfailstr)r%rthe_testfailederrd2ufuncs rtestz,Doc2UnitTester.__call__..Tester.tests&s{{; # dmm D)HJJx()'/6{QBVK(o-II3c&)nDEr'N)rBrCrDr\)rZr[srTesterrOs  Fr'r])rEip2pyunittestTestCaserB)r%r[r]rZs ` @rr@zDoc2UnitTester.__call__sL << # .DL FX&& F$-- r'N)FrArFr'rrHrHvs D$r'rHcR|jt|j|_|S)z-Change the function docstring via ip2py. )rEr^)r[s r ipdocstringrbs# ||T\\* Kr')rErr r_builtinsdoctestrrr!IPython.terminal.interactiveshellrrrrHrb ipdoctestr^rFr'rrgsTJ ==>  11l88v   !r'