o
    IXîhD8  ã                   @   sL  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
Z
d dlZd dlZd dlZd dlZe d¡ dd	„ Zd
d„ Ze de
jv d¡ZeejjejjƒZG dd„ deƒZeee jdZeee j dZ!eee j dZ"G dd„ dej#ƒZ$G dd„ dej#ƒZ%G dd„ dej#ƒZ&G dd„ dej#ƒZ'e(dkr¤e )¡  dS dS )é    N)Úsupport)Ú	os_helper)Úsocket_helper)ÚResourceDenied)Úsanepathname2urlÚnetworkc                 O   sN   t dƒD ] }z
| |i |¤ŽW   S  |y$ } z|}W Y d }~qd }~ww |‚)Né   )Úrange)ÚfuncÚexcÚargsÚkwargsÚiÚeÚlast_exc© r   ú>/opt/python-3.10.19/usr/lib/python3.10/test/test_urllib2net.pyÚ_retry_thrice   s   €þr   c                    s   ‡ ‡fdd„}|S )Nc                     s   t ˆˆ g| ¢R i |¤ŽS ©N)r   )r   r   ©r   r
   r   r   Úwrapped   s   z(_wrap_with_retry_thrice.<locals>.wrappedr   )r
   r   r   r   r   r   Ú_wrap_with_retry_thrice   s   r   ZTRAVISz%bpo-35411: skip FTP test on Travis CIc                   @   s*   e Zd ZdZdd„ Zdd„ Zd	dd„ZdS )
ÚTransientResourcezŽRaise ResourceDenied if an exception is raised while the context manager
    is in effect that matches the specified exception and attributes.c                 K   s   || _ || _d S r   )r   Úattrs)Úselfr   r   r   r   r   Ú__init__2   s   
zTransientResource.__init__c                 C   s   | S r   r   ©r   r   r   r   Ú	__enter__6   ó   zTransientResource.__enter__Nc                 C   s\   |dur*t | j|ƒr,| j ¡ D ]\}}t||ƒs dS t||ƒ|kr% dS qtdƒ‚dS dS )z§If type_ is a subclass of self.exc and value has attributes matching
        self.attrs, raise ResourceDenied.  Otherwise let the exception
        propagate (if any).Nz%an optional resource is not available)Ú
issubclassr   r   ÚitemsÚhasattrÚgetattrr   )r   Ztype_ÚvalueÚ	tracebackÚattrZ
attr_valuer   r   r   Ú__exit__9   s   
ÿùzTransientResource.__exit__)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r&   r   r   r   r   r   -   s
    r   )Úerrnoc                   @   s   e Zd ZdZdS )Ú	AuthTestsz&Tests urllib2 authentication features.N)r'   r(   r)   r*   r   r   r   r   r,   N   s    r,   c                   @   s   e Zd Zdd„ ZdS )ÚCloseSocketTestc                 C   sr   |   tjj¡ tj}t |¡  t|ƒ}|j	}|  
|j¡ | ¡  |  |j¡ W d   ƒ d S 1 s2w   Y  d S r   )Ú
addCleanupÚurllibÚrequestÚ
urlcleanupr   ÚTEST_HTTP_URLr   Útransient_internetÚ_urlopen_with_retryÚfpÚassertFalseÚclosedÚcloseÚ
assertTrue)r   ÚurlÚresponseÚsockr   r   r   Ú
test_closew   s   "ûzCloseSocketTest.test_closeN)r'   r(   r)   r=   r   r   r   r   r-   u   s    r-   c                   @   sd   e Zd Zdd„ Zedd„ ƒZdd„ Zdd„ Zd	d
„ Zdd„ Z	e
 d¡dd„ ƒZddd„Zdd„ ZdS )ÚOtherNetworkTestsc                 C   s   d S r   )ÚloggingÚ	getLoggerÚ
addHandlerÚStreamHandler)r   r?   Úloggerr   r   r   ÚsetUp†   r   zOtherNetworkTests.setUpc                 C   s&   ddd t jjfg}|  ||  ¡ ¡ d S )Nzftp://www.pythontest.net/READMEz*ftp://www.pythontest.net/non-existent-file)r/   ÚerrorÚURLErrorÚ
_test_urlsÚ_extra_handlers)r   Úurlsr   r   r   Útest_ftp   s   ÿþzOtherNetworkTests.test_ftpc              	   C   sŠ   t j}t|dƒ}z+| d¡ | ¡  dttj |¡ƒ dd t	j
jfg}| j||  ¡ dd W t |¡ nt |¡ w |  tt	jjd¡ d S )NÚwz	hi there
zfile:zfile:///nonsensename/etc/passwdT)Úretryz./relative_path/to/file)r   ÚTESTFNÚopenÚwriter8   r   ÚosÚpathÚabspathr/   rE   rF   rG   rH   ÚremoveZassertRaisesÚ
ValueErrorr0   Úurlopen)r   rM   ÚfrI   r   r   r   Ú	test_file˜   s   

ÿþzOtherNetworkTests.test_filec                 C   s\   d}t  |¡ tj |¡}tj |¡}|  | ¡ d¡ W d   ƒ d S 1 s'w   Y  d S )Nz)http://www.pythontest.net/index.html#frag©r   r3   r/   r0   ÚRequestrU   ÚassertEqualÚgeturl)r   Zurlwith_fragÚreqÚresr   r   r   Útest_urlwithfragÅ   ó   
ÿ"ýz"OtherNetworkTests.test_urlwithfragc                 C   s\   d}t  |¡ tj |¡}tj |¡}|  | ¡ d¡ W d   ƒ d S 1 s'w   Y  d S )Nz*http://www.pythontest.net/redir/with_frag/z)http://www.pythontest.net/elsewhere/#fragrX   )r   Zredirect_url_with_fragr\   r]   r   r   r   Útest_redirect_url_withfragÍ   r_   z,OtherNetworkTests.test_redirect_url_withfragc                 C   sª   t j}t |¡C tj ¡ }tj |¡}|  | 	¡ ¡ | 
|¡ |  | 	¡ ¡ |  | d¡¡ | dd¡ | 
|¡ |  | d¡d¡ W d   ƒ d S 1 sNw   Y  d S )Nz
User-agentz
User-Agentz
Test-Agent)r   r2   r   r3   r/   r0   Úbuild_openerrY   r6   Zheader_itemsrN   r9   Z
has_headerZ
add_headerrZ   Z
get_header)r   r:   Úopenerr0   r   r   r   Útest_custom_headersÕ   s   


"÷z%OtherNetworkTests.test_custom_headersz XXX: http://www.imdb.com is gonec              	   C   sª   d}t  |¡D ztj |¡}W d   ƒ n1 sw   Y  W n ty.   |  d¡ Y nw |  |¡ tj |¡}| ¡ }|  |¡ W d   ƒ d S 1 sNw   Y  d S )Nzhttp://www.imdb.comzOurlopen failed for site not sending                            Connection:close)	r   r3   r/   r0   rU   rT   Zfailr9   Úread)r   ZURLr]   r\   r   r   r   Útest_sites_no_connection_closeâ   s   ÿ€ÿ
"ôz0OtherNetworkTests.test_sites_no_connection_closeTc                 C   sâ  dd l }dd l}| d¡j}tjj|Ž j}|rt|tj	j
ƒ}|D ]Î}| j|d¾ t|tƒr4|\}}	}
nd  }	}
t |¡• z	|||	tjƒ}W n( tyo } z|
rdd|
||	t|ƒ|f }|  ||
|¡ n‚ W Y d }~n`d }~ww zDt8 t& t | ¡ }|dt|ƒ ƒ W d   ƒ n1 sw   Y  W d   ƒ n1 sŸw   Y  W d   ƒ n1 s®w   Y  W n tyÆ   td| tjd Y nw | ¡  W d   ƒ n1 sÕw   Y  | d¡ W d   ƒ n1 séw   Y  q d S )	Nr   Ztest_urllib2)r:   z5Didn't get expected error(s) %s for %s %s, got %s: %szread %d bytesz<timeout: %s>)Úfilegš™™™™™¹?) Útimer?   r@   Údebugr/   r0   ra   rN   r   rE   rF   ZsubTestÚ
isinstanceÚtupler   r3   r   ZINTERNET_TIMEOUTÚOSErrorÚtypeZassertIsInstanceÚtime_outÚsocket_peer_resetÚioerror_peer_resetrd   ÚlenÚTimeoutErrorÚprintÚsysÚstderrr8   Úsleep)r   rI   ÚhandlersrL   rg   r?   rh   rU   r:   r\   Zexpected_errrV   ÚerrÚmsgÚbufr   r   r   rG   ÷   s`   
ÿþ€üÿþü€ € €ÿ€ìå€ÿzOtherNetworkTests._test_urlsc                 C   s2   g }t j ¡ }|  |j¡ | d¡ | |¡ |S )Né   )r/   r0   ZCacheFTPHandlerr.   Úclear_cacheZ
setTimeoutÚappend)r   rv   Zcfhr   r   r   rH     s   


z!OtherNetworkTests._extra_handlersN)T)r'   r(   r)   rD   Úskip_ftp_test_on_travisrJ   rW   r^   r`   rc   ÚunittestÚskipre   rG   rH   r   r   r   r   r>   …   s    	
-

'r>   c                   @   sh   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdZe	dd„ ƒZ
e	dd„ ƒZe	dd„ ƒZe	dd„ ƒZdS )ÚTimeoutTestc                 C   s   |   tjj¡ d S r   )r.   r/   r0   r1   r   r   r   r   rD   *  s   zTimeoutTest.setUpc                 C   sp   |   t ¡ ¡ tj}tj|d d t|ƒ}|  |j	¡ |   |j
jj ¡ ¡ W d   ƒ d S 1 s1w   Y  d S ©N©Útimeout)ÚassertIsNoneÚsocketÚgetdefaulttimeoutr   r2   r   r3   r4   r.   r8   r5   ÚrawÚ_sockÚ
gettimeout©r   r:   Úur   r   r   Útest_http_basic.  s   "ýzTimeoutTest.test_http_basicc              
   C   s”   |   t ¡ ¡ tj}t |¡1 t d¡ zt|ƒ}|  	|j
¡ W t d ¡ nt d ¡ w |  |jjj ¡ d¡ W d   ƒ d S 1 sCw   Y  d S ©Né<   )r„   r…   r†   r   r2   r   r3   Úsetdefaulttimeoutr4   r.   r8   rZ   r5   r‡   rˆ   r‰   rŠ   r   r   r   Útest_http_default_timeout6  s   
"ùz%TimeoutTest.test_http_default_timeoutc              
   C   s–   |   t ¡ ¡ tj}t |¡2 t d¡ zt|d d}|  	|j
¡ W t d ¡ nt d ¡ w |   |jjj ¡ ¡ W d   ƒ d S 1 sDw   Y  d S ©NrŽ   r‚   )r„   r…   r†   r   r2   r   r3   r   r4   r.   r8   r5   r‡   rˆ   r‰   rŠ   r   r   r   Útest_http_no_timeoutB  s   
"ùz TimeoutTest.test_http_no_timeoutc                 C   sd   t j}t |¡  t|dd}|  |j¡ |  |jj	j
 ¡ d¡ W d   ƒ d S 1 s+w   Y  d S )Néx   r‚   )r   r2   r   r3   r4   r.   r8   rZ   r5   r‡   rˆ   r‰   rŠ   r   r   r   Útest_http_timeoutN  s   "ýzTimeoutTest.test_http_timeoutzftp://www.pythontest.net/c                 C   sp   |   t ¡ ¡ tj| jd d t| jƒ}|  |j¡ |   |j	j	j
j ¡ ¡ W d   ƒ d S 1 s1w   Y  d S r   )r„   r…   r†   r   r3   ÚFTP_HOSTr4   r.   r8   r5   r‡   rˆ   r‰   ©r   r‹   r   r   r   Útest_ftp_basicW  s   
"ýzTimeoutTest.test_ftp_basicc              
   C   s”   |   t ¡ ¡ t | j¡3 t d¡ zt| jƒ}|  |j	¡ W t d ¡ nt d ¡ w |  
|jjjj ¡ d¡ W d   ƒ d S 1 sCw   Y  d S r   )r„   r…   r†   r   r3   r•   r   r4   r.   r8   rZ   r5   r‡   rˆ   r‰   r–   r   r   r   Útest_ftp_default_timeout_  s   

"ùz$TimeoutTest.test_ftp_default_timeoutc              
   C   s–   |   t ¡ ¡ t | j¡4 t d¡ zt| jd d}|  |j	¡ W t d ¡ nt d ¡ w |   |j
j
jj ¡ ¡ W d   ƒ d S 1 sDw   Y  d S r‘   )r„   r…   r†   r   r3   r•   r   r4   r.   r8   r5   r‡   rˆ   r‰   r–   r   r   r   Útest_ftp_no_timeoutk  s   
"ùzTimeoutTest.test_ftp_no_timeoutc                 C   sd   t  | j¡" t| jdd}|  |j¡ |  |jjjj	 
¡ d¡ W d   ƒ d S 1 s+w   Y  d S r‘   )r   r3   r•   r4   r.   r8   rZ   r5   r‡   rˆ   r‰   r–   r   r   r   Útest_ftp_timeoutw  s
   "ýzTimeoutTest.test_ftp_timeoutN)r'   r(   r)   rD   rŒ   r   r’   r”   r•   r}   r—   r˜   r™   rš   r   r   r   r   r€   )  s    


r€   Ú__main__)*r+   r~   Útestr   Ztest.supportr   r   r   Ztest.test_urllib2r   rP   r…   Zurllib.errorr/   Zurllib.requestrs   Zrequiresr   r   ZskipIfÚenvironr}   r0   rU   rE   rF   r4   Úobjectr   rk   Z	ETIMEDOUTrm   Z
ECONNRESETrn   ro   ZTestCaser,   r-   r>   r€   r'   Úmainr   r   r   r   Ú<module>   sB    
	ÿÿ' %Vÿ