a zed@sfddlZddlZddlmZddlmZddlmZmZmZmZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZddlZddlmZddlmZejjredddlZdadd Zd d ZGd d d ejZ Gddde!Z"GdddejZ#GdddejZ$e%ej&dkdGdddejZ'e(dkrbe)dS)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@seZdZddZddZddZddZd d Zd d Zd e fddZ ddZ ddZ ddZ ddZddZddZeejdddZdd Zd!d"Zd#d$Zd%d&Zd'S)(HelperFunctionsTestscCs@tjdd|_tj|_tj|_tj|_ t j |_ t t j |_dSr)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 |jdSr)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 r/).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)Nz%s not in sys.modulesr) r:importedr modulesr r/ good_dir_pathr assertFalserr6 bad_dir_pathr)pth_filerrrpth_file_testsis  z#HelperFunctionsTests.pth_file_testscCsTt}|jddz2|t|j|jt||W|n |0dSNT)prep) PthFilecleanupcreater addpackagebase_dirfilenamesetrDrBrrrtest_addpackageps  z$HelperFunctionsTests.test_addpackage.csttj|}|d}tj||tddd,}|fdd||Wdn1sb0Y||fS)N.pthwutf-8)encodingcs tSr)rremoverpth_fnrrz/HelperFunctionsTests.make_pth..)rr r1r.open addCleanupwrite)r)contentspth_dirZpth_nameZ pth_basenamerCrrUrmake_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 rJrMZ assertRegexgetvaluereescaperr r.r)r]rVerr_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_ModuleNotFoundErrorrarerrrtest_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^rr@r rJrMZmaxDiffr0rbr r isinstancestr assertNotIn)r)r]rVrfr rrr#test_addpackage_import_bad_pth_files4  z8HelperFunctionsTests.test_addpackage_import_bad_pth_filecCsPt}|jddz.|t|jt||W|n |0dSrE)rGrHrIr rrKrMrDrBrrrtest_addsitedirs  z$HelperFunctionsTests.test_addsitedircCs|ttdSr)r0r _getuserbaser$r(rrrtest__getuserbasesz&HelperFunctionsTests.test__getuserbasecCsBtjdkrtjrd}n tjd}|ttt d|dS)NdarwinZosx_framework_user_userZpurelib) r platform _frameworkrnamer0r _get_pathrpr$Zget_path)r)schemerrr test_get_paths   z"HelperFunctionsTests.test_get_pathz.r~APPDATA~z os.path.isdirF)Z return_valuerrT)r rrrrZpatchrrrrrrobjectrZ swap_attrrMaddusersitepackagesZassert_called_once_withZassert_not_calledr@)r)rrrZ mock_isdirZmock_addsitedir known_pathsrrrtest_no_home_directory%s0N    z+HelperFunctionsTests.test_no_home_directoryN)__name__ __module__ __qualname__r*r-r4r<rDrNrr^rgrirnrorqryr skipUnlessr rrrrrrrrrrr;s&        ' rc@s0eZdZedddfddZddZd d d Zd S) rGtimeZ __testdir__Z__badcCsj|d|_tjd|_tj|j|j|_||_||_||_ tj|j|j|_ tj|j|j |_ dS)NrPrj) rLrr r1rKr. file_pathr= good_dirname bad_dirnamer?rA)r) filename_baser=rrrrr__init__Js zPthFile.__init__cCszt|jd}zRtd|dtd|dtd|j|dt|j|dt|j|dW|n |0t|j dS)NrQz#import @bad module namefile z import %s) rYrprintr=rrclosermkdirr?)r)FILErrrrIVs   zPthFile.createFcCstj|jrt|j|r@tj|j|_ |j rTtj|j=n|j rT|j tj|j<tj|j rnt |j tj|j rt |j dSr) rr r6rrTr r>getr=Zimported_moduler?rmdirrA)r)rFrrrrHls   zPthFile.cleanupN)F)rrrrrrIrHrrrrrGGs  rGc@seZdZddZddZddZddZed d d Z d d Z ddZ ddZ ddZ ddZejdejjeeejddddZdS)ImportSideEffectTestscCstjdd|_dSr)r r rr(rrrr*szImportSideEffectTests.setUpcCs|jtjdd<dSr)rr r r(rrrr-szImportSideEffectTests.tearDowncCs*dtjd_t|tjdjdS)Ntest)r r> __cached__r abs_pathsZ assertIsNoner(rrrtest_abs_paths_cached_Nones z0ImportSideEffectTests.test_abs_paths_cached_NonecCs4tt}tjD]}|||||qdSr)r removeduppathsrMr r rmadd)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)Nwin32rr5Zcpmbcszdid not alias mbcs) r rtlocalegetdefaultlocaler 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)Nr5ZHEAD)methodz Can't find )msg)r_Printer__dataspliturllibrequestZRequestrZ urlcleanuprZtransient_interneturlopenZgetcodeerror HTTPErrorcoder0)r)urlZreqdatarerrrtest_license_exists_at_urls Hz0ImportSideEffectTests.test_license_exists_at_urlN)rrrr*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-Irzz!import sys; print(repr(sys.path))rR)stdoutrSrz*.pthzfound z .pth files in: z-vz#import sys; print(set(sys.modules)))rstderrrSr > sre_compile_srerc sre_parse sre_constantsrcopyreg> typesheapqreprlib functools collections itertoolskeyword _collectionsoperatorweakref)rPopenr rPIPE communicater0 returncodereprevalglobrr r.rdZskipTestrr:r@ intersectionrm differencebuiltin_module_names) r)popenrZisolated_pathsr Z pth_filesrr>Zre_modsZcollection_modsrrrtest_startup_importss@    z'StartupImportTests.test_startup_importscCs&ttjddg}||ddS)Nrz9import sys; sys.exit(hasattr(sys, "__interactivehook__"))z''__interactivehook__' not added by siterrr rwaitrr)rrrrtest_startup_interactivehook s  z/StartupImportTests.test_startup_interactivehookcCs(ttjdddg}||ddS)Nrrzrz,'__interactivehook__' added in isolated mode)rrr rrr@rrrr%test_startup_interactivehook_isolateds  z8StartupImportTests.test_startup_interactivehook_isolatedcCs(ttjdddg}||ddS)NrrzzYimport site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))z6'__interactivehook__' not added by enablerlcompleter()rrrrr.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)Nrr5z._pthrQr)_winapitempfilemkdtemprZrrrmtreerr r.rr rZGetModuleFileNameZ dllhandleshutilr splitextrYr) r)linesexe_pthr Ztemp_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 r1r.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|]}qSrrr7_Zlibpathrrr8BrXz;_pthFileTests.test_underpth_nosite_file..rrj # commentfrom-env PYTHONPATH{};{}PATHrzzCimport sys; print("\n".join(sys.path) if sys.flags.no_site else "")ansi)r|rSrzsys.flags.no_site was Falsesys.path is incorrect)rr dirnamer__file__r rrangerrrr formatgetenvr check_outputrstriprrr0)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|]}qSrrrr rrr8^rXz4_pthFileTests.test_underpth_file..rrjr! import siter"r#r$r%rzimport 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)r/rrr|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|]}qSrrrr rrr8wrXz8_pthFileTests.test_underpth_dll_file..rrjr!r2F)rr"r#r$r%rzr3r{r'r4r5rr rtest_underpth_dll_filers<    z$_pthFileTests.test_underpth_dll_fileN)T)rrrrrr1r6r7rrrrr s   r __main__)*rZ test.supportrrrrrrrrrrrrcrrr r$rZ urllib.errorrZurllib.requestrr flagsno_siterr r rrZTestCaserrrGrrrrtr rmainrrrrsD     7]Dk