a zed@sjdZddlZddlZddlmZddlmZddlmZmZmZm Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZddlZddlZddlmZddlmZejjredddlZdad d Zd d ZGd ddej Z!Gddde"Z#Gdddej Z$Gdddej Z%e&ej'dkdGdddej Z(e)dkrfe*dS)zTests for 'site'. Tests assume the initial paths in sys.path once the interpreter has begun executing have not been removed. N)support) socket_helper)captured_stderrTESTFNEnvironmentVarGuard change_cwd)mock)copyzPython was invoked with -Sc Cs|tjddatjrxtjtjsxzttjt tjWn8t yv}z t dtj|fWYd}~n d}~00dS)Nz-unable to create user site directory (%r): %s) syspath OLD_SYS_PATHsiteENABLE_USER_SITEosisdir USER_SITEmakedirs addsitedirPermissionErrorunittestSkipTest)excrB/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_site.py setUpModule(s rcCsttjdd<dSN)r r r rrrrtearDownModule7src@seZdZdZddZddZddZdd Zd d Zd d Z de fddZ ddZ ddZ ddZddZddZddZeejdddZd d!Zd"d#Zd$d%Zd&d'Zd(S))HelperFunctionsTestsz Tests for helper functions. cCs@tjdd|_tj|_tj|_tj|_ t j |_ t t j |_dS)zSave a copy of sys.pathN)r r sys_pathr USER_BASEold_baserold_sitePREFIXES old_prefixes sysconfig _CONFIG_VARS original_varsr old_varsselfrrrsetUp?s zHelperFunctionsTests.setUpcCsV|jtjdd<|jt_|jt_|jt_ |j t _ t j durRt j t j |jdSzRestore sys.pathN)rr r r r rr!rr#r"r&r$r%clearupdater'r(rrrtearDownHs  zHelperFunctionsTests.tearDowncCshd}tjj|}tj|\}}|tj|||tj|krP|||n|tj||dS)N)Z BeginningZEnd)rr joinr makepath assertEqualabspathnormcase)r) path_partsZ original_dirZabs_dirZnorm_dirrrr test_makepathTs z"HelperFunctionsTests.test_makepathcCs8t}ddtjDD]}|||d||fqdS)NcSs*g|]"}|rtj|rt|dqS))rr existsr r0).0r rrr csz;HelperFunctionsTests.test_init_pathinfo..zB%s from sys.path not found in set returned by _init_pathinfo(): %s)r _init_pathinfor r assertIn)r)Zdir_setentryrrrtest_init_pathinfoasz'HelperFunctionsTests.test_init_pathinfocCsJ||jtjd|j|t|jdtj|t j |j dS)z>Contain common code for testing results of reading a .pth filez%s not in sys.modulesrN) r;importedr modulesr r0 good_dir_pathr assertFalserr7 bad_dir_pathr)pth_filerrrpth_file_testsis  z#HelperFunctionsTests.pth_file_testscCsTt}|jddz2|t|j|jt||W|n |0dSNT)prep) PthFilecleanupcreater addpackagebase_dirfilenamesetrErCrrrtest_addpackageps  z$HelperFunctionsTests.test_addpackage.csttj|}|d}tj||tddd,}|fdd||Wdn1sb0Y||fS)N.pthwutf-8)encodingcs tSr)rremoverpth_fnrrz/HelperFunctionsTests.make_pth..)rr r2r/open addCleanupwrite)r)contentspth_dirZpth_nameZ pth_basenamerDrrVrmake_pths (zHelperFunctionsTests.make_pthc Cs|d\}}t }t||tWdn1s:0Y||d||tt j ||||d||d||ddS)Nzimport bad-syntax zline 1 Tracebackzimport bad-syntax SyntaxError r_rr rKrNZ assertRegexgetvaluereescaperr r/r)r^rWerr_outrrr!test_addpackage_import_bad_syntaxs. z6HelperFunctionsTests.test_addpackage_import_bad_syntaxc Cs|d\}}t }t||tWdn1s:0Y||d||tt j ||||d||ddS)Nzrandompath import nosuchmodule zline 2r`ModuleNotFoundErrorrbrfrrrtest_addpackage_import_bad_execs. z4HelperFunctionsTests.test_addpackage_import_bad_execcCs|d\}}t&}|t||tWdn1s@0Yd|_||dt j D]}t |t rf| d|qfdS)Nzabcdef zabcdef)r_rrAr rKrNZmaxDiffr1rcr r isinstancestr assertNotIn)r)r^rWrgr rrr#test_addpackage_import_bad_pth_files4  z8HelperFunctionsTests.test_addpackage_import_bad_pth_filecCsPt}|jddz.|t|jt||W|n |0dSrF)rHrIrJr rrLrNrErCrrrtest_addsitedirs  z$HelperFunctionsTests.test_addsitedircCs|ttdSr)r1r _getuserbaser$r(rrrtest__getuserbasesz&HelperFunctionsTests.test__getuserbasecCsBtjdkrtjrd}n tjd}|ttt d|dS)NdarwinZosx_framework_user_userZpurelib) r platform _frameworkrnamer1r _get_pathrqr$Zget_path)r)schemerrr test_get_paths   z"HelperFunctionsTests.test_get_pathz.rAPPDATA~z os.path.isdirF)Z return_valuerrT)r rrrrZpatchrrrrrrobjectrZ swap_attrrNaddusersitepackagesZassert_called_once_withZassert_not_calledrA)r)rrrZ mock_isdirZmock_addsitedir known_pathsrrrtest_no_home_directory%s0N    z+HelperFunctionsTests.test_no_home_directoryN)__name__ __module__ __qualname____doc__r*r.r5r=rErOrr_rhrjrorprrrzr skipUnlessr rrrrrrrrrrr;s(        ' rc@s4eZdZdZedddfddZddZd d d Zd S)rHz/Helper class for handling testing of .pth filestimeZ __testdir__Z__badcCsj|d|_tjd|_tj|j|j|_||_||_||_ tj|j|j|_ tj|j|j |_ dS)zInitialize instance variablesrQrkN) rMrr r2rLr/ file_pathr> good_dirname bad_dirnamer@rB)r) filename_baser>rrrrr__init__Js zPthFile.__init__cCszt|jd}zRtd|dtd|dtd|j|dt|j|dt|j|dW|n |0t|j dS)aoCreate a .pth file with a comment, blank lines, an ``import ``, a line with self.good_dirname, and a line with self.bad_dirname. Creation of the directory for self.good_dir_path (based off of self.good_dirname) is also performed. Make sure to call self.cleanup() to undo anything done by this method. rRz#import @bad module namefile z import %sN) rZrprintr>rrclosermkdirr@)r)FILErrrrJVs   zPthFile.createFcCstj|jrt|j|r@tj|j|_ |j rTtj|j=n|j rT|j tj|j<tj|j rnt |j tj|j rt |j dS)zMake sure that the .pth file is deleted, self.imported is not in sys.modules, and that both self.good_dirname and self.bad_dirname are not existing directories.N) rr r7rrUr r?getr>Zimported_moduler@rmdirrB)r)rGrrrrIls   zPthFile.cleanupN)F)rrrrrrrJrIrrrrrHGs  rHc@seZdZdZddZddZddZdd Ze d d d Z d dZ ddZ ddZ ddZddZejdejjeeejddddZdS)ImportSideEffectTestsz(Test side-effects from importing 'site'.cCstjdd|_dS)zMake a copy of sys.pathN)r r rr(rrrr*szImportSideEffectTests.setUpcCs|jtjdd<dSr+)rr r r(rrrr.szImportSideEffectTests.tearDowncCs*dtjd_t|tjdjdS)zTest for __cached__ is None. Regarding to PEP 3147, __cached__ can be None. See also: https://bugs.python.org/issue30167 Ntest)r r? __cached__r abs_pathsZ assertIsNoner(rrrtest_abs_paths_cached_Nones z0ImportSideEffectTests.test_abs_paths_cached_NonecCs4tt}tjD]}|||||qdSr)r removeduppathsrNr r rnadd)r)Z seen_pathsr rrrtest_no_duplicate_pathss   z-ImportSideEffectTests.test_no_duplicate_pathsztest not implementedcCsdSrrr(rrrtest_add_build_dirsz(ImportSideEffectTests.test_add_build_dircCs$|ttd|ttddS)Nquitexitrhasattrbuiltinsr(rrrtest_setting_quitsz'ImportSideEffectTests.test_setting_quitcCs4|ttd|ttd|ttddS)N copyrightcreditslicenserr(rrrtest_setting_copyrightsz,ImportSideEffectTests.test_setting_copyrightcCs|ttddS)Nhelprr(rrrtest_setting_helpsz'ImportSideEffectTests.test_setting_helpcCsPtjdkrLddl}|ddrLtjjD]}|dkr0qLq0|ddS)Nwin32rr6Zcpmbcszdid not alias mbcs) r rulocalegetdefaultlocaler encodingsaliasesvaluesfail)r)rvaluerrrtest_aliasing_mbcss z(ImportSideEffectTests.test_aliasing_mbcscCs8dtjvr4z ddl}Wnty(Yn 0|ddS)N sitecustomizerz(sitecustomize not imported automatically)r r?r ImportErrorr)r)rrrrtest_sitecustomize_executeds    z1ImportSideEffectTests.test_sitecustomize_executedZnetworkZ HTTPSHandlerz$need SSL support to download licensec Cstjd}tjj|dd}|tjjzbt |Dtj |}| }Wdn1sd0YWdn1s0YWn.tj j y}z|j}WYd}~n d}~00|j|dd|ddS)Nr6ZHEAD)methodz Can't find )msg)r_Printer__dataspliturllibrequestZRequestr[ urlcleanuprZtransient_interneturlopenZgetcodeerror HTTPErrorcoder1)r)urlZreqdatarerrrtest_license_exists_at_urls Hz0ImportSideEffectTests.test_license_exists_at_urlN)rrrrr*r.rrrskiprrrrrrrrZrequires_resourceZsystem_must_validate_certrrrrrrrrrr~s$     rc@s,eZdZddZddZddZddZd S) StartupImportTestsc Cs8tjtjdddgtjdd}|d}||jdt|t |}|D]<}t t j t |d}|rH|dt|d |qHtjtjdd dd gtjtjdd }|\}}||jd||ft |}|d |hd}|||||d|||d||hdtj} ||| |dS)N-Ir{z!import sys; print(repr(sys.path))rS)stdoutrTrz*.pthzfound z .pth files in: z-vz#import sys; print(set(sys.modules)))rstderrrTr > sre_compile sre_constantsrd sre_parse_srercopyreg> itertoolstypesreprlibweakrefoperator collectionskeywordheapq functools _collections)rPopenr rPIPE communicater1 returncodereprevalglobrr r/reZskipTestrr;rA intersectionrn differencebuiltin_module_names) r)popenrZisolated_pathsr Z pth_filesrr?Zre_modsZcollection_modsrrrtest_startup_importss@    z'StartupImportTests.test_startup_importscCs&ttjddg}||ddS)Nr{9import sys; sys.exit(hasattr(sys, "__interactivehook__"))z''__interactivehook__' not added by siterrr rwaitrr)rrrrtest_startup_interactivehook s  z/StartupImportTests.test_startup_interactivehookcCs(ttjdddg}||ddS)Nrr{rz,'__interactivehook__' added in isolated mode)rrr rrrAr rrr%test_startup_interactivehook_isolateds  z8StartupImportTests.test_startup_interactivehook_isolatedcCs(ttjdddg}||ddS)Nrr{zYimport site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))z6'__interactivehook__' not added by enablerlcompleter()rr rrr.test_startup_interactivehook_isolated_explicits  zAStartupImportTests.test_startup_interactivehook_isolated_explicitN)rrrrr r r rrrrrs0rrzonly supported on Windowsc@s6eZdZd ddZddZddZdd Zd d Zd S) _pthFileTestsTc Csddl}t}|tjj|tj |tj t j d}| t j}tj |tj |d}tt j |t|||rtj|dd}ntj|dd}t|d&} |D]} t| | dqWdn1s0Y|S)Nrr6z._pthrRr)_winapitempfilemkdtempr[rrrmtreerr r/rr rZGetModuleFileNameZ dllhandleshutilr splitextrZr) r)linesexe_pthrZtemp_direxe_fileZ dll_src_fileZdll_fileZ _pth_fileflinerrr_create_underpth_exe"s   ,z"_pthFileTests._create_underpth_execCsDg}|D]6}|r|ddkrqtjtj||}||q|S)Nr#)rr r2r/append)r) sys_prefixrrrZabs_pathrrr"_calc_sys_path_for_underpth_nosite4s z0_pthFileTests._calc_sys_path_for_underpth_nositecstjtjtjtjtj}dgfddtdDdd}||}| tj||}tj }d|d<d |t d |d <tj|d d g|d d}|d}||d|||ddS)Nfake-path-namecsg|]}qSrrr8_Zlibpathrrr9BrYz;_pthFileTests.test_underpth_nosite_file..rrk # commentfrom-env PYTHONPATH{};{}PATHr{zCimport sys; print("\n".join(sys.path) if sys.flags.no_site else "")ansi)r}rTrzsys.flags.no_site was Falsesys.path is incorrect)rr dirnamer__file__r rrangerrrr formatgetenvr check_outputrstriprrr1)r) exe_prefixZ pth_linesrrr}outputZactual_sys_pathrr"rtest_underpth_nosite_file=s<    z'_pthFileTests.test_underpth_nosite_filec stjtjtjtjtj}|dgfddtdDddd}tj|}tj }d|d <d |t d |d <t j|d d tj|dtj|dfg|d}||ddS)Nrcsg|]}qSrrr r"rrr9^rYz4_pthFileTests.test_underpth_file..rrkr# import siter$r%r&r'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|r)rr r*rr+r rrr,rr r-r.rrr/rr)r1rrr}rrr"rtest_underpth_fileYs8    z _pthFileTests.test_underpth_filec stjtjtjtjtj}|jdgfddtdDddddd }tj|}tj }d |d <d |t d |d <t j|ddtj|dtj|d fg|d}||ddS)Nrcsg|]}qSrrr r"rrr9wrYz8_pthFileTests.test_underpth_dll_file..rrkr#r4F)rr$r%r&r'r{r5r|r)r6r7rr"rtest_underpth_dll_filers<    z$_pthFileTests.test_underpth_dll_fileN)T)rrrrrr3r8r9rrrrrs   r__main__)+rrZ test.supportrrrrrrrrrrrrdrrr r$rZ urllib.errorrZurllib.requestrr flagsno_siterr r rrZTestCaserrrHrrrrurrmainrrrrsF     7]Dk