o
    gfh                     @   s  	 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m	Z	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Zd dlZd dl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 d dlmZ ejjrte d	d dlZejduZ da!d
d Z"dd Z#G dd de j$Z%G dd de&Z'G dd de j$Z(G dd de j$Z)e *ej+dkdG dd de j$Z,e-dkre .  dS dS )    N)support)	os_helper)socket_helper)captured_stderr)TESTFNEnvironmentVarGuard
change_cwd)mock)copyzPython was invoked with -Sc               
   C   sv   t jd d  atjr7tjtjs9zttj t	tj W d S  t
y6 }  z
tdtj| f d } ~ ww d S d S )Nz-unable to create user site directory (%r): %s)syspathOLD_SYS_PATHsiteENABLE_USER_SITEosisdir	USER_SITEmakedirs
addsitedirPermissionErrorunittestSkipTest)exc r   D/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_site.pysetUpModule+   s   r   c                   C   s   t tjd d < d S N)r   r   r   r   r   r   r   tearDownModule:   s   r   c                   @   s   e Zd Z	 dd Zdd Zdd Zdd Zd	d
 Zdd Zde	fddZ
dd Zdd Zdd Zdd Zdd Zdd Zeeddd Zeejdd d! Zeedd"d# Zeedd$d% Zd&d' Zeedd(d) Zd*d+ Zd,S )-HelperFunctionsTestsc                 C   sB   	 t jd d  | _tj| _tj| _tj| _	t
j| _tt
j| _d S r   )r   r   sys_pathr   	USER_BASEold_baser   old_sitePREFIXESold_prefixes	sysconfig_CONFIG_VARSoriginal_varsr
   old_varsselfr   r   r   setUpB   s   zHelperFunctionsTests.setUpc                 C   s\   	 | j tjd d < | jt_| jt_| jt_	| j
t_tjd ur,tj  tj| j d S d S r   )r   r   r   r!   r   r    r"   r   r$   r#   r'   r%   r&   clearupdater(   r)   r   r   r   tearDownK   s   

zHelperFunctionsTests.tearDownc                 C   sj   d}t jj| }tj| \}}| t j|| |t j|kr)| || d S | t j|| d S )N)Z	BeginningZEnd)r   r   joinr   makepathassertEqualabspathnormcase)r*   
path_partsZoriginal_dirZabs_dirZnorm_dirr   r   r   test_makepathW   s   z"HelperFunctionsTests.test_makepathc                 C   s8   t  }dd tjD D ]}| ||d||f  qd S )Nc                 S   s*   g | ]}|rt j|rt|d  qS )   )r   r   existsr   r0   ).0r   r   r   r   
<listcomp>f   s
    
z;HelperFunctionsTests.test_init_pathinfo.<locals>.<listcomp>zB%s from sys.path not found in set returned by _init_pathinfo(): %s)r   _init_pathinfor   r   assertIn)r*   Zdir_setentryr   r   r   test_init_pathinfod   s   z'HelperFunctionsTests.test_init_pathinfoc                 C   sL   	 |  |jtjd|j  |  t|jd tj | t	j
|j d S )Nz%s not in sys.modulesr   )r;   importedr   modulesr   r0   good_dir_pathr   assertFalser   r7   bad_dir_pathr*   pth_filer   r   r   pth_file_testsl   s   z#HelperFunctionsTests.pth_file_testsc                 C   sR   t  }|jdd z|  t|j|jt  | | W |  d S |  w NT)prep)	PthFilecleanupcreater   
addpackagebase_dirfilenamesetrE   rC   r   r   r   test_addpackages   s   z$HelperFunctionsTests.test_addpackage.c                    sz   t j|}|d }t j|| t ddd}|  fdd || W d    ||fS 1 s4w   Y  ||fS )N.pthwutf-8)encodingc                      s
   t  S r   )r   remover   pth_fnr   r   <lambda>   s   
 z/HelperFunctionsTests.make_pth.<locals>.<lambda>)r   r   r2   r/   open
addCleanupwrite)r*   contentspth_dirZpth_nameZpth_basenamerD   r   rV   r   make_pth   s   
zHelperFunctionsTests.make_pthc              	   C   s   |  d\}}t }t||t  W d    n1 sw   Y  | | d | | tt	j
|| | | d | | d | | d d S )Nzimport bad-syntax
zline 1	Tracebackzimport bad-syntaxSyntaxErrorr^   r   r   rK   rN   ZassertRegexgetvaluereescaper   r   r/   r*   r]   rW   err_outr   r   r   !test_addpackage_import_bad_syntax   s   
z6HelperFunctionsTests.test_addpackage_import_bad_syntaxc              	   C   s   |  d\}}t }t||t  W d    n1 sw   Y  | | d | | tt	j
|| | | d | | d d S )Nzrandompath
import nosuchmodule
zline 2r_   ModuleNotFoundErrorra   re   r   r   r   test_addpackage_import_bad_exec   s   
z4HelperFunctionsTests.test_addpackage_import_bad_execc                 C   s0   |  d\}}t||t }| |t  d S )Nz

  

)r^   r   rK   rN   r1   )r*   r]   rW   known_pathsr   r   r   test_addpackage_empty_lines   s   z0HelperFunctionsTests.test_addpackage_empty_linesc                 C   s   |  d\}}t }| t||t  W d    n1 s w   Y  d | _| | d t	j
D ]}t|tr@| d| q3d S )Nzabc def
 zabc def)r^   r   rA   r   rK   rN   ZmaxDiffr1   rb   r   r   
isinstancestrassertNotIn)r*   r]   rW   rf   r   r   r   r   #test_addpackage_import_bad_pth_file   s   

z8HelperFunctionsTests.test_addpackage_import_bad_pth_filec                 C   sN   t  }|jdd z|  t|jt  | | W |  d S |  w rF   )rH   rI   rJ   r   r   rL   rN   rE   rC   r   r   r   test_addsitedir   s   z$HelperFunctionsTests.test_addsitedirc                 C   s   |  t t  d S r   )r1   r   _getuserbaser%   r)   r   r   r   test__getuserbase   s   z&HelperFunctionsTests.test__getuserbasezneed user sitec                 C   sB   t jdkrt jrd}ntjd }| tt t	
d| d S )NdarwinZosx_framework_user_userZpurelib)r   platform
_frameworkr   namer1   r   	_get_pathrr   r%   Zget_path)r*   schemer   r   r   test_get_path   s   

z"HelperFunctionsTests.test_get_pathz<requires access to PEP 370 user-site (site.ENABLE_USER_SITE)c                 C   s(  t j}| |tj tj }tj	tj
dd| g|d}| |d tj }tj	tj
ddd| g|d}|t  d krD| |d n| |dd tj }d|d	< tj	tj
dd| g|d}|t  d krp| |d n| |dd
 tj }d|d< tj	tj
ddg|d}| |dd d S )N-cz$import sys; sys.exit(%r in sys.path)envr6   z-sr   z%User site still added to path with -s1ZPYTHONNOUSERSITEz3User site still added to path with PYTHONNOUSERSITEz/tmpPYTHONUSERBASEz=import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))z#User base not set by PYTHONUSERBASE)r   r   r;   r   r   r   environr
   
subprocesscall
executabler1   getsitepackages)r*   Zusersiter~   rcr   r   r   test_s_option   sP   






z"HelperFunctionsTests.test_s_optionc                 C   s~   d t _t  }| t j| d t _dd l}d |_t }d|d< | t  dt   W d    d S 1 s8w   Y  d S )Nr   xoxor   )	r   r    getuserbaser1   r%   r&   r   
assertTrue
startswith)r*   	user_baser%   r   r   r   r   test_getuserbase   s   "z%HelperFunctionsTests.test_getuserbasec                 C   sL   d t _d t _t  }| t j| | |t j| | t jt   d S r   )r   r   r    getusersitepackagesr1   r   r   r   )r*   	user_siter   r   r   test_getusersitepackages  s   z-HelperFunctionsTests.test_getusersitepackagesc                 C   s   dgt _t  }tjdkrVtjdkr4| t|d tj	
dtjdtjd d  d}| |d | n| t|d tj	
dddtjd d  d}| |d	 | d S | t|d | |d d tj	
ddd}| |d | d S )
Nr   /lib   zpython%d.%dzsite-packagesr   r6   )r   r#   r   r   sepr   
platlibdirr1   lenr   r/   version_info)r*   dirsZwantedr   r   r   test_getsitepackages  s(   


z)HelperFunctionsTests.test_getsitepackagesc              
   C   st  d t _d t _t C}tddd + |d= |d= t  }| |dt	j
 | t  }| ||| W d    n1 s@w   Y  W d    n1 sOw   Y  tjddd	U}tjt d
5}tt dd t }t | || |  | | W d    n1 sw   Y  W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nzos.path.expanduserc                 S   s   | S r   r   )r   r   r   r   rX   :  s    z=HelperFunctionsTests.test_no_home_directory.<locals>.<lambda>r   APPDATA~zos.path.isdirF)Zreturn_valuer   r   T)r   r   r    r   r	   patchr   r   r   r   r   r   objectr   Z	swap_attrrN   addusersitepackagesZassert_called_once_withZassert_not_calledrA   )r*   r   r   r   Z
mock_isdirZmock_addsitedirrj   r   r   r   test_no_home_directory1  s<    

P z+HelperFunctionsTests.test_no_home_directoryc              
   C   s   d}d|d fdfD ]D\}}t dt j|d. t dt  t| | tj	
 | W d    n1 s:w   Y  W d    q
1 sIw   Y  q
d S )Nzbla-bla-blaT
)Frl   z	sys.flags)verbosez
sys.stderr)r	   r   ZMockioStringIOr   _tracer1   r   stderrrb   )r*   messager   outr   r   r   
test_traceS  s   
 zHelperFunctionsTests.test_traceN)__name__
__module____qualname__r+   r.   r5   r=   rE   rO   r   r^   rg   ri   rk   rp   rq   rs   r   
skipUnlessHAS_USER_SITEr{   r   r   r   r   r   r   r   r   r   r   r   r   r   >   s6    	




'





!r   c                   @   s2   e Zd Z	 edddfddZdd Zdd	d
ZdS )rH   timeZ__testdir__Z__badc                 C   sl   	 |d | _ tjd| _tj| j| j | _|| _|| _|| _	tj| j| j| _
tj| j| j	| _d S )NrQ   rl   )rM   r   r   r2   rL   r/   	file_pathr>   good_dirnamebad_dirnamer@   rB   )r*   filename_baser>   r   r   r   r   r   __init___  s   
zPthFile.__init__c                 C   s|   	 t | jd}z)td|d td|d td| j |d t| j|d t| j|d W |  n|  w t| j	 d S )NrR   z#import @bad module namefiler   z	import %s)
rY   r   printr>   r   r   closer   mkdirr@   )r*   FILEr   r   r   rJ   k  s   
zPthFile.createFc                 C   s   	 t j| jrt | j |r!tj| j| _	| j	r tj| j= n
| j	r+| j	tj| j< t j| j
r8t | j
 t j| jrGt | j d S d S r   )r   r   r7   r   rU   r   r?   getr>   Zimported_moduler@   rmdirrB   )r*   rG   r   r   r   rI     s   
zPthFile.cleanupN)F)r   r   r   r   r   rJ   rI   r   r   r   r   rH   \  s    
rH   c                   @   s   e Zd Z	 dd Zdd Zdd Zdd Zed	d
d Z	dd Z
dd Zdd Zdd Zdd Zejdejjeeejdddd ZdS )ImportSideEffectTestsc                 C   s   	 t jd d  | _d S r   )r   r   r   r)   r   r   r   r+        zImportSideEffectTests.setUpc                 C   s   	 | j tjd d < d S r   )r   r   r   r)   r   r   r   r.     r   zImportSideEffectTests.tearDownc                 C   s,   	 d t jd _t  | t jd j d S )Ntest)r   r?   
__cached__r   	abs_pathsZassertIsNoner)   r   r   r   test_abs_paths_cached_None  s   z0ImportSideEffectTests.test_abs_paths_cached_Nonec                 C   s4   t   t }tjD ]}| || || q
d S r   )r   removeduppathsrN   r   r   ro   add)r*   Z
seen_pathsr   r   r   r   test_no_duplicate_paths  s   
z-ImportSideEffectTests.test_no_duplicate_pathsztest not implementedc                 C   s   d S r   r   r)   r   r   r   test_add_build_dir  s   z(ImportSideEffectTests.test_add_build_dirc                 C   s$   |  ttd |  ttd d S )Nquitexitr   hasattrbuiltinsr)   r   r   r   test_setting_quit  s   z'ImportSideEffectTests.test_setting_quitc                 C   s4   |  ttd |  ttd |  ttd d S )N	copyrightcreditslicenser   r)   r   r   r   test_setting_copyright  s   z,ImportSideEffectTests.test_setting_copyrightc                 C   s   |  ttd d S )Nhelpr   r)   r   r   r   test_setting_help  s   z'ImportSideEffectTests.test_setting_helpc                 C   sZ   t jdkr)dd l}| d dr+tjj D ]	}|dkr! d S q| d d S d S d S )Nwin32r   r6   Zcpmbcszdid not alias mbcs)	r   rv   localeZgetdefaultlocaler   	encodingsaliasesvaluesfail)r*   r   valuer   r   r   test_aliasing_mbcs  s   
z(ImportSideEffectTests.test_aliasing_mbcsc                 C   s>   dt jvrzdd l}W n
 ty   Y d S w | d d S d S )Nsitecustomizer   z(sitecustomize not imported automatically)r   r?   r   ImportErrorr   )r*   r   r   r   r   test_sitecustomize_executed  s   
z1ImportSideEffectTests.test_sitecustomize_executedZnetworkZHTTPSHandlerz$need SSL support to download licensec              
   C   s   t j d }tjj|dd}| tjj z1t	|" tj
|}| }W d    n1 s2w   Y  W d    n1 sAw   Y  W n tjjy^ } z	|j}W Y d }~nd }~ww | j|dd| d d S )Nr6   ZHEAD)method   zCan't find )msg)r   _Printer__dataspliturllibrequestZRequestrZ   
urlcleanupr   Ztransient_interneturlopenZgetcodeerror	HTTPErrorcoder1   )r*   urlZreqdatar   er   r   r   test_license_exists_at_url  s    
z0ImportSideEffectTests.test_license_exists_at_urlN)r   r   r   r+   r.   r   r   r   skipr   r   r   r   r   r   r   r   Zrequires_resourceZsystem_must_validate_certr   r   r   r   r   r   r   r   r   r     s&    	



r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
StartupImportTestsc           
      C   s8  t jtjdddgt jdd}| d }| |jdt| t	|}|D ]}t

tjt
|d}|rB| dt| d	|  q$t jtjdd
ddgt jt jdd}| \}}| |jd||f t	|}| d| h d}| ||| | d|| | d|| h dtj}	| ||	| d S )N-Ir|   z!import sys; print(repr(sys.path))rS   )stdoutrT   r   z*.pthzfound z .pth files in: z-vz#import sys; print(set(sys.modules)))r   r   rT   r   >   rc   sre_constants	sre_parsesre_compile_srer   copyreg>
   reprlibcollectionsheapq	itertoolsoperator_collectionsweakrefkeywordtypes	functools)r   Popenr   r   PIPEcommunicater1   
returncodereprevalglobr   r   r/   rd   ZskipTestr   r;   rA   intersectionro   
differencebuiltin_module_names)
r*   popenr   Zisolated_pathsr   Z	pth_filesr   r?   Zre_modsZcollection_modsr   r   r   test_startup_imports  sB   z'StartupImportTests.test_startup_importsc                 C   s&   t tjddg }| |d d S )Nr|   9import sys; sys.exit(hasattr(sys, "__interactivehook__"))z''__interactivehook__' not added by siter   r  r   r   waitr   r*   rr   r   r   test_startup_interactivehook"  s   
z/StartupImportTests.test_startup_interactivehookc                 C   (   t tjdddg }| |d d S )Nr   r|   r  z,'__interactivehook__' added in isolated mode)r   r  r   r   r  rA   r  r   r   r   %test_startup_interactivehook_isolated'     z8StartupImportTests.test_startup_interactivehook_isolatedc                 C   r  )Nr   r|   zYimport site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))z6'__interactivehook__' not added by enablerlcompleter()r  r  r   r   r   .test_startup_interactivehook_isolated_explicit-  r  zAStartupImportTests.test_startup_interactivehook_isolated_explicitN)r   r   r   r  r  r  r  r   r   r   r   r     s
    0r   r   zonly supported on Windowsc                   @   s6   e Zd ZdddZdd Zdd Zdd	 Zd
d ZdS )_pthFileTestsTc                 C   s   dd l }t }| tj| tj|tj	t
jd }|t
j}tj|tj	|d }tt
j| t|| |rJtj|d d }n
tj|d d }t|d}	|D ]}
t|
|	d q\W d    |S 1 spw   Y  |S )Nr   r6   z._pthrR   r   )_winapitempfilemkdtemprZ   r   rmtreer   r   r/   r   r   r   ZGetModuleFileNameZ	dllhandleshutilr
   splitextrY   r   )r*   linesexe_pthr  Ztemp_direxe_fileZdll_src_fileZdll_fileZ	_pth_filefliner   r   r   _create_underpth_exe6  s&   
z"_pthFileTests._create_underpth_exec                 C   sD   g }|D ]}|r|d dkrqt jt j||}|| q|S )Nr   #)r   r   r2   r/   append)r*   
sys_prefixr  r   r#  Zabs_pathr   r   r   "_calc_sys_path_for_underpth_nositeH  s   z0_pthFileTests._calc_sys_path_for_underpth_nositec                    s   t jt jtj t jtj}dg fddtdD dd}| |}| 	t j||}t j
 }d|d< d	|t d
|d
< tj|ddg|dd}| d}| |d | ||d d S )Nfake-path-namec                       g | ]} qS r   r   r8   _Zlibpathr   r   r9   V      z;_pthFileTests.test_underpth_nosite_file.<locals>.<listcomp>r   rl   	# commentfrom-env
PYTHONPATH{};{}PATHr|   zCimport sys; print("\n".join(sys.path) if sys.flags.no_site else "")ansi)r~   rT   r   zsys.flags.no_site was Falsesys.path is incorrect)r   r   dirnamer   __file__r   r   ranger$  r(  r   r
   formatgetenvr   check_outputrstripr   r   r1   )r*   
exe_prefixZ	pth_linesr!  r   r~   outputZactual_sys_pathr   r-  r   test_underpth_nosite_fileQ  s<   


z'_pthFileTests.test_underpth_nosite_filec              
      s   t jt jtj t jtj}| dg fddtdD ddd}t j|}t j	
 }d|d	< d
|t d|d< tj|ddt j|d t j|df g|d}| |d d S )Nr)  c                    r*  r   r   r+  r-  r   r   r9   r  r.  z4_pthFileTests.test_underpth_file.<locals>.<listcomp>r   rl   r/  import siter0  r1  r2  r3  r|   import sys; sys.exit(not sys.flags.no_site and %r in sys.path and %r in sys.path and %r not in sys.path and all("\r" not in p and "\n" not in p for p in sys.path))r}   r5  r   r   r6  r   r7  r   r   r$  r8  r   r
   r9  r:  r   r   r/   r   r*   r=  r!  r'  r~   r   r   r-  r   test_underpth_filem  s8   
z _pthFileTests.test_underpth_filec              
      s   t jt jtj t jtj}| jdg fddtdD ddddd	}t j|}t j	
 }d
|d< d|t d|d< tj|ddt j|d t j|d
f g|d}| |d d S )Nr)  c                    r*  r   r   r+  r-  r   r   r9     r.  z8_pthFileTests.test_underpth_dll_file.<locals>.<listcomp>r   rl   r/  r@  F)r   r0  r1  r2  r3  r|   rA  r}   r5  rB  rC  r   r-  r   test_underpth_dll_file  s<   
z$_pthFileTests.test_underpth_dll_fileN)T)r   r   r   r$  r(  r?  rD  rE  r   r   r   r   r  3  s    
	r  __main__)/r   Ztest.supportr   r   r   r   r   Ztest.support.os_helperr   r   r   r   r   r  r   r   rc   r  r   r   r%   r  Zurllib.errorr   Zurllib.requestr	   r
   flagsno_siter   r   r   r   r   r   r   ZTestCaser   r   rH   r   r   r   rv   r  r   mainr   r   r   r   <module>   sR    

   7]Ck