
    cW                       U d Z ddlm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mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  eee!e"e"e!ee!         f                  Z# ej$        e%          Z&ej'        hZ(de)d<   de)d<   ej*        d1d            Z+d2dZ,d3d#Z- G d$ d%          Z. G d& d'          Z/d4d+Z0d5d0Z1dS )6z$Checker Manager and Checker classes.    )annotationsN)Any)	Generator)List)Optional)Sequence)Tuple)defaults)
exceptions)	processor)utils)expand_paths)
parse_args)Checkers)LoadedPlugin)StyleGuideManagerr   _mp_pluginsargparse.Namespace_mp_optionspluginsoptionsreturnGenerator[None, None, None]c              #  0   K   | |ca a	 d V  b bd S # b bw xY wN)r   r   )r   r   s     .lib/python3.11/site-packages/flake8/checker.py_mp_preforkr   4   s<        'K%K$$$$s    argvSequence[str]Nonec                    t          j         t           j        t           j                   	 t          t          f d S # t
          $ r  t          |           \  }}|j        |caaY d S w xY wr   )signalSIGINTSIG_IGNr   r   	NameErrorr   checkers)r   r   r   s      r   _mp_initr'   A   sr     M&-000=[     = = =%d++#*#3W [[[[=s   ; &A%$A%filenamestr#tuple[str, Results, dict[str, int]]c                ^    t          | t          t                                                    S )Nr(   r   r   )FileCheckerr   r   
run_checksr(   s    r   _mp_runr0   N   s)    ;  jll    c                  b    e Zd ZdZdd
ZddZddZddZddZddZ	ddZ
ddZddZddZdS ) ManageraR  Manage the parallelism and checker instances for each plugin and file.

    This class will be responsible for the following:

    - Determining the parallelism of Flake8, e.g.:

      * Do we use :mod:`multiprocessing` or is it unavailable?

      * Do we automatically decide on the number of jobs to use or did the
        user provide that?

    - Falling back to a serial way of processing files if we run into an
      OSError related to :mod:`multiprocessing`

    - Organizing the results of each checker so we can group the output
      together and make our output deterministic.
    style_guider   r   r   r   r   r   r    c                    || _         |j        | _        || _        |                                 | _        ddddd| _        g | j        j        | j        j        R | _        || _        g | _	        dS )z Initialize our Manager instance.r   )fileslogical linesphysical linestokensN)
r4   r   r   
_job_countjobs
statisticsexcludeextend_excluder   results)selfr4   r   r   s       r   __init__zManager.__init__g   s{     '"*OO%%		
 
 M-L0KLL	BDr1   c                    | j         D ]0\  }}}t          j        D ]}| j        |xx         ||         z  cc<   1| j        dxx         t	          | j                  z  cc<   d S )Nr6   )r?   r
   STATISTIC_NAMESr<   len	filenames)r@   _r<   	statistics       r   _process_statisticszManager._process_statistics|   s     $ 	D 	DAq*%5 D D		***j.CC****D   C$7$77     r1   intc                    t          j        | j        j                  rt                              d           dS | j        j        }|j        r%	 t          j	                    S # t          $ r Y dS w xY w|j        S )Nz]The --jobs option is not compatible with supplying input using - . Ignoring --jobs arguments.r   )r   is_using_stdinr   rE   LOGwarningr;   is_automultiprocessing	cpu_countNotImplementedErrorn_jobs)r@   r;   s     r   r:   zManager._job_count   s      677 	KK=   1|  < 	&0222&   qq
 {s   A" "
A0/A0r(   r)   r?   Resultsc           
     j    | j         }d}|D ]&\  }}}}}	||                    ||||||	          z  }'|S )Nr   )coder(   line_numbercolumn_numbertextphysical_line)r4   handle_error)
r@   r(   r?   r4   reported_results_count
error_coderV   columnrX   rY   s
             r   _handle_resultszManager._handle_results   si    &!"FM 	 	BZfdM"k&>&>!'$+ '? ' ' "" &%r1   tuple[int, int]c                   dx}}| j                             t          j        d                     | j         D ]\  }}}|                    t          j        dd                     | j                            |          5  ||                     ||          z  }ddd           n# 1 swxY w Y   |t          |          z  }||fS )a  Report all of the errors found in the managed file checkers.

        This iterates over each of the checkers and reports the errors sorted
        by line number.

        :returns:
            A tuple of the total results found and the results reported.
        r   )key      N)r?   sortoperator
itemgetterr4   processing_filer^   rD   )r@   results_reportedresults_foundr(   r?   rF   s         r   reportzManager.report   s"    ,-,=h1!44555$(L 	* 	* HgqLLX0A66L777!11(;; L L D$8$87$K$KK L L L L L L L L L L L L L L LS\\)MM/00s   B((B,	/B,	c                D   t          | j        | j                  5  t          | j        | j                  }ddd           n# 1 swxY w Y   ||                                  dS d}	 t          |                    t          | j
                            | _        |                                 |                                 d}|s*|                                 |                                 dS dS # |s)|                                 |                                 w w xY w)zRun the checkers in parallel.NFT)r   r   r   _try_initialize_processpoolr;   r   
run_seriallistimap_unorderedr0   rE   r?   closejoin	terminate)r@   poolpool_closeds      r   run_parallelzManager.run_parallel   sZ   t|44 	E 	E.ty$)DDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E  	OOF	 3 3GT^ L LMMDLJJLLLIIKKKK    		 ;    		s   AA	A(AC2 2-Dc                8      fd j         D              _        dS )zRun the checkers in serial.c                l    g | ]0}t          |j        j                                                   1S )r,   )r-   r   r   r.   ).0r(   r@   s     r   
<listcomp>z&Manager.run_serial.<locals>.<listcomp>   sR     
 
 
  !   jll
 
 
r1   N)rE   r?   r@   s   `r   rm   zManager.run_serial   s5    
 
 
 
 !N
 
 
r1   c                   	 | j         dk    r.t          | j                  dk    r|                                  dS |                                  dS # t
          $ r/ t                              d           t          j	        d          w xY w)a#  Run all the checkers.

        This will intelligently decide whether to run the checks in parallel
        or whether to run them in serial.

        If running the checks in parallel causes a problem (e.g.,
        :issue:`117`) this also implements fallback to serial processing.
        rb   z"Flake8 was interrupted by the userzEarly quit while running checksN)
r;   rD   rE   ru   rm   KeyboardInterruptrL   rM   r   	EarlyQuitrz   s    r   runzManager.run   s    	Jy1} "T^!4!4q!8 "!!#####!!!!!  	J 	J 	JKK<===&'HIII	Js   7A A 9B
c                    t                               d           t          t          | j        j        | j        j        | j        j        | j                            | _        dS )zStart checking files.

        :param paths:
            Path names to check. This is passed directly to
            :meth:`~Manager.make_checkers`.
        zMaking checkers)pathsstdin_display_namefilename_patternsr=   N)	rL   infotupler   r   rE   r   r(   r=   rz   s    r   startzManager.start   s[     	"###l,#'<#B"&,"7	  
 
r1   c                .    |                                   dS )zStop checking files.N)rH   rz   s    r   stopzManager.stop  s      """""r1   N)r4   r   r   r   r   r   r   r    r   r    )r   rI   )r(   r)   r?   rS   r   rI   )r   r_   )__name__
__module____qualname____doc__rA   rH   r:   r^   rj   ru   rm   r~   r   r    r1   r   r3   r3   T   s         $E E E E*8 8 8 8   <& & & &1 1 1 1$   (	
 	
 	
 	
J J J J$
 
 
 
"# # # # # #r1   r3   c                      e Zd ZdZd,d
Zd-dZd.dZd/dZd0dZe	d1d            Z
d2dZd2dZd3d!Zd2d"Zd4d$Zd5d&Zd6d*Zd+S )7r-   z;Manage running checks for a file and aggregate the results.r(   r)   r   r   r   r   r   r    c               V   || _         || _        || _        g | _        dddd| _        |                                 | _        || _        d| _        | j        S| j        j        | _        | j        	                                 | _        t          | j        j                  | j        d<   dS dS )zInitialize our file checker.r   )r9   r7   r8   FNr8   )r   r(   r   r?   r<   _make_processorr   display_nameshould_processshould_ignore_filerD   lines)r@   r(   r   r   s       r   rA   zFileChecker.__init__
  s       "
 

 --//$#> 	J $ 7D&*n&G&G&I&I"ID03DN4H0I0IDO,---	J 	Jr1   c                    d| j          S )z)Provide helpful debugging representation.zFileChecker for r/   rz   s    r   __repr__zFileChecker.__repr__#  s    1$-111r1   processor.FileProcessor | Nonec           	         	 t          j        | j        | j                  S # t          $ r:}|                     dddt          |          j         d|            Y d }~d S d }~ww xY w)NE902r   : )r   FileProcessorr(   r   OSErrorrj   typer   )r@   es     r   r   zFileChecker._make_processor'  s{    
	*4=$,GGG 	 	 	 KK1a)9&@&@Q&@&@AAA44444	s   ! 
A%/A  A%r\   
str | NonerV   rI   r]   rX   c                    ||                     dd          \  }}t          | d          r"| j        | j                            |          }nd}| j                            |||||f           |S )z2Report an error by storing it in the results list.N rb   r   )splithasattrr   noqa_line_forr?   append)r@   r\   rV   r]   rX   lines         r   rj   zFileChecker.report4  s      	2#zz#q11J 4%% 	$. 	>//<<DDDZfdDIJJJr1   pluginr   	argumentsr   c                   | j         J | j                    	 | j                             |j        |          }n-# t          $ r }t          j        |j        |          d}~ww xY w	  |j        di ||S # t          $ rH}t                              d|j        d           t          j        | j        |j        |          d}~ww xY w)z!Run the check in a single plugin.N)plugin_name	exceptionz(Plugin %s raised an unexpected exceptionT)exc_info)r(   r   r   r   )r   r(   keyword_arguments_for
parametersAttributeErrorr    PluginRequestedUnknownParametersr   obj	ExceptionrL   criticalPluginExecutionFailed)r@   r   r   paramsaeall_excs         r   	run_checkzFileChecker.run_checkI  s   ~884=888	^99!9 FF  	 	 	="/2   		6:44	4V444 
	 
	 
	LL:#    
 2"/!   
	s.    9 
A#AA#'A6 6
C ACCr   r   r_   c                   t          | j                  dk    rF| j        d         r9t          | j        d                   dk    r| j        d         }|dd         \  }}njt          | t          j                  rIt          | j                  dk    r1t          | j        d                   dk    rd}| j        d         \  }}nd}d\  }}|dk    r|rt          | t
                    rt          |          dk    rqd}d}|d         }|Y|                    d                              d          }t          |          dz
  }|d         }t          |          }	||	k    r|	}||z  }||z  }||fS )	Nrb   rc      r   )rb   r   r      
)rD   args
isinstancetokenize
TokenErrorSyntaxErrorrstripr   )
r   tokenrowr]   column_offset
row_offsetrY   r   logical_linelogical_line_lengths
             r   _extract_syntax_informationz'FileChecker._extract_syntax_informationb  s    	!#	!q!	! IN1%&&*	!
 N1%E!*KCy("566		!IN##q(		! IN1%&&!+		!
 E#.+KCE KC QJ$	$$	$ 9k22$	$ E

a	$	$ MJ "!HM
  
1
 &,,T2288>> ZZ!^
$Qx&),&7&7#// 10F:Cm#FF{r1   c                F   | j         J | j                    | j                                         }| j        j        D ]d}|                     ||          }	 |                                }n# t          $ r |}Y nw xY w|D ] \  }}}}|                     d|||           !edS )z1Run all checks expecting an abstract syntax tree.N)treer\   rV   r]   rX   )	r   r(   	build_astr   r   r   r~   r   rj   )	r@   astr   checkerrunnerrV   offsetrX   rF   s	            r   run_ast_checkszFileChecker.run_ast_checks  s    ~884=888n&&((l' 	 	FnnV#n66G! ! ! ! ! !28  .fdA# +!	     	 	s   A++A:9A:c                \   | j         J | j                                         \  }}}|sdS | j                             |           t                              d|                                           | j        j        D ]}| j                             |           | 	                    ||          pd}|D ]\\  }}t          ||          \  }}	||	cxk    rdk    rn nt                              d|           |                     d||	|           ]| j                                          dS )z(Run all checks expecting a logical line.NzLogical line: "%s")r   r   r   z#position of error out of bounds: %sr   )r   build_logical_lineupdate_staterL   debugr   r   r   update_checker_state_forr   find_offsetrM   rj   next_logical_line)
r@   commentsr   mappingr   r?   r   rX   rV   r   s
             r   run_logical_checkszFileChecker.run_logical_checks  sx   ~)))*..*K*K*M*M', 	F##G,,,		&(;(;(=(=>>>l/ 	 	FN33F;;;nnV,nGGM2G ' 	 	-8-I-I*]- O O O O1 O O O O OKK EvNNN# +(	     		 	((*****r1   rY   c                l   | j         J | j        j        D ]}| j                             |           |                     ||          }|hd}	 |d         }n# t
          t          f$ r Y nw xY wt          |t                    r|f}|D ]*}|\  }}| 	                    d| j         j
        ||           +dS )zoRun all checks for a given physical line.

        A single physical check may return multiple errors.
        N)rY   r   r   )r   r   rY   r   r   
IndexError	TypeErrorr   rI   rj   rV   )r@   rY   r   resultr   result_singlerX   s          r   run_physical_checkszFileChecker.run_physical_checks  s   
 ~)))l0 	 	FN33F;;;^^F-^HHF  $$*1IMM"I.   D mS11 '$YF%+  M*7'M4KK#'$(N$>,!	      %	 	s   AA+*A+c                   | j         J d}| j        }| j         }d}|                                D ]}|dxx         dz  cc<   |                     ||           |dd         \  }}|t          j        k    rt          j        ||          }n/|dk    r)t          j        |          r|                     |           |d         }|j	        r6| 
                    |j        d                    |                                  dS dS )	zProcess tokens and trigger checks.

        Instead of using this directly, you should use
        :meth:`flake8.checker.FileChecker.run_checks`.
        Nr    r9   rb   rc   r   )r   r<   generate_tokenscheck_physical_eolr   OPcount_parenthesestoken_is_newlinehandle_newliner9   r   r   r   )r@   parensr<   file_processorprev_physicalr   
token_typerX   s           r   process_tokenszFileChecker.process_tokens  s3    ~)))_
#3355 		% 		%Ex   A%   ##E=999$QqSzJX[( 4"4VTBB1 4-e44 4''
333!!HMM  	&$$^%9"%=>>>##%%%%%	& 	&r1   r*   c           
     H   | j         | j        s| j        | j        | j        fS 	 |                                  |                                  n# t          t          j	        f$ r}t          |t          j	                  rdnd}|                     |          \  }}|                     |||t          |          j         d|j        d                     | j        | j        | j        fcY d}~S d}~ww xY w| j         j        d         }|| j        d<   | j        | j        | j        fS )zRun checks against the file.Nr   E999r   r   r7   )r   r   r   r?   r<   r   r   r   r   r   r   r   rj   r   r   r   )r@   r   rU   r   r]   logical_liness         r   r.   zFileChecker.run_checks  s?   > 	D)< 	D$dlDOCC	D!!!!!!!X01 	D 	D 	D'8+>??K66VD::1==KCKKc6d1gg.>+M+M!&)+M+MNNN$dlDOCCCCCCC		D 1/B+8( $,??s   (A C1#BC,&C1,C1r   c                `   | j         J |t          j        k    r/|                                  | j                                          dS t          | j         j                  dk    r4| j                                          | j                                          dS |                                  dS )z3Handle the logic when encountering a newline token.Nrb   )	r   r   NEWLINEr   reset_blank_beforerD   r9   visited_new_blank_linedelete_first_token)r@   r   s     r   r   zFileChecker.handle_newline  s    ~))))) 	&##%%%N--/////&''1, 	&N11333N--/////##%%%%%r1   r   tokenize.TokenInfor   c                   | j         J t          j        |          r@|d         dk    r|                     |           dS |                     |d                    dS t          j        |          rw|d         d         }| j                             |          5  | j                             |          D ]}|                     |           	 ddd           dS # 1 swxY w Y   dS dS )z@Run physical checks if and only if it is at the end of the line.Nr   r   rc   r   )rV   )r   is_eol_tokenr   is_multiline_stringinside_multiline
split_line)r@   r   r   line_nor   s        r   r   zFileChecker.check_physical_eol%  sS    ~)))!%(( 	3 Qx2~ 3((77777((q22222*511 	3 AhqkG00W0EE 3 3 N55e<< 3 3D,,T222233 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3	3 	3s   3CC #C N)r(   r)   r   r   r   r   r   r    )r   r)   )r   r   )
r\   r   rV   rI   r]   rI   rX   r)   r   r)   )r   r   r   r   r   r   )r   r   r   r_   r   )rY   r)   r   r    )r   r*   )r   rI   r   r    )r   r   r   r)   r   r    )r   r   r   r   rA   r   r   rj   r   staticmethodr   r   r   r   r   r.   r   r   r   r1   r   r-   r-     s3       EEJ J J J22 2 2 2      *   2 8 8 8 \8t   *+ + + +4   >& & & &6@ @ @ @$& & & &3 3 3 3 3 3r1   r-   	job_countrI    multiprocessing.pool.Pool | Nonec                    	 t          j        | t          |f          S # t          $ r}|j        t
          vr Y d}~nd}~wt          $ r Y nw xY wdS )z@Return a new process pool instance if we are able to create one.)initargsN)rO   Poolr'   r   errnoSERIAL_RETRY_ERRNOSImportError)r  r   errs      r   rl   rl   E  s    
#Ix4'JJJJ   9// 		 	 	 	 	    4s    
A=AAr   r   processor._LogicalMappingr_   c                    t          | t                    r| S |D ]}|d         }| |k    r
|d         } nd}dx} }|d         |d         | z   |z
  fS )z*Find the offset tuple for a single offset.r   rb   )r   r   )r   r   )r   r   r   token_offsetpositions        r   r   r   U  s     &%    " "Qx\! 	QxHE	  !!QK!v-<==r1   )r   r   r   r   r   r   )r   r   r   r    )r(   r)   r   r*   )r  rI   r   r   r   r  )r   rI   r   r
  r   r_   )2r   
__future__r   argparse
contextlibr  loggingmultiprocessing.poolrO   re   r"   r   typingr   r   r   r   r   r	   flake8r
   r   r   r   flake8.discover_filesr   flake8.options.parse_argsr   flake8.plugins.finderr   r   flake8.style_guider   r)   rI   rS   	getLoggerr   rL   ENOSPCr  __annotations__contextmanagerr   r'   r0   r3   r-   rl   r   r   r1   r   <module>r     s   * * * " " " " " "                                                                           . . . . . . 0 0 0 0 0 0 * * * * * * . . . . . . 0 0 0 0 0 0
uS#sC#67
8g!! 
L          	% 	% 	% 	%
= 
= 
= 
=   p# p# p# p# p# p# p# p#f{3 {3 {3 {3 {3 {3 {3 {3|	    > > > > > >r1   