o
    b{                     @   s  d 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Zddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZmZ ddlmZ dd	lmZ d
Zejdd dkZejdd dkZi ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1iZejejejejejejej fZ!ej"d2 Z#ej"d3 Z$ej"d4 Z%d5d6 Z&d7d8 Z'G d9d: d:eZ(G d;d< d<eZ)d=d> Z*d?d@ Z+dS )Az*Checker for string formatting operations.
    N)Counter)CallSite)Const)ITokenCheckerIAstroidCheckerIRawChecker)BaseCheckerBaseTokenChecker)utils)check_messages)z__builtin__.unicodez__builtin__.strzbuiltins.str   )   r   )r      ZE1300)z3Unsupported format character %r (%#02x) at index %dbad-format-characterzEUsed when an unsupported format character is used in a format string.ZE1301)z4Format string ends in middle of conversion specifiertruncated-format-stringzNUsed when a format string terminates before the end of a conversion specifier.ZE1302)z?Mixing named and unnamed conversion specifiers in format stringmixed-format-stringzUsed when a format string contains both named (e.g. '%(foo)d') and unnamed (e.g. '%d') conversion specifiers.  This is also used when a named conversion specifier contains * for the minimum field width and/or precision.ZE1303)z*Expected mapping for format string, not %sformat-needs-mappingzoUsed when a format string that uses named conversion specifiers is used with an argument that is not a mapping.ZW1300)z7Format string dictionary key should be a string, not %sbad-format-string-keyzyUsed when a format string that uses named conversion specifiers is used with a dictionary whose keys are not all strings.ZW1301)z)Unused key %r in format string dictionaryunused-format-string-keyzUsed when a format string that uses named conversion specifiers is used with a dictionary that contains keys not required by the format string.ZE1304)z*Missing key %r in format string dictionarymissing-format-string-keyzUsed when a format string that uses named conversion specifiers is used with a dictionary that doesn't contain all the keys required by the format string.ZE1305)z$Too many arguments for format stringtoo-many-format-argsz^Used when a format string that uses unnamed conversion specifiers is given too many arguments.ZE1306)z&Not enough arguments for format stringtoo-few-format-argsz\Used when a format string that uses unnamed conversion specifiers is given too few argumentsZE1307)z)Argument %r does not match format type %rbad-string-format-typezSUsed when a type required by format string is not suitable for actual argument typeZE1310)z!Suspicious argument in %s.%s callbad-str-strip-callzGThe argument to a str.{l,r,}strip call contains a duplicate character, ZW1302)zInvalid format stringbad-format-stringz.Used when a PEP 3101 format string is invalid.ZW1303)z-Missing keyword argument %r for format stringmissing-format-argument-keyzhUsed when a PEP 3101 format string that uses named fields doesn't receive one or more required keywords.ZW1304)zUnused format argument %runused-format-string-argumentz}Used when a PEP 3101 format string that uses named fields is used with an argument that is not required by the format string.ZW1305)zTFormat string contains both automatic field numbering and manual field specificationformat-combined-specificationzUsed when a PEP 3101 format string contains both automatic field numbering (e.g. '{}') and manual field specification (e.g. '{0}').ZW1306)z2Missing format attribute %r in format specifier %rmissing-format-attributezUsed when a PEP 3101 format string uses an attribute specifier ({0.length}), but the argument passed for formatting doesn't have that attribute.ZW1307)z2Using invalid lookup key %r in format specifier %rinvalid-format-indexzUsed when a PEP 3101 format string uses a lookup specifier ({a[1]}), but the argument passed for formatting doesn't contain or doesn't have that key as an attribute.ZW1308)zKDuplicate string formatting argument %r, consider passing as named argument$duplicate-string-formatting-argumentzmUsed when we detect that a string formatting is repeating an argument instead of using named string argumentsz.strz.floatz.intc                 C   sJ   g }|D ]\}}|r| d| q| d| qt| d| S )z^ Given a list of format specifiers, returns
    the final access path (e.g. a.b.c[0][1]).
    z.{}z[{!r}] )appendformatstrjoin)keypartspathis_attribute	specifier r+   V/var/www/secure340b-portal/env/lib/python3.10/site-packages/pylint/checkers/strings.pyget_access_path   s   r-   c                 C   sT   |dv rdS t | tjr(|  } | tkr|dkS | tkr |dv S | tkr&dS dS dS )NsrTcz	deEfFgGn%F)
isinstanceastroidInstancepytypeBUILTINS_STRBUILTINS_FLOATBUILTINS_INT)arg_typeformat_typer+   r+   r,   arg_matches_format_type   s   r9   c                   @   sV   e Zd ZdZefZdZeZe	e dd Z
e	e dd Zdd Zd	d
 Zdd ZdS )StringFormatCheckerzChecks string formatting operations to ensure that the format string
    is valid and the arguments match the format string.
    stringc              
   C   s>  |j dkrd S |j}|j}t|tjrt|jtsd S |j}zt	|\}}}}W n8 tj
yP }	 z||	j }
| jd||
t|
|	jfd W Y d }	~	d S d }	~	w tjya   | jd|d Y d S w |ro|ro| jd|d d S |rt|tjrt }d}|jD ]#\}}t|tjr|j}t|tr|| q| jd||d qd	}q|s|D ]}||vr| jd
||d q|D ]}||vr| jd||d q|jD ]3\}}t|tjsq||jd }t|}|d ur|d tjfvrt||s| jd|| |fd qd S t|ttjfrt|j}| jd||d d S d S d}t|tjr:t|}d }t|dr9|j}t|}nt|ttjtjffrL|g}d}nd }|d ur||kr`| jd|d n||k rl| jd|d t ||D ]+\}}|szqqt|}|d tjfvrt||s| jd|| |fd qqd S d S )N%r   nodeargsr   r>   r   Fr   Tr   r   r   r   r+   elts   r   r   )!opleftrightr0   r1   r   valuer$   r
   Zparse_format_stringZUnsupportedFormatCharacterindexadd_messageordIncompleteFormatStringDictsetitemsaddget
safe_inferUninferabler9   r3   OTHER_NODESTupletype__name__hasattrrA   lenDictCompzip)selfr>   rD   r?   format_stringrequired_keysZrequired_num_argsZrequired_key_typesZrequired_arg_typeser/   keysZunknown_keysk_r&   argr8   r7   	type_nameZ	args_eltsZ	rhs_tuplenum_argsr+   r+   r,   visit_binop   s   











zStringFormatChecker.visit_binopc                 C   s   t |j}t|tjrat|jtjrc|jjdv re|jdv rT|j	rTt |j	d }t|tj
r5t|jts7d S t|jtt|jkrR| jd||jj|jfd d S d S |jdkrg| || d S d S d S d S d S )N)r$   unicodebytes)striplstriprstripr   r   r=   r#   )r
   rP   funcr0   r1   ZBoundMethodboundr2   namer?   r   rF   r$   rW   rL   rH   _check_new_format)rZ   r>   rj   ra   r+   r+   r,   
visit_callR  s,   



zStringFormatChecker.visit_callc                 C   sD   t dd |D }| D ]\}}|dkrq| jd||fd qd S )Nc                 s   s"    | ]}t |tjr|jV  qd S N)r0   r1   Namerl   ).0ra   r+   r+   r,   	<genexpr>h  s    
zAStringFormatChecker._detect_vacuous_formatting.<locals>.<genexpr>rB   r    r=   )r   rM   rH   )rZ   r>   positional_argumentscounterrl   countr+   r+   r,   _detect_vacuous_formattingg  s   z.StringFormatChecker._detect_vacuous_formattingc                 C   s  t |jtjrt |jjtjsdS |js|jrdS z	t|j	
 }W n tjy-   Y dS w t |tjr:t |jts<dS zt|}W n tjyN   Y dS w zt|j\}}}W n tjyl   | jd|d Y dS w |j}|j}	dd |D }
|r|r| jd|d dS d}|tdd	 |
D 7 }|
r|
D ]}|r||	vr| jd
||fd q|	D ]}||
vr| jd||fd q|p|}|s|rtdd	 |
D }|	s|rd}nd}|r|p|}t||kr| jd|d nt||k r| jd|d | || | |||	 dS )z" Check the new string formatting. Nr   r@   c                 S   s"   h | ]}t |d  tr|d  qS )r   )r0   r$   rq   fieldr+   r+   r,   	<setcomp>  s   " z8StringFormatChecker._check_new_format.<locals>.<setcomp>r   Fc                 s       | ]	}|d krdV  qdS )r!   rB   Nr+   rw   r+   r+   r,   rr         z8StringFormatChecker._check_new_format.<locals>.<genexpr>r   r=   r   c                 s   rz   )r!   TNr+   rw   r+   r+   r,   rr     r{   Tr   r   )r0   rj   r1   	Attributeexprr   Zstarargskwargsnextrk   inferInferenceErrorrF   r$   r   Z	from_callr
   Zparse_format_method_stringrJ   rH   rs   Zkeyword_argumentssumanyrW   rv   _check_new_format_specifiers)rZ   r>   rj   ZstrnodeZ	call_sitefieldsrc   Z
manual_posrs   Znamed_argumentsZnamed_fieldsZ
check_argsrx   emptyr+   r+   r,   rm   r  s|   
z%StringFormatChecker._check_new_formatc                 C   s:  |D ]\}}|dkrd}t |tjr&zt||}W n tjy%   Y qw ||vr+q|| }|tjdfv r7qzt|	 }W n
 tj
yI   Y qw |rQ|tju rRq|jr]t |jtjr]q|}g }	|D ]\}
}|tju rn n|	|
|f |
rz	||d }W n tjy   t|dr| rY  nt||	}| jd||f|d Y  ntw d}t|drz
|t|}W n tjtjtjfy   d	}Y n tj
y   Y  nGw |tju r n?nz|d
 W  n5 tjy   d	}Y nw |rt||	}| jd||f|d  nzt|	 }W qc tj
y   Y  nw qdS )zg
        Check attribute and index access in the format
        string ("{0.a}" and "{0[a]}").
        r!   r   Nhas_dynamic_getattrr   )r?   r>   FgetitemT__getitem__r   )r0   numbersNumberr
   Zget_argument_from_callZNoSuchArgumentErrorr1   rQ   r   r   r   parent	Argumentsr"   getattrZNotFoundErrorrV   r   r-   rH   r   r   ZAstroidIndexErrorZAstroidTypeErrorZAttributeInferenceError)rZ   r>   r   namedr&   
specifiersargnameargumentpreviousparsedr)   r*   r(   Z
warn_errorr+   r+   r,   r     s   







z0StringFormatChecker._check_new_format_specifiersN)rU   
__module____qualname____doc__r   __implements__rl   MSGSmsgsr   rd   rn   rv   rm   r   r+   r+   r+   r,   r:      s    
y
Sr:   c                       s   e Zd ZdZeeefZdZddddZ	ddd	d
ddffZ
dZdZ fddZdd Zdd Zee	  dd Zee	  dd Zee	  dd Zdd Zdd Zdd  Z  ZS )!StringConstantCheckerzCheck string literalsr;   )zRAnomalous backslash in string: '%s'. String constant might be missing an r prefix.anomalous-backslash-in-stringzBUsed when a backslash is in a literal string but not as an escape.)zaAnomalous Unicode escape in byte string: '%s'. String constant might be missing an r or u prefix."anomalous-unicode-escape-in-stringzSUsed when an escape like \u is encountered in a byte string where it has no effect.)z)Implicit string concatenation found in %simplicit-str-concat-in-sequenceziString literals are implicitly concatenated in a literal iterable definition : maybe a comma is missing ?)ZW1401ZW1402ZW1403z check-str-concat-over-line-jumpsFZynz<y_or_n>zThis flag controls whether the implicit-str-concat-in-sequence should generate a warning on implicit string concatenation in sequences defined over several lines.)defaultrT   metavarhelpzabfnrtvx
	\'"01234567ZuUNc                    s    t t| j|i | i | _d S ro   )superr   __init__string_tokens)rZ   r?   r~   	__class__r+   r,   r   [  s   
zStringConstantChecker.__init__c                 C   s   d|j v | _d S )Nunicode_literals)Zfuture_imports_unicode_literals)rZ   moduler+   r+   r,   process_module_  s   z$StringConstantChecker.process_modulec                 C   s   d}t |D ]v\}\}}}}}|tjkr|}q|tjkr|| ||d  |d }	|	t|k rR||	 jtjtjtj	fv rR|	d7 }	|	t|k rR||	 jtjtjtj	fv s;|	t|k r\||	 nd }
|dkrs|d t|d |d  
|f}t||
f| j|< qd S )Nasciir   rB   )	enumeratetokenizeENCODINGSTRINGprocess_string_tokenrW   rT   NEWLINENLCOMMENTencodestr_evalr   )rZ   tokensencodingitok_typetokenstartr`   linej
next_tokenr+   r+   r,   process_tokensb  s0   

"z$StringConstantChecker.process_tokensc                 C      |  |d d S )Nlistcheck_for_concatenated_stringsrZ   r>   r+   r+   r,   
visit_list{     z StringConstantChecker.visit_listc                 C   r   )NrL   r   r   r+   r+   r,   	visit_set  r   zStringConstantChecker.visit_setc                 C   r   )Ntupler   r   r+   r+   r,   visit_tuple  r   z!StringConstantChecker.visit_tuplec                 C   s   |j D ]M}t|trP| tv rP|jdk rq|j|jf| jvr q| j|j|jf \}}||jkrP|d urP|j	t
jkrP|jd |jksF| jjrP| jd|j|fd qd S )Nr   r   r   r?   )rA   r0   r   r3   _AST_NODE_STR_TYPES
col_offsetlinenor   rF   rT   r   r   r   configZ check_str_concat_over_line_jumpsrH   )rZ   Ziterable_nodeZiterable_typeeltZmatching_tokenr   r+   r+   r,   r     s,   


z4StringConstantChecker.check_for_concatenated_stringsc           	      C   s   d }d }t |D ]\}}|dv r|} nq|d u rd S |d |  }||d  }|d d |dd    kr=d| krFn n|dd }n|dd }d|vrY| ||| d S d S )Nz'"r   rB   r)r   lowerprocess_non_raw_string_token)	rZ   r   	start_row
quote_charrG   r/   prefixZafter_prefixstring_bodyr+   r+   r,   r     s"   ,z*StringConstantChecker.process_string_tokenc                 C   s   d}	 | d|}|dkrdS ||d  }|||d  }|| jv r;d|v r'n"ts,| jr1d|vr1n| jd	||fd
 n|| jvrI| jd||fd
 |d7 }q)a  check for bad escapes in a non-raw string.

        prefix: lowercase string of eg 'ur' string prefix markers.
        string_body: the un-parsed body of the string, not including the quote
        marks.
        start_row: integer line number in the source.
        r   T\r   rB   r   ubr   r   r   N)findUNICODE_ESCAPE_CHARACTERS_PY3Kr   rH   ESCAPE_CHARACTERS)rZ   r   r   r   r   Z	next_charmatchr+   r+   r,   r     s.   

z2StringConstantChecker.process_non_raw_string_token)rU   r   r   r   r   r   r   r   rl   r   optionsr   r   r   r   r   r   r^   r   r   r   r   r   r   __classcell__r+   r+   r   r,   r   &  s<    






r   c                 C   s    |  t|  |  t|  dS )z.required method to auto register this checker N)Zregister_checkerr:   r   )Zlinterr+   r+   r,   register  s   r   c                 C   sf   | dd   dv r| dd } n| d   dv r| dd } | dd dv r-| dd	 S | dd
 S )a%  
    Mostly replicate `ast.literal_eval(token)` manually to avoid any performance hit.
    This supports f-strings, contrary to `ast.literal_eval`.
    We have to support all string literal notations:
    https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
    r   r   )frrfN)r   r   frB   r   )z"""z'''r   r   )r   )r   r+   r+   r,   r     s   r   ),r   builtinssysr   r   collectionsr   r1   Zastroid.argumentsr   Zastroid.node_classesr   Zpylint.interfacesr   r   r   Zpylint.checkersr   r	   r
   Zpylint.checkers.utilsr   r   version_infor   Z_PY27r   ListLambdaFunctionDefListCompSetCompGeneratorExprR   rU   r4   r5   r6   r-   r9   r:   r   r   r   r+   r+   r+   r,   <module>   s    '.4:@EJPW_fmv



  Z C