
    daf^                         d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
d Zd Zd Zd Zd ZddZd	 Zd
 Zedk(  r e        yy)a  
Command line tool to bisect failing CPython tests.

Find the test_os test method which alters the environment:

    ./python -m test.bisect_cmd --fail-env-changed test_os

Find a reference leak in "test_os", write the list of failing tests into the
"bisect" file:

    ./python -m test.bisect_cmd -o bisect -R 3:3 test_os

Load an existing list of tests from a file using -i option:

    ./python -m test --list-cases -m FileTests test_os > tests
    ./python -m test.bisect_cmd -i tests test_os
    Nc                     t        | d      5 }|D ]  }t        ||        |j                          d d d        y # 1 sw Y   y xY w)Nw)file)openprintflush)filenametestsfpnames       */root/Python-3.12.4/Lib/test/bisect_cmd.pywrite_testsr      sE    	h	  	!D$R 	!

  s	   %;Ac                 X    | sy t        dt        |      d|        t        | |       | S )NzWriting z tests into )r   lenr   )r	   r
   s     r   write_outputr   &   s(    	E
H
=>% O    c                 $    dj                  |       S )N )join)argss    r   format_shell_argsr   .   s    88D>r   c                      t         j                  g} | j                  t        j                                | j                  t        j
                                | j                  d       | S )N)z-Xfaulthandler)sys
executableextend
subprocess_args_from_interpreter_flags"_optim_args_from_interpreter_flags)cmds    r   
python_cmdr!   2   sJ    >>
CJJz6689JJz<<>?JJ%&Jr   c                 t   t               }|j                  g d       |j                  | j                         t        j                  |t        j
                  d      }|j                  }|r1t        |      }t        d|d|       t        j                  |       |j                  j                         }|S )N)-mtestz--list-casesT)stdoutuniversal_newlineszFailed to list tests: z failed with exit code )r!   r   	test_argsr   runPIPE
returncoder   r   r   exitr%   
splitlines)r   r    procexitcoder
   s        r   
list_casesr/   :   s    
,CJJ-.JJt~~>>#!+-13D H$h  	!KK""$ELr   c                    t        j                         }	 t        ||       t               }|j	                  dddd|g       |j	                  | j
                         t        dt        |      z         t        j                  j                          t        j                  j                          t        j                  |      }|j                  t        j                   j#                  |      rt        j$                  |       S S # t        j                   j#                  |      rt        j$                  |       w w xY w)Nz-ur#   r$   z--matchfilez+ %s)tempfilemktempr   r!   r   r'   r   r   r   r%   r   stderrr   r(   r*   ospathexistsunlink)r   r
   
huntrleakstmpr    r-   s         r   	run_testsr:   K   s    
//
CCl

D$s;<

4>>"f(--.



~~c"77>>#IIcN 277>>#IIcN s   B:D 7D=c                     t        j                         } | j                  ddd       | j                  ddd       | j                  dd	t        d
d       | j                  ddt        dd       | j	                         \  }}||_        |S )Nz-iz--inputzUTest names produced by --list-tests written into a file. If not set, run --list-tests)helpz-oz--outputzResult of the bisectionz-nz--max-tests   z:Maximum number of tests to stop the bisection (default: 1))typedefaultr<   z-Nz
--max-iterd   z5Maximum number of bisection iterations (default: 100))argparseArgumentParseradd_argumentintparse_known_argsr'   )parserr   r'   s      r   
parse_argsrG   _   s    $$&F
iI  J j6  8
m#q,  - lc.  /
 --/OD)DNKr   c                     t               } dD ]*  }|| j                  v st        d| d       t                , | j                  r<t	        | j                        5 }|D cg c]  }|j                          }}d d d        nt        |       }t        dt              z         t        dt        | j                        z         t        d| j                  d| j                  d       t        | j                  |      }t                t        j                         }d	}	 t        |      | j                  kD  r|| j                  k  rt        |      }t        |d
z  d	      }t!        j"                  ||      }	t        d| d| j                   dt        |	       dt        |              t                t%        | |	      }
t        d|dt        |             t        d|
       |
r$t        d       |	}t        | j                  |      }nt        d       t                |d	z  }t        |      | j                  kD  r|| j                  k  rt        dt        |      z         |D ]  }t        d|z          t                |rt        d|z         t)        j*                  t        j                         |z
        }t        |      | j                  k  r&t        d|dt-        j.                  |             y t        d|dt-        j.                  |             t1        j2                  d	       y c c}w # 1 sw Y   xY w# t&        $ r# t                t        d       t                Y &w xY w)N)z-wz--rerunz
--verbose2z	WARNING: z% option should not be used to bisect!zStart bisection with %s testszTest arguments: %sz!Bisection will stop when getting z1 or less tests (-n/--max-tests option), or after z" iterations (-N/--max-iter option)r=      z[+] Iteration /z: run z tests/zran r+   z*Tests failed: continuing with this subtestz;Tests succeeded: skipping this subtest, trying a new subsetzBisection interrupted!zTests (%s):z* %szOutput written into %szBisection completed in z iterations and )secondszBisection failed after )rG   r'   r   inputr   stripr/   r   r   	max_testsmax_iterr   outputtime	monotonicmaxrandomsampler:   KeyboardInterruptmathceildatetime	timedeltar   r+   )r   optr   liner
   rP   
start_time	iterationntestsubtestsr.   r$   dts                r   mainrb   s   s   <D. $.. IcU"GHIG
 zz$** 	2.01dTZZ\1E1	2 	2 4 	
)CJ
67	
!24>>!B
BC	 ^^T]], - $++u-F	G!JI%j4>>)i4==.HJE
A&E}}UE2HN9+Qt}}o >Xws5zl< =G x0Huc%j9:&(#BC %dkk59STGNI) %j4>>)i4==.H4 
-#e*
$% ftm	G&/0	4>>#j0	1B
5zT^^#H..r:< 	= 	H..r:< 	=s 2	2 	2L  &'s1   M L<7M"D/M <MM(M:9M:__main__)N)__doc__rA   rY   os.pathr4   rW   rT   r   r   r1   rQ   r   r   r   r!   r/   r:   rG   rb   __name__ r   r   <module>rh      se   $       
  "((BJ zF r   