
    daf7                     n   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j                   d       d Zd	 Z eej&                  j(                  ej*                  j,                        Z G d
 de      Z eee j6                        Z eee j:                        Z eee j:                        Z G d dej@                        Z! G d dej@                        Z" G d dej@                        Z# G d dej@                        Z$e%dk(  r ejL                          yy)    N)support)	os_helper)socket_helper)ResourceDenied)sanepathname2urlnetworkc                 b    t        d      D ]  }	  | |i |c S  # |$ r}|}Y d }~d }~ww xY w)N   )range)funcexcargskwargsielast_excs          //root/Python-3.12.4/Lib/test/test_urllib2net.py_retry_thricer      sK    1X 	((( N  	H	s   .).c                       fd}|S )Nc                  $    t        g| i |S N)r   )r   r   r   r   s     r   wrappedz(_wrap_with_retry_thrice.<locals>.wrapped   s    T38888     )r   r   r   s   `` r   _wrap_with_retry_thricer      s    9Nr   c                   $    e Zd ZdZd Zd ZddZy)TransientResourcezRaise ResourceDenied if an exception is raised while the context manager
    is in effect that matches the specified exception and attributes.c                      || _         || _        y r   )r   attrs)selfr   r   s      r   __init__zTransientResource.__init__+   s    
r   c                     | S r   r   r    s    r   	__enter__zTransientResource.__enter__/   s    r   Nc                     |ct        | j                  |      rL| j                  j                         D ]$  \  }}t	        ||      s yt        ||      |k7  s$ y t        d      yy)zIf 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    type_value	tracebackattr
attr_values         r   __exit__zTransientResource.__exit__2   so     DHHe!<$(JJ$4$4$6 N jud+5$':5	N %%LMM "=r   )NNN)__name__
__module____qualname____doc__r!   r$   r/   r   r   r   r   r   &   s    INr   r   )errnoc                       e Zd ZdZy)	AuthTestsz&Tests urllib2 authentication features.N)r0   r1   r2   r3   r   r   r   r6   r6   G   s    0r   r6   c                       e Zd Zd Zy)CloseSocketTestc                    | j                  t        j                  j                         t        j
                  }t        j                  |      5  t        |      }|j                  }| j                  |j                         |j                          | j                  |j                         d d d        y # 1 sw Y   y xY wr   )
addCleanupurllibrequest
urlcleanupr   TEST_HTTP_URLr   transient_internet_urlopen_with_retryfpassertFalseclosedclose
assertTrue)r    urlresponsesocks       r   
test_closezCloseSocketTest.test_closep   s    112 ##--c2 	)*3/H;;DT[[)NNOODKK(	) 	) 	)s   AB66B?N)r0   r1   r2   rI   r   r   r   r8   r8   n   s    )r   r8   c                       e Zd Zd Z ej
                  d      d        Zd Zd Z ej
                  d      d        Z	d Z
 ej                  d      d	        Zdd
Zd Zy)OtherNetworkTestsc                      y r   )logging	getLogger
addHandlerStreamHandler)r    rM   loggers      r   setUpzOtherNetworkTests.setUp   s    r   walltimec                     dddd t         j                  j                  fg}| j                  || j	                                y )Nzftp://www.pythontest.net/READMEz*ftp://www.pythontest.net/non-existent-file)r;   errorURLError
_test_urls_extra_handlers)r    urlss     r   test_ftpzOtherNetworkTests.test_ftp   sA     .-96<<((* 	d2245r   c                    t         j                  }t        |d      }	 |j                  d       |j	                          dt        t        j                  j                  |            z   dd t        j                  j                  fg}| j                  || j                         d       t        j                  |       | j                  t         t        j"                  j$                  d       y # t        j                  |       w xY w)Nwz	hi there
zfile:zfile:///nonsensename/etc/passwdT)retryz./relative_path/to/file)r   TESTFNopenwriterD   r   ospathabspathr;   rU   rV   rW   rX   removeassertRaises
ValueErrorr<   urlopen)r    r^   frY   s       r   	test_filezOtherNetworkTests.test_file   s    !!
	GGL!GGI*277??6+BCC2D&&(D
 OOD$"6"6"8OEIIf*fnn&<&<=VW IIfs   BC. .Dc                    d}t        j                  |      5  t        j                  j	                  |      }t        j                  j                  |      }| j                  |j                         d       d d d        y # 1 sw Y   y xY w)Nz)http://www.pythontest.net/index.html#fragr   r?   r;   r<   Requestrg   assertEqualgeturl)r    urlwith_fragreqress       r   test_urlwithfragz"OtherNetworkTests.test_urlwithfrag   sp    B--l; 	A..((6C..((-CSZZ\?A	A 	A 	A   AB  B	c                    d}t        j                  |      5  t        j                  j	                  |      }t        j                  j                  |      }| j                  |j                         d       d d d        y # 1 sw Y   y xY w)Nz*http://www.pythontest.net/redir/with_frag/z)http://www.pythontest.net/elsewhere/#fragrk   )r    redirect_url_with_fragrp   rq   s       r   test_redirect_url_withfragz,OtherNetworkTests.test_redirect_url_withfrag   ss    !M--.DE 	A..(()?@C..((-CSZZ\?A	A 	A 	Ars   c                 X   t         j                  }t        j                  |      5  t        j
                  j                         }t        j
                  j                  |      }| j                  |j                                |j                  |       | j                  |j                                | j                  |j                  d             |j                  dd       |j                  |       | j                  |j                  d      d       d d d        y # 1 sw Y   y xY w)Nz
User-agentz
User-Agentz
Test-Agent)r   r>   r   r?   r;   r<   build_openerrl   rB   header_itemsr_   rE   
has_header
add_headerrm   
get_header)r    rF   openerr<   s       r   test_custom_headersz%OtherNetworkTests.test_custom_headers   s    ##--c2 		L^^002Fnn,,S1GW1134KK OOG0023OOG..|<=|L9KK W//=lK		L 		L 		Ls   C1D  D)z XXX: http://www.imdb.com is gonec                    d}t        j                  |      5  	 t        j                  j	                  |      5 }	 d d d        | j                         t        j                  j	                  |      }|j                         }| j                  |       d d d        y # 1 sw Y   cxY w# t        $ r | j                  d       Y qw xY w# 1 sw Y   y xY w)Nzhttp://www.imdb.comzOurlopen failed for site not sending                            Connection:close)	r   r?   r;   r<   rg   rE   rf   failread)r    URLrq   rp   s       r   test_sites_no_connection_closez0OtherNetworkTests.test_sites_no_connection_close   s    
 $--c2 	!%^^++C0 C $..((-C((*COOC 	! 	!  .		 - ..		! 	!sE   C	B)BB)AC	B&	"B))CC	CC		Cc                    dd l }dd l}|j                  d      j                  }t	        j
                  j                  | j                  }|r$t        |t        j                  j                        }|D ]  }| j                  |      5  t        |t              r|\  }}	}
nd x}	}
t        j                  |      5  	  |||	t         j"                        }	 t$        5  t&        5  t(        5  |j+                         } |dt-        |      z         d d d        d d d        d d d        |j7                          d d d        |j?                  d       d d d         y # 1 sw Y   MxY w# 1 sw Y   QxY w# 1 sw Y   UxY w# t.        $ r! t1        d|z  t2        j4                         Y w xY w# t8        $ r;}|
r.d|
d|d	|	d
t;        |      d|
}| j=                  ||
|       n Y d }~d }~ww xY w# 1 sw Y   xY w# 1 sw Y   xY w)Nr   test_urllib2)rF   zread %d bytesz<timeout: %s>)filezDidn't get expected error(s) z for  z, got z: g?) timerM   rN   debugr;   r<   rx   r_   r   rU   rV   subTest
isinstancetupler   r?   r   INTERNET_TIMEOUTtime_outsocket_peer_resetioerror_peer_resetr   lenTimeoutErrorprintsysstderrrD   OSErrortypeassertIsInstancesleep)r    rY   handlersr]   r   rM   r   rg   rF   rp   expected_errrh   buferrmsgs                  r   rW   zOtherNetworkTests._test_urls   s   !!.177..--x8==-gv||7L7LMG 	 C#&  c5)-0*Cl)--C,"55c: ""#Cg.F.FGJ!) B!2B!3B '(ffh %oC&@ A	B B B 	)"* 

37   	 (B B B B B B
  , J!/C"7cjjIJ # "'$0#sDIs$LC 11#|SI! J	"	" "   s   1G<8G0:F)E<E0	 E$'%EE$E0	E<$G04G<E!E$$E-)E0	0E95E<<'F&	#G0%F&	&G0)	G-	21G(	#G0(G-	-G00G95G<<H	c                     g }t         j                  j                         }| j                  |j                         |j                  d       |j                  |       |S )N   )r;   r<   CacheFTPHandlerr:   clear_cache
setTimeoutappend)r    r   cfhs      r   rX   z!OtherNetworkTests._extra_handlers  sF    nn,,.(qr   N)T)r0   r1   r2   rR   r   requires_resourcerZ   ri   rr   rv   r~   unittestskipr   rW   rX   r   r   r   rK   rK   ~   s    7 Wz*6 +6XZA Wz*A +AL X]]56! 7!(% Nr   rK   c                       e Zd Zd Zd Zd Zd Zd ZdZ e	j                  d      d        Zd	 Z e	j                  d      d
        Z e	j                  d      d        Zy)TimeoutTestc                 V    | j                  t        j                  j                         y r   )r:   r;   r<   r=   r#   s    r   rR   zTimeoutTest.setUp&  s    112r   c                    | j                  t        j                                t        j                  }t        j                  |d       5  t        |      }| j                  |j                         | j                  |j                  j                  j                  j                                d d d        y # 1 sw Y   y xY wNtimeout)assertIsNonesocketgetdefaulttimeoutr   r>   r   r?   r@   r:   rD   rA   raw_sock
gettimeoutr    rF   us      r   test_http_basiczTimeoutTest.test_http_basic*  s    &2245##--c4@ 	;#C(AOOAGG$addhhnn779:	; 	; 	;s   A$B88Cc                    | j                  t        j                                t        j                  }t        j                  |      5  t        j                  d       	 t        |      }| j                  |j                         t        j                  d        | j                  |j                  j                  j                  j                         d       d d d        y # t        j                  d        w xY w# 1 sw Y   y xY wN<   )r   r   r   r   r>   r   r?   setdefaulttimeoutr@   r:   rD   rm   rA   r   r   r   r   s      r   test_http_default_timeoutz%TimeoutTest.test_http_default_timeout2  s    &2245##--c2 	>$$R(/',(((.QTTXX^^668"=	> 	> ((.	> 	>s%   	C< &C"AC<"C99C<<Dc                    | j                  t        j                                t        j                  }t        j                  |      5  t        j                  d       	 t        |d       }| j                  |j                         t        j                  d        | j                  |j                  j                  j                  j                                d d d        y # t        j                  d        w xY w# 1 sw Y   y xY wNr   r   )r   r   r   r   r>   r   r?   r   r@   r:   rD   rA   r   r   r   r   s      r   test_http_no_timeoutz TimeoutTest.test_http_no_timeout>  s    &2245##--c2 	;$$R(/'T:(((.addhhnn779:	; 	; ((.	; 	;s%   	C= (C#AC=#C::C==Dc                 D   t         j                  }t        j                  |      5  t	        |d      }| j                  |j                         | j                  |j                  j                  j                  j                         d       d d d        y # 1 sw Y   y xY w)Nx   r   )r   r>   r   r?   r@   r:   rD   rm   rA   r   r   r   r   s      r   test_http_timeoutzTimeoutTest.test_http_timeoutJ  sq    ##--c2 	?#C5AOOAGG$QTTXX^^668#>	? 	? 	?s   A'BBzftp://www.pythontest.net/rS   c                    | j                  t        j                                t        j                  | j
                  d       5  t        | j
                        }| j                  |j                         | j                  |j                  j                  j                  j                  j                                d d d        y # 1 sw Y   y xY wr   )r   r   r   r   r?   FTP_HOSTr@   r:   rD   rA   r   r   r   r    r   s     r   test_ftp_basiczTimeoutTest.test_ftp_basicS  s    &2245--dmmTJ 	>#DMM2AOOAGG$addggkk//::<=	> 	> 	>s   A8CCc                 ,   | j                  t        j                                t        j                  | j
                        5  t        j                  d       	 t        | j
                        }| j                  |j                         t        j                  d        | j                  |j                  j                  j                  j                  j                         d       d d d        y # t        j                  d        w xY w# 1 sw Y   y xY wr   )r   r   r   r   r?   r   r   r@   r:   rD   rm   rA   r   r   r   r   s     r   test_ftp_default_timeoutz$TimeoutTest.test_ftp_default_timeout[  s    &2245--dmm< 	A$$R(/'6(((.QTTWW[[..99;R@	A 	A ((.	A 	As%   D
0C0
AD
0DD

Dc                 .   | j                  t        j                                t        j                  | j
                        5  t        j                  d       	 t        | j
                  d       }| j                  |j                         t        j                  d        | j                  |j                  j                  j                  j                  j                                d d d        y # t        j                  d        w xY w# 1 sw Y   y xY wr   )r   r   r   r   r?   r   r   r@   r:   rD   rA   r   r   r   r   s     r   test_ftp_no_timeoutzTimeoutTest.test_ftp_no_timeoutf  s    &2245--dmm< 	>$$R(/'tD(((.addggkk//::<=	> 	> ((.	> 	>s%   D2C1AD1DDDc                 `   t        j                  | j                        5  t        | j                  d      }| j	                  |j
                         | j                  |j                  j                  j                  j                  j                         d       d d d        y # 1 sw Y   y xY wr   )r   r?   r   r@   r:   rD   rm   rA   r   r   r   r   s     r   test_ftp_timeoutzTimeoutTest.test_ftp_timeoutr  sx    --dmm< 	A#DMM2>AOOAGG$QTTWW[[..99;R@	A 	A 	As   A;B$$B-N)r0   r1   r2   rR   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   %  s    3;
>
;? +HWz*> +>	A Wz*	> +	> Wz*A +Ar   r   __main__)'r4   r   testr   test.supportr   r   r   test.test_urllib2r   ra   r   urllib.errorr;   urllib.requestr   requiresr   r   r<   rg   rU   rV   r@   objectr   r   	ETIMEDOUTr   
ECONNRESETr   r   TestCaser6   r8   rK   r   r0   mainr   r   r   <module>r      s      " & ' . 	    
     .fnn.D.D.4ll.C.CE N N8 WEOO<%gU5E5EF &we6F6FG 1!! 1N)h'' ) d)) dNRA(## RAj zHMMO r   