a 4dg@sdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddZ ddZ ddZ d d Zd d Zdd dZddZddZedkredS)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 NcCsLt|d.}|D]}t||dq|Wdn1s>0YdS)Nw)file)openprintflush)filenametestsfpnamer +/usr/local/lib/python3.9/test/bisect_cmd.py write_testss r cCs*|sdStdt||ft|||S)NzWriting %s tests into %s)rlenr )rrr r r write_output&s  rcCs d|S)N )join)argsr r r format_shell_args.srcCs(tjg}|t|t|S)N)sys executableextend subprocess_args_from_interpreter_flags"_optim_args_from_interpreter_flags)cmdr r r python_cmd2srcCslt}|gd||jtj|tjdd}|j}|r^t|}td||ft ||j }|S)N)-mtestz --list-casesT)stdoutuniversal_newlinesz1Failed to list tests: %s failed with exit code %s) rr test_argsrrunPIPE returncoderrrexitr splitlines)rrprocexitcoderr r r list_cases9s    r(c Cst}zht||t}|ddd|g||jtdt|t |}|j Wt j |rnt |Snt j |rt |0dS)Nrrz --matchfilez+ %s)tempfilemktempr rrr rrrr!r#ospathexistsunlink)rrZ huntrleakstmprr&r r r run_testsJs       r0cCsft}|jdddd|jdddd|jdd td d d |jd dtddd |\}}||_|S)Nz-iz--inputzUTest names produced by --list-tests written into a file. If not set, run --list-tests)helpz-oz--outputzResult of the bisectionz-nz --max-testsz:Maximum number of tests to stop the bisection (default: 1))typedefaultr1z-Nz --max-iterdz5Maximum number of bisection iterations (default: 100))argparseArgumentParser add_argumentintparse_known_argsr )parserrr r r r parse_argsZs    r<c CsRt}d|jvsd|jvr(tdt|jrht|j}dd|D}Wdqp1s\0Ynt|}tdt|tdt|jtd|j|j ft |j |}tt }d }zt||jkr||j krt|}t|d d }t||}td |t|t|ftt||}td |t|ftd ||rltd|}t |j |}ntdt|d 7}qWn(tyttdtYn0tdt||D]} td| qt|rtd|tt |} t||jkr6td|tj| dftd ntd|tj| dfdS)Nz-wz --verbose2z;WARNING: -w/--verbose2 option should not be used to bisect!cSsg|] }|qSr )strip).0liner r r vzmain..zStart bisection with %s testszTest arguments: %szxBisection will stop when getting %s or less tests (-n/--max-tests option), or after %s iterations (-N/--max-iter option)r2z![+] Iteration %s: run %s tests/%szran %s tests/%sr$z*Tests failed: continuing with this subtestz;Tests succeeded: skipping this subtest, trying a new subsetzBisection interrupted!z Tests (%s):z* %szOutput written into %sz+Bisection completed in %s iterations and %s)Zsecondsz+Bisection failed after %s iterations and %s)r<r rinputrr(rrZ max_testsZmax_iterroutputtime monotonicmaxrandomsampler0KeyboardInterruptmathceildatetimeZ timedeltarr$) rr rrDZ start_timeZ iterationZntestZsubtestsr'rZdtr r r mainnsn .        rN__main__)N)__doc__r6rMZos.pathr+rKrHrrr)rEr rrrr(r0r<rN__name__r r r r s& D