
    ;e                         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             dS dS )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          ||           |                                 d d d            d S # 1 swxY w Y   d S )Nw)file)openprintflush)filenametestsfpnames       &/usr/lib/python3.11/test/bisect_cmd.pywrite_testsr      s    	h		  	! 	!D$R     



                 s   +A		AAc                 r    | sd S t          dt          |          d|            t          | |           | S )NzWriting z tests into )r   lenr   )r	   r
   s     r   write_outputr   &   sE     	EE



HH
=>>>%   O    c                 ,    d                     |           S )N )join)argss    r   format_shell_argsr   .   s    88D>>r   c                      t           j        g} |                     t          j                               |                     t          j                               | S N)sys
executableextend
subprocess_args_from_interpreter_flags"_optim_args_from_interpreter_flags)cmds    r   
python_cmdr!   2   sG    >
CJJz688999JJz<>>???Jr   c                 z   t                      }|                    g d           |                    | j                   t          j        |t          j        d          }|j        }|r8t          |          }t          d|d|           t          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/   9   s    
,,CJJ---...JJt~>#!+-13 3 3D H $$hh  	! 	! 	!K""$$ELr   c                    t          j                    }	 t          ||           t                      }|                    ddd|g           |                    | j                   t          dt          |          z             t          j	        |          }|j
        t          j                            |          rt          j        |           S S # t          j                            |          rt          j        |           w w xY w)Nr#   r$   z--matchfilez+ %s)tempfilemktempr   r!   r   r'   r   r   r   r(   r*   ospathexistsunlink)r   r
   
huntrleakstmpr    r-   s         r   	run_testsr9   J   s    
/

CCll

D&-5666

4>"""f(---...~c""7>># 	IcNNNN	27>># 	IcNNNN	s   B
C 6D
c                  D   t          j                    } |                     ddd           |                     ddd           |                     dd	t          d
d           |                     ddt          dd           |                                 \  }}||_        |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_argsrF   Z   s    $&&F
iI  J J J j6  8 8 8
m#q,  - - - lc.  / / /
 --//OD)DNKr   c            
      d   t                      } d| j        v s	d| j        v rt          d           t                       | j        r9t	          | j                  5 }d |D             }d d d            n# 1 swxY w Y   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        k    r4|| j	        k    r(t          |          }t          |dz  d
          }t          j        ||          }t          d|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        k    r|| j	        k    (n;# t$          $ r. t                       t          d           t                       Y nw xY w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        |
                     t/          j        d
           d S t          d|dt+          j        |
                     d S )Nz-wz
--verbose2z;WARNING: -w/--verbose2 option should not be used to bisect!c                 6    g | ]}|                                 S  )strip).0lines     r   
<listcomp>zmain.<locals>.<listcomp>v   s     111dTZZ\\111r   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 )rF   r'   r   inputr   r/   r   r   	max_testsmax_iterr   outputtime	monotonicmaxrandomsampler9   KeyboardInterruptmathceildatetime	timedeltar   r+   )r   r   r
   rS   
start_time	iterationntestsubtestsr.   r$   dts              r   mainrc   n   s   <<Dt~!?!?KLLLz !$* 	211b111E	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 4  	
)CJJ
6777	
!24>!B!B
BCCC	E ^^^T]]], - - - $+u--F	GGG!!JI%jj4>))i4=.H.HJJE
A&&E}UE22HEYYHs5zzz; < < <GGG x00HEuuuc%jjj9:::&(### UBCCC %dk599STTTGGGNI) %jj4>))i4=.H.H*    &'''
 
-#e**
$%%%  ftm	GGG 1&/000	4>##j0	1	1B
5zzT^##H.r::::< 	= 	= 	=H.r::::< 	= 	= 	= 	= 	=s%   A22A69A6)EI7 75J/.J/__main__r   )__doc__r@   r\   os.pathr3   rZ   rW   r   r   r1   rT   r   r   r   r!   r/   r9   rF   rc   __name__rI   r   r   <module>rh      s
   $          



            "      (A= A= A=H zDFFFFF r   