a
    td`                     @   s   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m	Z	m
Z
 ddlmZ g dZedZddd	Zdd
dZdd Zdd Zdd Zdd Zdd ZdddZdS )z,
API for the command-line I{pyflakes} tool.
    )with_statementN)checker__version__)reporter)check	checkPathcheckRecursiveiterSourceCodemains&   ^#!.*\bpython([23](\.\d+)?|w)?[dmu]?\sc                 C   sn  |du rt  }ztj| |d}W  n t y   t d }|jd }|j|j	|j
  }}}tjr|du r|  }	t|	|kr|	|d  }tjdkrt|trz|d}W n ty   d}Y n0 |d8 }|du r||d n|||||| Y dS  ty   ||d Y dS 0 t| }
tj||
|d}|jjd	d
 d |jD ]}|| qRt|jS )a  
    Check the Python source given by C{codeString} for flakes.

    @param codeString: The Python source to check.
    @type codeString: C{str}

    @param filename: The name of the file the source came from, used to report
        errors.
    @type filename: C{str}

    @param reporter: A L{Reporter} instance, where errors and warnings will be
        reported.

    @return: The number of warnings emitted.
    @rtype: C{int}
    N)filename   r   )   asciizproblem decoding source)file_tokensr   c                 S   s   | j S N)lineno)m r   +lib/python3.9/site-packages/pyflakes/api.py<lambda>K       zcheck.<locals>.<lambda>)key)modReporter_makeDefaultReporterastparseSyntaxErrorsysexc_infoargsr   offsettextr   ZPYPY
splitlineslenversion_info
isinstancebytesdecodeUnicodeDecodeErrorunexpectedErrorZsyntaxError	ExceptionZmake_tokensZCheckermessagessortZflake)Z
codeStringr   r   Ztreevaluemsgr   r    r!   linesr   wZwarningr   r   r   r      s@    



r   c                 C   s   |du rt  }z6t| d}| }W d   n1 s:0    Y  W n2 tyx   t d }|| |jd  Y dS 0 t	|| |S )z
    Check the given path, printing out any warnings detected.

    @param reporter: A L{Reporter} instance, where errors and warnings will be
        reported.

    @return: the number of warnings printed
    Nrbr   )
r   r   openreadIOErrorr   r   r)   r   r   )r   r   fZcodestrr.   r   r   r   r   Q   s    	*r   c                 C   s   |  drdS |  drdS d}zNt| d0}||}|sNW d   W dS W d   n1 sb0    Y  W n ty   Y dS 0 t|S )z0Return True if filename points to a Python file.z.pyT~F   r1   N)endswithr2   r3   r4   PYTHON_SHEBANG_REGEXmatch)r   Z	max_bytesr5   r!   r   r   r   isPythonFilef   s    


4r;   c                 c   s^   | D ]T}t j|rRt |D ]0\}}}|D ] }t j||}t|r,|V  q,qq|V  qdS )z
    Iterate over all Python source files in C{paths}.

    @param paths: A list of paths.  Directories will be recursed into and
        any .py files found will be yielded.  Any non-directories will be
        yielded as-is.
    N)ospathisdirwalkjoinr;   )pathsr=   dirpathdirnames	filenamesr   	full_pathr   r   r   r	   |   s    r	   c                 C   s$   d}t | D ]}|t||7 }q|S )a;  
    Recursively check all source files in C{paths}.

    @param paths: A list of paths to Python source files and directories
        containing Python source files.
    @param reporter: A L{Reporter} where all of the warnings and errors
        will be reported to.
    @return: The number of warnings found.
    r   )r	   r   )rA   r   warningsZ
sourcePathr   r   r   r      s    
r   c                    s`   ddl }zt|| }W n ty*   Y dS 0  fdd}z| || W n tyZ   Y n0 dS )zHandles a signal with sys.exit.

    Some of these signals (SIGPIPE, for example) don't exist or are invalid on
    Windows. So, ignore errors that might arise.
    r   Nc                    s   t   d S r   )r   exit)Zsigr5   messager   r   handler   s    z_exitOnSignal.<locals>.handler)signalgetattrAttributeError
ValueError)ZsigNamerI   rK   Z	sigNumberrJ   r   rH   r   _exitOnSignal   s    rO   c                   C   s   dt t t f S )zQ
    Retrieve and format package version along with python version & OS used
    z%s Python %s on %s)r   platformZpython_versionsystemr   r   r   r   _get_version   s    rR   c                 C   s   ddl }tdd tdd |j| dd}|jd	d
dt d |jdddd |j|dj}t }|rtt	||}nt
tj d|}t|dkdS )z&Entry point for the script "pyflakes".r   NSIGINTz... stoppedSIGPIPEr   z$Check Python source files for errors)progdescriptionz-Vz	--versionversion)actionrW   r=   *z7Path(s) of Python file(s) to check. STDIN if not given.)nargshelp)r   z<stdin>)argparserO   ArgumentParseradd_argumentrR   
parse_argsr=   r   r   r   r   r   stdinr3   
SystemExit)rU   r   r\   parserr   rF   r   r   r   r
      s     

r
   )N)N)NN)__doc__Z
__future__r   r   r<   rP   rer   Zpyflakesr   r   r   r   __all__compiler9   r   r   r;   r	   r   rO   rR   r
   r   r   r   r   <module>   s$   

=
