3
tûœ]JC  ã               @   s–   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 ddl
mZ ddhZd	Zd
ZdZdZdhZdZdZdZdd„ ZG dd„ deƒZdd„ ZdS )z0Checkers for various standard library functions.é    N)ÚInstance)ÚConst)ÚBaseCheckerÚutils)ÚIAstroidCheckerÚopenÚfilezunittest.casezthreading.Threadz	copy.copyzos._Environz	os.getenvzsubprocess.Popenzsubprocess.runÚ_ioc       
      C   sÆ   t | tƒsdS t| ƒ}d}d|k}|t|ƒ s>t| ƒt|ƒkrBdS d|k}d|k}d|k}d|k}d|k}d	|kr†|s~|s~|r‚dS d
}|r’|r’dS || | | }	|	dkr®dS |p¼|p¼|p¼|sÂdS d
S )NFzrwatb+UxÚxÚrÚwÚaÚtÚbÚUTé   )Ú
isinstanceÚstrÚsetÚlen)
ÚmodeÚmodesÚ_modeZcreatingÚreadingÚwritingZ	appendingÚtextÚbinaryÚtotal© r   ú8/tmp/pip-build-fn42xldr/pylint/pylint/checkers/stdlib.pyÚ_check_mode_str,   s0    
r    c               @   s®  e Zd ZefZdZdˆddddd‰ifdŠd‹dŒd�dŽd�d�d‘d#œ
Zd$d%d&d'd(d)d*hd’d.d/d0d1d2hd“d4d5d6d7d8d9d:d;d<h	id”d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLhd•dNdOdPhd–dQdRd9d:d;d<hd—dSdTdUdVdWdXhd˜dZd[d\d]d^hd™d_hdšd`dadbdcdddedfhd›dghidhœZdidj„ Zdkdl„ Z	dmdn„ Z
dodp„ Zejddddddddd!ƒ	dqdr„ ƒZejdƒdsdt„ ƒZejdƒdudv„ ƒZejdƒdwdx„ ƒZejdƒdydz„ ƒZd{d|„ Zd}d~„ Zdd€„ Zd�d‚„ Zdƒd„„ Zd…d†„ Zd‡S )œÚStdlibCheckerÚstdlibú""%s" is not a valid mode for open.úbad-open-modeú„Python supports: r, w, a[, x] modes with b, +, and U (only with r) options. See http://docs.python.org/2/library/functions.html#openz)Using datetime.time in a boolean context.zboolean-datetimezÍUsing datetime.time in a boolean context can hide subtle bugs when the time they represent matches midnight UTC. This behaviour was fixed in Python 3.5. See http://bugs.python.org/issue13936 for reference.Z
maxversioné   é   ú*Redundant use of %s with constant value %rúredundant-unittest-assertú´The first argument of assertTrue and assertFalse is a condition. If a constant is passed as parameter, that condition will be always true. In this case a warning should be emitted.úUsing deprecated method %s()údeprecated-methodú�The method is marked as deprecated and will be removed in a future version of Python. Consider looking for an alternative in the documentation.ú*threading.Thread needs the target functionúbad-thread-instantiationú¹The warning is emitted when a threading.Thread class is instantiated without the target function being passed. By default, the first parameter is the group param, not the target param. ú<Using copy.copy(os.environ). Use os.environ.copy() instead. úshallow-copy-environúžos.environ is not a dict object but proxy object, so shallow copy has still effects on original object. See https://bugs.python.org/issue15373 for reference. ú$%s does not support %s type argumentúinvalid-envvar-valueúxEnv manipulation functions support only string type arguments. See https://docs.python.org/3/library/os.html#os.getenv. ú,%s default type is %s. Expected str or None.úinvalid-envvar-defaultú©Env manipulation functions return None or str values. Supplying anything different as a default may cause bugs. See https://docs.python.org/3/library/os.html#os.getenv. úGUsing preexec_fn keyword which may be unsafe in the presence of threadsúsubprocess-popen-preexec-fná+  The preexec_fn parameter is not safe to use in the presence of threads in your application. The child process could deadlock before exec is called. If you must use it, keep it trivial! Minimize the number of libraries you call into.https://docs.python.org/3/library/subprocess.html#popen-constructorúGUsing subprocess.run without explicitly set `check` is not recommended.úsubprocess-run-checkúÁThe check parameter should always be used with explicitly set `check` keyword to make clear what the error-handling behavior is.https://docs.python.org/3/library/subprocess.html#subprocess.runs)
ZW1501ZW1502ZW1503ZW1505ZW1506ZW1507ZE1507ZW1508ZW1509ZW1510zcgi.parse_qszcgi.parse_qslzctypes.c_bufferz2distutils.command.register.register.check_metadataz,distutils.command.sdist.sdist.check_metadataztkinter.Misc.tk_menuBarz tkinter.Menu.tk_bindForTraversalé   é   r   zcommands.getstatusz	os.popen2z	os.popen3z	os.popen4zmacostools.touchedé   z#unittest.case.TestCase.assertEqualsz&unittest.case.TestCase.assertNotEqualsz)unittest.case.TestCase.assertAlmostEqualsz,unittest.case.TestCase.assertNotAlmostEqualszunittest.case.TestCase.assert_z)xml.etree.ElementTree.Element.getchildrenz)xml.etree.ElementTree.Element.getiteratorz+xml.etree.ElementTree.XMLParser.getiteratorz'xml.etree.ElementTree.XMLParser.doctypezinspect.getargspecZfailUnlessEqualZassertEqualsZfailIfEqualZassertNotEqualsZfailUnlessAlmostEqualZassertAlmostEqualsZfailIfAlmostEqualZassertNotAlmostEqualsZ
failUnlessZassert_ZfailUnlessRaisesZfailIfZassertRaisesRegexpZassertRegexpMatchesZassertNotRegexpMatchesr   zbase64.encodestringzbase64.decodestringzntpath.splituncz
cgi.escapez#configparser.RawConfigParser.readfpzinspect.getmoduleinfozlogging.warnzlogging.Logger.warnzlogging.LoggerAdapter.warnznntplib._NNTPBase.xpathzplatform.popené   zimportlib.find_loaderzplistlib.readPlistzplistlib.writePlistzplistlib.readPlistFromByteszplistlib.writePlistToByteszasyncio.tasks.asynczfractions.gcdzinspect.getargvalueszinspect.formatargspeczinspect.formatargvalueszinspect.getcallargszplatform.linux_distributionzplatform.distz4importlib._bootstrap_external.FileLoader.load_module)r   r@   r&   c             C   s0   |j  r,|j r,t|jƒdkr,| jd|d� d S )Nr   zbad-thread-instantiation)Únode)ÚkwargsÚkeywordsr   ÚargsÚadd_message)ÚselfrD   r   r   r   Ú_check_bad_thread_instantiation÷   s    z-StdlibChecker._check_bad_thread_instantiationc             C   s2   |j r.x&|j D ]}|jdkr| jd|d� qW d S )NÚ
preexec_fnzsubprocess-popen-preexec-fn)rD   )rF   ÚargrH   )rI   rD   Úkeywordr   r   r   Ú_check_for_preexec_fn_in_popenû   s    
z,StdlibChecker._check_for_preexec_fn_in_popenc             C   s.   dd„ |j pf D ƒ}d|kr*| jd|d� d S )Nc             S   s   h | ]
}|j ’qS r   )rL   )Ú.0rM   r   r   r   ú	<setcomp>  s    z;StdlibChecker._check_for_check_kw_in_run.<locals>.<setcomp>Úcheckzsubprocess-run-check)rD   )rF   rH   )rI   rD   rE   r   r   r   Ú_check_for_check_kw_in_run  s    z(StdlibChecker._check_for_check_kw_in_runc             C   s@   t j|dd�}x,|jƒ D ] }|jƒ tkr| jd|d� P qW d S )Nr   )Úpositionzshallow-copy-environ)rD   )r   Úget_argument_from_callÚinferredÚqnameÚ
OS_ENVIRONrH   )rI   rD   rL   rU   r   r   r   Ú_check_shallow_copy_environ  s
    z)StdlibChecker._check_shallow_copy_environc             C   s$  �yxþ|j jƒ D ]ð}|tjkr q|jƒ jtkrLt|j ddƒtkrô| j	|ƒ n¨|jƒ jt
krh| j||ƒ nŒt|tjƒr¤|jƒ tkrŒ| j|ƒ qô|jƒ tkrô| j|ƒ nPt|tjƒrô|jƒ }|tkrÌ| j|ƒ n(|tkrâ| j||ƒ n|tkrô| j|ƒ | j||ƒ qW W n tjk
�r   dS X dS )zVisit a Call node.ÚnameN)ÚfuncÚinferÚastroidÚUninferableÚrootrY   ÚOPEN_MODULEÚgetattrÚ
OPEN_FILESÚ_check_open_modeÚUNITTEST_CASEÚ_check_redundant_assertr   ÚClassDefrV   ÚTHREADING_THREADrJ   ÚSUBPROCESS_POPENrN   ÚFunctionDefÚ	COPY_COPYrX   ÚENV_GETTERSÚ_check_env_functionÚSUBPROCESS_RUNrR   Ú_check_deprecated_methodÚInferenceError)rI   rD   rU   rY   r   r   r   Ú
visit_call  s2    

zStdlibChecker.visit_callc             C   s   |j dkr| j|jƒ d S )NÚnot)ÚopÚ_check_datetimeÚoperand)rI   rD   r   r   r   Úvisit_unaryop4  s    
zStdlibChecker.visit_unaryopc             C   s   | j |jƒ d S )N)rr   Útest)rI   rD   r   r   r   Úvisit_if9  s    zStdlibChecker.visit_ifc             C   s   | j |jƒ d S )N)rr   ru   )rI   rD   r   r   r   Úvisit_ifexp=  s    zStdlibChecker.visit_ifexpc             C   s   x|j D ]}| j|ƒ qW d S )N)Úvaluesrr   )rI   rD   Úvaluer   r   r   Úvisit_boolopA  s    zStdlibChecker.visit_boolopc                sê   t jd }t|jtjƒr"|jj}nt|jtjƒr:|jj}nd S tj	tj
tjf}t||ƒs\d S |jƒ }t‡fdd„||fD ƒƒr’ˆjd||fd� nTxRˆj| jƒ D ]@\}‰ |t jkr¢t‡ fdd„||fD ƒƒr¢ˆjd||fd� P q¢W d S )Nr   c             3   s   | ]}|ˆ j d  kV  qdS )r   N)Ú
deprecated)rO   rY   )rI   r   r   ú	<genexpr>[  s    z9StdlibChecker._check_deprecated_method.<locals>.<genexpr>zdeprecated-method)rD   rG   c             3   s   | ]}|ˆ kV  qd S )Nr   )rO   rY   )Ú	func_listr   r   r|   `  s    )ÚsysÚversion_infor   rZ   r\   Ú	AttributeÚattrnameÚNamerY   ÚBoundMethodZUnboundMethodrh   rV   ÚanyrH   r{   Úitems)rI   rD   rU   Zpy_versÚ	func_nameZacceptable_nodesrV   Z
since_versr   )r}   rI   r   rm   F  s&    



z&StdlibChecker._check_deprecated_methodc             C   sP   t |tjƒrL|jrLt |jd tjƒrL|jdkrL| jd|j|jd jf|d� d S )Nr   Ú
assertTrueÚassertFalsezredundant-unittest-assert)rG   rD   )r‡   rˆ   )r   r\   rƒ   rG   r   rY   rH   ry   )rI   rD   r[   r   r   r   rd   e  s    
z%StdlibChecker._check_redundant_assertc             C   sP   yt |jƒ ƒ}W n tjk
r&   dS X t|tƒrL|jƒ dkrL| jd|d� dS )z[ Check that a datetime was inferred.
        If so, emit boolean-datetime warning.
        Nzdatetime.timezboolean-datetime)rD   )Únextr[   r\   rn   r   r   rV   rH   )rI   rD   rU   r   r   r   rr   r  s    zStdlibChecker._check_datetimec             C   sh   yt j|ddd�}W n t jk
r*   dS X |rdt j|ƒ}t|tjƒrdt|jƒ rd| j	d||jd� dS )z>Check that the mode argument of an open or file call is valid.r   r   )rS   rM   Nzbad-open-mode)rD   rG   )
r   rT   ZNoSuchArgumentErrorÚ
safe_inferr   r\   r   r    ry   rH   )rI   rD   Zmode_argr   r   r   rb   }  s    

zStdlibChecker._check_open_modec             C   sÄ   d}d}|j r dd„ |j D ƒ}nd }|jr6|jd }n|rL||krL|| }nd }|rn| j|dtj|ƒ|dd� t|jƒd	krˆ|jd
 }n|rž||krž|| }nd }|rÀ| j||dtj|ƒdd� d S )NÚkeyÚdefaultc             S   s   i | ]}|j |j“qS r   )ry   rL   )rO   rM   r   r   r   ú
<dictcomp>Ž  s    z5StdlibChecker._check_env_function.<locals>.<dictcomp>r   zinvalid-envvar-valueF)rD   ÚmessageÚcall_argr[   Ú
allow_noner@   r   zinvalid-envvar-defaultT)rD   r[   rŽ   r�   r�   )rF   rG   Ú_check_invalid_envvar_valuer   rŠ   r   )rI   rD   r[   Zenv_name_kwargZenv_value_kwargrE   Zenv_name_argZenv_value_argr   r   r   rk   Š  s:    

z!StdlibChecker._check_env_functionc             C   s„   |t jd fkrd S |jƒ }t|tƒrhd}|jd kr:| }nt|jtƒsJd}|r€| j||||jƒ fd� n| j||||jƒ fd� d S )NFT)rD   rG   )	r\   r]   rV   r   r   ry   r   rH   Zpytype)rI   rD   r[   rŽ   r�   r�   rY   Úemitr   r   r   r‘   ±  s    

z)StdlibChecker._check_invalid_envvar_valueN)r#   r$   r%   )r&   r'   )r(   r)   r*   )r+   r,   r-   )r.   r/   r0   )r1   r2   r3   )r4   r5   r6   )r7   r8   r9   )r:   r;   r<   )r=   r>   r?   )r@   rA   r   )r@   rB   r   )r&   r   r   )r&   r   r   )r&   r@   r   )r&   r&   r   )r&   rC   r   )r&   rC   rC   )r&   r'   r   )r&   rA   r   )Ú__name__Ú
__module__Ú__qualname__r   Z__implements__rY   Zmsgsr{   rJ   rN   rR   rX   r   Zcheck_messagesro   rt   rv   rw   rz   rm   rd   rr   rb   rk   r‘   r   r   r   r   r!   J   s                    'r!   c             C   s   | j t| ƒƒ dS )z.required method to auto register this checker N)Zregister_checkerr!   )Zlinterr   r   r   ÚregisterÂ  s    r–   )Ú__doc__r~   r\   Zastroid.basesr   Zastroid.node_classesr   Zpylint.checkersr   r   Zpylint.interfacesr   ra   rc   rf   ri   rW   rj   rg   rl   r_   r    r!   r–   r   r   r   r   Ú<module>   s(     z