PK!Buxx)__pycache__/__init__.cpython-36.opt-1.pycnu[3 \/@sdS)Nrrr%/usr/lib64/python3.6/test/__init__.pysPK!Buxx)__pycache__/__init__.cpython-36.opt-2.pycnu[3 \/@sdS)Nrrr%/usr/lib64/python3.6/test/__init__.pysPK!Buxx#__pycache__/__init__.cpython-36.pycnu[3 \/@sdS)Nrrr%/usr/lib64/python3.6/test/__init__.pysPK!)-=-=1support/__pycache__/__init__.cpython-36.opt-1.pycnu[3 f@s^ dZedkredddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZ ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl!Z"ddl#Z#ddl$m%Z%yddl&Z&ddl'Z'Wnek rBdZ&dZ'YnXy ddl(Z)Wnek rjdZ)YnXy ddl*Z*Wnek rdZ*YnXy ddl+Z+Wnek rdZ+YnXy ddl,Z,Wnek rdZ,YnXy ddl-Z-Wnek r dZ-YnXy ddl.Z.Wnek r2dZ.YnXy ddl/Z/Wnek rZdZ/YnXddd d d d d ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;dd?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbg\Z0Gdcd d e1Z2Gddd d e2Z3Gdedde2Z4Gdfdde j5Z6ej7dfdhdiZ8dgfdkdldZ9dmdnZ:dodpZ;dqd=ZZ=ffdjfdsdZ>dtd9Z?dZ@dZAdaBdaCdZDdjZEdaFdudZGdvdZHdwdZIdxdyZJejjKdzr.dhd{d|ZLd}d~ZMddZNddZOddZPnejQZMejRZNddZOddZPddZQddZRddZSddZTddZUddZVdd#ZWdidd$ZXddZYdd%ZZdd&Z[dd'Z\djdd(Z]dZ^dZ_ej`ejafddJZbe^fddKZcddMZdddZeeeZfddZgdmZhdpZie jjekjldjKddZme jje*dZne jje+dZoe jje,dZpe jje-dZqejjKdZrejsdZtetdk oxetdkZuejdkreurdndZvndZvejwdkrdZxndZxdjyexejzZxdZ{xLdqD]BZ|yej}ej~e|e|kreWnek rYnXe|Z{PqWexdZejdkr:ddlZejdeZejZdZejwdkrejjdkrexdZyejeWnek rYnXedeefdZnBejdkrydjeWn&ek rexdjedǃZYnXdZxFdrD]dYZd?d@Ze jeedAdBZdCdDZdEdFZGdGdPdPejjZGdHd[d[eZdadId!ZdJd2ZdadKdLZdMd;ZdNdOZdPd"ZfdQdRd?Z dfffdSd@Z GdTd]d]Z dUdVZ dWdXZ ffdYdZZgfd[daZdad\dGZej7d]d^Zd_dbZGd`dadaZGdbdcdcZej7dddeZdS(z7Supporting definitions for the Python regression tests.z test.supportz.support must be imported from the test packageN)get_test_runner PIPE_MAX_SIZEverbose max_memuse use_resourcesfailfastError TestFailed TestDidNotRunResourceDenied import_moduleimport_fresh_module CleanImportunloadforgetrecord_original_stdoutget_original_stdoutcaptured_stdoutcaptured_stdincaptured_stderrTESTFNSAVEDCWDunlinkrmtreetemp_cwdfindfilecreate_empty_file can_symlinkfs_is_case_insensitiveis_resource_enabledrequiresrequires_freebsd_versionrequires_linux_versionrequires_mac_verrequires_hashdigestcheck_syntax_errorTransientResourcetime_outsocket_peer_resetioerror_peer_resettransient_internetBasicTestRunner run_unittest run_doctestskip_unless_symlink requires_gzip requires_bz2 requires_lzma bigmemtestbigaddrspacetest cpython_only get_attributerequires_IEEE_754skip_unless_xattr requires_zlibanticipate_failureload_package_testsdetect_api_mismatch check__all__requires_android_levelrequires_multiprocessing_queue is_jython is_androidcheck_impl_detail unix_shellsetswitchintervalHOST IPV6_ENABLEDfind_unused_port bind_portopen_urlresourcebind_unix_socket temp_umask reap_children TestHandlerthreading_setupthreading_cleanup reap_threads start_threadscheck_warningscheck_no_resource_warningEnvironmentVarGuardrun_with_locale swap_item swap_attrMatcher set_memlimitSuppressCrashReportsortdict run_with_tzPGOmissing_compiler_executablefd_countc@seZdZdZdS)r z*Base class for regression test exceptions.N)__name__ __module__ __qualname____doc__rdrd-/usr/lib64/python3.6/test/support/__init__.pyr |sc@seZdZdZdS)r z Test failed.N)r`rarbrcrdrdrdrer sc@seZdZdZdS)r zTest did not run any subtests.N)r`rarbrcrdrdrdrer sc@seZdZdZdS)r zTest skipped because it requested a disallowed resource. This is raised when a test calls requires() for a resource that has not be enabled. It is used to distinguish between expected and unexpected skips. N)r`rarbrcrdrdrdrer sTc cs8|r.tjtjddtdVWdQRXndVdS)zContext manager to suppress package and module deprecation warnings when importing them. If ignore is False, this context manager has no effect. ignorez.+ (module|package)N)warningscatch_warningsfilterwarningsDeprecationWarning)rfrdrdre_ignore_deprecated_importss  rkF) required_oncCsft|Ty tj|Stk rV}z&tjjt|r8tj t |WYdd}~XnXWdQRXdS)acImport and return the module to be tested, raising SkipTest if it is not available. If deprecated is True, any module or package deprecation messages will be suppressed. If a module is required on a platform but optional for others, set required_on to an iterable of platform prefixes which will be compared against sys.platform. N) rk importlibr ImportErrorsysplatform startswithtupleunittestSkipTeststr)name deprecatedrlmsgrdrdrer s  cCs^|tjkrt|tj|=x>ttjD]0}||ks@|j|dr&tj|||<tj|=q&WdS)zyHelper function to save and remove a module from sys.modules Raise ImportError if the module can't be imported. .N)romodules __import__listrq)rv orig_modulesmodnamerdrdre_save_and_remove_modules rc Cs>d}ytj|||<Wntk r.d}YnXdtj|<|S)zHelper function to save and block a module in sys.modules Return True if the module was in sys.modules, False otherwise. TFN)rorzKeyError)rvr}Zsavedrdrdre_save_and_block_modules  rcCs|r tjSddS)zDecorator to mark a test that is known to be broken in some cases Any use of this decorator should have a comment identifying the associated tracker issue. cSs|S)Nrd)frdrdresz$anticipate_failure..)rsZexpectedFailure)Z conditionrdrdrer:scCsF|dkr d}tjjtjjtjjt}|j|||d}|j||S)zGeneric load_tests implementation for simple test packages. Most packages can implement load_tests using this function as follows: def load_tests(*args): return load_package_tests(os.path.dirname(__file__), *args) Nztest*)Z start_dirZ top_level_dirpattern)ospathdirname__file__ZdiscoverZaddTests)Zpkg_dirloaderZstandard_testsrZtop_dirZ package_testsrdrdrer;s c Cst|i}g}t||zfyHx|D]}t||q&Wx |D]}t||s>|j|q>Wtj|}Wntk r~d}YnXWdx|jD]\} } | tj | <qWx|D] } tj | =qWX|SQRXdS)aImport and return a module, deliberately bypassing sys.modules. This function imports and returns a fresh copy of the named Python module by removing the named module from sys.modules before doing the import. Note that unlike reload, the original module is not affected by this operation. *fresh* is an iterable of additional module names that are also removed from the sys.modules cache before doing the import. *blocked* is an iterable of module names that are replaced with None in the module cache during the import to ensure that attempts to import them raise ImportError. The named module and any modules named in the *fresh* and *blocked* parameters are saved before starting the import and then reinserted into sys.modules when the fresh import is complete. Module and package deprecation messages are suppressed during this import if *deprecated* is True. This function will raise ImportError if the named module cannot be imported. N) rkrrappendrmr rnitemsrorz) rvZfreshZblockedrwr}Znames_to_removeZ fresh_nameZ blocked_nameZ fresh_moduleZ orig_namemoduleZname_to_removerdrdrers$      c Cs>yt||}Wn&tk r4tjd||fYnX|SdS)z?Get an attribute, raising SkipTest if AttributeError is raised.zobject %r has no attribute %rN)getattrAttributeErrorrsrt)objrvZ attributerdrdrer6s cCs|adS)N)_original_stdout)stdoutrdrdrer0scCs tptjS)N)rrorrdrdrdrer4sc Cs&y tj|=Wntk r YnXdS)N)rorzr)rvrdrdrer7s cGsny||Stk rh}zDtdkrHtd|jj|ftd|j|ftj|tj||Sd}~XnXdS)Nz%s: %sz re-run %s%r) OSErrorrprint __class__r`rchmodstatS_IRWXU)rfuncargserrrdrdre _force_run=srwincCs|||r|}ntjj|\}}|p(d}d}x<|dkrjtj|}|rJ|n||ksVdStj||d9}q0Wtjd|tdddS)NrygMbP?g?rz)tests may fail, delete still pending for ) stacklevel) rrsplitlistdirtimesleeprgwarnRuntimeWarning)rpathnamewaitallrrvtimeoutLrdrdre_waitforHs     rcCsttj|dS)N)rrr)filenamerdrdre_unlinkisrcCsttj|dS)N)rrrmdir)rrdrdre_rmdirlsrcs,fddt|ddtdd|dS)Ncsxt|tj|D]}tjj||}ytj|j}Wn<tk rn}z td||ft j dd}WYdd}~XnXt j |rt |ddt|tj|qt|tj|qWdS)Nz-support.rmtree(): os.lstat(%r) failed with %s)filerT)r)rrrrjoinlstatst_moderrro __stderr__rS_ISDIRrrr)rrvfullnamemodeexc) _rmtree_innerrdrerps   z_rmtree.._rmtree_innerT)rcSst|tj|S)N)rrr)prdrdrersz_rmtree..)r)rrd)rre_rmtreeos rc Cs^y ddl}Wntk r Yn:X|jt|d}|jjj||t|}|rZ|d|S|S)Nrr)ctypesrnZcreate_unicode_bufferlenwindllkernel32ZGetLongPathNameW)rrbufferZlengthrdrdre _longpaths    rc sFytj|dStk r"YnXfdd|tj|dS)Nc sx~t|tj|D]l}tjj||}ytj|j}Wntk rJd}YnXtj |rn|t|tj |qt|tj |qWdS)Nr) rrrrrrrrrrrr)rrvrr)rrdrers  z_rmtree.._rmtree_inner)shutilrrrr)rrd)rrers  cCs|S)Nrd)rrdrdrersc Cs*y t|Wnttfk r$YnXdS)N)rFileNotFoundErrorNotADirectoryError)rrdrdrers c Cs&y t|Wntk r YnXdS)N)rr)rrdrdrers rc Cs&y t|Wntk r YnXdS)N)rr)rrdrdrers cCsBtjj|}tjjtjj|}tjj||d}tj|||S)aMove a PEP 3147/488 pyc file to its legacy pyc location. :param source: The file system path to the source file. The source file does not need to exist, however the PEP 3147/488 pyc file must exist. :return: The file system path to the legacy pyc file. c) rmutilcache_from_sourcerrrabspathrrename)sourceZpyc_fileZup_oneZ legacy_pycrdrdremake_legacy_pycs   rcCs\t|xNtjD]D}tjj||d}t|dx dD]}ttjj||dq8WqWdS) z'Forget' a module was ever imported. This removes the module from sys.modules and deletes any PEP 3147/488 or legacy .pyc files. z.pyrrr) optimizationN)rrr) rrorrrrrmrr)r~rroptrdrdrers    csttdrtjSd}tjjdrddlddld}d}Gfdddj}j j }|j }|sjj |}j j}|j||j|j|j|}|sj t|j|@sd}ntjdkrVdd lm} mm} m} dd lm} | j| d } | jdkrd }nFGfd dd| }|}| |}| j|dksR| j|dkrVd}|sy.ddlm}|}|j|j |j!Wn\t"k r}z>t#|}t$|dkr|ddd}dj%t&|j'|}WYdd}~XnX|t_(| t_tjS)Nresultrrrcs.eZdZdjjfdjjfdjjfgZdS)z*_is_gui_available..USEROBJECTFLAGSZfInheritZ fReserveddwFlagsN)r`rarbwintypesZBOOLDWORD_fields_rd)rrdreUSEROBJECTFLAGSs  rz,gui not available (WSF_VISIBLE flag not set)darwin)cdllc_intpointer Structure) find_libraryZApplicationServicesz0gui tests cannot run without OS X window managercseZdZdfdfgZdS)z._is_gui_available..ProcessSerialNumberZ highLongOfPSNZ lowLongOfPSNN)r`rarbrrd)rrdreProcessSerialNumbersrz#cannot run without OS X gui process)Tk2z [...]zTk unavailable due to {}: {}))hasattr_is_gui_availablerrorprqrZctypes.wintypesrrZuser32ZGetProcessWindowStationZWinErrorrrZGetUserObjectInformationWZbyrefZsizeofboolrrrrZ ctypes.utilrZ LoadLibraryZCGMainDisplayIDZGetCurrentProcessZSetFrontProcessZtkinterrZwithdrawupdateZdestroy Exceptionrurformattyper`reason)rZ UOI_FLAGSZ WSF_VISIBLErZdllhZuofZneededresrrrrZ app_servicesrZpsnZpsn_prrooteZ err_stringrd)rrrersh         rcCstdkp|tkS)zTest whether a resource is enabled. Known resources are set by regrtest.py. If not running under regrtest.py, all resources are assumed enabled unless use_resources has been set. N)r)resourcerdrdrer $scCs>t|s |dkrd|}t||dkr:t r:ttjdS)z@Raise ResourceDenied if the specified resource is not available.Nz"Use of the %r resource not enabledgui)r r rr)rrxrdrdrer!,s csfdd}|S)zDecorator raising SkipTest if the OS is `sysname` and the version is less than `min_version`. For example, @_requires_unix_version('FreeBSD', (7, 2)) raises SkipTest if the FreeBSD version is less than 7.2. cs$tjfdd}|_|S)Nc stjkrztjjddd}yttt|jd}Wntk rLYn.X|krzdjtt }t j d||f||S)N-rrryz(%s version %s or higher required, not %s) rpsystemreleaserrrmapint ValueErrorrrursrt)rkw version_txtversionmin_version_txt)r min_versionsysnamerdrewrapper=s z:_requires_unix_version..decorator..wrapper) functoolswrapsr)rr)rr)rre decorator<sz)_requires_unix_version..decoratorrd)rrrrd)rrre_requires_unix_version5srcGs td|S)zDecorator raising SkipTest if the OS is FreeBSD and the FreeBSD version is less than `min_version`. For example, @requires_freebsd_version(7, 2) raises SkipTest if the FreeBSD version is less than 7.2. ZFreeBSD)r)rrdrdrer"PscGs td|S)zDecorator raising SkipTest if the OS is Linux and the Linux version is less than `min_version`. For example, @requires_linux_version(2, 6, 32) raises SkipTest if the Linux version is less than 2.6.32. ZLinux)r)rrdrdrer#Yscsfdd}|S)zDecorator raising SkipTest if the OS is Mac OS X and the OS X version if less than min_version. For example, @requires_mac_ver(10, 5) raises SkipTest if the OS X version is lesser than 10.5. cs"tjfdd}|_|S)Nc sxtjdkrntjd}yttt|jd}Wntk rBYn,X|krndjtt }t j d||f||S)Nrrryz&Mac OS X %s or higher required, not %s) rorpZmac_verrrrrrrrrursrt)rrrrr)rrrdrerjs   z4requires_mac_ver..decorator..wrapper)rrr)rr)r)rrerisz#requires_mac_ver..decoratorrd)rrrd)rrer$bs csfdd}|S)aDecorator raising SkipTest if a hashing algorithm is not available The hashing algorithm could be missing or blocked by a strict crypto policy. If 'openssl' is True, then the decorator checks that OpenSSL provides the algorithm. Otherwise the check falls back to built-in implementations. ValueError: [digital envelope routines: EVP_DigestInit_ex] disabled for FIPS ValueError: unsupported hash type md4 cstjfdd}|S)Nc sXy&rtdk rtjn tjWn&tk rLtjddYnX||S)Nz hash digest 'z' is not available.)_hashlibnewhashlibrrsrt)rkwargs) digestnameropensslrdrers  z7requires_hashdigest..decorator..wrapper)rr)rr)rr)rrers z&requires_hashdigest..decoratorrd)rrrrd)rrrer%}s z 127.0.0.1z::1cCs"tj||}t|}|j~|S)a Returns an unused port that should be suitable for binding. This is achieved by creating a temporary socket with the same family and type as the 'sock' parameter (default is AF_INET, SOCK_STREAM), and binding it to the specified host address (defaults to 0.0.0.0) with the port set to 0, eliciting an unused ephemeral port from the OS. The temporary socket is then closed and deleted, and the ephemeral port is returned. Either this method or bind_port() should be used for any tests where a server socket needs to be bound to a particular port for the duration of the test. Which one to use depends on whether the calling code is creating a python socket, or if an unused port needs to be provided in a constructor or passed to an external program (i.e. the -accept argument to openssl's s_server mode). Always prefer bind_port() over find_unused_port() where possible. Hard coded ports should *NEVER* be used. As soon as a server socket is bound to a hard coded port, the ability to run multiple instances of the test simultaneously on the same host is compromised, which makes the test a ticking time bomb in a buildbot environment. On Unix buildbots, this may simply manifest as a failed test, which can be recovered from without intervention in most cases, but on Windows, the entire python process can completely and utterly wedge, requiring someone to log in to the buildbot and manually kill the affected process. (This is easy to reproduce on Windows, unfortunately, and can be traced to the SO_REUSEADDR socket option having different semantics on Windows versus Unix/Linux. On Unix, you can't have two AF_INET SOCK_STREAM sockets bind, listen and then accept connections on identical host/ports. An EADDRINUSE OSError will be raised at some point (depending on the platform and the order bind and listen were called on each socket). However, on Windows, if SO_REUSEADDR is set on the sockets, no EADDRINUSE will ever be raised when attempting to bind two identical host/ports. When accept() is called on each socket, the second caller's process will steal the port from the first caller, leaving them both in an awkwardly wedged state where they'll no longer respond to any signals or graceful kills, and must be forcibly killed via OpenProcess()/TerminateProcess(). The solution on Windows is to use the SO_EXCLUSIVEADDRUSE socket option instead of SO_REUSEADDR, which effectively affords the same semantics as SO_REUSEADDR on Unix. Given the propensity of Unix developers in the Open Source world compared to Windows ones, this is a common mistake. A quick look over OpenSSL's 0.9.8g source shows that they use SO_REUSEADDR when openssl.exe is called with the 's_server' option, for example. See http://bugs.python.org/issue2550 for more info. The following site also has a very thorough description about the implications of both REUSEADDR and EXCLUSIVEADDRUSE on Windows: http://msdn2.microsoft.com/en-us/library/ms740621(VS.85).aspx) XXX: although this approach is a vast improvement on previous attempts to elicit unused ports, it rests heavily on the assumption that the ephemeral port returned to us by the OS won't immediately be dished back out to some other process when we close and delete our temporary socket but before our calling code has a chance to bind the returned port. We can deal with this issue if/when we come across it. )socketrHclose)familyZsocktypeZtempsockportrdrdrerGs 8 c Cs|jtjkr|jtjkrttdr>|jtjtjdkr>t dttdr~y |jtjtj dkrft dWnt k r|YnXttdr|j tjtj d|j|df|jd}|S)a%Bind the socket to a free port and return the port number. Relies on ephemeral ports in order to ensure we are using an unbound port. This is important as many tests may be running simultaneously, especially in a buildbot environment. This method raises an exception if the sock.family is AF_INET and sock.type is SOCK_STREAM, *and* the socket has SO_REUSEADDR or SO_REUSEPORT set on it. Tests should *never* set these socket options for TCP/IP sockets. The only case for setting these options is testing multicasting via multiple UDP sockets. Additionally, if the SO_EXCLUSIVEADDRUSE socket option is available (i.e. on Windows), it will be set on the socket. This will prevent anyone else from bind()'ing to our host/port for the duration of the test. SO_REUSEADDRrzHtests should never set the SO_REUSEADDR socket option on TCP/IP sockets! SO_REUSEPORTzHtests should never set the SO_REUSEPORT socket option on TCP/IP sockets!SO_EXCLUSIVEADDRUSEr)rrAF_INETr SOCK_STREAMrZ getsockoptZ SOL_SOCKETrr rrZ setsockoptrbindZ getsockname)sockhostrrdrdrerHs     c Cs:y|j|Wn&tk r4|jtjdYnXdS)zBBind a unix socket, raising SkipTest if PermissionError is raised.zcannot bind AF_UNIX socketsN)r PermissionErrorrrsrt)r ZaddrrdrdrerJs cCsZtjrVd}z.dec)rr)rrrd)rresystem_must_validate_certs rriZdoubleZIEEEztest requires IEEE 754 doublesz requires zlibz requires gzipz requires bz2z requires lzmajavaANDROID_API_LEVELwin32z/system/bin/shz/bin/shz$testz@testz {}_{}_tmpæİŁφКא،تก €u -àòɘŁğrZNFDntru-共Ł♡ͣztWARNING: The filename %r CAN be encoded by the filesystem encoding (%s). Unicode filename tests may not be effectives-surrogateescapewrccsd}|dkr&tj}d}tjj|}nBytj|d}Wn.tk rf|sNtjd|t ddYnX|rttj }z |VWd|r|tj krt |XdS)aReturn a context manager that creates a temporary directory. Arguments: path: the directory to create temporarily. If omitted or None, defaults to creating a temporary directory using tempfile.mkdtemp. quiet: if False (the default), the context manager raises an exception on error. Otherwise, if the path is specified and cannot be created, only a warning is issued. FNTz+tests may fail, unable to create temp dir: )r) tempfilemkdtemprrrealpathmkdirrrgrrgetpidr)rquietZ dir_createdpidrdrdretemp_dirs&   r2ccsftj}ytj|Wn.tk rD|s,tjd|tddYnXztjVWdtj|XdS)agReturn a context manager that changes the current working directory. Arguments: path: the directory to use as the temporary current working directory. quiet: if False (the default), the context manager raises an exception on error. Otherwise, it issues only a warning and keeps the current working directory the same. z)tests may fail, unable to change CWD to: r*)rN)rgetcwdchdirrrgrr)rr0Z saved_dirrdrdre change_cwd s  r5tempcwdccs:t||d$}t||d }|VWdQRXWdQRXdS)a Context manager that temporarily creates and changes the CWD. The function temporarily changes the current working directory after creating a temporary directory in the current directory with name *name*. If *name* is None, the temporary directory is created using tempfile.mkdtemp. If *quiet* is False (default) and it is not possible to create or change the CWD, an error is raised. If *quiet* is True, only a warning is raised and the original CWD is used. )rr0)r0N)r2r5)rvr0Z temp_pathZcwd_dirrdrdrer$sumaskc cs&tj|}z dVWdtj|XdS)z8Context manager that temporarily sets the process umask.N)rr7)r7ZoldmaskrdrdrerK8s  datacCsbtjj|r|S|dk r&tjj||}tgtj}x*|D]"}tjj||}tjj|r8|Sq8W|S)a[Try to find a file on sys.path or in the test directory. If it is not found the argument passed to the function is returned (this does not necessarily signal failure; could still be the legitimate path). Setting *subdir* indicates a relative path to use to find the file rather than looking directly in the path directories. N)rrisabsr TEST_HOME_DIRroexists)rZsubdirrZdnfnrdrdrerIs    cCs(tj|tjtjBtjB}tj|dS)z>Create an empty file. If the file already exists, truncate it.N)ropenO_WRONLYO_CREATO_TRUNCr)rfdrdrdrer[scCs,t|j}dd|D}dj|}d|S)z%Like repr(dict), but in sorted order.cSsg|] }d|qS)z%r: %rrd).0Zpairrdrdre cszsortdict..z, z{%s})sortedrr)dictrZ reprpairsZ withcommasrdrdrer[`s  c Cs*ttd}z|jS|jttXdS)z` Create an invalid file descriptor by opening and closing a file and return its fd. wbN)r=rfilenorr)rrdrdre make_bad_fdgs  rH)linenooffsetc Csp|jt}t|ddWdQRX|j}|j|j|dk rJ|j|j||j|j|dk rl|j|j|dS)Nz exec) assertRaises SyntaxErrorcompileZ exceptionZassertIsNotNonerI assertEqualrJ)testcaseZ statementrIrJcmrrdrdrer&ss   c sVddl}ddl}jdd|jj|djdd}tjjt |}fdd}tjj |r|||}|dk rt|St |t dt rtd |td |jj}tr|jjd|j|d d}tr|jjdd krtj|d}zBt|d.} |j} x| r| j| |j} qWWdQRXWd|jX||}|dk rF|Std|dS)Nrcheckr/rcs>t|f}dkr|S|r2|jd|S|jdS)Nr)r=seekr)r<r)rrRrrdrecheck_valid_files z*open_urlresource..check_valid_fileZurlfetchz fetching %s ...)rAccept-Encodinggzip)rzContent-Encoding)ZfileobjrFzinvalid resource %r)rVrW)Zurllib.requestZ urllib.parsepopparseZurlparserrrr TEST_DATA_DIRr;rr!rrrZrequestZ build_openerrWZ addheadersrr=ZheadersgetZGzipFilereadwriterr ) Zurlrrurllibrr<rUropeneroutsrd)rrRrrerI~s<         c@s4eZdZdZddZddZeddZdd Zd S) WarningsRecorderzyConvenience wrapper for the warnings list returned on entry to the warnings.catch_warnings() context manager. cCs||_d|_dS)Nr) _warnings_last)selfZ warnings_listrdrdre__init__szWarningsRecorder.__init__cCsDt|j|jkr t|jd|S|tjjkr0dStd||fdS)Nrz%r has no attribute %rrY)rrerfrrgWarningMessage_WARNING_DETAILSr)rgattrrdrdre __getattr__s  zWarningsRecorder.__getattr__cCs|j|jdS)N)rerf)rgrdrdrergszWarningsRecorder.warningscCst|j|_dS)N)rrerf)rgrdrdreresetszWarningsRecorder.resetN) r`rarbrcrhrlpropertyrgrmrdrdrdrerds  rdc cs tjd}|jjd}|r"|jtjdd }tjdjdt |VWdQRXt |}g}xz|D]r\}}d} xH|ddD]8}|j } t j |t| t jrt| j|rd} |j|qW| rf| rf|j||jfqfW|rtd |d |rtd |d dS) zCatch the warnings, then check if all the expected warnings have been raised and re-raise unexpected warnings. If 'quiet' is True, only re-raise the unexpected warnings. rZ__warningregistry__T)recordrgalwaysNFzunhandled warning %srz)filter (%r, %s) did not catch any warning)ro _getframe f_globalsr]clearrgrhrz simplefilterrdr|messagerematchruI issubclassrremoverr`AssertionError) filtersr0frameregistrywZreraiseZmissingrxcatseenZwarningrdrdre_filterwarningss0    rcOs.|jd}|s$dtff}|dkr$d}t||S)aContext manager to silence warnings. Accept 2-tuples as positional arguments: ("message regexp", WarningCategory) Optional argument: - if 'quiet' is True, it does not fail if a filter catches nothing (default True without argument, default False if some filters are defined) Without argument, it defaults to: check_warnings(("", Warning), quiet=True) r0rNT)r]Warningr)r|rr0rdrdrerRs   rc csHtjdd&}tjd||ddV|r.tWdQRX|j|gdS)aContext manager to check that no warnings are emitted. This context manager enables a given warning within its scope and checks that no warnings are emitted even with that warning enabled. If force_gc is True, a garbage collection is attempted before checking for warnings. This may help to catch warnings emitted when objects are deleted, such as ResourceWarning. Other keyword arguments are passed to warnings.filterwarnings(). T)rorp)rucategoryN)rgrhri gc_collectrO)rPrurZforce_gcwarnsrdrdrecheck_no_warningssrc csBtjdd }tjdtddVtWdQRX|j|gdS)a"Context manager to check that no ResourceWarning is emitted. Usage: with check_no_resource_warning(self): f = open(...) ... del f You must remove the object which may emit ResourceWarning before the end of the context manager. T)rorp)rN)rgrhriResourceWarningrrO)rPrrdrdrerSs c@s(eZdZdZddZddZddZdS) ra,Context manager to force import to return a new module reference. This is useful for testing module-level behaviours, such as the emission of a DeprecationWarning on import. Use like this: with CleanImport("foo"): importlib.import_module("foo") # new reference cGsNtjj|_x<|D]4}|tjkrtj|}|j|kr>tj|j=tj|=qWdS)N)rorzcopyoriginal_modulesr`)rgZ module_namesZ module_namerrdrdrerh?s      zCleanImport.__init__cCs|S)Nrd)rgrdrdre __enter__LszCleanImport.__enter__cGstjj|jdS)N)rorzrr)rg ignore_excrdrdre__exit__OszCleanImport.__exit__N)r`rarbrcrhrrrdrdrdrer3s  c@sheZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZdS)rTz_Class to help protect the environment variable properly. Can be used as a context manager.cCstj|_i|_dS)N)renviron_environ_changed)rgrdrdrerhXszEnvironmentVarGuard.__init__cCs |j|S)N)r)rgenvvarrdrdre __getitem__\szEnvironmentVarGuard.__getitem__cCs*||jkr|jj||j|<||j|<dS)N)rrr])rgrvaluerdrdre __setitem___s zEnvironmentVarGuard.__setitem__cCs2||jkr|jj||j|<||jkr.|j|=dS)N)rrr])rgrrdrdre __delitem__es  zEnvironmentVarGuard.__delitem__cCs |jjS)N)rkeys)rgrdrdrerlszEnvironmentVarGuard.keyscCs t|jS)N)iterr)rgrdrdre__iter__oszEnvironmentVarGuard.__iter__cCs t|jS)N)rr)rgrdrdre__len__rszEnvironmentVarGuard.__len__cCs |||<dS)Nrd)rgrrrdrdresetuszEnvironmentVarGuard.setcCs ||=dS)Nrd)rgrrdrdreunsetxszEnvironmentVarGuard.unsetcCs|S)Nrd)rgrdrdrer{szEnvironmentVarGuard.__enter__cGsJx<|jjD].\}}|dkr0||jkr:|j|=q ||j|<q W|jt_dS)N)rrrrr)rgrkvrdrdrer~s   zEnvironmentVarGuard.__exit__N)r`rarbrcrhrrrrrrrrrrrdrdrdrerTSsc@s(eZdZdZddZddZddZdS) DirsOnSysPathaContext manager to temporarily add directories to sys.path. This makes a copy of sys.path, appends any directories given as positional arguments, then reverts sys.path to the copied settings when the context ends. Note that *all* sys.path modifications in the body of the context manager, including replacement of the object, will be reverted at the end of the block. cGs(tjdd|_tj|_tjj|dS)N)rororiginal_valueoriginal_objectextend)rgpathsrdrdrerhszDirsOnSysPath.__init__cCs|S)Nrd)rgrdrdrerszDirsOnSysPath.__enter__cGs|jt_|jtjdd<dS)N)rrorr)rgrrdrdrerszDirsOnSysPath.__exit__N)r`rarbrcrhrrrdrdrdrers rc@s*eZdZdZddZddZd ddZdS) r'zRaise ResourceDenied if an exception is raised while the context manager is in effect that matches the specified exception and attributes.cKs||_||_dS)N)rattrs)rgrrrdrdrerhszTransientResource.__init__cCs|S)Nrd)rgrdrdrerszTransientResource.__enter__NcCsT|dk rPt|j|rPx:|jjD]$\}}t||s4Pt|||kr Pq WtddS)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)ryrrrrrr )rgZtype_r tracebackrkZ attr_valuerdrdrers zTransientResource.__exit__)NNN)r`rarbrcrhrrrdrdrdrer's)errnog>@)rerrnosc #spd!d"d#d$d%d&g}d(d*d,d.d/g}td||gsRdd|Ddd|Dfdd}tj}zy|dk rtj|dVWntjk r}z&trtjj j dd|WYdd}~Xnt k rZ}zpx^|j }t |d krt |dt r|d}n*t |dkr8t |d t r8|d }nPqW||WYdd}~XnXWdtj|XdS)0zReturn a context manager that raises ResourceDenied when various issues with the Internet connection manifest themselves as exceptions. ECONNREFUSEDo ECONNRESETh EHOSTUNREACHq ENETUNREACHe ETIMEDOUTn EADDRNOTAVAILc EAI_AGAINr*EAI_FAILr EAI_NONAMEr EAI_NODATA WSANO_DATA*zResource %r is not availablecSsg|]\}}tt||qSrd)rr)rBrvnumrdrdrerCsz&transient_internet..cSsg|]\}}tt||qSrd)rr)rBrvrrdrdrerCscst|dd}t|tjst|tjr,|kst|tjjrTd|jkoNdknst|tjj rd|j ksd|j ksd|j ks|krt st j jjdd|dS) NriiWConnectionRefusedError TimeoutErrorEOFErrorr )r isinstancerrZgaierrorr`errorZ HTTPErrorcodeZURLErrorrrrostderrr_r)rn)captured_errnosdenied gai_errnosrdre filter_errors     z(transient_internet..filter_errorNrrr)rr)rr)rr)rr)rr)rr)rr)rr)rr)rr)rr)r rZgetdefaulttimeoutZsetdefaulttimeoutnntplibZNNTPTemporaryErrorrrorr_rrrr) Z resource_namerrZdefault_errnosZdefault_gai_errnosrZ old_timeoutrard)rrrrer+sP     c csFddl}tt|}tt||jztt|VWdtt||XdS)zReturn a context manager used by captured_stdout/stdin/stderr that temporarily replaces the sys stream *stream_name* with a StringIO.rN)iorrosetattrStringIO)Z stream_namerZ orig_stdoutrdrdrecaptured_outputs  rcCstdS)zCapture the output of sys.stdout: with captured_stdout() as stdout: print("hello") self.assertEqual(stdout.getvalue(), "hello\n") r)rrdrdrdrerscCstdS)zCapture the output of sys.stderr: with captured_stderr() as stderr: print("hello", file=sys.stderr) self.assertEqual(stderr.getvalue(), "hello\n") r)rrdrdrdrer%scCstdS)a Capture the input to sys.stdin: with captured_stdin() as stdin: stdin.write('hello\n') stdin.seek(0) # call test code that consumes from sys.stdin captured = input() self.assertEqual(captured, "hello") stdin)rrdrdrdrer.s cCs*tjtrtjdtjtjdS)aForce as many objects as possible to be collected. In non-CPython implementations of Python, this is needed because timely deallocation is not guaranteed by the garbage collector. (Even in CPython this can be the case in case of reference cycles.) This means that __del__ methods may be called later than expected and weakrefs may remain alive for longer than expected. This function tries its best to force all garbage objects to disappear. g?N)gcZcollectr@rrrdrdrdrer;s  rc cs.tj}tjz dVWd|r(tjXdS)N)r isenableddisableenable)Zhave_gcrdrdre disable_gcKs  rcCs:tjdp d}d}x|jD]}|jdr|}qW|dkS)z,Find if Python was built with optimizations. PY_CFLAGSrz-O-O0-Og)rrr) sysconfigget_config_varrrq)ZcflagsZ final_optrrdrdrepython_is_optimizedVs  rZnPZ0ngettotalrefcountZ2PZ0PrcCstjt|tS)N)structcalcsize_header_align)fmtrdrdre calcobjsizegsrcCstjt|tS)N)rr_vheaderr)rrdrdre calcvobjsizejsr cCspddl}tj|}t|tkr(|jt@sBt|tkrLt|jt@rL||j7}dt|||f}|j|||dS)Nrz&wrong size for %s: got %d, expected %d) _testcapiro getsizeofr __flags___TPFLAGS_HEAPTYPE_TPFLAGS_HAVE_GCZSIZEOF_PYGC_HEADrO)testosizerrrxrdrdre check_sizeofqs  rcsfdd}|S)Ncs$fdd}j|_j|_|S)Ncsy ddl}t|}|j|}Wn(tk r6YnBd}}Yn0Xx,D]$}y|j||PWqPYqPXqPWz ||S|r|r|j||XdS)Nr)localer setlocaler)rkwdsrrZ orig_localeloc)catstrrlocalesrdreinners$     z1run_with_locale..decorator..inner)r`rc)rr)rr)rrersz"run_with_locale..decoratorrd)rrrrd)rrrerUscsfdd}|S)Ncs"fdd}j|_j|_|S)Ncsy tj}Wntk r(tjdYnXdtjkr@tjd}nd}tjd<|z ||S|dkrrtjd=n |tjd<tjXdS)Nztzset requiredZTZ)rtzsetrrsrtrr)rrrZorig_tz)rtzrdrers       z-run_with_tz..decorator..inner)r`rc)rr)r)rrerszrun_with_tz..decoratorrd)rrrd)rrer\s cCsdttdtd}tjd|tjtjB}|dkr>td|ftt|j d||j dj }|a |t krrt }|t dkrtd|f|adS)Ni)rmgtz(\d+(\.\d+)?) (K|M|G|T)b?$zInvalid memory limit %rrr*z$Memory limit %r too low to be useful)_1M_1Grvrw IGNORECASEVERBOSErrfloatgrouplowerreal_max_memuseMAX_Py_ssize_t_2Gr)limitZsizesrZmemlimitrdrdrerYs $ c@s(eZdZdZddZddZddZdS) _MemoryWatchdogz`An object which periodically watches the process' memory consumption and prints it out. cCsdjtjd|_d|_dS)Nz/proc/{pid}/statm)r1F)rrr/procfilestarted)rgrdrdrerhsz_MemoryWatchdog.__init__cCsyt|jd}Wn<tk rL}z tjdj|ttjj dSd}~XnXt d}t j tj |g|t jd|_|jd|_dS)Nrz!/proc not available for stats: {}zmemory_watchdog.py)rrT)r=r rrgrrrrorflushr subprocessPopen executableZDEVNULL mem_watchdogrr )rgrrZwatchdog_scriptrdrdrestarts   z_MemoryWatchdog.startcCs|jr|jj|jjdS)N)r rZ terminatewait)rgrdrdrestops z_MemoryWatchdog.stopN)r`rarbrcrhrrrdrdrdrer sr csfdd}|S)atDecorator for bigmem tests. 'size' is a requested size for the test (in arbitrary, test-interpreted units.) 'memuse' is the number of bytes per unit for the test, or a good estimate of it. For example, a test that needs two byte buffers, of 4 GiB each, could be decorated with @bigmemtest(size=_4G, memuse=2). The 'size' argument is normally passed to the decorated test method as an extra argument. If 'dry_run' is true, the value passed to the test method may be less than the requested value. If 'dry_run' is false, it means the test doesn't support dummy runs when -M is not specified. cs fdd__S)Nc sj}j}tsd}n|}ts$ rFt||krFtjd||dtr|tr|ttdj||ddt}|j nd}z ||S|r|j XdS) Niz'not enough memory: %.1fG minimum neededir*z* ... expected peak memory use: {peak:.1f}G)Zpeaki@i@) rmemuserrsrtrrrr rr)rgrrmaxsizeZwatchdog)dry_runrrrdrers*    z.bigmemtest..decorator..wrapper)rr)r)rrr)rrrerszbigmemtest..decoratorrd)rrrrrd)rrrrer3s !csfdd}|S)z0Decorator for tests that fill the address space.csDttkr8td kr$td kr$tjdq@tjdtd n|SdS) Nr?rz-not enough memory: try a 32-bit build insteadz'not enough memory: %.1fG minimum neededir*llli@)rrrsrt)rg)rrdrer3sz!bigaddrspacetest..wrapperrd)rrrd)rrer41s c@seZdZddZdS)r,cCstj}|||S)N)rsZ TestResult)rgrrrdrdrerunDszBasicTestRunner.runN)r`rarbrrdrdrdrer,CscCs|S)Nrd)rrdrdre_idIsrcCs<|dkrt rtjtjSt|r(tStjdj|SdS)Nrzresource {0!r} is not enabled)rrsskiprr rr)rrdrdrerequires_resourceLs  rcCs&trt|krtjd|tfStSdS)Nz%s at Android API level %d)rA_ANDROID_API_LEVELrsrr)levelrrdrdrer>Ts  cCstdd|S)z9 Decorator for tests only applicable on CPython. T)cpython) impl_detail)rrdrdrer5[scKsVtf|rtS|dkrLt|\}}|r,d}nd}t|j}|jdj|}tj|S)Nz*implementation detail not available on {0}z%implementation detail specific to {0}z or ) rBr _parse_guardsrDrrrrsr)rxguardsZ guardnamesdefaultrdrdrer!as   r!c CsTtdkr:ddl}y|jdaWntk r8daYnXd}trF|Stj||S)z8Skip decorator for tests that use multiprocessing.Queue.NrTFz6requires a functioning shared semaphore implementation)_have_mp_queuemultiprocessingZQueuernrsr)rr&rxrdrdrer?os cCs*|sddidfSt|jd}|| fS)Nr TFr)r|values)r#Zis_truerdrdrer"~s r"cKs t|\}}|jtjj|S)a5This function returns True or False depending on the host platform. Examples: if check_impl_detail(): # only on CPython (default) if check_impl_detail(jython=True): # only on Jython if check_impl_detail(cpython=False): # everywhere except on CPython )r"r]rpZpython_implementationr)r#r$rdrdrerBs cs,ttdsStjfdd}|SdS)zEDecorator to temporarily turn off tracing for the duration of a test.gettracec s.tj}ztjd||Stj|XdS)N)ror(settrace)rrZoriginal_trace)rrdrers   zno_tracing..wrapperN)rrorr)rrrd)rre no_tracings r*cCs tt|S)aDecorator for tests which involve reference counting. To start, the decorator does not run the test if is not run by CPython. After that, any trace function is unset during the test to prevent unexpected refcounts caused by the trace function. )r*r5)rrdrdre refcount_testsr+cCsRg}xB|jD]8}t|tjr2t|||j|q ||r |j|q W||_dS)z>Recursively filter test cases in a suite based on a predicate.N)Z_testsrrs TestSuite _filter_suiter)suiteZpredZnewtestsrrdrdrer-s    r-cCsttjttdk d}|j|}tdk r4tj|j|js>t |j st |j dkrl|j rl|j dd}n6t |j dkr|j r|j dd}nd}ts|d7}t|dS)z2Run tests from a unittest.TestSuite-derived class.N) verbosityZcapture_outputrrzmultiple errors occurredz!; run in verbose mode for details)rrorrjunit_xml_listrrZget_xml_elementZtestsRunr Z wasSuccessfulrerrorsZfailuresr )r.Zrunnerrrrdrdre _run_suites"  r2cCstdkr dSt|jSdS)NT)_match_test_funcid)rrdrdre match_testsr5cCsd|kotjd| S)Nryz[?*\[\]])rvsearch)rrdrdre_is_full_match_testsr7csr|tkr dS|sd}f}nHttt|r4t|j}n.djttj|}t j |j fdd}|}t |a|a dS)N|cs$|r dStt|jdSdS)NTry)anyrr)Ztest_id) regex_matchrdrematch_test_regexsz)set_match_tests..match_test_regex)_match_test_patternsallrr7r __contains__rfnmatch translatervrNrwrrr3)ZpatternsrZregexr;rd)r:reset_match_testss   rAcGstjtjf}tj}xh|D]`}t|trT|tjkrJ|jtjtj|qzt dqt||rj|j|q|jtj |qWt |t t |dS)z1Run tests from unittest.TestCase-derived classes.z)str arguments must be keys in sys.modulesN)rsr,ZTestCaserrurorzZaddTestZ findTestCasesrZ makeSuiter-r5r2)classesZ valid_typesr.clsrdrdrer-s        cCsdS)z,Just used to check if docstrings are enabledNrdrdrdrdre_check_docstrings(srDWITH_DOC_STRINGSztest requires docstringscCs`ddl}|dkrt}nd}|j|||d\}}|rBtd||ftrXtd|j|f||fS)aRun doctest on the given module. Return (#failures, #tests). If optional argument verbosity is not specified (or is None), pass support's belief about verbosity on to doctest. Else doctest's usual behavior is used (it searches sys.argv for -v). rN)r optionflagsz%d of %d doctests failedz,doctest (%s) ... %d tests with zero failures)doctestrZtestmodr rr`)rr/rFrGrrrdrdrer.9scCs tjjfS)N)rorzrrdrdrdre modules_setupTsrHcCs:ddtjjD}tjjtjj|tjj|dS)NcSs"g|]\}}|jdr||fqS)z encodings.)rq)rBrrrdrdrerC[sz#modules_cleanup..)rorzrrsr)Z oldmodulesZ encodingsrdrdremodules_cleanupWs  rIcCs"trtjtjjfSdffSdS)Nr)_thread_count threading _danglingrrdrdrdrerNzscGsJtsdSd}x8t|D],}tjtjf}||kr2PtjdtqWdS)Ndg{Gz?)rJrangerKrLrMrrr)Zoriginal_valuesZ _MAX_COUNTcountr'rdrdrerOs cs"tsStjfdd}|S)zUse this function when threads are being used. This will ensure that the threads are cleaned up even when the test fails. If threading is unavailable this function does nothing. c st}z|St|XdS)N)rNrO)rkey)rrdrerszreap_threads..decorator)rJrr)rrrd)rrerPsN@ccstj}z dVWdtj}||}xjtj}||kr8Ptj|kr|tj|}d||d|dd|d|d }t|tjdtq&WXdS) aH bpo-31234: Context manager to wait until all threads created in the with statement exit. Use _thread.count() to check if threads exited. Indirectly, wait until threads exit the internal t_bootstrap() C function of the _thread module. threading_setup() and threading_cleanup() are designed to emit a warning if a test leaves running threads in the background. This context manager is designed to cleanup threads started by the _thread.start_new_thread() which doesn't allow to wait for thread exit, whereas thread.Thread has a join() method. Nz!wait_threads() failed to cleanup z threads after z.1fz seconds (count: z , old count: )g{Gz?)rJrKrZ monotonicr{rr)rZ old_countZ start_timeZdeadlinerPZdtrxrdrdrewait_threads_exits   $ rTc CsZttdrVd}xFy2tj|tj\}}|dkr.Ptd|tjdWqPYqXqWdS)zUse this function at the end of test_main() whenever sub-processes are started. This will help ensure that no extra children (zombies) stick around to hog resources and create problems when looking for refleaks. waitpidrrz2Warning -- reap_children() reaped child process %s)rNrY)rrrUWNOHANGrror)Z any_processr1ZstatusrdrdrerLs ccs*t|}g}zZy$x|D]}|j|j|qWWn*trVtdt|t|fYnXdVWdz|rt|tj}}xltddD]^}|d7}x$|D]}|jt |tjdqWdd|D}|sPtrtdt||fqWWdd d|D}|r"t j t j td t|XXdS) Nz/Can't start %d threads, only %d threads startedrr<g{Gz?cSsg|]}|jr|qSrd)isAlive)rBrrdrdrerCsz!start_threads..z7Unable to join %d threads during a period of %d minutescSsg|]}|jr|qSrd)rX)rBrrdrdrerCszUnable to join %d threads)r|rrrrrrrOrmax faulthandlerZdump_tracebackrorr{)ZthreadsZunlockr rZendtimeZ starttimerrdrdrerQs>     c csnt||rtt|tt|}|r(|t|8}tdd|D}|S)a Returns the set of items in ref_api not in other_api, except for a defined list of items to be ignored in this check. By default this skips private attributes beginning with '_' but includes all magic methods, i.e. those starting and ending in '__'. css(|] }|jd s|jdr|VqdS)___N)rqendswith)rBrrdrdre sz&detect_api_mismatch..)rrx)Zref_apiZ other_apirfZ missing_itemsrdrdrer< s  cCs|dkr|jf}nt|tr"|f}t|}xbt|D]V}|jds4||krLq4t||}t|dd|kst|d r4t|tj  r4|j |q4W|j |j |dS)aAssert that the __all__ variable of 'module' contains all public names. The module's public names (its API) are detected automatically based on whether they match the public name convention and were defined in 'module'. The 'name_of_module' argument can specify (as a string or tuple thereof) what module(s) an API could be defined in in order to be detected as a public API. One case for this is when 'module' imports part of its public API from other modules, possibly a C backend (like 'csv' and its '_csv'). The 'extra' argument can be a set of names that wouldn't otherwise be automatically detected as "public", like objects without a proper '__module__' attribute. If provided, it will be added to the automatically detected ones. The 'blacklist' argument can be a set of names that must not be treated as part of the public API even though their names indicate otherwise. Usage: import bar import foo import unittest from test import support class MiscTestCase(unittest.TestCase): def test__all__(self): support.check__all__(self, foo) class OtherTestCase(unittest.TestCase): def test__all__(self): extra = {'BAR_CONST', 'FOO_CONST'} blacklist = {'baz'} # Undocumented name. # bar imports part of its API from _bar. support.check__all__(self, bar, ('bar', '_bar'), extra=extra, blacklist=blacklist) Nrra) r`rrurrxrqrrtypes ModuleTypeaddZassertCountEqual__all__)Z test_caserZname_of_moduleZextraZ blacklistZexpectedrvrrdrdrer= s)    c@s(eZdZdZdZdZddZddZdS)rZzTry to prevent a crash report from popping up. On Windows, don't display the Windows Error Reporting dialog. On UNIX, disable the creation of coredump file. Nc Csrtjjdrddl}|jj|_d}|jj||_|jj|j|Byddl }|j Wnt t fk rlYnLXi|_ x|j|j|jgD].}|j ||j}|j||j}||f|j |<qWntdk r y*tjtj|_tjtjd|jdfWnttfk rYnXtjdkrnddd d g}tj|tjtjd }||jd} WdQRX| jd krntd ddd|S)zOn Windows, disable Windows Error Reporting dialogs using SetErrorMode. On UNIX, try to save the previous core file size limit, then set soft limit to 0. rrNrrrz/usr/bin/defaultsr^zcom.apple.CrashReporterZ DialogType)rrs developerz:this test triggers the Crash Reporter, that is intentionalrT)endr ) rorprqrrr_k32 SetErrorMode old_valuemsvcrtCrtSetReportModerrn old_modesCRT_WARN CRT_ERROR CRT_ASSERTZCRTDBG_MODE_FILECrtSetReportFileZCRTDBG_FILE_STDERRrZ getrlimit RLIMIT_CORE setrlimitrrrrPIPEZ communicaterar) rgrZSEM_NOGPFAULTERRORBOXr report_typeold_modeold_filecmdprocrrdrdrer2 sN        zSuppressCrashReport.__enter__c Gs|jdkrdStjjdrl|jj|j|jrddl}xj|jjD]$\}\}}|j |||j ||qBWn6t dk ryt j t j |jWnttfk rYnXdS)zARestore Windows ErrorMode or core file behavior to initial value.Nrr)rrorprqrrrrrrrrrrrr)rgrrrrrrdrdrers s   zSuppressCrashReport.__exit__)r`rarbrcrrrrrdrdrdrerZ) s Ac srtdyjWn$ttfk r@tdYnXdfdd}|j|t|dS)zOverride 'object_to_patch'.'attr_name' with 'new_value'. Also, add a cleanup procedure to 'test_instance' to restore 'object_to_patch' value for 'attr_name'. The 'attr_name' should be a valid attribute for 'object_to_patch'. FNTcs rtn tdS)N)rr[rd) attr_is_local attr_nameobject_to_patchrrdrecleanup szpatch..cleanup)rrjrrZ addCleanupr)Z test_instancerrZ new_valuerrd)rrrrrepatch s  rc CsFy ddl}Wntk r YnX|jr4tjdddl}|j|S)zi Run code in a subinterpreter. Raise unittest.SkipTest if the tracemalloc module is enabled. rNzUrun_in_subinterp() cannot be used if tracemalloc module is tracing memory allocations) tracemallocrnZ is_tracingrsrtrrun_in_subinterp)rrrrdrdrer s  rcsHGfddd|}d||||jttt|jdS)NcseZdZfddZdS)z%check_free_after_iterating..Ac s*dy tWntk r$YnXdS)NT)next StopIteration)rg)doneitrdre__del__ s  z-check_free_after_iterating..A.__del__N)r`rarbrrd)rrrdreA srF)rLrrrZ assertTrue)rrrCrrrd)rrrecheck_free_after_iterating s   rcCs|ddlm}m}m}|j}|j|xP|jD]F}|r@||kr@q.t||}|rPn |dkrZq.|j|ddkr.|dSq.WdS)a<Check if the compiler components used to build the interpreter exist. Check for the existence of the compiler executables whose names are listed in 'cmd_names' or all the compiler executables when 'cmd_names' is empty and return the first missing executable or None when none is found missing. r) ccompilerrspawnN) Z distutilsrrrZ new_compilerZcustomize_compilerZ executablesrZfind_executable)Z cmd_namesrrrZcompilerrvrrdrdrer^ s     cCs@d}tr6||kr6tdkr.tjddgjdkatr6|}tj|S)Ngh㈵>Zgetpropzro.kernel.qemu1)rA_is_android_emulatorrZ check_outputrarorD)ZintervalZminimum_intervalrdrdrerD s c cs>tjj}tj}ztjdVWd|r8tj|ddXdS)NT)rZ all_threads)rorrGrZ is_enabledrr)rArrdrdredisable_faulthandler s  rc /Cstjjd r8ytjd}t|dStk r6YnXd}ttdrjytjd}Wnt k rhYnXd}tjd kryd dl }|j Wnt t fk rYn0Xi}x(|j|j|jfD]}|j |d ||<qWzpd }xft|D]Z}ytj|}Wn4t k r(}z|jtjkrWYdd}~XqXtj||d7}qWWd|dk rzx*|j|j|jfD]}|j |||q`WX|S) z/Count the number of open file descriptors. linuxfreebsdz /proc/self/fdrsysconf SC_OPEN_MAXNrr)rr)rorprqrrrrrrrrrrrnrrrrOduprZEBADFr) namesZMAXFDrrrrPrAZfd2rrdrdrer_ sP          c@s(eZdZdZddZddZddZdS) SaveSignalsz Save an restore signal handlers. This class is only able to save/restore signal handlers registered by the Python signal module: see bpo-13285 for "external" signal handlers. c Csjddl}||_ttd|j|_x>dD]6}yt||}Wntk rNw&YnX|jj|q&Wi|_dS)NrrSIGKILLSIGSTOP)rr) signalr|rONSIGsignalsrrrzrf)rgrZsignamesignumrdrdrerhM s zSaveSignals.__init__cCs4x.|jD]$}|jj|}|dkr"q||j|<qWdS)N)rr getsignalrf)rgrhandlerrdrdresaveZ s   zSaveSignals.savecCs*x$|jjD]\}}|jj||q WdS)N)rfrr)rgrrrdrdrerestoref szSaveSignals.restoreN)r`rarbrcrhrrrdrdrdrerD s  rc@s(eZdZdZddZddZddZdS) FakePathz.Simple implementing of the path protocol. cCs ||_dS)N)r)rgrrdrdrerhn szFakePath.__init__cCsd|jdS)Nz )r)rgrdrdre__repr__q szFakePath.__repr__cCs6t|jts$t|jtr,t|jtr,|jn|jSdS)N)rr BaseExceptionrry)rgrdrdre __fspath__t s    zFakePath.__fspath__N)r`rarbrcrhrrrdrdrdrerk src cs.tj}ztj|dVWdtj|XdS)z>Temporarily change the integer string conversion length limit.N)roget_int_max_str_digitsset_int_max_str_digits)Z max_digitsZcurrentrdrdreadjust_int_max_str_digits| s   r)T)F)F)N)Nii@i@i@ii) rrrrrrrrr r!r")r&r$r'r(r))NF)F)r6F)N)Fi@ii)T)N)Nr)rR)N(rcr`rncollections.abc collections contextlibZdatetimerrZr?rrrrmimportlib.utilrZlogging.handlersrerrrprvrrrrrrorr+rrrsZ urllib.errorr`rgZ testresultrrJrLZmultiprocessing.processr&zlibrWbz2Zlzmarrrrr r r rtr contextmanagerrkr rrr:r;rr6rrrrr0rrrrrrrqrrrrrrrrrrrr r!rr"r#r$r%rErr r rGrHrJrrFrrZ SOCK_MAX_SIZEZ skipUnlessr __getformat__r7r9r0r1r2r@rrrArCrvrrr/Z FS_NONASCII characterfsdecodefsencode UnicodeErrorZTESTFN_UNICODEZ unicodedata normalizegetfilesystemencodingZTESTFN_ENCODINGZTESTFN_UNENCODABLEZgetwindowsversionencodeUnicodeEncodeErrorrdecodeUnicodeDecodeErrorZTESTFN_UNDECODABLEZTESTFN_NONASCIIr3rr]r}r2r5rrrKrrrrZTEST_SUPPORT_DIRr:rr\rrr[rHr&rIobjectrdrrRrrrSrabcMutableMappingrTrr'rrr(rr)r*r+rrrrrrrrrrrrrrrrUr\rrrZ_4GrrrYr r3r4r,rrr>r5r!r%r?r"rBr*r+r-r2r3r<r5r7rAr-rDZMISSING_C_DOCSTRINGSZHAVE_DOCSTRINGSZrequires_docstringsr.rHrIZenvironment_alteredrNrOrPrTrLrQrWrVrbZskipIfZrequires_type_collectingrcrdrfrgrMrXrrrr/rzr|r8r~rr<r=rZrrrr^rrDrr_rrrrdrdrdres                      2   !  J    > %                 %      2 ' 5M            $ # 0           (        " #   "    :_"  ;'PK!JtVV1support/__pycache__/__init__.cpython-36.opt-2.pycnu[3 f@sX edkredddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z!ddl"Z"ddl#m$Z$yddl%Z%ddl&Z&Wnek r>dZ%dZ&YnXy ddl'Z(Wnek rfdZ(YnXy ddl)Z)Wnek rdZ)YnXy ddl*Z*Wnek rdZ*YnXy ddl+Z+Wnek rdZ+YnXy ddl,Z,Wnek rdZ,YnXy ddl-Z-Wnek r.dZ-YnXy ddl.Z.Wnek rVdZ.YnXdddd d d d d ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;dd?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dag\Z/Gdbd d e0Z1Gdcd d e1Z2Gddd d e1Z3Gdeddej4Z5ej6dedgdhZ7dffdjdkdZ8dldmZ9dndoZ:dpd<Z;dqd=ZdZ?dZ@daAdaBdZCdiZDdaEdtdZFdudZGdvdZHdwdxZIejjJdyr*dgdzd{ZKd|d}ZLd~dZMddZNddZOnejPZLejQZMddZNddZOddZPddZQddZRddZSddZTddZUdd"ZVdhdd#ZWddZXdd$ZYdd%ZZdd&Z[didd'Z\dZ]dZ^ej_ej`fddIZae]fddJZbddLZcddZdedZeddZfdlZgdoZhejiejjkdjJddZlejie)dZmejie*dZnejie+dZoejie,dZpejjJdZqejrdZsesdk otesdkZtejdkretrdndZundZuejvdkrdZwndZwdjxewejyZwdZzxLdpD]BZ{yej|ej}e{e{kre~Wne~k rYnXe{ZzPqWewdZejdkr6ddlZejdeZejZdZejvdkrejjdkrewdZyejeWnek rYnXedeefdZnBejdkrydjeWn&ek rewdjedƃZYnXdZxFdqD]d?Zejeed@dAZdBdCZdDdEZGdFdOdOejjZGdGdZdZeZdadHd ZdId1ZdadJdKZdLd:ZdMdNZdOd!ZfdPdQd>Z dfffdRd?Z GdSd\d\Z dTdUZ dVdWZ ffdXdYZgfdZd`Zdad[dFZej6d\d]Zd^daZGd_d`d`ZGdadbdbZej6dcddZdS(z test.supportz.support must be imported from the test packageN)get_test_runner PIPE_MAX_SIZEverbose max_memuse use_resourcesfailfastError TestFailed TestDidNotRunResourceDenied import_moduleimport_fresh_module CleanImportunloadforgetrecord_original_stdoutget_original_stdoutcaptured_stdoutcaptured_stdincaptured_stderrTESTFNSAVEDCWDunlinkrmtreetemp_cwdfindfilecreate_empty_file can_symlinkfs_is_case_insensitiveis_resource_enabledrequiresrequires_freebsd_versionrequires_linux_versionrequires_mac_verrequires_hashdigestcheck_syntax_errorTransientResourcetime_outsocket_peer_resetioerror_peer_resettransient_internetBasicTestRunner run_unittest run_doctestskip_unless_symlink requires_gzip requires_bz2 requires_lzma bigmemtestbigaddrspacetest cpython_only get_attributerequires_IEEE_754skip_unless_xattr requires_zlibanticipate_failureload_package_testsdetect_api_mismatch check__all__requires_android_levelrequires_multiprocessing_queue is_jython is_androidcheck_impl_detail unix_shellsetswitchintervalHOST IPV6_ENABLEDfind_unused_port bind_portopen_urlresourcebind_unix_socket temp_umask reap_children TestHandlerthreading_setupthreading_cleanup reap_threads start_threadscheck_warningscheck_no_resource_warningEnvironmentVarGuardrun_with_locale swap_item swap_attrMatcher set_memlimitSuppressCrashReportsortdict run_with_tzPGOmissing_compiler_executablefd_countc@s eZdZdS)r N)__name__ __module__ __qualname__rcrc-/usr/lib64/python3.6/test/support/__init__.pyr |sc@s eZdZdS)r N)r`rarbrcrcrcrdr sc@s eZdZdS)r N)r`rarbrcrcrcrdr sc@s eZdZdS)r N)r`rarbrcrcrcrdr sTc cs8|r.tjtjddtdVWdQRXndVdS)Nignorez.+ (module|package))warningscatch_warningsfilterwarningsDeprecationWarning)rercrcrd_ignore_deprecated_importss  rjF) required_oncCsft|Ty tj|Stk rV}z&tjjt|r8tj t |WYdd}~XnXWdQRXdS)N) rj importlibr ImportErrorsysplatform startswithtupleunittestSkipTeststr)name deprecatedrkmsgrcrcrdr s  cCs^|tjkrt|tj|=x>ttjD]0}||ks@|j|dr&tj|||<tj|=q&WdS)N.)rnmodules __import__listrp)ru orig_modulesmodnamercrcrd_save_and_remove_modules r~c Cs>d}ytj|||<Wntk r.d}YnXdtj|<|S)NTF)rnryKeyError)rur|Zsavedrcrcrd_save_and_block_modules  rcCs|r tjSddS)NcSs|S)Nrc)frcrcrdsz$anticipate_failure..)rrZexpectedFailure)Z conditionrcrcrdr:scCsF|dkr d}tjjtjjtjjt}|j|||d}|j||S)Nztest*)Z start_dirZ top_level_dirpattern)ospathdirname__file__ZdiscoverZaddTests)Zpkg_dirloaderZstandard_testsrZtop_dirZ package_testsrcrcrdr;s c Cst|i}g}t||zfyHx|D]}t||q&Wx |D]}t||s>|j|q>Wtj|}Wntk r~d}YnXWdx|jD]\} } | tj | <qWx|D] } tj | =qWX|SQRXdS)N) rjr~rappendrlr rmitemsrnry) ruZfreshZblockedrvr|Znames_to_removeZ fresh_nameZ blocked_nameZ fresh_moduleZ orig_namemoduleZname_to_removercrcrdrs$      c Cs>yt||}Wn&tk r4tjd||fYnX|SdS)Nzobject %r has no attribute %r)getattrAttributeErrorrrrs)objruZ attributercrcrdr6s cCs|adS)N)_original_stdout)stdoutrcrcrdr0scCs tptjS)N)rrnrrcrcrcrdr4sc Cs&y tj|=Wntk r YnXdS)N)rnryr)rurcrcrdr7s cGsny||Stk rh}zDtdkrHtd|jj|ftd|j|ftj|tj||Sd}~XnXdS)Nz%s: %sz re-run %s%r) OSErrorrprint __class__r`rchmodstatS_IRWXU)rfuncargserrrcrcrd _force_run=srwincCs|||r|}ntjj|\}}|p(d}d}x<|dkrjtj|}|rJ|n||ksVdStj||d9}q0Wtjd|tdddS)NrxgMbP?g?rz)tests may fail, delete still pending for ) stacklevel) rrsplitlistdirtimesleeprfwarnRuntimeWarning)rpathnamewaitallrrutimeoutLrcrcrd_waitforHs     rcCsttj|dS)N)rrr)filenamercrcrd_unlinkisrcCsttj|dS)N)rrrmdir)rrcrcrd_rmdirlsrcs,fddt|ddtdd|dS)Ncsxt|tj|D]}tjj||}ytj|j}Wn<tk rn}z td||ft j dd}WYdd}~XnXt j |rt |ddt|tj|qt|tj|qWdS)Nz-support.rmtree(): os.lstat(%r) failed with %s)filerT)r)rrrrjoinlstatst_moderrrn __stderr__rS_ISDIRrrr)rrufullnamemodeexc) _rmtree_innerrcrdrps   z_rmtree.._rmtree_innerT)rcSst|tj|S)N)rrr)prcrcrdrsz_rmtree..)r)rrc)rrd_rmtreeos rc Cs^y ddl}Wntk r Yn:X|jt|d}|jjj||t|}|rZ|d|S|S)Nrr)ctypesrmZcreate_unicode_bufferlenwindllkernel32ZGetLongPathNameW)rrbufferZlengthrcrcrd _longpaths    rc sFytj|dStk r"YnXfdd|tj|dS)Nc sx~t|tj|D]l}tjj||}ytj|j}Wntk rJd}YnXtj |rn|t|tj |qt|tj |qWdS)Nr) rrrrrrrrrrrr)rrurr)rrcrdrs  z_rmtree.._rmtree_inner)shutilrrrr)rrc)rrdrs  cCs|S)Nrc)rrcrcrdrsc Cs*y t|Wnttfk r$YnXdS)N)rFileNotFoundErrorNotADirectoryError)rrcrcrdrs c Cs&y t|Wntk r YnXdS)N)rr)rrcrcrdrs rc Cs&y t|Wntk r YnXdS)N)rr)rrcrcrdrs cCsBtjj|}tjjtjj|}tjj||d}tj|||S)Nc) rlutilcache_from_sourcerrrabspathrrename)sourceZpyc_fileZup_oneZ legacy_pycrcrcrdmake_legacy_pycs   rcCs\t|xNtjD]D}tjj||d}t|dx dD]}ttjj||dq8WqWdS)Nz.pyrrr) optimization)rrr) rrnrrrrrlrr)r}rroptrcrcrdrs    csttdrtjSd}tjjdrddlddld}d}Gfdddj}j j }|j }|sjj |}j j}|j||j|j|j|}|sj t|j|@sd}ntjdkrVdd lm} mm} m} dd lm} | j| d } | jdkrd }nFGfd dd| }|}| |}| j|dksR| j|dkrVd}|sy.ddlm}|}|j|j |j!Wn\t"k r}z>t#|}t$|dkr|ddd}dj%t&|j'|}WYdd}~XnX|t_(| t_tjS)Nresultrrrcs.eZdZdjjfdjjfdjjfgZdS)z*_is_gui_available..USEROBJECTFLAGSZfInheritZ fReserveddwFlagsN)r`rarbwintypesZBOOLDWORD_fields_rc)rrcrdUSEROBJECTFLAGSs  rz,gui not available (WSF_VISIBLE flag not set)darwin)cdllc_intpointer Structure) find_libraryZApplicationServicesz0gui tests cannot run without OS X window managercseZdZdfdfgZdS)z._is_gui_available..ProcessSerialNumberZ highLongOfPSNZ lowLongOfPSNN)r`rarbrrc)rrcrdProcessSerialNumbersrz#cannot run without OS X gui process)Tk2z [...]zTk unavailable due to {}: {}))hasattr_is_gui_availablerrnrorprZctypes.wintypesrrZuser32ZGetProcessWindowStationZWinErrorrrZGetUserObjectInformationWZbyrefZsizeofboolrrrrZ ctypes.utilrZ LoadLibraryZCGMainDisplayIDZGetCurrentProcessZSetFrontProcessZtkinterrZwithdrawupdateZdestroy Exceptionrtrformattyper`reason)rZ UOI_FLAGSZ WSF_VISIBLErZdllhZuofZneededresrrrrZ app_servicesrZpsnZpsn_prrooteZ err_stringrc)rrrdrsh         rcCstdkp|tkS)N)r)resourcercrcrdr $scCs>t|s |dkrd|}t||dkr:t r:ttjdS)Nz"Use of the %r resource not enabledgui)r r rr)rrwrcrcrdr!,s csfdd}|S)Ncs$tjfdd}|_|S)Nc stjkrztjjddd}yttt|jd}Wntk rLYn.X|krzdjtt }t j d||f||S)N-rrrxz(%s version %s or higher required, not %s) rosystemreleaserrqmapint ValueErrorrrtrrrs)rkw version_txtversionmin_version_txt)r min_versionsysnamercrdwrapper=s z:_requires_unix_version..decorator..wrapper) functoolswrapsr)rr)rr)rrd decorator<sz)_requires_unix_version..decoratorrc)rrrrc)rrrd_requires_unix_version5srcGs td|S)NZFreeBSD)r)rrcrcrdr"PscGs td|S)NZLinux)r)rrcrcrdr#Yscsfdd}|S)Ncs"tjfdd}|_|S)Nc sxtjdkrntjd}yttt|jd}Wntk rBYn,X|krndjtt }t j d||f||S)Nrrrxz&Mac OS X %s or higher required, not %s) rnroZmac_verrqrrrrrrtrrrs)rrrrr)rrrcrdrjs   z4requires_mac_ver..decorator..wrapper)rrr)rr)r)rrdrisz#requires_mac_ver..decoratorrc)rrrc)rrdr$bs csfdd}|S)Ncstjfdd}|S)Nc sXy&rtdk rtjn tjWn&tk rLtjddYnX||S)Nz hash digest 'z' is not available.)_hashlibnewhashlibrrrrs)rkwargs) digestnameropensslrcrdrs  z7requires_hashdigest..decorator..wrapper)rr)rr)rr)rrdrs z&requires_hashdigest..decoratorrc)rrrrc)rrrdr%}s z 127.0.0.1z::1cCs"tj||}t|}|j~|S)N)socketrHclose)familyZsocktypeZtempsockportrcrcrdrGs 8 c Cs|jtjkr|jtjkrttdr>|jtjtjdkr>t dttdr~y |jtjtj dkrft dWnt k r|YnXttdr|j tjtj d|j|df|jd}|S)N SO_REUSEADDRrzHtests should never set the SO_REUSEADDR socket option on TCP/IP sockets! SO_REUSEPORTzHtests should never set the SO_REUSEPORT socket option on TCP/IP sockets!SO_EXCLUSIVEADDRUSEr)rrAF_INETr SOCK_STREAMrZ getsockoptZ SOL_SOCKETrr rrZ setsockoptrbindZ getsockname)sockhostrrcrcrdrHs     c Cs:y|j|Wn&tk r4|jtjdYnXdS)Nzcannot bind AF_UNIX sockets)r PermissionErrorrrrrs)r ZaddrrcrcrdrJs cCsZtjrVd}z.dec)rr)rrrc)rrdsystem_must_validate_certs rriZdoubleZIEEEztest requires IEEE 754 doublesz requires zlibz requires gzipz requires bz2z requires lzmajavaANDROID_API_LEVELwin32z/system/bin/shz/bin/shz$testz@testz {}_{}_tmpæİŁφКא،تก €u -àòɘŁğrZNFDntru-共Ł♡ͣztWARNING: The filename %r CAN be encoded by the filesystem encoding (%s). Unicode filename tests may not be effectives-surrogateescapewrccsd}|dkr&tj}d}tjj|}nBytj|d}Wn.tk rf|sNtjd|t ddYnX|rttj }z |VWd|r|tj krt |XdS)NFTz+tests may fail, unable to create temp dir: )r) tempfilemkdtemprrrealpathmkdirrrfrrgetpidr)rquietZ dir_createdpidrcrcrdtemp_dirs&   r1ccsftj}ytj|Wn.tk rD|s,tjd|tddYnXztjVWdtj|XdS)Nz)tests may fail, unable to change CWD to: r))r)rgetcwdchdirrrfrr)rr/Z saved_dirrcrcrd change_cwd s  r4tempcwdccs:t||d$}t||d }|VWdQRXWdQRXdS)N)rr/)r/)r1r4)rur/Z temp_pathZcwd_dirrcrcrdr$sumaskc cs&tj|}z dVWdtj|XdS)N)rr6)r6ZoldmaskrcrcrdrK8s  datacCsbtjj|r|S|dk r&tjj||}tgtj}x*|D]"}tjj||}tjj|r8|Sq8W|S)N)rrisabsr TEST_HOME_DIRrnexists)rZsubdirrZdnfnrcrcrdrIs    cCs(tj|tjtjBtjB}tj|dS)N)ropenO_WRONLYO_CREATO_TRUNCr)rfdrcrcrdr[scCs,t|j}dd|D}dj|}d|S)NcSsg|] }d|qS)z%r: %rrc).0Zpairrcrcrd cszsortdict..z, z{%s})sortedrr)dictrZ reprpairsZ withcommasrcrcrdr[`s  c Cs*ttd}z|jS|jttXdS)Nwb)r<rfilenorr)rrcrcrd make_bad_fdgs  rG)linenooffsetc Csp|jt}t|ddWdQRX|j}|j|j|dk rJ|j|j||j|j|dk rl|j|j|dS)Nz exec) assertRaises SyntaxErrorcompileZ exceptionZassertIsNotNonerH assertEqualrI)testcaseZ statementrHrIcmrrcrcrdr&ss   c sVddl}ddl}jdd|jj|djdd}tjjt |}fdd}tjj |r|||}|dk rt|St |t dt rtd |td |jj}tr|jjd|j|d d}tr|jjdd krtj|d}zBt|d.} |j} x| r| j| |j} qWWdQRXWd|jX||}|dk rF|Std|dS)Nrcheckr/rcs>t|f}dkr|S|r2|jd|S|jdS)Nr)r<seekr)r;r)rrQrrcrdcheck_valid_files z*open_urlresource..check_valid_fileZurlfetchz fetching %s ...)rAccept-Encodinggzip)rzContent-Encoding)ZfileobjrEzinvalid resource %r)rUrV)Zurllib.requestZ urllib.parsepopparseZurlparserrrr TEST_DATA_DIRr:rr!rrrZrequestZ build_openerrVZ addheadersrr<ZheadersgetZGzipFilereadwriterr ) Zurlrrurllibrr;rTropeneroutsrc)rrQrrdrI~s<         c@s0eZdZddZddZeddZddZd S) WarningsRecordercCs||_d|_dS)Nr) _warnings_last)selfZ warnings_listrcrcrd__init__szWarningsRecorder.__init__cCsDt|j|jkr t|jd|S|tjjkr0dStd||fdS)Nrz%r has no attribute %rrX)rrdrerrfWarningMessage_WARNING_DETAILSr)rfattrrcrcrd __getattr__s  zWarningsRecorder.__getattr__cCs|j|jdS)N)rdre)rfrcrcrdrfszWarningsRecorder.warningscCst|j|_dS)N)rrdre)rfrcrcrdresetszWarningsRecorder.resetN)r`rarbrgrkpropertyrfrlrcrcrcrdrcs rcc cs tjd}|jjd}|r"|jtjdd }tjdjdt |VWdQRXt |}g}xz|D]r\}}d} xH|ddD]8}|j } t j |t| t jrt| j|rd} |j|qW| rf| rf|j||jfqfW|rtd|d |rtd |d dS) NrZ__warningregistry__T)recordrfalwaysFzunhandled warning %srz)filter (%r, %s) did not catch any warning)rn _getframe f_globalsr\clearrfrgry simplefilterrcr{messagerematchrtI issubclassrremoverr`AssertionError) filtersr/frameregistrywZreraiseZmissingrwcatseenZwarningrcrcrd_filterwarningss0    rcOs.|jd}|s$dtff}|dkr$d}t||S)Nr/rT)r\Warningr)r{rr/rcrcrdrRs   rc csHtjdd&}tjd||ddV|r.tWdQRX|j|gdS)NT)rnro)rtcategory)rfrgrh gc_collectrN)rOrtrZforce_gcwarnsrcrcrdcheck_no_warningssrc csBtjdd }tjdtddVtWdQRX|j|gdS)NT)rnro)r)rfrgrhResourceWarningrrN)rOrrcrcrdrSs c@s$eZdZddZddZddZdS)rcGsNtjj|_x<|D]4}|tjkrtj|}|j|kr>tj|j=tj|=qWdS)N)rnrycopyoriginal_modulesr`)rfZ module_namesZ module_namerrcrcrdrg?s      zCleanImport.__init__cCs|S)Nrc)rfrcrcrd __enter__LszCleanImport.__enter__cGstjj|jdS)N)rnryrr)rf ignore_excrcrcrd__exit__OszCleanImport.__exit__N)r`rarbrgrrrcrcrcrdr3s  c@sdeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ dS)rTcCstj|_i|_dS)N)renviron_environ_changed)rfrcrcrdrgXszEnvironmentVarGuard.__init__cCs |j|S)N)r)rfenvvarrcrcrd __getitem__\szEnvironmentVarGuard.__getitem__cCs*||jkr|jj||j|<||j|<dS)N)rrr\)rfrvaluercrcrd __setitem___s zEnvironmentVarGuard.__setitem__cCs2||jkr|jj||j|<||jkr.|j|=dS)N)rrr\)rfrrcrcrd __delitem__es  zEnvironmentVarGuard.__delitem__cCs |jjS)N)rkeys)rfrcrcrdrlszEnvironmentVarGuard.keyscCs t|jS)N)iterr)rfrcrcrd__iter__oszEnvironmentVarGuard.__iter__cCs t|jS)N)rr)rfrcrcrd__len__rszEnvironmentVarGuard.__len__cCs |||<dS)Nrc)rfrrrcrcrdsetuszEnvironmentVarGuard.setcCs ||=dS)Nrc)rfrrcrcrdunsetxszEnvironmentVarGuard.unsetcCs|S)Nrc)rfrcrcrdr{szEnvironmentVarGuard.__enter__cGsJx<|jjD].\}}|dkr0||jkr:|j|=q ||j|<q W|jt_dS)N)rrrrr)rfrkvrcrcrdr~s   zEnvironmentVarGuard.__exit__N)r`rarbrgrrrrrrrrrrrcrcrcrdrTSsc@s$eZdZddZddZddZdS) DirsOnSysPathcGs(tjdd|_tj|_tjj|dS)N)rnroriginal_valueoriginal_objectextend)rfpathsrcrcrdrgszDirsOnSysPath.__init__cCs|S)Nrc)rfrcrcrdrszDirsOnSysPath.__enter__cGs|jt_|jtjdd<dS)N)rrnrr)rfrrcrcrdrszDirsOnSysPath.__exit__N)r`rarbrgrrrcrcrcrdrs rc@s&eZdZddZddZdddZdS) r'cKs||_||_dS)N)rattrs)rfrrrcrcrdrgszTransientResource.__init__cCs|S)Nrc)rfrcrcrdrszTransientResource.__enter__NcCsT|dk rPt|j|rPx:|jjD]$\}}t||s4Pt|||kr Pq WtddS)Nz%an optional resource is not available)rxrrrrrr )rfZtype_r tracebackrjZ attr_valuercrcrdrs zTransientResource.__exit__)NNN)r`rarbrgrrrcrcrcrdr's)errnog>@)rerrnosc #spd d!d"d#d$d%g}d'd)d+d-d.g}td||gsRdd|Ddd|Dfdd}tj}zy|dk rtj|dVWntjk r}z&trtjj j dd|WYdd}~Xnt k rZ}zpx^|j }t |dkrt |dt r|d}n*t |dkr8t |dt r8|d}nPqW||WYdd}~XnXWdtj|XdS)/N ECONNREFUSEDo ECONNRESETh EHOSTUNREACHq ENETUNREACHe ETIMEDOUTn EADDRNOTAVAILc EAI_AGAINr)EAI_FAILr EAI_NONAMEr EAI_NODATA WSANO_DATA*zResource %r is not availablecSsg|]\}}tt||qSrc)rr)rArunumrcrcrdrBsz&transient_internet..cSsg|]\}}tt||qSrc)rr)rArurrcrcrdrBscst|dd}t|tjst|tjr,|kst|tjjrTd|jkoNdknst|tjj rd|j ksd|j ksd|j ks|krt st j jjdd|dS) NriiWConnectionRefusedError TimeoutErrorEOFErrorr )r isinstancerrZgaierrorr_errorZ HTTPErrorcodeZURLErrorrrrnstderrr^r)rn)captured_errnosdenied gai_errnosrcrd filter_errors     z(transient_internet..filter_errorrrr)rr)rr)rr)rr)rr)rr)rr)rr)rr)rr)rr)r rZgetdefaulttimeoutZsetdefaulttimeoutnntplibZNNTPTemporaryErrorrrnrr^rrrr) Z resource_namerrZdefault_errnosZdefault_gai_errnosrZ old_timeoutrarc)rrrrdr+sP     c csFddl}tt|}tt||jztt|VWdtt||XdS)Nr)iorrnsetattrStringIO)Z stream_namerZ orig_stdoutrcrcrdcaptured_outputs  rcCstdS)Nr)rrcrcrcrdrscCstdS)Nr)rrcrcrcrdr%scCstdS)Nstdin)rrcrcrcrdr.s cCs*tjtrtjdtjtjdS)Ng?)gcZcollectr@rrrcrcrcrdr;s  rc cs.tj}tjz dVWd|r(tjXdS)N)r isenableddisableenable)Zhave_gcrcrcrd disable_gcKs  rcCs:tjdp d}d}x|jD]}|jdr|}qW|dkS)N PY_CFLAGSrz-O-O0-Og)rrr) sysconfigget_config_varrrp)ZcflagsZ final_optrrcrcrdpython_is_optimizedVs  rZnPZ0ngettotalrefcountZ2PZ0PrcCstjt|tS)N)structcalcsize_header_align)fmtrcrcrd calcobjsizegsrcCstjt|tS)N)rr_vheaderr)rrcrcrd calcvobjsizejsr cCspddl}tj|}t|tkr(|jt@sBt|tkrLt|jt@rL||j7}dt|||f}|j|||dS)Nrz&wrong size for %s: got %d, expected %d) _testcapirn getsizeofr __flags___TPFLAGS_HEAPTYPE_TPFLAGS_HAVE_GCZSIZEOF_PYGC_HEADrN)testosizerrrwrcrcrd check_sizeofqs  rcsfdd}|S)Ncs$fdd}j|_j|_|S)Ncsy ddl}t|}|j|}Wn(tk r6YnBd}}Yn0Xx,D]$}y|j||PWqPYqPXqPWz ||S|r|r|j||XdS)Nr)localer setlocaler)rkwdsrrZ orig_localeloc)catstrrlocalesrcrdinners$     z1run_with_locale..decorator..inner)r`__doc__)rr)rr)rrdrsz"run_with_locale..decoratorrc)rrrrc)rrrdrUscsfdd}|S)Ncs"fdd}j|_j|_|S)Ncsy tj}Wntk r(tjdYnXdtjkr@tjd}nd}tjd<|z ||S|dkrrtjd=n |tjd<tjXdS)Nztzset requiredZTZ)rtzsetrrrrsrr)rrrZorig_tz)rtzrcrdrs       z-run_with_tz..decorator..inner)r`r)rr)r)rrdrszrun_with_tz..decoratorrc)rrrc)rrdr\s cCsdttdtd}tjd|tjtjB}|dkr>td|ftt|j d||j dj }|a |t krrt }|t dkrtd|f|adS)Ni)rmgtz(\d+(\.\d+)?) (K|M|G|T)b?$zInvalid memory limit %rrr)z$Memory limit %r too low to be useful)_1M_1Grurv IGNORECASEVERBOSErrfloatgrouplowerreal_max_memuseMAX_Py_ssize_t_2Gr)limitZsizesrZmemlimitrcrcrdrYs $ c@s$eZdZddZddZddZdS)_MemoryWatchdogcCsdjtjd|_d|_dS)Nz/proc/{pid}/statm)r0F)rrr.procfilestarted)rfrcrcrdrgsz_MemoryWatchdog.__init__cCsyt|jd}Wn<tk rL}z tjdj|ttjj dSd}~XnXt d}t j tj |g|t jd|_|jd|_dS)Nrz!/proc not available for stats: {}zmemory_watchdog.py)rrT)r<r rrfrrrrnrflushr subprocessPopen executableZDEVNULL mem_watchdogrr )rfrrZwatchdog_scriptrcrcrdstarts   z_MemoryWatchdog.startcCs|jr|jj|jjdS)N)r rZ terminatewait)rfrcrcrdstops z_MemoryWatchdog.stopN)r`rarbrgrrrcrcrcrdr sr csfdd}|S)Ncs fdd__S)Nc sj}j}tsd}n|}ts$ rFt||krFtjd||dtr|tr|ttdj||ddt}|j nd}z ||S|r|j XdS) Niz'not enough memory: %.1fG minimum neededir)z* ... expected peak memory use: {peak:.1f}G)Zpeaki@i@) rmemuserrrrsrrrr rr)rfrrmaxsizeZwatchdog)dry_runrrrcrdrs*    z.bigmemtest..decorator..wrapper)rr)r)rrr)rrrdrszbigmemtest..decoratorrc)rrrrrc)rrrrdr3s !csfdd}|S)NcsDttkr8td kr$td kr$tjdq@tjdtd n|SdS) Nr?rz-not enough memory: try a 32-bit build insteadz'not enough memory: %.1fG minimum neededir)llli@)rrrrrs)rf)rrcrdr3sz!bigaddrspacetest..wrapperrc)rrrc)rrdr41s c@seZdZddZdS)r,cCstj}|||S)N)rrZ TestResult)rfrrrcrcrdrunDszBasicTestRunner.runN)r`rarbrrcrcrcrdr,CscCs|S)Nrc)rrcrcrd_idIsrcCs<|dkrt rtjtjSt|r(tStjdj|SdS)Nrzresource {0!r} is not enabled)rrrskiprr rr)rrcrcrdrequires_resourceLs  rcCs&trt|krtjd|tfStSdS)Nz%s at Android API level %d)rA_ANDROID_API_LEVELrrrr)levelrrcrcrdr>Ts  cCstdd|S)NT)cpython) impl_detail)rrcrcrdr5[scKsVtf|rtS|dkrLt|\}}|r,d}nd}t|j}|jdj|}tj|S)Nz*implementation detail not available on {0}z%implementation detail specific to {0}z or ) rBr _parse_guardsrCrrrrrr)rwguardsZ guardnamesdefaultrcrcrdr!as   r!c CsTtdkr:ddl}y|jdaWntk r8daYnXd}trF|Stj||S)NrTFz6requires a functioning shared semaphore implementation)_have_mp_queuemultiprocessingZQueuermrrr)rr&rwrcrcrdr?os cCs*|sddidfSt|jd}|| fS)Nr TFr)r{values)r#Zis_truercrcrdr"~s r"cKs t|\}}|jtjj|S)N)r"r\roZpython_implementationr)r#r$rcrcrdrBs cs,ttdsStjfdd}|SdS)Ngettracec s.tj}ztjd||Stj|XdS)N)rnr(settrace)rrZoriginal_trace)rrcrdrs   zno_tracing..wrapper)rrnrr)rrrc)rrd no_tracings r*cCs tt|S)N)r*r5)rrcrcrd refcount_testsr+cCsRg}xB|jD]8}t|tjr2t|||j|q ||r |j|q W||_dS)N)Z_testsrrr TestSuite _filter_suiter)suiteZpredZnewtestsrrcrcrdr-s    r-cCsttjttdk d}|j|}tdk r4tj|j|js>t |j st |j dkrl|j rl|j dd}n6t |j dkr|j r|j dd}nd}ts|d7}t|dS)N) verbosityZcapture_outputrrzmultiple errors occurredz!; run in verbose mode for details)rrnrrjunit_xml_listrrZget_xml_elementZtestsRunr Z wasSuccessfulrerrorsZfailuresr )r.Zrunnerrrrcrcrd _run_suites"  r2cCstdkr dSt|jSdS)NT)_match_test_funcid)rrcrcrd match_testsr5cCsd|kotjd| S)Nrxz[?*\[\]])rusearch)rrcrcrd_is_full_match_testsr7csr|tkr dS|sd}f}nHttt|r4t|j}n.djttj|}t j |j fdd}|}t |a|a dS)N|cs$|r dStt|jdSdS)NTrx)anyrr)Ztest_id) regex_matchrcrdmatch_test_regexsz)set_match_tests..match_test_regex)_match_test_patternsallrr7r __contains__rfnmatch translaterurMrvrqr3)ZpatternsrZregexr;rc)r:rdset_match_testss   rAcGstjtjf}tj}xh|D]`}t|trT|tjkrJ|jtjtj|qzt dqt||rj|j|q|jtj |qWt |t t |dS)Nz)str arguments must be keys in sys.modules)rrr,ZTestCaserrtrnryZaddTestZ findTestCasesrZ makeSuiter-r5r2)classesZ valid_typesr.clsrcrcrdr-s        cCsdS)Nrcrcrcrcrd_check_docstrings(srDWITH_DOC_STRINGSztest requires docstringscCs`ddl}|dkrt}nd}|j|||d\}}|rBtd||ftrXtd|j|f||fS)Nr)r optionflagsz%d of %d doctests failedz,doctest (%s) ... %d tests with zero failures)doctestrZtestmodr rr`)rr/rFrGrrrcrcrdr.9scCs tjjfS)N)rnryrrcrcrcrd modules_setupTsrHcCs:ddtjjD}tjjtjj|tjj|dS)NcSs"g|]\}}|jdr||fqS)z encodings.)rp)rArrrcrcrdrB[sz#modules_cleanup..)rnryrrrr)Z oldmodulesZ encodingsrcrcrdmodules_cleanupWs  rIcCs"trtjtjjfSdffSdS)Nr)_thread_count threading _danglingrrcrcrcrdrNzscGsJtsdSd}x8t|D],}tjtjf}||kr2PtjdtqWdS)Ndg{Gz?)rJrangerKrLrMrrr)Zoriginal_valuesZ _MAX_COUNTcountr'rcrcrdrOs cs"tsStjfdd}|S)Nc st}z|St|XdS)N)rNrO)rkey)rrcrdrszreap_threads..decorator)rJrr)rrrc)rrdrPsN@ccstj}z dVWdtj}||}xjtj}||kr8Ptj|kr|tj|}d||d|dd|d|d }t|tjdtq&WXdS)Nz!wait_threads() failed to cleanup z threads after z.1fz seconds (count: z , old count: )g{Gz?)rJrKrZ monotonicrzrr)rZ old_countZ start_timeZdeadlinerPZdtrwrcrcrdwait_threads_exits   $ rTc CsZttdrVd}xFy2tj|tj\}}|dkr.Ptd|tjdWqPYqXqWdS)Nwaitpidrrz2Warning -- reap_children() reaped child process %s)rrX)rrrUWNOHANGrrnr)Z any_processr0ZstatusrcrcrdrLs ccs*t|}g}zZy$x|D]}|j|j|qWWn*trVtdt|t|fYnXdVWdz|rt|tj}}xltddD]^}|d7}x$|D]}|jt |tjdqWdd|D}|sPtrtdt||fqWWdd d|D}|r"t j t j td t|XXdS) Nz/Can't start %d threads, only %d threads startedrr<g{Gz?cSsg|]}|jr|qSrc)isAlive)rArrcrcrdrBsz!start_threads..z7Unable to join %d threads during a period of %d minutescSsg|]}|jr|qSrc)rX)rArrcrcrdrBszUnable to join %d threads)r{rrrrrrrOrmax faulthandlerZdump_tracebackrnrrz)ZthreadsZunlockr rZendtimeZ starttimerrcrcrdrQs>     c csnt||rtt|tt|}|r(|t|8}tdd|D}|S)Ncss(|] }|jd s|jdr|VqdS)___N)rpendswith)rArrcrcrd sz&detect_api_mismatch..)rrx)Zref_apiZ other_apireZ missing_itemsrcrcrdr< s  cCs|dkr|jf}nt|tr"|f}t|}xbt|D]V}|jds4||krLq4t||}t|dd|kst|d r4t|tj  r4|j |q4W|j |j |dS)Nrra) r`rrtrrxrprrtypes ModuleTypeaddZassertCountEqual__all__)Z test_caserZname_of_moduleZextraZ blacklistZexpectedrurrcrcrdr= s)    c@s$eZdZdZdZddZddZdS)rZNc Csrtjjdrddl}|jj|_d}|jj||_|jj|j|Byddl }|j Wnt t fk rlYnLXi|_ x|j|j|jgD].}|j ||j}|j||j}||f|j |<qWntdk r y*tjtj|_tjtjd|jdfWnttfk rYnXtjdkrndddd g}tj|tjtjd }||jd} WdQRX| jd krntd d dd|S)Nrrrrrz/usr/bin/defaultsr]zcom.apple.CrashReporterZ DialogType)rrs developerz:this test triggers the Crash Reporter, that is intentionalrT)endr ) rnrorprrr_k32 SetErrorMode old_valuemsvcrtCrtSetReportModerrm old_modesCRT_WARN CRT_ERROR CRT_ASSERTZCRTDBG_MODE_FILECrtSetReportFileZCRTDBG_FILE_STDERRrZ getrlimit RLIMIT_CORE setrlimitrrrrPIPEZ communicaterar) rfrZSEM_NOGPFAULTERRORBOXr report_typeold_modeold_filecmdprocrrcrcrdr2 sN        zSuppressCrashReport.__enter__c Gs|jdkrdStjjdrl|jj|j|jrddl}xj|jjD]$\}\}}|j |||j ||qBWn6t dk ryt j t j |jWnttfk rYnXdS)Nrr)rrnrorprrrrrrrrrrrr)rfrrrrrrcrcrdrs s   zSuppressCrashReport.__exit__)r`rarbrrrrrcrcrcrdrZ) sAc srtdyjWn$ttfk r@tdYnXdfdd}|j|t|dS)NFTcs rtn tdS)N)rr[rc) attr_is_local attr_nameobject_to_patchrrcrdcleanup szpatch..cleanup)rrjrrZ addCleanupr)Z test_instancerrZ new_valuerrc)rrrrrdpatch s  rc CsFy ddl}Wntk r YnX|jr4tjdddl}|j|S)NrzUrun_in_subinterp() cannot be used if tracemalloc module is tracing memory allocations) tracemallocrmZ is_tracingrrrsrrun_in_subinterp)rrrrcrcrdr s  rcsHGfddd|}d||||jttt|jdS)NcseZdZfddZdS)z%check_free_after_iterating..Ac s*dy tWntk r$YnXdS)NT)next StopIteration)rf)doneitrcrd__del__ s  z-check_free_after_iterating..A.__del__N)r`rarbrrc)rrrcrdA srF)rKrrrZ assertTrue)rrrCrrrc)rrrdcheck_free_after_iterating s   rcCs|ddlm}m}m}|j}|j|xP|jD]F}|r@||kr@q.t||}|rPn |dkrZq.|j|ddkr.|dSq.WdS)Nr) ccompilerrspawn) Z distutilsrrrZ new_compilerZcustomize_compilerZ executablesrZfind_executable)Z cmd_namesrrrZcompilerrurrcrcrdr^ s     cCs@d}tr6||kr6tdkr.tjddgjdkatr6|}tj|S)Ngh㈵>Zgetpropzro.kernel.qemu1)rA_is_android_emulatorrZ check_outputrarnrD)ZintervalZminimum_intervalrcrcrdrD s c cs>tjj}tj}ztjdVWd|r8tj|ddXdS)NT)rZ all_threads)rnrrFrZ is_enabledrr)r@rrcrcrddisable_faulthandler s  rc /Cstjjd r8ytjd}t|dStk r6YnXd}ttdrjytjd}Wnt k rhYnXd}tjdkryd dl }|j Wnt t fk rYn0Xi}x(|j|j|jfD]}|j |d ||<qWzpd }xft|D]Z}ytj|}Wn4t k r(}z|jtjkrWYdd}~XqXtj||d7}qWWd|dk rzx*|j|j|jfD]}|j |||q`WX|S) Nlinuxfreebsdz /proc/self/fdrsysconf SC_OPEN_MAXrr)rr)rnrorprrrrrrrrrrrmrrrrOduprZEBADFr) namesZMAXFDrrrrPr@Zfd2rrcrcrdr_ sP          c@s$eZdZddZddZddZdS) SaveSignalsc Csjddl}||_ttd|j|_x>dD]6}yt||}Wntk rNw&YnX|jj|q&Wi|_dS)NrrSIGKILLSIGSTOP)rr) signalr{rONSIGsignalsrrryrf)rfrZsignamesignumrcrcrdrgM s zSaveSignals.__init__cCs4x.|jD]$}|jj|}|dkr"q||j|<qWdS)N)rr getsignalrf)rfrhandlerrcrcrdsaveZ s   zSaveSignals.savecCs*x$|jjD]\}}|jj||q WdS)N)rfrr)rfrrrcrcrdrestoref szSaveSignals.restoreN)r`rarbrgrrrcrcrcrdrD s   rc@s$eZdZddZddZddZdS)FakePathcCs ||_dS)N)r)rfrrcrcrdrgn szFakePath.__init__cCsd|jdS)Nz )r)rfrcrcrd__repr__q szFakePath.__repr__cCs6t|jts$t|jtr,t|jtr,|jn|jSdS)N)rr BaseExceptionrrx)rfrcrcrd __fspath__t s    zFakePath.__fspath__N)r`rarbrgrrrcrcrcrdrk src cs.tj}ztj|dVWdtj|XdS)N)rnget_int_max_str_digitsset_int_max_str_digits)Z max_digitsZcurrentrcrcrdadjust_int_max_str_digits| s   r)T)F)F)N)Nii@i@i@ii) rrrrrrrrrr r!)r%r#r&r'r()NF)F)r5F)N)Fi@ii)T)N)Nr)rR)N(r`rmcollections.abc collections contextlibZdatetimerrZr?rrrrlimportlib.utilrZlogging.handlersrerrrorurrrrrrnrr*rrrrZ urllib.errorr_rfZ testresultrrJrLZmultiprocessing.processr&zlibrVbz2Zlzmarrrrr r r rsr contextmanagerrjr r~rr:r;rr6rrrrr0rrrrrrrprrrrrrrrrrrr r!rr"r#r$r%rErrr rGrHrJrrFrrZ SOCK_MAX_SIZEZ skipUnlessr __getformat__r7r9r0r1r2r@rrrArCrurrr.Z FS_NONASCII characterfsdecodefsencode UnicodeErrorZTESTFN_UNICODEZ unicodedata normalizegetfilesystemencodingZTESTFN_ENCODINGZTESTFN_UNENCODABLEZgetwindowsversionencodeUnicodeEncodeErrorrdecodeUnicodeDecodeErrorZTESTFN_UNDECODABLEZTESTFN_NONASCIIr2rr]r}r1r4rrrKrrrrZTEST_SUPPORT_DIRr9rr[rrr[rGr&rIobjectrcrrRrrrSrabcMutableMappingrTrr'rrr(rr)r*r+rrrrrrrrrrrrrrrrUr\rrrZ_4GrrrYr r3r4r,rrr>r5r!r%r?r"rBr*r+r-r2r3r<r5r7rAr-rDZMISSING_C_DOCSTRINGSrZHAVE_DOCSTRINGSZrequires_docstringsr.rHrIZenvironment_alteredrNrOrPrTrLrQrWrVrbZskipIfZrequires_type_collectingrcrdrfrgrMrXrrrr/rzr|r8r~rr<r=rZrrrr^rrDrr_rrrrcrcrcrds                      2   !  J    > %                 %      2 ' 5M           $ # 0           (        " #   "    :_"  ;'PK!Bx==+support/__pycache__/__init__.cpython-36.pycnu[3 f@s^ dZedkredddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZ ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl!Z"ddl#Z#ddl$m%Z%yddl&Z&ddl'Z'Wnek rBdZ&dZ'YnXy ddl(Z)Wnek rjdZ)YnXy ddl*Z*Wnek rdZ*YnXy ddl+Z+Wnek rdZ+YnXy ddl,Z,Wnek rdZ,YnXy ddl-Z-Wnek r dZ-YnXy ddl.Z.Wnek r2dZ.YnXy ddl/Z/Wnek rZdZ/YnXddd d d d d ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;dd?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbg\Z0Gdcd d e1Z2Gddd d e2Z3Gdedde2Z4Gdfdde j5Z6ej7dfdhdiZ8dgfdkdldZ9dmdnZ:dodpZ;dqd=ZZ=ffdjfdsdZ>dtd9Z?dZ@dZAdaBdaCdZDdjZEdaFdudZGdvdZHdwdZIdxdyZJejjKdzr.dhd{d|ZLd}d~ZMddZNddZOddZPnejQZMejRZNddZOddZPddZQddZRddZSddZTddZUddZVdd#ZWdidd$ZXddZYdd%ZZdd&Z[dd'Z\djdd(Z]dZ^dZ_ej`ejafddJZbe^fddKZcddMZdddZeeeZfddZgdmZhdpZie jjekjldjKddZme jje*dZne jje+dZoe jje,dZpe jje-dZqejjKdZrejsdZtetdk oxetdkZuejdkreurdndZvndZvejwdkrdZxndZxdjyexejzZxdZ{xLdqD]BZ|yej}ej~e|e|kreWnek rYnXe|Z{PqWexdZejdkr:ddlZejdeZejZdZejwdkrejjdkrexdZyejeWnek rYnXedeefdZnBejdkrydjeWn&ek rexdjedǃZYnXdZxFdrD]dYZd?d@Ze jeedAdBZdCdDZdEdFZGdGdPdPejjZGdHd[d[eZdadId!ZdJd2ZdadKdLZdMd;ZdNdOZdPd"ZfdQdRd?Z dfffdSd@Z GdTd]d]Z dUdVZ dWdXZ ffdYdZZgfd[daZdad\dGZej7d]d^Zd_dbZGd`dadaZGdbdcdcZej7dddeZdS(z7Supporting definitions for the Python regression tests.z test.supportz.support must be imported from the test packageN)get_test_runner PIPE_MAX_SIZEverbose max_memuse use_resourcesfailfastError TestFailed TestDidNotRunResourceDenied import_moduleimport_fresh_module CleanImportunloadforgetrecord_original_stdoutget_original_stdoutcaptured_stdoutcaptured_stdincaptured_stderrTESTFNSAVEDCWDunlinkrmtreetemp_cwdfindfilecreate_empty_file can_symlinkfs_is_case_insensitiveis_resource_enabledrequiresrequires_freebsd_versionrequires_linux_versionrequires_mac_verrequires_hashdigestcheck_syntax_errorTransientResourcetime_outsocket_peer_resetioerror_peer_resettransient_internetBasicTestRunner run_unittest run_doctestskip_unless_symlink requires_gzip requires_bz2 requires_lzma bigmemtestbigaddrspacetest cpython_only get_attributerequires_IEEE_754skip_unless_xattr requires_zlibanticipate_failureload_package_testsdetect_api_mismatch check__all__requires_android_levelrequires_multiprocessing_queue is_jython is_androidcheck_impl_detail unix_shellsetswitchintervalHOST IPV6_ENABLEDfind_unused_port bind_portopen_urlresourcebind_unix_socket temp_umask reap_children TestHandlerthreading_setupthreading_cleanup reap_threads start_threadscheck_warningscheck_no_resource_warningEnvironmentVarGuardrun_with_locale swap_item swap_attrMatcher set_memlimitSuppressCrashReportsortdict run_with_tzPGOmissing_compiler_executablefd_countc@seZdZdZdS)r z*Base class for regression test exceptions.N)__name__ __module__ __qualname____doc__rdrd-/usr/lib64/python3.6/test/support/__init__.pyr |sc@seZdZdZdS)r z Test failed.N)r`rarbrcrdrdrdrer sc@seZdZdZdS)r zTest did not run any subtests.N)r`rarbrcrdrdrdrer sc@seZdZdZdS)r zTest skipped because it requested a disallowed resource. This is raised when a test calls requires() for a resource that has not be enabled. It is used to distinguish between expected and unexpected skips. N)r`rarbrcrdrdrdrer sTc cs8|r.tjtjddtdVWdQRXndVdS)zContext manager to suppress package and module deprecation warnings when importing them. If ignore is False, this context manager has no effect. ignorez.+ (module|package)N)warningscatch_warningsfilterwarningsDeprecationWarning)rfrdrdre_ignore_deprecated_importss  rkF) required_oncCsft|Ty tj|Stk rV}z&tjjt|r8tj t |WYdd}~XnXWdQRXdS)acImport and return the module to be tested, raising SkipTest if it is not available. If deprecated is True, any module or package deprecation messages will be suppressed. If a module is required on a platform but optional for others, set required_on to an iterable of platform prefixes which will be compared against sys.platform. N) rk importlibr ImportErrorsysplatform startswithtupleunittestSkipTeststr)name deprecatedrlmsgrdrdrer s  cCs^|tjkrt|tj|=x>ttjD]0}||ks@|j|dr&tj|||<tj|=q&WdS)zyHelper function to save and remove a module from sys.modules Raise ImportError if the module can't be imported. .N)romodules __import__listrq)rv orig_modulesmodnamerdrdre_save_and_remove_modules rc Cs>d}ytj|||<Wntk r.d}YnXdtj|<|S)zHelper function to save and block a module in sys.modules Return True if the module was in sys.modules, False otherwise. TFN)rorzKeyError)rvr}Zsavedrdrdre_save_and_block_modules  rcCs|r tjSddS)zDecorator to mark a test that is known to be broken in some cases Any use of this decorator should have a comment identifying the associated tracker issue. cSs|S)Nrd)frdrdresz$anticipate_failure..)rsZexpectedFailure)Z conditionrdrdrer:scCsF|dkr d}tjjtjjtjjt}|j|||d}|j||S)zGeneric load_tests implementation for simple test packages. Most packages can implement load_tests using this function as follows: def load_tests(*args): return load_package_tests(os.path.dirname(__file__), *args) Nztest*)Z start_dirZ top_level_dirpattern)ospathdirname__file__ZdiscoverZaddTests)Zpkg_dirloaderZstandard_testsrZtop_dirZ package_testsrdrdrer;s c Cst|i}g}t||zfyHx|D]}t||q&Wx |D]}t||s>|j|q>Wtj|}Wntk r~d}YnXWdx|jD]\} } | tj | <qWx|D] } tj | =qWX|SQRXdS)aImport and return a module, deliberately bypassing sys.modules. This function imports and returns a fresh copy of the named Python module by removing the named module from sys.modules before doing the import. Note that unlike reload, the original module is not affected by this operation. *fresh* is an iterable of additional module names that are also removed from the sys.modules cache before doing the import. *blocked* is an iterable of module names that are replaced with None in the module cache during the import to ensure that attempts to import them raise ImportError. The named module and any modules named in the *fresh* and *blocked* parameters are saved before starting the import and then reinserted into sys.modules when the fresh import is complete. Module and package deprecation messages are suppressed during this import if *deprecated* is True. This function will raise ImportError if the named module cannot be imported. N) rkrrappendrmr rnitemsrorz) rvZfreshZblockedrwr}Znames_to_removeZ fresh_nameZ blocked_nameZ fresh_moduleZ orig_namemoduleZname_to_removerdrdrers$      c Cs>yt||}Wn&tk r4tjd||fYnX|SdS)z?Get an attribute, raising SkipTest if AttributeError is raised.zobject %r has no attribute %rN)getattrAttributeErrorrsrt)objrvZ attributerdrdrer6s cCs|adS)N)_original_stdout)stdoutrdrdrer0scCs tptjS)N)rrorrdrdrdrer4sc Cs&y tj|=Wntk r YnXdS)N)rorzr)rvrdrdrer7s cGsny||Stk rh}zDtdkrHtd|jj|ftd|j|ftj|tj||Sd}~XnXdS)Nz%s: %sz re-run %s%r) OSErrorrprint __class__r`rchmodstatS_IRWXU)rfuncargserrrdrdre _force_run=srwincCs|||r|}ntjj|\}}|p(d}d}x<|dkrjtj|}|rJ|n||ksVdStj||d9}q0Wtjd|tdddS)NrygMbP?g?rz)tests may fail, delete still pending for ) stacklevel) rrsplitlistdirtimesleeprgwarnRuntimeWarning)rpathnamewaitallrrvtimeoutLrdrdre_waitforHs     rcCsttj|dS)N)rrr)filenamerdrdre_unlinkisrcCsttj|dS)N)rrrmdir)rrdrdre_rmdirlsrcs,fddt|ddtdd|dS)Ncsxt|tj|D]}tjj||}ytj|j}Wn<tk rn}z td||ft j dd}WYdd}~XnXt j |rt |ddt|tj|qt|tj|qWdS)Nz-support.rmtree(): os.lstat(%r) failed with %s)filerT)r)rrrrjoinlstatst_moderrro __stderr__rS_ISDIRrrr)rrvfullnamemodeexc) _rmtree_innerrdrerps   z_rmtree.._rmtree_innerT)rcSst|tj|S)N)rrr)prdrdrersz_rmtree..)r)rrd)rre_rmtreeos rc Cs^y ddl}Wntk r Yn:X|jt|d}|jjj||t|}|rZ|d|S|S)Nrr)ctypesrnZcreate_unicode_bufferlenwindllkernel32ZGetLongPathNameW)rrbufferZlengthrdrdre _longpaths    rc sFytj|dStk r"YnXfdd|tj|dS)Nc sx~t|tj|D]l}tjj||}ytj|j}Wntk rJd}YnXtj |rn|t|tj |qt|tj |qWdS)Nr) rrrrrrrrrrrr)rrvrr)rrdrers  z_rmtree.._rmtree_inner)shutilrrrr)rrd)rrers  cCs|S)Nrd)rrdrdrersc Cs*y t|Wnttfk r$YnXdS)N)rFileNotFoundErrorNotADirectoryError)rrdrdrers c Cs&y t|Wntk r YnXdS)N)rr)rrdrdrers rc Cs&y t|Wntk r YnXdS)N)rr)rrdrdrers cCsBtjj|}tjjtjj|}tjj||d}tj|||S)aMove a PEP 3147/488 pyc file to its legacy pyc location. :param source: The file system path to the source file. The source file does not need to exist, however the PEP 3147/488 pyc file must exist. :return: The file system path to the legacy pyc file. c) rmutilcache_from_sourcerrrabspathrrename)sourceZpyc_fileZup_oneZ legacy_pycrdrdremake_legacy_pycs   rcCs\t|xNtjD]D}tjj||d}t|dx dD]}ttjj||dq8WqWdS) z'Forget' a module was ever imported. This removes the module from sys.modules and deletes any PEP 3147/488 or legacy .pyc files. z.pyrrr) optimizationN)rrr) rrorrrrrmrr)r~rroptrdrdrers    csttdrtjSd}tjjdrddlddld}d}Gfdddj}j j }|j }|sjj |}j j}|j||j|j|j|}|sj t|j|@sd}ntjdkrVdd lm} mm} m} dd lm} | j| d } | jdkrd }nFGfd dd| }|}| |}| j|dksR| j|dkrVd}|sy.ddlm}|}|j|j |j!Wn\t"k r}z>t#|}t$|dkr|ddd}dj%t&|j'|}WYdd}~XnX|t_(| t_tjS)Nresultrrrcs.eZdZdjjfdjjfdjjfgZdS)z*_is_gui_available..USEROBJECTFLAGSZfInheritZ fReserveddwFlagsN)r`rarbwintypesZBOOLDWORD_fields_rd)rrdreUSEROBJECTFLAGSs  rz,gui not available (WSF_VISIBLE flag not set)darwin)cdllc_intpointer Structure) find_libraryZApplicationServicesz0gui tests cannot run without OS X window managercseZdZdfdfgZdS)z._is_gui_available..ProcessSerialNumberZ highLongOfPSNZ lowLongOfPSNN)r`rarbrrd)rrdreProcessSerialNumbersrz#cannot run without OS X gui process)Tk2z [...]zTk unavailable due to {}: {}))hasattr_is_gui_availablerrorprqrZctypes.wintypesrrZuser32ZGetProcessWindowStationZWinErrorrrZGetUserObjectInformationWZbyrefZsizeofboolrrrrZ ctypes.utilrZ LoadLibraryZCGMainDisplayIDZGetCurrentProcessZSetFrontProcessZtkinterrZwithdrawupdateZdestroy Exceptionrurformattyper`reason)rZ UOI_FLAGSZ WSF_VISIBLErZdllhZuofZneededresrrrrZ app_servicesrZpsnZpsn_prrooteZ err_stringrd)rrrersh         rcCstdkp|tkS)zTest whether a resource is enabled. Known resources are set by regrtest.py. If not running under regrtest.py, all resources are assumed enabled unless use_resources has been set. N)r)resourcerdrdrer $scCs>t|s |dkrd|}t||dkr:t r:ttjdS)z@Raise ResourceDenied if the specified resource is not available.Nz"Use of the %r resource not enabledgui)r r rr)rrxrdrdrer!,s csfdd}|S)zDecorator raising SkipTest if the OS is `sysname` and the version is less than `min_version`. For example, @_requires_unix_version('FreeBSD', (7, 2)) raises SkipTest if the FreeBSD version is less than 7.2. cs$tjfdd}|_|S)Nc stjkrztjjddd}yttt|jd}Wntk rLYn.X|krzdjtt }t j d||f||S)N-rrryz(%s version %s or higher required, not %s) rpsystemreleaserrrmapint ValueErrorrrursrt)rkw version_txtversionmin_version_txt)r min_versionsysnamerdrewrapper=s z:_requires_unix_version..decorator..wrapper) functoolswrapsr)rr)rr)rre decorator<sz)_requires_unix_version..decoratorrd)rrrrd)rrre_requires_unix_version5srcGs td|S)zDecorator raising SkipTest if the OS is FreeBSD and the FreeBSD version is less than `min_version`. For example, @requires_freebsd_version(7, 2) raises SkipTest if the FreeBSD version is less than 7.2. ZFreeBSD)r)rrdrdrer"PscGs td|S)zDecorator raising SkipTest if the OS is Linux and the Linux version is less than `min_version`. For example, @requires_linux_version(2, 6, 32) raises SkipTest if the Linux version is less than 2.6.32. ZLinux)r)rrdrdrer#Yscsfdd}|S)zDecorator raising SkipTest if the OS is Mac OS X and the OS X version if less than min_version. For example, @requires_mac_ver(10, 5) raises SkipTest if the OS X version is lesser than 10.5. cs"tjfdd}|_|S)Nc sxtjdkrntjd}yttt|jd}Wntk rBYn,X|krndjtt }t j d||f||S)Nrrryz&Mac OS X %s or higher required, not %s) rorpZmac_verrrrrrrrrursrt)rrrrr)rrrdrerjs   z4requires_mac_ver..decorator..wrapper)rrr)rr)r)rrerisz#requires_mac_ver..decoratorrd)rrrd)rrer$bs csfdd}|S)aDecorator raising SkipTest if a hashing algorithm is not available The hashing algorithm could be missing or blocked by a strict crypto policy. If 'openssl' is True, then the decorator checks that OpenSSL provides the algorithm. Otherwise the check falls back to built-in implementations. ValueError: [digital envelope routines: EVP_DigestInit_ex] disabled for FIPS ValueError: unsupported hash type md4 cstjfdd}|S)Nc sXy&rtdk rtjn tjWn&tk rLtjddYnX||S)Nz hash digest 'z' is not available.)_hashlibnewhashlibrrsrt)rkwargs) digestnameropensslrdrers  z7requires_hashdigest..decorator..wrapper)rr)rr)rr)rrers z&requires_hashdigest..decoratorrd)rrrrd)rrrer%}s z 127.0.0.1z::1cCs"tj||}t|}|j~|S)a Returns an unused port that should be suitable for binding. This is achieved by creating a temporary socket with the same family and type as the 'sock' parameter (default is AF_INET, SOCK_STREAM), and binding it to the specified host address (defaults to 0.0.0.0) with the port set to 0, eliciting an unused ephemeral port from the OS. The temporary socket is then closed and deleted, and the ephemeral port is returned. Either this method or bind_port() should be used for any tests where a server socket needs to be bound to a particular port for the duration of the test. Which one to use depends on whether the calling code is creating a python socket, or if an unused port needs to be provided in a constructor or passed to an external program (i.e. the -accept argument to openssl's s_server mode). Always prefer bind_port() over find_unused_port() where possible. Hard coded ports should *NEVER* be used. As soon as a server socket is bound to a hard coded port, the ability to run multiple instances of the test simultaneously on the same host is compromised, which makes the test a ticking time bomb in a buildbot environment. On Unix buildbots, this may simply manifest as a failed test, which can be recovered from without intervention in most cases, but on Windows, the entire python process can completely and utterly wedge, requiring someone to log in to the buildbot and manually kill the affected process. (This is easy to reproduce on Windows, unfortunately, and can be traced to the SO_REUSEADDR socket option having different semantics on Windows versus Unix/Linux. On Unix, you can't have two AF_INET SOCK_STREAM sockets bind, listen and then accept connections on identical host/ports. An EADDRINUSE OSError will be raised at some point (depending on the platform and the order bind and listen were called on each socket). However, on Windows, if SO_REUSEADDR is set on the sockets, no EADDRINUSE will ever be raised when attempting to bind two identical host/ports. When accept() is called on each socket, the second caller's process will steal the port from the first caller, leaving them both in an awkwardly wedged state where they'll no longer respond to any signals or graceful kills, and must be forcibly killed via OpenProcess()/TerminateProcess(). The solution on Windows is to use the SO_EXCLUSIVEADDRUSE socket option instead of SO_REUSEADDR, which effectively affords the same semantics as SO_REUSEADDR on Unix. Given the propensity of Unix developers in the Open Source world compared to Windows ones, this is a common mistake. A quick look over OpenSSL's 0.9.8g source shows that they use SO_REUSEADDR when openssl.exe is called with the 's_server' option, for example. See http://bugs.python.org/issue2550 for more info. The following site also has a very thorough description about the implications of both REUSEADDR and EXCLUSIVEADDRUSE on Windows: http://msdn2.microsoft.com/en-us/library/ms740621(VS.85).aspx) XXX: although this approach is a vast improvement on previous attempts to elicit unused ports, it rests heavily on the assumption that the ephemeral port returned to us by the OS won't immediately be dished back out to some other process when we close and delete our temporary socket but before our calling code has a chance to bind the returned port. We can deal with this issue if/when we come across it. )socketrHclose)familyZsocktypeZtempsockportrdrdrerGs 8 c Cs|jtjkr|jtjkrttdr>|jtjtjdkr>t dttdr~y |jtjtj dkrft dWnt k r|YnXttdr|j tjtj d|j|df|jd}|S)a%Bind the socket to a free port and return the port number. Relies on ephemeral ports in order to ensure we are using an unbound port. This is important as many tests may be running simultaneously, especially in a buildbot environment. This method raises an exception if the sock.family is AF_INET and sock.type is SOCK_STREAM, *and* the socket has SO_REUSEADDR or SO_REUSEPORT set on it. Tests should *never* set these socket options for TCP/IP sockets. The only case for setting these options is testing multicasting via multiple UDP sockets. Additionally, if the SO_EXCLUSIVEADDRUSE socket option is available (i.e. on Windows), it will be set on the socket. This will prevent anyone else from bind()'ing to our host/port for the duration of the test. SO_REUSEADDRrzHtests should never set the SO_REUSEADDR socket option on TCP/IP sockets! SO_REUSEPORTzHtests should never set the SO_REUSEPORT socket option on TCP/IP sockets!SO_EXCLUSIVEADDRUSEr)rrAF_INETr SOCK_STREAMrZ getsockoptZ SOL_SOCKETrr rrZ setsockoptrbindZ getsockname)sockhostrrdrdrerHs     c CsJ|jtjksty|j|Wn&tk rD|jtjdYnXdS)zBBind a unix socket, raising SkipTest if PermissionError is raised.zcannot bind AF_UNIX socketsN) rrZAF_UNIXAssertionErrorr PermissionErrorrrsrt)r ZaddrrdrdrerJs cCsZtjrVd}z.dec)rr)rrrd)rresystem_must_validate_certs rriZdoubleZIEEEztest requires IEEE 754 doublesz requires zlibz requires gzipz requires bz2z requires lzmajavaANDROID_API_LEVELwin32z/system/bin/shz/bin/shz$testz@testz {}_{}_tmpæİŁφКא،تก €u -àòɘŁğrZNFDntru-共Ł♡ͣztWARNING: The filename %r CAN be encoded by the filesystem encoding (%s). Unicode filename tests may not be effectives-surrogateescapewrccsd}|dkr&tj}d}tjj|}nBytj|d}Wn.tk rf|sNtjd|t ddYnX|rttj }z |VWd|r|tj krt |XdS)aReturn a context manager that creates a temporary directory. Arguments: path: the directory to create temporarily. If omitted or None, defaults to creating a temporary directory using tempfile.mkdtemp. quiet: if False (the default), the context manager raises an exception on error. Otherwise, if the path is specified and cannot be created, only a warning is issued. FNTz+tests may fail, unable to create temp dir: )r) tempfilemkdtemprrrealpathmkdirrrgrrgetpidr)rquietZ dir_createdpidrdrdretemp_dirs&   r3ccsftj}ytj|Wn.tk rD|s,tjd|tddYnXztjVWdtj|XdS)agReturn a context manager that changes the current working directory. Arguments: path: the directory to use as the temporary current working directory. quiet: if False (the default), the context manager raises an exception on error. Otherwise, it issues only a warning and keeps the current working directory the same. z)tests may fail, unable to change CWD to: r+)rN)rgetcwdchdirrrgrr)rr1Z saved_dirrdrdre change_cwd s  r6tempcwdccs:t||d$}t||d }|VWdQRXWdQRXdS)a Context manager that temporarily creates and changes the CWD. The function temporarily changes the current working directory after creating a temporary directory in the current directory with name *name*. If *name* is None, the temporary directory is created using tempfile.mkdtemp. If *quiet* is False (default) and it is not possible to create or change the CWD, an error is raised. If *quiet* is True, only a warning is raised and the original CWD is used. )rr1)r1N)r3r6)rvr1Z temp_pathZcwd_dirrdrdrer$sumaskc cs&tj|}z dVWdtj|XdS)z8Context manager that temporarily sets the process umask.N)rr8)r8ZoldmaskrdrdrerK8s  datacCsbtjj|r|S|dk r&tjj||}tgtj}x*|D]"}tjj||}tjj|r8|Sq8W|S)a[Try to find a file on sys.path or in the test directory. If it is not found the argument passed to the function is returned (this does not necessarily signal failure; could still be the legitimate path). Setting *subdir* indicates a relative path to use to find the file rather than looking directly in the path directories. N)rrisabsr TEST_HOME_DIRroexists)rZsubdirrZdnfnrdrdrerIs    cCs(tj|tjtjBtjB}tj|dS)z>Create an empty file. If the file already exists, truncate it.N)ropenO_WRONLYO_CREATO_TRUNCr)rfdrdrdrer[scCs,t|j}dd|D}dj|}d|S)z%Like repr(dict), but in sorted order.cSsg|] }d|qS)z%r: %rrd).0Zpairrdrdre cszsortdict..z, z{%s})sortedrr)dictrZ reprpairsZ withcommasrdrdrer[`s  c Cs*ttd}z|jS|jttXdS)z` Create an invalid file descriptor by opening and closing a file and return its fd. wbN)r>rfilenorr)rrdrdre make_bad_fdgs  rI)linenooffsetc Csp|jt}t|ddWdQRX|j}|j|j|dk rJ|j|j||j|j|dk rl|j|j|dS)Nz exec) assertRaises SyntaxErrorcompileZ exceptionZassertIsNotNonerJ assertEqualrK)testcaseZ statementrJrKcmrrdrdrer&ss   c sVddl}ddl}jdd|jj|djdd}tjjt |}fdd}tjj |r|||}|dk rt|St |t dt rtd |td |jj}tr|jjd|j|d d}tr|jjdd krtj|d}zBt|d.} |j} x| r| j| |j} qWWdQRXWd|jX||}|dk rF|Std|dS)Nrcheckr/rcs>t|f}dkr|S|r2|jd|S|jdS)Nr)r>seekr)r=r)rrSrrdrecheck_valid_files z*open_urlresource..check_valid_fileZurlfetchz fetching %s ...)rAccept-Encodinggzip)rzContent-Encoding)ZfileobjrGzinvalid resource %r)rWrX)Zurllib.requestZ urllib.parsepopparseZurlparserrrr TEST_DATA_DIRr<rr!rrrZrequestZ build_openerrXZ addheadersrr>ZheadersgetZGzipFilereadwriterr ) Zurlrrurllibrr=rVropeneroutsrd)rrSrrerI~s<         c@s4eZdZdZddZddZeddZdd Zd S) WarningsRecorderzyConvenience wrapper for the warnings list returned on entry to the warnings.catch_warnings() context manager. cCs||_d|_dS)Nr) _warnings_last)selfZ warnings_listrdrdre__init__szWarningsRecorder.__init__cCsDt|j|jkr t|jd|S|tjjkr0dStd||fdS)Nrz%r has no attribute %rrZ)rrfrgrrgWarningMessage_WARNING_DETAILSr)rhattrrdrdre __getattr__s  zWarningsRecorder.__getattr__cCs|j|jdS)N)rfrg)rhrdrdrergszWarningsRecorder.warningscCst|j|_dS)N)rrfrg)rhrdrdreresetszWarningsRecorder.resetN) r`rarbrcrirmpropertyrgrnrdrdrdreres  rec cs tjd}|jjd}|r"|jtjdd }tjdjdt |VWdQRXt |}g}xz|D]r\}}d} xH|ddD]8}|j } t j |t| t jrt| j|rd} |j|qW| rf| rf|j||jfqfW|rtd |d |rtd |d dS) zCatch the warnings, then check if all the expected warnings have been raised and re-raise unexpected warnings. If 'quiet' is True, only re-raise the unexpected warnings. rZ__warningregistry__T)recordrgalwaysNFzunhandled warning %srz)filter (%r, %s) did not catch any warning)ro _getframe f_globalsr^clearrgrhrz simplefilterrer|messagerematchruI issubclassrremoverr`r) filtersr1frameregistrywZreraiseZmissingrxcatseenZwarningrdrdre_filterwarningss0    rcOs.|jd}|s$dtff}|dkr$d}t||S)aContext manager to silence warnings. Accept 2-tuples as positional arguments: ("message regexp", WarningCategory) Optional argument: - if 'quiet' is True, it does not fail if a filter catches nothing (default True without argument, default False if some filters are defined) Without argument, it defaults to: check_warnings(("", Warning), quiet=True) r1rNT)r^Warningr)r|rr1rdrdrerRs   rc csHtjdd&}tjd||ddV|r.tWdQRX|j|gdS)aContext manager to check that no warnings are emitted. This context manager enables a given warning within its scope and checks that no warnings are emitted even with that warning enabled. If force_gc is True, a garbage collection is attempted before checking for warnings. This may help to catch warnings emitted when objects are deleted, such as ResourceWarning. Other keyword arguments are passed to warnings.filterwarnings(). T)rprq)rvcategoryN)rgrhri gc_collectrP)rQrvrZforce_gcwarnsrdrdrecheck_no_warningssrc csBtjdd }tjdtddVtWdQRX|j|gdS)a"Context manager to check that no ResourceWarning is emitted. Usage: with check_no_resource_warning(self): f = open(...) ... del f You must remove the object which may emit ResourceWarning before the end of the context manager. T)rprq)rN)rgrhriResourceWarningrrP)rQrrdrdrerSs c@s(eZdZdZddZddZddZdS) ra,Context manager to force import to return a new module reference. This is useful for testing module-level behaviours, such as the emission of a DeprecationWarning on import. Use like this: with CleanImport("foo"): importlib.import_module("foo") # new reference cGsNtjj|_x<|D]4}|tjkrtj|}|j|kr>tj|j=tj|=qWdS)N)rorzcopyoriginal_modulesr`)rhZ module_namesZ module_namerrdrdreri?s      zCleanImport.__init__cCs|S)Nrd)rhrdrdre __enter__LszCleanImport.__enter__cGstjj|jdS)N)rorzrr)rh ignore_excrdrdre__exit__OszCleanImport.__exit__N)r`rarbrcrirrrdrdrdrer3s  c@sheZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZdS)rTz_Class to help protect the environment variable properly. Can be used as a context manager.cCstj|_i|_dS)N)renviron_environ_changed)rhrdrdreriXszEnvironmentVarGuard.__init__cCs |j|S)N)r)rhenvvarrdrdre __getitem__\szEnvironmentVarGuard.__getitem__cCs*||jkr|jj||j|<||j|<dS)N)rrr^)rhrvaluerdrdre __setitem___s zEnvironmentVarGuard.__setitem__cCs2||jkr|jj||j|<||jkr.|j|=dS)N)rrr^)rhrrdrdre __delitem__es  zEnvironmentVarGuard.__delitem__cCs |jjS)N)rkeys)rhrdrdrerlszEnvironmentVarGuard.keyscCs t|jS)N)iterr)rhrdrdre__iter__oszEnvironmentVarGuard.__iter__cCs t|jS)N)rr)rhrdrdre__len__rszEnvironmentVarGuard.__len__cCs |||<dS)Nrd)rhrrrdrdresetuszEnvironmentVarGuard.setcCs ||=dS)Nrd)rhrrdrdreunsetxszEnvironmentVarGuard.unsetcCs|S)Nrd)rhrdrdrer{szEnvironmentVarGuard.__enter__cGsJx<|jjD].\}}|dkr0||jkr:|j|=q ||j|<q W|jt_dS)N)rrrrr)rhrkvrdrdrer~s   zEnvironmentVarGuard.__exit__N)r`rarbrcrirrrrrrrrrrrdrdrdrerTSsc@s(eZdZdZddZddZddZdS) DirsOnSysPathaContext manager to temporarily add directories to sys.path. This makes a copy of sys.path, appends any directories given as positional arguments, then reverts sys.path to the copied settings when the context ends. Note that *all* sys.path modifications in the body of the context manager, including replacement of the object, will be reverted at the end of the block. cGs(tjdd|_tj|_tjj|dS)N)rororiginal_valueoriginal_objectextend)rhpathsrdrdreriszDirsOnSysPath.__init__cCs|S)Nrd)rhrdrdrerszDirsOnSysPath.__enter__cGs|jt_|jtjdd<dS)N)rrorr)rhrrdrdrerszDirsOnSysPath.__exit__N)r`rarbrcrirrrdrdrdrers rc@s*eZdZdZddZddZd ddZdS) r'zRaise ResourceDenied if an exception is raised while the context manager is in effect that matches the specified exception and attributes.cKs||_||_dS)N)rattrs)rhrrrdrdreriszTransientResource.__init__cCs|S)Nrd)rhrdrdrerszTransientResource.__enter__NcCsT|dk rPt|j|rPx:|jjD]$\}}t||s4Pt|||kr Pq WtddS)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)rzrrrrrr )rhZtype_r tracebackrlZ attr_valuerdrdrers zTransientResource.__exit__)NNN)r`rarbrcrirrrdrdrdrer's)errnog>@)rerrnosc #spd!d"d#d$d%d&g}d(d*d,d.d/g}td||gsRdd|Ddd|Dfdd}tj}zy|dk rtj|dVWntjk r}z&trtjj j dd|WYdd}~Xnt k rZ}zpx^|j }t |d krt |dt r|d}n*t |dkr8t |d t r8|d }nPqW||WYdd}~XnXWdtj|XdS)0zReturn a context manager that raises ResourceDenied when various issues with the Internet connection manifest themselves as exceptions. ECONNREFUSEDo ECONNRESETh EHOSTUNREACHq ENETUNREACHe ETIMEDOUTn EADDRNOTAVAILc EAI_AGAINr+EAI_FAILr EAI_NONAMEr EAI_NODATA WSANO_DATA*zResource %r is not availablecSsg|]\}}tt||qSrd)rr)rCrvnumrdrdrerDsz&transient_internet..cSsg|]\}}tt||qSrd)rr)rCrvrrdrdrerDscst|dd}t|tjst|tjr,|kst|tjjrTd|jkoNdknst|tjj rd|j ksd|j ksd|j ks|krt st j jjdd|dS) NriiWConnectionRefusedError TimeoutErrorEOFErrorr )r isinstancerrZgaierrorraerrorZ HTTPErrorcodeZURLErrorrrrostderrr`r)rn)captured_errnosdenied gai_errnosrdre filter_errors     z(transient_internet..filter_errorNrrr)rr)rr)rr)rr)rr)rr)rr)rr)rr)rr)rr)r rZgetdefaulttimeoutZsetdefaulttimeoutnntplibZNNTPTemporaryErrorrrorr`rrrr) Z resource_namerrZdefault_errnosZdefault_gai_errnosrZ old_timeoutrard)rrrrer+sP     c csFddl}tt|}tt||jztt|VWdtt||XdS)zReturn a context manager used by captured_stdout/stdin/stderr that temporarily replaces the sys stream *stream_name* with a StringIO.rN)iorrosetattrStringIO)Z stream_namerZ orig_stdoutrdrdrecaptured_outputs  rcCstdS)zCapture the output of sys.stdout: with captured_stdout() as stdout: print("hello") self.assertEqual(stdout.getvalue(), "hello\n") r)rrdrdrdrerscCstdS)zCapture the output of sys.stderr: with captured_stderr() as stderr: print("hello", file=sys.stderr) self.assertEqual(stderr.getvalue(), "hello\n") r)rrdrdrdrer%scCstdS)a Capture the input to sys.stdin: with captured_stdin() as stdin: stdin.write('hello\n') stdin.seek(0) # call test code that consumes from sys.stdin captured = input() self.assertEqual(captured, "hello") stdin)rrdrdrdrer.s cCs*tjtrtjdtjtjdS)aForce as many objects as possible to be collected. In non-CPython implementations of Python, this is needed because timely deallocation is not guaranteed by the garbage collector. (Even in CPython this can be the case in case of reference cycles.) This means that __del__ methods may be called later than expected and weakrefs may remain alive for longer than expected. This function tries its best to force all garbage objects to disappear. g?N)gcZcollectr@rrrdrdrdrer;s  rc cs.tj}tjz dVWd|r(tjXdS)N)r isenableddisableenable)Zhave_gcrdrdre disable_gcKs  rcCs:tjdp d}d}x|jD]}|jdr|}qW|dkS)z,Find if Python was built with optimizations. PY_CFLAGSrz-O-O0-Og)rrr) sysconfigget_config_varrrq)ZcflagsZ final_optrrdrdrepython_is_optimizedVs  rZnPZ0ngettotalrefcountZ2PZ0PrcCstjt|tS)N)structcalcsize_header_align)fmtrdrdre calcobjsizegsrcCstjt|tS)N)rr_vheaderr)rrdrdre calcvobjsizejsr cCspddl}tj|}t|tkr(|jt@sBt|tkrLt|jt@rL||j7}dt|||f}|j|||dS)Nrz&wrong size for %s: got %d, expected %d) _testcapiro getsizeofr __flags___TPFLAGS_HEAPTYPE_TPFLAGS_HAVE_GCZSIZEOF_PYGC_HEADrP)testosizerrrxrdrdre check_sizeofqs  rcsfdd}|S)Ncs$fdd}j|_j|_|S)Ncsy ddl}t|}|j|}Wn(tk r6YnBd}}Yn0Xx,D]$}y|j||PWqPYqPXqPWz ||S|r|r|j||XdS)Nr)localer setlocaler)rkwdsrrZ orig_localeloc)catstrrlocalesrdreinners$     z1run_with_locale..decorator..inner)r`rc)rr)rr)rrersz"run_with_locale..decoratorrd)rrrrd)rrrerUscsfdd}|S)Ncs"fdd}j|_j|_|S)Ncsy tj}Wntk r(tjdYnXdtjkr@tjd}nd}tjd<|z ||S|dkrrtjd=n |tjd<tjXdS)Nztzset requiredZTZ)rtzsetrrsrtrr)rrrZorig_tz)rtzrdrers       z-run_with_tz..decorator..inner)r`rc)rr)r)rrerszrun_with_tz..decoratorrd)rrrd)rrer\s cCsdttdtd}tjd|tjtjB}|dkr>td|ftt|j d||j dj }|a |t krrt }|t dkrtd|f|adS)Ni)rmgtz(\d+(\.\d+)?) (K|M|G|T)b?$zInvalid memory limit %rrr+z$Memory limit %r too low to be useful)_1M_1Grwrx IGNORECASEVERBOSErrfloatgrouplowerreal_max_memuseMAX_Py_ssize_t_2Gr)limitZsizesrZmemlimitrdrdrerYs $ c@s(eZdZdZddZddZddZdS) _MemoryWatchdogz`An object which periodically watches the process' memory consumption and prints it out. cCsdjtjd|_d|_dS)Nz/proc/{pid}/statm)r2F)rrr0procfilestarted)rhrdrdrerisz_MemoryWatchdog.__init__cCsyt|jd}Wn<tk rL}z tjdj|ttjj dSd}~XnXt d}t j tj |g|t jd|_|jd|_dS)Nrz!/proc not available for stats: {}zmemory_watchdog.py)rrT)r>r rrgrrrrorflushr subprocessPopen executableZDEVNULL mem_watchdogrr )rhrrZwatchdog_scriptrdrdrestarts   z_MemoryWatchdog.startcCs|jr|jj|jjdS)N)r rZ terminatewait)rhrdrdrestops z_MemoryWatchdog.stopN)r`rarbrcrirrrdrdrdrer sr csfdd}|S)atDecorator for bigmem tests. 'size' is a requested size for the test (in arbitrary, test-interpreted units.) 'memuse' is the number of bytes per unit for the test, or a good estimate of it. For example, a test that needs two byte buffers, of 4 GiB each, could be decorated with @bigmemtest(size=_4G, memuse=2). The 'size' argument is normally passed to the decorated test method as an extra argument. If 'dry_run' is true, the value passed to the test method may be less than the requested value. If 'dry_run' is false, it means the test doesn't support dummy runs when -M is not specified. cs fdd__S)Nc sj}j}tsd}n|}ts$ rFt||krFtjd||dtr|tr|ttdj||ddt}|j nd}z ||S|r|j XdS) Niz'not enough memory: %.1fG minimum neededir+z* ... expected peak memory use: {peak:.1f}G)Zpeaki@i@) rmemuserrsrtrrrr rr)rhrrmaxsizeZwatchdog)dry_runrrrdrers*    z.bigmemtest..decorator..wrapper)rr)r)rrr)rrrerszbigmemtest..decoratorrd)rrrrrd)rrrrer3s !csfdd}|S)z0Decorator for tests that fill the address space.csDttkr8td kr$td kr$tjdq@tjdtd n|SdS) Nr?rz-not enough memory: try a 32-bit build insteadz'not enough memory: %.1fG minimum neededir+llli@)rrrsrt)rh)rrdrer3sz!bigaddrspacetest..wrapperrd)rrrd)rrer41s c@seZdZddZdS)r,cCstj}|||S)N)rsZ TestResult)rhrrrdrdrerunDszBasicTestRunner.runN)r`rarbrrdrdrdrer,CscCs|S)Nrd)rrdrdre_idIsrcCs<|dkrt rtjtjSt|r(tStjdj|SdS)Nrzresource {0!r} is not enabled)rrsskiprr rr)rrdrdrerequires_resourceLs  rcCs&trt|krtjd|tfStSdS)Nz%s at Android API level %d)rA_ANDROID_API_LEVELrsrr)levelrrdrdrer>Ts  cCstdd|S)z9 Decorator for tests only applicable on CPython. T)cpython) impl_detail)rrdrdrer5[scKsVtf|rtS|dkrLt|\}}|r,d}nd}t|j}|jdj|}tj|S)Nz*implementation detail not available on {0}z%implementation detail specific to {0}z or ) rBr _parse_guardsrErrrrsr)rxguardsZ guardnamesdefaultrdrdrer!as   r!c CsTtdkr:ddl}y|jdaWntk r8daYnXd}trF|Stj||S)z8Skip decorator for tests that use multiprocessing.Queue.NrTFz6requires a functioning shared semaphore implementation)_have_mp_queuemultiprocessingZQueuernrsr)rr&rxrdrdrer?os cCsH|sddidfSt|jd}t|j|gt|ks>t|| fS)Nr TFr)r|valuesrr)r#Zis_truerdrdrer"~s  r"cKs t|\}}|jtjj|S)a5This function returns True or False depending on the host platform. Examples: if check_impl_detail(): # only on CPython (default) if check_impl_detail(jython=True): # only on Jython if check_impl_detail(cpython=False): # everywhere except on CPython )r"r^rpZpython_implementationr)r#r$rdrdrerBs cs,ttdsStjfdd}|SdS)zEDecorator to temporarily turn off tracing for the duration of a test.gettracec s.tj}ztjd||Stj|XdS)N)ror(settrace)rrZoriginal_trace)rrdrers   zno_tracing..wrapperN)rrorr)rrrd)rre no_tracings r*cCs tt|S)aDecorator for tests which involve reference counting. To start, the decorator does not run the test if is not run by CPython. After that, any trace function is unset during the test to prevent unexpected refcounts caused by the trace function. )r*r5)rrdrdre refcount_testsr+cCsRg}xB|jD]8}t|tjr2t|||j|q ||r |j|q W||_dS)z>Recursively filter test cases in a suite based on a predicate.N)Z_testsrrs TestSuite _filter_suiter)suiteZpredZnewtestsrrdrdrer-s    r-cCsttjttdk d}|j|}tdk r4tj|j|js>t |j st |j dkrl|j rl|j dd}n6t |j dkr|j r|j dd}nd}ts|d7}t|dS)z2Run tests from a unittest.TestSuite-derived class.N) verbosityZcapture_outputrrzmultiple errors occurredz!; run in verbose mode for details)rrorrjunit_xml_listrrZget_xml_elementZtestsRunr Z wasSuccessfulrerrorsZfailuresr )r.Zrunnerrrrdrdre _run_suites"  r2cCstdkr dSt|jSdS)NT)_match_test_funcid)rrdrdre match_testsr5cCsd|kotjd| S)Nryz[?*\[\]])rwsearch)rrdrdre_is_full_match_testsr7csr|tkr dS|sd}f}nHttt|r4t|j}n.djttj|}t j |j fdd}|}t |a|a dS)N|cs$|r dStt|jdSdS)NTry)anyrr)Ztest_id) regex_matchrdrematch_test_regexsz)set_match_tests..match_test_regex)_match_test_patternsallrr7r __contains__rfnmatch translaterwrOrxrrr3)ZpatternsrZregexr;rd)r:reset_match_testss   rAcGstjtjf}tj}xh|D]`}t|trT|tjkrJ|jtjtj|qzt dqt||rj|j|q|jtj |qWt |t t |dS)z1Run tests from unittest.TestCase-derived classes.z)str arguments must be keys in sys.modulesN)rsr,ZTestCaserrurorzZaddTestZ findTestCasesrZ makeSuiter-r5r2)classesZ valid_typesr.clsrdrdrer-s        cCsdS)z,Just used to check if docstrings are enabledNrdrdrdrdre_check_docstrings(srDWITH_DOC_STRINGSztest requires docstringscCs`ddl}|dkrt}nd}|j|||d\}}|rBtd||ftrXtd|j|f||fS)aRun doctest on the given module. Return (#failures, #tests). If optional argument verbosity is not specified (or is None), pass support's belief about verbosity on to doctest. Else doctest's usual behavior is used (it searches sys.argv for -v). rN)r optionflagsz%d of %d doctests failedz,doctest (%s) ... %d tests with zero failures)doctestrZtestmodr rr`)rr/rFrGrrrdrdrer.9scCs tjjfS)N)rorzrrdrdrdre modules_setupTsrHcCs:ddtjjD}tjjtjj|tjj|dS)NcSs"g|]\}}|jdr||fqS)z encodings.)rq)rCrrrdrdrerD[sz#modules_cleanup..)rorzrrtr)Z oldmodulesZ encodingsrdrdremodules_cleanupWs  rIcCs"trtjtjjfSdffSdS)Nr)_thread_count threading _danglingrrdrdrdrerNzscGsJtsdSd}x8t|D],}tjtjf}||kr2PtjdtqWdS)Ndg{Gz?)rJrangerKrLrMrrr)Zoriginal_valuesZ _MAX_COUNTcountr'rdrdrerOs cs"tsStjfdd}|S)zUse this function when threads are being used. This will ensure that the threads are cleaned up even when the test fails. If threading is unavailable this function does nothing. c st}z|St|XdS)N)rNrO)rkey)rrdrerszreap_threads..decorator)rJrr)rrrd)rrerPsN@ccstj}z dVWdtj}||}xjtj}||kr8Ptj|kr|tj|}d||d|dd|d|d }t|tjdtq&WXdS) aH bpo-31234: Context manager to wait until all threads created in the with statement exit. Use _thread.count() to check if threads exited. Indirectly, wait until threads exit the internal t_bootstrap() C function of the _thread module. threading_setup() and threading_cleanup() are designed to emit a warning if a test leaves running threads in the background. This context manager is designed to cleanup threads started by the _thread.start_new_thread() which doesn't allow to wait for thread exit, whereas thread.Thread has a join() method. Nz!wait_threads() failed to cleanup z threads after z.1fz seconds (count: z , old count: )g{Gz?)rJrKrZ monotonicrrr)rZ old_countZ start_timeZdeadlinerPZdtrxrdrdrewait_threads_exits   $ rTc CsZttdrVd}xFy2tj|tj\}}|dkr.Ptd|tjdWqPYqXqWdS)zUse this function at the end of test_main() whenever sub-processes are started. This will help ensure that no extra children (zombies) stick around to hog resources and create problems when looking for refleaks. waitpidrrz2Warning -- reap_children() reaped child process %s)rNrZ)rrrUWNOHANGrror)Z any_processr2ZstatusrdrdrerLs ccs*t|}g}zZy$x|D]}|j|j|qWWn*trVtdt|t|fYnXdVWdz|rt|tj}}xltddD]^}|d7}x$|D]}|jt |tjdqWdd|D}|sPtrtdt||fqWWdd d|D}|r"t j t j td t|XXdS) Nz/Can't start %d threads, only %d threads startedrr<g{Gz?cSsg|]}|jr|qSrd)isAlive)rCrrdrdrerDsz!start_threads..z7Unable to join %d threads during a period of %d minutescSsg|]}|jr|qSrd)rX)rCrrdrdrerDszUnable to join %d threads)r|rrrrrrrOrmax faulthandlerZdump_tracebackrorr)ZthreadsZunlockr rZendtimeZ starttimerrdrdrerQs>     c csnt||rrrwrHrprrwrxrrrrr)ruZtmp_dirZtmp_fpZtmp_namefpZkernel_versionrrdrdre can_xattr s,    r|cCs t}d}|r|Stj||S)zDSkip decorator for tests that require functional extended attributesz(no non-broken extended attribute support)r|rsr)rrvrxrdrdrer8 scCs$t pt}d}|r|Stj||S)z;Skip decorator for tests not run in (non-extended) PGO taskz#Not run for (non-extended) PGO task)r] PGO_EXTENDEDrsr)rrvrxrdrdreskip_if_pgo_task s r~cCs^tj|dH}|j}|j}||kr,|j}ytjj||Stk rNdSXWdQRXdS)zKDetects if the file system for the specified directory is case-insensitive.)rxFN) r,ZNamedTemporaryFilervupperrrrsamefiler)Z directorybase base_pathZ case_pathrdrdrer s)rfcCs>tt|tt|}|r(|t|8}tdd|D}|S)a Returns the set of items in ref_api not in other_api, except for a defined list of items to be ignored in this check. By default this skips private attributes beginning with '_' but includes all magic methods, i.e. those starting and ending in '__'. css(|] }|jd s|jdr|VqdS)___N)rqendswith)rCrrdrdre sz&detect_api_mismatch..)rrx)Zref_apiZ other_apirfZ missing_itemsrdrdrer< s  cCs|dkr|jf}nt|tr"|f}t|}xbt|D]V}|jds4||krLq4t||}t|dd|kst|d r4t|tj  r4|j |q4W|j |j |dS)aAssert that the __all__ variable of 'module' contains all public names. The module's public names (its API) are detected automatically based on whether they match the public name convention and were defined in 'module'. The 'name_of_module' argument can specify (as a string or tuple thereof) what module(s) an API could be defined in in order to be detected as a public API. One case for this is when 'module' imports part of its public API from other modules, possibly a C backend (like 'csv' and its '_csv'). The 'extra' argument can be a set of names that wouldn't otherwise be automatically detected as "public", like objects without a proper '__module__' attribute. If provided, it will be added to the automatically detected ones. The 'blacklist' argument can be a set of names that must not be treated as part of the public API even though their names indicate otherwise. Usage: import bar import foo import unittest from test import support class MiscTestCase(unittest.TestCase): def test__all__(self): support.check__all__(self, foo) class OtherTestCase(unittest.TestCase): def test__all__(self): extra = {'BAR_CONST', 'FOO_CONST'} blacklist = {'baz'} # Undocumented name. # bar imports part of its API from _bar. support.check__all__(self, bar, ('bar', '_bar'), extra=extra, blacklist=blacklist) Nrra) r`rrurrxrqrrtypes ModuleTypeaddZassertCountEqual__all__)Z test_caserZname_of_moduleZextraZ blacklistZexpectedrvrrdrdrer= s)    c@s(eZdZdZdZdZddZddZdS)rZzTry to prevent a crash report from popping up. On Windows, don't display the Windows Error Reporting dialog. On UNIX, disable the creation of coredump file. Nc Csrtjjdrddl}|jj|_d}|jj||_|jj|j|Byddl }|j Wnt t fk rlYnLXi|_ x|j|j|jgD].}|j ||j}|j||j}||f|j |<qWntdk r y*tjtj|_tjtjd|jdfWnttfk rYnXtjdkrnddd d g}tj|tjtjd }||jd} WdQRX| jd krntd ddd|S)zOn Windows, disable Windows Error Reporting dialogs using SetErrorMode. On UNIX, try to save the previous core file size limit, then set soft limit to 0. rrNrrrz/usr/bin/defaultsr_zcom.apple.CrashReporterZ DialogType)rrs developerz:this test triggers the Crash Reporter, that is intentionalrT)endr ) rorprqrrr_k32 SetErrorMode old_valuemsvcrtCrtSetReportModerrn old_modesCRT_WARN CRT_ERROR CRT_ASSERTZCRTDBG_MODE_FILECrtSetReportFileZCRTDBG_FILE_STDERRrZ getrlimit RLIMIT_CORE setrlimitrrrrPIPEZ communicaterar) rhrZSEM_NOGPFAULTERRORBOXr report_typeold_modeold_filecmdprocrrdrdrer2 sN        zSuppressCrashReport.__enter__c Gs|jdkrdStjjdrl|jj|j|jrddl}xj|jjD]$\}\}}|j |||j ||qBWn6t dk ryt j t j |jWnttfk rYnXdS)zARestore Windows ErrorMode or core file behavior to initial value.Nrr)rrorprqrrrrrrrrrrrr)rhrrrrrrdrdrers s   zSuppressCrashReport.__exit__)r`rarbrcrrrrrdrdrdrerZ) s Ac srtdyjWn$ttfk r@tdYnXdfdd}|j|t|dS)zOverride 'object_to_patch'.'attr_name' with 'new_value'. Also, add a cleanup procedure to 'test_instance' to restore 'object_to_patch' value for 'attr_name'. The 'attr_name' should be a valid attribute for 'object_to_patch'. FNTcs rtn tdS)N)rr[rd) attr_is_local attr_nameobject_to_patchrrdrecleanup szpatch..cleanup)rrjrrZ addCleanupr)Z test_instancerrZ new_valuerrd)rrrrrepatch s  rc CsFy ddl}Wntk r YnX|jr4tjdddl}|j|S)zi Run code in a subinterpreter. Raise unittest.SkipTest if the tracemalloc module is enabled. rNzUrun_in_subinterp() cannot be used if tracemalloc module is tracing memory allocations) tracemallocrnZ is_tracingrsrtrrun_in_subinterp)rrrrdrdrer s  rcsHGfddd|}d||||jttt|jdS)NcseZdZfddZdS)z%check_free_after_iterating..Ac s*dy tWntk r$YnXdS)NT)next StopIteration)rh)doneitrdre__del__ s  z-check_free_after_iterating..A.__del__N)r`rarbrrd)rrrdreA srF)rMrrrZ assertTrue)rrrCrrrd)rrrecheck_free_after_iterating s   rcCsddlm}m}m}|j}|j|xd|jD]Z}|r@||kr@q.t||}|rd|dk sntd|n |dkrnq.|j |ddkr.|dSq.WdS)a<Check if the compiler components used to build the interpreter exist. Check for the existence of the compiler executables whose names are listed in 'cmd_names' or all the compiler executables when 'cmd_names' is empty and return the first missing executable or None when none is found missing. r) ccompilerrspawnNz%the '%s' executable is not configured) Z distutilsrrrZ new_compilerZcustomize_compilerZ executablesrrZfind_executable)Z cmd_namesrrrZcompilerrvrrdrdrer^ s       cCs@d}tr6||kr6tdkr.tjddgjdkatr6|}tj|S)Ngh㈵>Zgetpropzro.kernel.qemu1)rA_is_android_emulatorrZ check_outputrarorD)ZintervalZminimum_intervalrdrdrerD s c cs>tjj}tj}ztjdVWd|r8tj|ddXdS)NT)rZ all_threads)rorrHrZ is_enabledrr)rBrrdrdredisable_faulthandler s  rc /Cstjjd r8ytjd}t|dStk r6YnXd}ttdrjytjd}Wnt k rhYnXd}tjd kryd dl }|j Wnt t fk rYn0Xi}x(|j|j|jfD]}|j |d ||<qWzpd }xft|D]Z}ytj|}Wn4t k r(}z|jtjkrWYdd}~XqXtj||d7}qWWd|dk rzx*|j|j|jfD]}|j |||q`WX|S) z/Count the number of open file descriptors. linuxfreebsdz /proc/self/fdrsysconf SC_OPEN_MAXNrr)rr)rorprqrrrrrrrrrrrnrrrrOduprZEBADFr) namesZMAXFDrrrrPrBZfd2rrdrdrer_ sP          c@s(eZdZdZddZddZddZdS) SaveSignalsz Save an restore signal handlers. This class is only able to save/restore signal handlers registered by the Python signal module: see bpo-13285 for "external" signal handlers. c Csjddl}||_ttd|j|_x>dD]6}yt||}Wntk rNw&YnX|jj|q&Wi|_dS)NrrSIGKILLSIGSTOP)rr) signalr|rONSIGsignalsrrr{rf)rhrZsignamesignumrdrdreriM s zSaveSignals.__init__cCs4x.|jD]$}|jj|}|dkr"q||j|<qWdS)N)rr getsignalrf)rhrhandlerrdrdresaveZ s   zSaveSignals.savecCs*x$|jjD]\}}|jj||q WdS)N)rfrr)rhrrrdrdrerestoref szSaveSignals.restoreN)r`rarbrcrirrrdrdrdrerD s  rc@s(eZdZdZddZddZddZdS) FakePathz.Simple implementing of the path protocol. cCs ||_dS)N)r)rhrrdrdrerin szFakePath.__init__cCsd|jdS)Nz )r)rhrdrdre__repr__q szFakePath.__repr__cCs6t|jts$t|jtr,t|jtr,|jn|jSdS)N)rr BaseExceptionrrz)rhrdrdre __fspath__t s    zFakePath.__fspath__N)r`rarbrcrirrrdrdrdrerk src cs.tj}ztj|dVWdtj|XdS)z>Temporarily change the integer string conversion length limit.N)roget_int_max_str_digitsset_int_max_str_digits)Z max_digitsZcurrentrdrdreadjust_int_max_str_digits| s   r)T)F)F)N)Nii@i@i@ii) rrrrrrrr r!r"r#)r'r%r(r)r*)NF)F)r7F)N)Fi@ii)T)N)Nr)rR)N(rcr`rncollections.abc collections contextlibZdatetimerrZr?rrrrmimportlib.utilrZlogging.handlersrerrrprwrrrrrrorr,rrrsZ urllib.errorrargZ testresultrrJrLZmultiprocessing.processr&zlibrXbz2Zlzmarrrrr r r rtr contextmanagerrkr rrr:r;rr6rrrrr0rrrrrrrqrrrrrrrrrrrr r!rr"r#r$r%rErr r rGrHrJrrFrrZ SOCK_MAX_SIZEZ skipUnlessr __getformat__r7r9r0r1r2r@rrrArCrvrrr0Z FS_NONASCII characterfsdecodefsencode UnicodeErrorZTESTFN_UNICODEZ unicodedataZ normalizegetfilesystemencodingZTESTFN_ENCODINGZTESTFN_UNENCODABLEZgetwindowsversionencodeUnicodeEncodeErrorrdecodeUnicodeDecodeErrorZTESTFN_UNDECODABLEZTESTFN_NONASCIIr4rr]r}r3r6rrrKrrrrZTEST_SUPPORT_DIRr;rr]rrr[rIr&rIobjectrerrRrrrSrabcMutableMappingrTrr'rrr(rr)r*r+rrrrrrrrrrrrrrrrUr\rrrZ_4GrrrYr r3r4r,rrr>r5r!r%r?r"rBr*r+r-r2r3r<r5r7rAr-rDZMISSING_C_DOCSTRINGSZHAVE_DOCSTRINGSZrequires_docstringsr.rHrIZenvironment_alteredrNrOrPrTrLrQrWrVrbZskipIfZrequires_type_collectingrcrdrfrgrMrXrrrr/rzr|r8r~rr<r=rZrrrr^rrDrr_rrrrdrdrdres                      2   !  J    > %                 %      2 ' 5M            $ # 0           (        " #   "    :_"  ;'PK!Yj6support/__pycache__/script_helper.cpython-36.opt-1.pycnu[3 f)@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZmZdaddZGdddejdd#Zd d Zd dZddZddZejejdddZddZd$ddZd%ddZd&ddZd'd!d"ZdS)(N)source_from_cache)make_legacy_pycstrip_python_stderrc CsVtdkrRdtjkrdadSytjtjdddgWntjk rLdaYnXdatS)a  Returns True if our sys.executable interpreter requires environment variables in order to be able to run at all. This is designed to be used with @unittest.skipIf() to annotate tests that need to use an assert_python*() function to launch an isolated mode (-I) or no environment mode (-E) sub-interpreter process. A normal build & test does not run into this situation but it can happen when trying to run the standard library test suite from an interpreter that doesn't have an obvious home with Python's current home finding logic. Setting PYTHONHOME is one way to get most of the testsuite to run in that situation. PYTHONPATH or PYTHONUSERSITE are other common environment variables that might impact whether or not the interpreter can start. NZ PYTHONHOMETz-Ez-czimport sys; sys.exit(0)F)$__cached_interp_requires_environmentosenviron subprocessZ check_callsys executableZCalledProcessErrorr r 2/usr/lib64/python3.6/test/support/script_helper.py interpreter_requires_environments   r c@seZdZdZddZdS)_PythonRunResultz2Helper for reporting Python subprocess run resultscCsd }|j|j}}t||kr0d|| d}t||krNd|| d}|jddj}|jddj}td|j|||fdS) z4Provide helpful details about failed subcommand runsPds(... truncated stdout ...)Ns(... truncated stderr ...)asciireplacezRProcess return code is %d command line: %r stdout: --- %s --- stderr: --- %s ---i@)outerrlendecoderstripAssertionErrorrc)selfcmd_linemaxlenrrr r r fail>s   z_PythonRunResult.failN)__name__ __module__ __qualname____doc__rr r r r r;srrrrc Ost}d|kr|jd}n | o$| }tjddg}|rB|jdn| rX| rX|jd|jddri}tjdkrtjd|d<n tjj}d |krd |d <|j ||j |t j |t j t j t j |d }|*z|j\}}Wd|jt jXWdQRX|j} t|}t| |||fS) NZ __isolatedz-XZ faulthandlerz-Iz-EZ __cleanenvZwin32Z SYSTEMROOTTERM)stdinstdoutstderrenv)r popr r appendplatformrrcopyupdateextendrPopenPIPEZ communicatekill_cleanup returncoderr) argsenv_varsZ env_requiredisolatedrr'procrrrr r r run_python_until_end[s:            r7cOs4t||\}}|jr|s&|j r0| r0|j||S)N)r7rr)Zexpected_successr3r4resrr r r _assert_pythons r9cOstd||S)a| Assert that running the interpreter with `args` and optional environment variables `env_vars` succeeds (rc == 0) and return a (return code, stdout, stderr) tuple. If the __cleanenv keyword is set, env_vars is used as a fresh environment. Python is started in isolated mode (command line option -I), except if the __isolated keyword is set to False. T)T)r9)r3r4r r r assert_python_oks r:cOstd||S)z Assert that running the interpreter with `args` and optional environment variables `env_vars` fails (rc != 0) and return a (return code, stdout, stderr) tuple. See assert_python_ok() for more options. F)F)r9)r3r4r r r assert_python_failuresr;)r%r&cOsXtjg}ts|jd|j||jdttj}d|d<t j |ft j ||d|S)zRun a Python subprocess with the given arguments. kw is extra keyword args to pass to subprocess.Popen. Returns a Popen object. z-Er'Zvt100r")r$r%r&) r r r r)r- setdefaultdictrrrr.r/)r%r&r3kwrr'r r r spawn_pythons   r?cCs2|jj|jj}|jj|jtj|S)z?Run the given Popen process until completion and return stdout.)r$closer%readwaitrr1)pdatar r r kill_pythons    rEFcCsP|}|s|tjd7}tjj||}t|ddd}|j||jtj|S)Npywzutf-8)encoding) rextseppathjoinopenwriter@ importlibinvalidate_caches)Z script_dirscript_basenamesourceZ omit_suffixZscript_filename script_nameZ script_filer r r make_scripts rSc Cs|tjd}tjj||}tj|d}|dkr~|jtj}t|dkrr|ddkrrt t |}tjj |}|}n tjj |}|j |||j |tjj||fS)NziprG __pycache__)rrIrJrKzipfileZipFilesplitseprrrbasenamerMr@) zip_dir zip_basenamerRZ name_in_zip zip_filenamezip_namezip_filepartsZ legacy_pycr r r make_zip_scripts      rcr#cCstj|t|d|dS)N__init__)rmkdirrS)Zpkg_dirZ init_sourcer r r make_pkgs rfcs0g}t|dd}|j|tjj|} t|||} |j| |rjtj|dd}tj| dd} |j|| ffddtd|dD} tjj | d tjj| } |tj d} tjj || }t j |d }x&| D]}tjj || }|j ||qW|j | | |jx|D]}tj|q W|tjj || fS) Nrdr#T)doraisecsg|]}tjjg|qSr )rr[rK).0i)pkg_namer r sz make_zip_pkg..rgrTrG)rSr)rrJr\ py_compilecompiler-rangerKrIrXrYrMr@unlink)r]r^rkrPrQZdepthZcompiledrqZ init_nameZ init_basenamerRZ pkg_namesZscript_name_in_zipr_r`ranameZinit_name_in_zipr )rkr make_zip_pkgs.         rs)rrr)F)N)r#)rgF) collectionsrNr rZos.pathZtempfilerrn contextlibZshutilrXimportlib.utilrZ test.supportrrrr namedtuplerr7r9r:r;r/ZSTDOUTr?rErSrcrfrsr r r r s4 $3    PK!>Z6support/__pycache__/script_helper.cpython-36.opt-2.pycnu[3 f)@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZmZdaddZGdddejdd#Zd d Zd dZddZddZejejdddZddZd$ddZd%ddZd&ddZd'd!d"ZdS)(N)source_from_cache)make_legacy_pycstrip_python_stderrc CsVtdkrRdtjkrdadSytjtjdddgWntjk rLdaYnXdatS)NZ PYTHONHOMETz-Ez-czimport sys; sys.exit(0)F)$__cached_interp_requires_environmentosenviron subprocessZ check_callsys executableZCalledProcessErrorr r 2/usr/lib64/python3.6/test/support/script_helper.py interpreter_requires_environments   r c@seZdZddZdS)_PythonRunResultcCsd}|j|j}}t||kr0d|| d}t||krNd|| d}|jddj}|jddj}td|j|||fdS) NPds(... truncated stdout ...)s(... truncated stderr ...)asciireplacezRProcess return code is %d command line: %r stdout: --- %s --- stderr: --- %s ---i@)outerrlendecoderstripAssertionErrorrc)selfcmd_linemaxlenrrr r r fail>s   z_PythonRunResult.failN)__name__ __module__ __qualname__rr r r r r;srrrrc Ost}d|kr|jd}n | o$| }tjddg}|rB|jdn| rX| rX|jd|jddri}tjdkrtjd|d<n tjj}d |krd |d <|j ||j |t j |t j t j t j |d }|*z|j\}}Wd|jt jXWdQRX|j} t|}t| |||fS) NZ __isolatedz-XZ faulthandlerz-Iz-EZ __cleanenvZwin32Z SYSTEMROOTTERM)stdinstdoutstderrenv)r popr r appendplatformrrcopyupdateextendrPopenPIPEZ communicatekill_cleanup returncoderr) argsenv_varsZ env_requiredisolatedrr&procrrrr r r run_python_until_end[s:            r6cOs4t||\}}|jr|s&|j r0| r0|j||S)N)r6rr)Zexpected_successr2r3resrr r r _assert_pythons r8cOstd||S)NT)T)r8)r2r3r r r assert_python_oks r9cOstd||S)NF)F)r8)r2r3r r r assert_python_failuresr:)r$r%cOsXtjg}ts|jd|j||jdttj}d|d<t j |ft j ||d|S)Nz-Er&Zvt100r!)r#r$r%) r r r r(r, setdefaultdictrrrr-r.)r$r%r2kwrr&r r r spawn_pythons   r>cCs2|jj|jj}|jj|jtj|S)N)r#closer$readwaitrr0)pdatar r r kill_pythons    rDFcCsP|}|s|tjd7}tjj||}t|ddd}|j||jtj|S)Npywzutf-8)encoding) rextseppathjoinopenwriter? importlibinvalidate_caches)Z script_dirscript_basenamesourceZ omit_suffixZscript_filename script_nameZ script_filer r r make_scripts rRc Cs|tjd}tjj||}tj|d}|dkr~|jtj}t|dkrr|ddkrrt t |}tjj |}|}n tjj |}|j |||j |tjj||fS)NziprF __pycache__)rrHrIrJzipfileZipFilesplitseprrrbasenamerLr?) zip_dir zip_basenamerQZ name_in_zip zip_filenamezip_namezip_filepartsZ legacy_pycr r r make_zip_scripts      rbr"cCstj|t|d|dS)N__init__)rmkdirrR)Zpkg_dirZ init_sourcer r r make_pkgs recs0g}t|dd}|j|tjj|} t|||} |j| |rjtj|dd}tj| dd} |j|| ffddtd|dD} tjj | d tjj| } |tj d} tjj || }t j |d }x&| D]}tjj || }|j ||qW|j | | |jx|D]}tj|q W|tjj || fS) Nrcr"T)doraisecsg|]}tjjg|qSr )rrZrJ).0i)pkg_namer r sz make_zip_pkg..rfrSrF)rRr(rrIr[ py_compilecompiler,rangerJrHrWrXrLr?unlink)r\r]rjrOrPZdepthZcompiledrpZ init_nameZ init_basenamerQZ pkg_namesZscript_name_in_zipr^r_r`nameZinit_name_in_zipr )rjr make_zip_pkgs.         rr)rrr)F)N)r")rfF) collectionsrMr rZos.pathZtempfilerrm contextlibZshutilrWimportlib.utilrZ test.supportrrrr namedtuplerr6r8r9r:r.ZSTDOUTr>rDrRrbrerrr r r r s4 $3    PK!Yj0support/__pycache__/script_helper.cpython-36.pycnu[3 f)@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZmZdaddZGdddejdd#Zd d Zd dZddZddZejejdddZddZd$ddZd%ddZd&ddZd'd!d"ZdS)(N)source_from_cache)make_legacy_pycstrip_python_stderrc CsVtdkrRdtjkrdadSytjtjdddgWntjk rLdaYnXdatS)a  Returns True if our sys.executable interpreter requires environment variables in order to be able to run at all. This is designed to be used with @unittest.skipIf() to annotate tests that need to use an assert_python*() function to launch an isolated mode (-I) or no environment mode (-E) sub-interpreter process. A normal build & test does not run into this situation but it can happen when trying to run the standard library test suite from an interpreter that doesn't have an obvious home with Python's current home finding logic. Setting PYTHONHOME is one way to get most of the testsuite to run in that situation. PYTHONPATH or PYTHONUSERSITE are other common environment variables that might impact whether or not the interpreter can start. NZ PYTHONHOMETz-Ez-czimport sys; sys.exit(0)F)$__cached_interp_requires_environmentosenviron subprocessZ check_callsys executableZCalledProcessErrorr r 2/usr/lib64/python3.6/test/support/script_helper.py interpreter_requires_environments   r c@seZdZdZddZdS)_PythonRunResultz2Helper for reporting Python subprocess run resultscCsd }|j|j}}t||kr0d|| d}t||krNd|| d}|jddj}|jddj}td|j|||fdS) z4Provide helpful details about failed subcommand runsPds(... truncated stdout ...)Ns(... truncated stderr ...)asciireplacezRProcess return code is %d command line: %r stdout: --- %s --- stderr: --- %s ---i@)outerrlendecoderstripAssertionErrorrc)selfcmd_linemaxlenrrr r r fail>s   z_PythonRunResult.failN)__name__ __module__ __qualname____doc__rr r r r r;srrrrc Ost}d|kr|jd}n | o$| }tjddg}|rB|jdn| rX| rX|jd|jddri}tjdkrtjd|d<n tjj}d |krd |d <|j ||j |t j |t j t j t j |d }|*z|j\}}Wd|jt jXWdQRX|j} t|}t| |||fS) NZ __isolatedz-XZ faulthandlerz-Iz-EZ __cleanenvZwin32Z SYSTEMROOTTERM)stdinstdoutstderrenv)r popr r appendplatformrrcopyupdateextendrPopenPIPEZ communicatekill_cleanup returncoderr) argsenv_varsZ env_requiredisolatedrr'procrrrr r r run_python_until_end[s:            r7cOs4t||\}}|jr|s&|j r0| r0|j||S)N)r7rr)Zexpected_successr3r4resrr r r _assert_pythons r9cOstd||S)a| Assert that running the interpreter with `args` and optional environment variables `env_vars` succeeds (rc == 0) and return a (return code, stdout, stderr) tuple. If the __cleanenv keyword is set, env_vars is used as a fresh environment. Python is started in isolated mode (command line option -I), except if the __isolated keyword is set to False. T)T)r9)r3r4r r r assert_python_oks r:cOstd||S)z Assert that running the interpreter with `args` and optional environment variables `env_vars` fails (rc != 0) and return a (return code, stdout, stderr) tuple. See assert_python_ok() for more options. F)F)r9)r3r4r r r assert_python_failuresr;)r%r&cOsXtjg}ts|jd|j||jdttj}d|d<t j |ft j ||d|S)zRun a Python subprocess with the given arguments. kw is extra keyword args to pass to subprocess.Popen. Returns a Popen object. z-Er'Zvt100r")r$r%r&) r r r r)r- setdefaultdictrrrr.r/)r%r&r3kwrr'r r r spawn_pythons   r?cCs2|jj|jj}|jj|jtj|S)z?Run the given Popen process until completion and return stdout.)r$closer%readwaitrr1)pdatar r r kill_pythons    rEFcCsP|}|s|tjd7}tjj||}t|ddd}|j||jtj|S)Npywzutf-8)encoding) rextseppathjoinopenwriter@ importlibinvalidate_caches)Z script_dirscript_basenamesourceZ omit_suffixZscript_filename script_nameZ script_filer r r make_scripts rSc Cs|tjd}tjj||}tj|d}|dkr~|jtj}t|dkrr|ddkrrt t |}tjj |}|}n tjj |}|j |||j |tjj||fS)NziprG __pycache__)rrIrJrKzipfileZipFilesplitseprrrbasenamerMr@) zip_dir zip_basenamerRZ name_in_zip zip_filenamezip_namezip_filepartsZ legacy_pycr r r make_zip_scripts      rcr#cCstj|t|d|dS)N__init__)rmkdirrS)Zpkg_dirZ init_sourcer r r make_pkgs rfcs0g}t|dd}|j|tjj|} t|||} |j| |rjtj|dd}tj| dd} |j|| ffddtd|dD} tjj | d tjj| } |tj d} tjj || }t j |d }x&| D]}tjj || }|j ||qW|j | | |jx|D]}tj|q W|tjj || fS) Nrdr#T)doraisecsg|]}tjjg|qSr )rr[rK).0i)pkg_namer r sz make_zip_pkg..rgrTrG)rSr)rrJr\ py_compilecompiler-rangerKrIrXrYrMr@unlink)r]r^rkrPrQZdepthZcompiledrqZ init_nameZ init_basenamerRZ pkg_namesZscript_name_in_zipr_r`ranameZinit_name_in_zipr )rkr make_zip_pkgs.         rs)rrr)F)N)r#)rgF) collectionsrNr rZos.pathZtempfilerrn contextlibZshutilrXimportlib.utilrZ test.supportrrrr namedtuplerr7r9r:r;r/ZSTDOUTr?rErSrcrfrsr r r r s4 $3    PK!r&XX3support/__pycache__/testresult.cpython-36.opt-1.pycnu[3 \ @s6dZddlZddlZddlZddlZddlZddlZddljj Z ddl m Z Gdddej Z GdddZdd d Zdd d Zed kr2GdddejZejZejejeejZeeddejDZeejZejeZ e!dej"e!dddx(e j#e j$D]Z%e!e%j&ddqWe!dS)z=Test runner and result class for the regression test suite. N)datetimecseZdZdddZdddZfddZeddZfd d Zd$d d Z ddZ eddZ fddZ fddZ fddZfddZfddZfddZddZd d!Zd"d#ZZS)%RegressionTestResult=F -cs\tj||ddd|_tjd|_|jjdtjj dd|_ d|_ g|_ t ||_dS)Nr)stream descriptions verbosityTZ testsuitestart )super__init__bufferETZElement_RegressionTestResult__suitesetrZutcnowZ isoformat_RegressionTestResult__e!_RegressionTestResult__start_timeZ_RegressionTestResult__resultsbool_RegressionTestResult__verbose)selfrr r ) __class__//usr/lib64/python3.6/test/support/testresult.pyrs zRegressionTestResult.__init__cCsLy |j}Wntk r"t|SXy|Stk rBt|SXt|S)N)idAttributeErrorstr TypeErrorrepr)clstestZtest_idrrrZ__getIds   zRegressionTestResult.__getIdcsVtj|tj|jd|_}tj|_|j rR|j j |j |d|j j dS)NZtestcasez ... )r startTestr SubElementrrtime perf_counterrrrwritegetDescriptionflush)rr!e)rrrr"+s   zRegressionTestResult.startTestFc KsP|j}d|_|dkrdS|jd|jd|j||jd|jdd|jd|jdd|jrz|jdtj|jd|r|jdk r|jjj }|t j |d_ |j dk r|j jj }|t j |d _ x|jD]t\}}| s| rqt j ||} t|d r>xD|jD],\} } | r,| j| t| n t| | _ q Wqt|| _ qWdS) NnameZstatusrunresultZ completedr$z0.6fz system-outz system-erritems)rrpop_RegressionTestResult__getIdrr$r%Z_stdout_buffergetvaluerstriprr#textZ_stderr_bufferr-hasattrr) rr!Zcaptureargsr)stdoutstderrkvZe2Zk2Zv2rrr _add_result3s4     z RegressionTestResult._add_resultcCs|jr|jj|ddS)Nr)rrr&)rcZwordrrrZ__writeSszRegressionTestResult.__writecCslt|tr0|jdkr|j}q8|jd|j}nt|}tj||d}tj|||}|dj|dj|dS)Nbuiltins.)typemessager=) isinstancer> __module____name__r tracebackformat_exceptionjoin)r Zerr_typeZ err_valueZerr_tbtypenamemsgtbrrrZ__makeErrorDictWs  z$RegressionTestResult.__makeErrorDictcs4|j|d|j|dtj|||jdddS)NT)errorEERROR)r9$_RegressionTestResult__makeErrorDictr addError_RegressionTestResult__write)rr!err)rrrrMjszRegressionTestResult.addErrorcs4|j|d|j|dtj|||jdddS)NT)outputxzexpected failure)r9rLr addExpectedFailurerN)rr!rO)rrrrRosz'RegressionTestResult.addExpectedFailurecs4|j|d|j|dtj|||jdddS)NT)ZfailureFFAIL)r9rLr addFailurerN)rr!rO)rrrrUtszRegressionTestResult.addFailurecs2|j||dtj|||jdd|dS)N)ZskippedSzskipped )r9r addSkiprN)rr!reason)rrrrWyszRegressionTestResult.addSkipcs&|j|tj||jdddS)Nr<ok)r9r addSuccessrN)rr!)rrrrZ~s  zRegressionTestResult.addSuccesscs*|j|ddtj||jdddS)NZUNEXPECTED_SUCCESS)Zoutcomeuzunexpected success)r9r addUnexpectedSuccessrN)rr!)rrrr\s z)RegressionTestResult.addUnexpectedSuccesscCs2|jr|jjd|jd|j|jd|jdS)NrrKrT)rrr&printErrorListerrorsfailures)rrrr printErrorss z RegressionTestResult.printErrorscCs`xZ|D]R\}}|jj|j|jj|d|j|d|jj|j|jjd|qWdS)Nz: rz%s )rr& separator1r' separator2)rZflavorr^r!rOrrrr]s z#RegressionTestResult.printErrorListcCsH|j}|jdt|j|jdtt|j|jdtt|j|S)NZtestsr^r_)rrrZtestsRunlenr^r_)rr)rrrget_xml_elements z$RegressionTestResult.get_xml_element)F)rBrA __qualname__rarbr classmethodr/r"r9rNrLrMrRrUrWrZr\r`r]rd __classcell__rr)rrrs"           rc@seZdZdddZddZdS)QuietRegressionTestRunnerFcCst|dd|_||j_dS)Nr)rr,r)rrrrrrrsz"QuietRegressionTestRunner.__init__cCs||j|jS)N)r,)rr!rrrr+s zQuietRegressionTestRunner.runN)F)rBrArerr+rrrrrhs rhFcCs&|rtjtjt||dStjt|dS)N)Z resultclassrr )r) functoolspartialunittestZTextTestRunnerrrh)r rrrrget_test_runner_classs rlcCst|||S)N)rl)rr Zcapture_outputrrrget_test_runnersrm__main__c@s,eZdZddZddZddZddZd S) TestTestscCsdS)Nr)rrrr test_passszTestTests.test_passcCstjddS)Ng?)r$Zsleep)rrrrtest_pass_slowszTestTests.test_pass_slowcCs*tdtjdtdtjd|jddS)Nr5)filer6zfailure message)printsysr5r6Zfail)rrrr test_failszTestTests.test_failcCs(tdtjdtdtjdtddS)Nr5)rrr6z error message)rsrtr5r6 RuntimeError)rrrr test_errorszTestTests.test_errorN)rBrArerprqrurwrrrrrosroccs|]}|dkVqdS)z-vNr).0arrr srzzOutput:zXML: r=)end)F)F)'__doc__riiortr$rCrkZxml.etree.ElementTreeZetreeZ ElementTreerrZTextTestResultrrhrlrmrBZTestCaseroZ TestSuiteZsuiteZaddTestZ makeSuiteStringIOrsumargvZ runner_clsr5Zrunnerr+r,rsr0Z tostringlistrdsdecoderrrrs4         PK! P  3support/__pycache__/testresult.cpython-36.opt-2.pycnu[3 \ @s2ddlZddlZddlZddlZddlZddlZddljjZ ddl m Z Gdddej Z GdddZ ddd Zdd d Zed kr.Gd ddejZejZejejeejZeeddejDZeejZejeZe dej!e dddx(e j"ej#D]Z$e e$j%ddqWe dS)N)datetimecseZdZdddZdddZfddZeddZfd d Zd$d d Z ddZ eddZ fddZ fddZ fddZfddZfddZfddZddZd d!Zd"d#ZZS)%RegressionTestResult=F -cs\tj||ddd|_tjd|_|jjdtjj dd|_ d|_ g|_ t ||_dS)Nr)stream descriptions verbosityTZ testsuitestart )super__init__bufferETZElement_RegressionTestResult__suitesetrZutcnowZ isoformat_RegressionTestResult__e!_RegressionTestResult__start_timeZ_RegressionTestResult__resultsbool_RegressionTestResult__verbose)selfrr r ) __class__//usr/lib64/python3.6/test/support/testresult.pyrs zRegressionTestResult.__init__cCsLy |j}Wntk r"t|SXy|Stk rBt|SXt|S)N)idAttributeErrorstr TypeErrorrepr)clstestZtest_idrrrZ__getIds   zRegressionTestResult.__getIdcsVtj|tj|jd|_}tj|_|j rR|j j |j |d|j j dS)NZtestcasez ... )r startTestr SubElementrrtime perf_counterrrrwritegetDescriptionflush)rr!e)rrrr"+s   zRegressionTestResult.startTestFc KsP|j}d|_|dkrdS|jd|jd|j||jd|jdd|jd|jdd|jrz|jdtj|jd|r|jdk r|jjj }|t j |d_ |j dk r|j jj }|t j |d _ x|jD]t\}}| s| rqt j ||} t|d r>xD|jD],\} } | r,| j| t| n t| | _ q Wqt|| _ qWdS) NnameZstatusrunresultZ completedr$z0.6fz system-outz system-erritems)rrpop_RegressionTestResult__getIdrr$r%Z_stdout_buffergetvaluerstriprr#textZ_stderr_bufferr-hasattrr) rr!Zcaptureargsr)stdoutstderrkvZe2Zk2Zv2rrr _add_result3s4     z RegressionTestResult._add_resultcCs|jr|jj|ddS)Nr)rrr&)rcZwordrrrZ__writeSszRegressionTestResult.__writecCslt|tr0|jdkr|j}q8|jd|j}nt|}tj||d}tj|||}|dj|dj|dS)Nbuiltins.)typemessager=) isinstancer> __module____name__r tracebackformat_exceptionjoin)r Zerr_typeZ err_valueZerr_tbtypenamemsgtbrrrZ__makeErrorDictWs  z$RegressionTestResult.__makeErrorDictcs4|j|d|j|dtj|||jdddS)NT)errorEERROR)r9$_RegressionTestResult__makeErrorDictr addError_RegressionTestResult__write)rr!err)rrrrMjszRegressionTestResult.addErrorcs4|j|d|j|dtj|||jdddS)NT)outputxzexpected failure)r9rLr addExpectedFailurerN)rr!rO)rrrrRosz'RegressionTestResult.addExpectedFailurecs4|j|d|j|dtj|||jdddS)NT)ZfailureFFAIL)r9rLr addFailurerN)rr!rO)rrrrUtszRegressionTestResult.addFailurecs2|j||dtj|||jdd|dS)N)ZskippedSzskipped )r9r addSkiprN)rr!reason)rrrrWyszRegressionTestResult.addSkipcs&|j|tj||jdddS)Nr<ok)r9r addSuccessrN)rr!)rrrrZ~s  zRegressionTestResult.addSuccesscs*|j|ddtj||jdddS)NZUNEXPECTED_SUCCESS)Zoutcomeuzunexpected success)r9r addUnexpectedSuccessrN)rr!)rrrr\s z)RegressionTestResult.addUnexpectedSuccesscCs2|jr|jjd|jd|j|jd|jdS)NrrKrT)rrr&printErrorListerrorsfailures)rrrr printErrorss z RegressionTestResult.printErrorscCs`xZ|D]R\}}|jj|j|jj|d|j|d|jj|j|jjd|qWdS)Nz: rz%s )rr& separator1r' separator2)rZflavorr^r!rOrrrr]s z#RegressionTestResult.printErrorListcCsH|j}|jdt|j|jdtt|j|jdtt|j|S)NZtestsr^r_)rrrZtestsRunlenr^r_)rr)rrrget_xml_elements z$RegressionTestResult.get_xml_element)F)rBrA __qualname__rarbr classmethodr/r"r9rNrLrMrRrUrWrZr\r`r]rd __classcell__rr)rrrs"           rc@seZdZdddZddZdS)QuietRegressionTestRunnerFcCst|dd|_||j_dS)Nr)rr,r)rrrrrrrsz"QuietRegressionTestRunner.__init__cCs||j|jS)N)r,)rr!rrrr+s zQuietRegressionTestRunner.runN)F)rBrArerr+rrrrrhs rhFcCs&|rtjtjt||dStjt|dS)N)Z resultclassrr )r) functoolspartialunittestZTextTestRunnerrrh)r rrrrget_test_runner_classs rlcCst|||S)N)rl)rr Zcapture_outputrrrget_test_runnersrm__main__c@s,eZdZddZddZddZddZd S) TestTestscCsdS)Nr)rrrr test_passszTestTests.test_passcCstjddS)Ng?)r$Zsleep)rrrrtest_pass_slowszTestTests.test_pass_slowcCs*tdtjdtdtjd|jddS)Nr5)filer6zfailure message)printsysr5r6Zfail)rrrr test_failszTestTests.test_failcCs(tdtjdtdtjdtddS)Nr5)rrr6z error message)rsrtr5r6 RuntimeError)rrrr test_errorszTestTests.test_errorN)rBrArerprqrurwrrrrrosroccs|]}|dkVqdS)z-vNr).0arrr srzzOutput:zXML: r=)end)F)F)&riiortr$rCrkZxml.etree.ElementTreeZetreeZ ElementTreerrZTextTestResultrrhrlrmrBZTestCaseroZ TestSuiteZsuiteZaddTestZ makeSuiteStringIOrsumargvZ runner_clsr5Zrunnerr+r,rsr0Z tostringlistrdsdecoderrrrs2         PK!r&XX-support/__pycache__/testresult.cpython-36.pycnu[3 \ @s6dZddlZddlZddlZddlZddlZddlZddljj Z ddl m Z Gdddej Z GdddZdd d Zdd d Zed kr2GdddejZejZejejeejZeeddejDZeejZejeZ e!dej"e!dddx(e j#e j$D]Z%e!e%j&ddqWe!dS)z=Test runner and result class for the regression test suite. N)datetimecseZdZdddZdddZfddZeddZfd d Zd$d d Z ddZ eddZ fddZ fddZ fddZfddZfddZfddZddZd d!Zd"d#ZZS)%RegressionTestResult=F -cs\tj||ddd|_tjd|_|jjdtjj dd|_ d|_ g|_ t ||_dS)Nr)stream descriptions verbosityTZ testsuitestart )super__init__bufferETZElement_RegressionTestResult__suitesetrZutcnowZ isoformat_RegressionTestResult__e!_RegressionTestResult__start_timeZ_RegressionTestResult__resultsbool_RegressionTestResult__verbose)selfrr r ) __class__//usr/lib64/python3.6/test/support/testresult.pyrs zRegressionTestResult.__init__cCsLy |j}Wntk r"t|SXy|Stk rBt|SXt|S)N)idAttributeErrorstr TypeErrorrepr)clstestZtest_idrrrZ__getIds   zRegressionTestResult.__getIdcsVtj|tj|jd|_}tj|_|j rR|j j |j |d|j j dS)NZtestcasez ... )r startTestr SubElementrrtime perf_counterrrrwritegetDescriptionflush)rr!e)rrrr"+s   zRegressionTestResult.startTestFc KsP|j}d|_|dkrdS|jd|jd|j||jd|jdd|jd|jdd|jrz|jdtj|jd|r|jdk r|jjj }|t j |d_ |j dk r|j jj }|t j |d _ x|jD]t\}}| s| rqt j ||} t|d r>xD|jD],\} } | r,| j| t| n t| | _ q Wqt|| _ qWdS) NnameZstatusrunresultZ completedr$z0.6fz system-outz system-erritems)rrpop_RegressionTestResult__getIdrr$r%Z_stdout_buffergetvaluerstriprr#textZ_stderr_bufferr-hasattrr) rr!Zcaptureargsr)stdoutstderrkvZe2Zk2Zv2rrr _add_result3s4     z RegressionTestResult._add_resultcCs|jr|jj|ddS)Nr)rrr&)rcZwordrrrZ__writeSszRegressionTestResult.__writecCslt|tr0|jdkr|j}q8|jd|j}nt|}tj||d}tj|||}|dj|dj|dS)Nbuiltins.)typemessager=) isinstancer> __module____name__r tracebackformat_exceptionjoin)r Zerr_typeZ err_valueZerr_tbtypenamemsgtbrrrZ__makeErrorDictWs  z$RegressionTestResult.__makeErrorDictcs4|j|d|j|dtj|||jdddS)NT)errorEERROR)r9$_RegressionTestResult__makeErrorDictr addError_RegressionTestResult__write)rr!err)rrrrMjszRegressionTestResult.addErrorcs4|j|d|j|dtj|||jdddS)NT)outputxzexpected failure)r9rLr addExpectedFailurerN)rr!rO)rrrrRosz'RegressionTestResult.addExpectedFailurecs4|j|d|j|dtj|||jdddS)NT)ZfailureFFAIL)r9rLr addFailurerN)rr!rO)rrrrUtszRegressionTestResult.addFailurecs2|j||dtj|||jdd|dS)N)ZskippedSzskipped )r9r addSkiprN)rr!reason)rrrrWyszRegressionTestResult.addSkipcs&|j|tj||jdddS)Nr<ok)r9r addSuccessrN)rr!)rrrrZ~s  zRegressionTestResult.addSuccesscs*|j|ddtj||jdddS)NZUNEXPECTED_SUCCESS)Zoutcomeuzunexpected success)r9r addUnexpectedSuccessrN)rr!)rrrr\s z)RegressionTestResult.addUnexpectedSuccesscCs2|jr|jjd|jd|j|jd|jdS)NrrKrT)rrr&printErrorListerrorsfailures)rrrr printErrorss z RegressionTestResult.printErrorscCs`xZ|D]R\}}|jj|j|jj|d|j|d|jj|j|jjd|qWdS)Nz: rz%s )rr& separator1r' separator2)rZflavorr^r!rOrrrr]s z#RegressionTestResult.printErrorListcCsH|j}|jdt|j|jdtt|j|jdtt|j|S)NZtestsr^r_)rrrZtestsRunlenr^r_)rr)rrrget_xml_elements z$RegressionTestResult.get_xml_element)F)rBrA __qualname__rarbr classmethodr/r"r9rNrLrMrRrUrWrZr\r`r]rd __classcell__rr)rrrs"           rc@seZdZdddZddZdS)QuietRegressionTestRunnerFcCst|dd|_||j_dS)Nr)rr,r)rrrrrrrsz"QuietRegressionTestRunner.__init__cCs||j|jS)N)r,)rr!rrrr+s zQuietRegressionTestRunner.runN)F)rBrArerr+rrrrrhs rhFcCs&|rtjtjt||dStjt|dS)N)Z resultclassrr )r) functoolspartialunittestZTextTestRunnerrrh)r rrrrget_test_runner_classs rlcCst|||S)N)rl)rr Zcapture_outputrrrget_test_runnersrm__main__c@s,eZdZddZddZddZddZd S) TestTestscCsdS)Nr)rrrr test_passszTestTests.test_passcCstjddS)Ng?)r$Zsleep)rrrrtest_pass_slowszTestTests.test_pass_slowcCs*tdtjdtdtjd|jddS)Nr5)filer6zfailure message)printsysr5r6Zfail)rrrr test_failszTestTests.test_failcCs(tdtjdtdtjdtddS)Nr5)rrr6z error message)rsrtr5r6 RuntimeError)rrrr test_errorszTestTests.test_errorN)rBrArerprqrurwrrrrrosroccs|]}|dkVqdS)z-vNr).0arrr srzzOutput:zXML: r=)end)F)F)'__doc__riiortr$rCrkZxml.etree.ElementTreeZetreeZ ElementTreerrZTextTestResultrrhrlrmrBZTestCaseroZ TestSuiteZsuiteZaddTestZ makeSuiteStringIOrsumargvZ runner_clsr5Zrunnerr+r,rsr0Z tostringlistrdsdecoderrrrs4         PK!}&support/__init__.pynu["""Supporting definitions for the Python regression tests.""" if __name__ != 'test.support': raise ImportError('support must be imported from the test package') import collections.abc import contextlib import datetime import errno import faulthandler import fnmatch import functools import gc import hashlib import importlib import importlib.util import io import logging.handlers import nntplib import os import platform import re import shutil import socket import stat import struct import subprocess import sys import sysconfig import tempfile import time import types import unittest import urllib.error import warnings from .testresult import get_test_runner try: import _thread, threading except ImportError: _thread = None threading = None try: import multiprocessing.process except ImportError: multiprocessing = None try: import zlib except ImportError: zlib = None try: import gzip except ImportError: gzip = None try: import bz2 except ImportError: bz2 = None try: import lzma except ImportError: lzma = None try: import resource except ImportError: resource = None try: import _hashlib except ImportError: _hashlib = None __all__ = [ # globals "PIPE_MAX_SIZE", "verbose", "max_memuse", "use_resources", "failfast", # exceptions "Error", "TestFailed", "TestDidNotRun", "ResourceDenied", # imports "import_module", "import_fresh_module", "CleanImport", # modules "unload", "forget", # io "record_original_stdout", "get_original_stdout", "captured_stdout", "captured_stdin", "captured_stderr", # filesystem "TESTFN", "SAVEDCWD", "unlink", "rmtree", "temp_cwd", "findfile", "create_empty_file", "can_symlink", "fs_is_case_insensitive", # unittest "is_resource_enabled", "requires", "requires_freebsd_version", "requires_linux_version", "requires_mac_ver", "requires_hashdigest", "check_syntax_error", "TransientResource", "time_out", "socket_peer_reset", "ioerror_peer_reset", "transient_internet", "BasicTestRunner", "run_unittest", "run_doctest", "skip_unless_symlink", "requires_gzip", "requires_bz2", "requires_lzma", "bigmemtest", "bigaddrspacetest", "cpython_only", "get_attribute", "requires_IEEE_754", "skip_unless_xattr", "requires_zlib", "anticipate_failure", "load_package_tests", "detect_api_mismatch", "check__all__", "requires_android_level", "requires_multiprocessing_queue", # sys "is_jython", "is_android", "check_impl_detail", "unix_shell", "setswitchinterval", # network "HOST", "IPV6_ENABLED", "find_unused_port", "bind_port", "open_urlresource", "bind_unix_socket", # processes 'temp_umask', "reap_children", # logging "TestHandler", # threads "threading_setup", "threading_cleanup", "reap_threads", "start_threads", # miscellaneous "check_warnings", "check_no_resource_warning", "EnvironmentVarGuard", "run_with_locale", "swap_item", "swap_attr", "Matcher", "set_memlimit", "SuppressCrashReport", "sortdict", "run_with_tz", "PGO", "missing_compiler_executable", "fd_count", ] class Error(Exception): """Base class for regression test exceptions.""" class TestFailed(Error): """Test failed.""" class TestDidNotRun(Error): """Test did not run any subtests.""" class ResourceDenied(unittest.SkipTest): """Test skipped because it requested a disallowed resource. This is raised when a test calls requires() for a resource that has not be enabled. It is used to distinguish between expected and unexpected skips. """ @contextlib.contextmanager def _ignore_deprecated_imports(ignore=True): """Context manager to suppress package and module deprecation warnings when importing them. If ignore is False, this context manager has no effect. """ if ignore: with warnings.catch_warnings(): warnings.filterwarnings("ignore", ".+ (module|package)", DeprecationWarning) yield else: yield def import_module(name, deprecated=False, *, required_on=()): """Import and return the module to be tested, raising SkipTest if it is not available. If deprecated is True, any module or package deprecation messages will be suppressed. If a module is required on a platform but optional for others, set required_on to an iterable of platform prefixes which will be compared against sys.platform. """ with _ignore_deprecated_imports(deprecated): try: return importlib.import_module(name) except ImportError as msg: if sys.platform.startswith(tuple(required_on)): raise raise unittest.SkipTest(str(msg)) def _save_and_remove_module(name, orig_modules): """Helper function to save and remove a module from sys.modules Raise ImportError if the module can't be imported. """ # try to import the module and raise an error if it can't be imported if name not in sys.modules: __import__(name) del sys.modules[name] for modname in list(sys.modules): if modname == name or modname.startswith(name + '.'): orig_modules[modname] = sys.modules[modname] del sys.modules[modname] def _save_and_block_module(name, orig_modules): """Helper function to save and block a module in sys.modules Return True if the module was in sys.modules, False otherwise. """ saved = True try: orig_modules[name] = sys.modules[name] except KeyError: saved = False sys.modules[name] = None return saved def anticipate_failure(condition): """Decorator to mark a test that is known to be broken in some cases Any use of this decorator should have a comment identifying the associated tracker issue. """ if condition: return unittest.expectedFailure return lambda f: f def load_package_tests(pkg_dir, loader, standard_tests, pattern): """Generic load_tests implementation for simple test packages. Most packages can implement load_tests using this function as follows: def load_tests(*args): return load_package_tests(os.path.dirname(__file__), *args) """ if pattern is None: pattern = "test*" top_dir = os.path.dirname( # Lib os.path.dirname( # test os.path.dirname(__file__))) # support package_tests = loader.discover(start_dir=pkg_dir, top_level_dir=top_dir, pattern=pattern) standard_tests.addTests(package_tests) return standard_tests def import_fresh_module(name, fresh=(), blocked=(), deprecated=False): """Import and return a module, deliberately bypassing sys.modules. This function imports and returns a fresh copy of the named Python module by removing the named module from sys.modules before doing the import. Note that unlike reload, the original module is not affected by this operation. *fresh* is an iterable of additional module names that are also removed from the sys.modules cache before doing the import. *blocked* is an iterable of module names that are replaced with None in the module cache during the import to ensure that attempts to import them raise ImportError. The named module and any modules named in the *fresh* and *blocked* parameters are saved before starting the import and then reinserted into sys.modules when the fresh import is complete. Module and package deprecation messages are suppressed during this import if *deprecated* is True. This function will raise ImportError if the named module cannot be imported. """ # NOTE: test_heapq, test_json and test_warnings include extra sanity checks # to make sure that this utility function is working as expected with _ignore_deprecated_imports(deprecated): # Keep track of modules saved for later restoration as well # as those which just need a blocking entry removed orig_modules = {} names_to_remove = [] _save_and_remove_module(name, orig_modules) try: for fresh_name in fresh: _save_and_remove_module(fresh_name, orig_modules) for blocked_name in blocked: if not _save_and_block_module(blocked_name, orig_modules): names_to_remove.append(blocked_name) fresh_module = importlib.import_module(name) except ImportError: fresh_module = None finally: for orig_name, module in orig_modules.items(): sys.modules[orig_name] = module for name_to_remove in names_to_remove: del sys.modules[name_to_remove] return fresh_module def get_attribute(obj, name): """Get an attribute, raising SkipTest if AttributeError is raised.""" try: attribute = getattr(obj, name) except AttributeError: raise unittest.SkipTest("object %r has no attribute %r" % (obj, name)) else: return attribute verbose = 1 # Flag set to 0 by regrtest.py use_resources = None # Flag set to [] by regrtest.py max_memuse = 0 # Disable bigmem tests (they will still be run with # small sizes, to make sure they work.) real_max_memuse = 0 junit_xml_list = None # list of testsuite XML elements failfast = False # _original_stdout is meant to hold stdout at the time regrtest began. # This may be "the real" stdout, or IDLE's emulation of stdout, or whatever. # The point is to have some flavor of stdout the user can actually see. _original_stdout = None def record_original_stdout(stdout): global _original_stdout _original_stdout = stdout def get_original_stdout(): return _original_stdout or sys.stdout def unload(name): try: del sys.modules[name] except KeyError: pass def _force_run(path, func, *args): try: return func(*args) except OSError as err: if verbose >= 2: print('%s: %s' % (err.__class__.__name__, err)) print('re-run %s%r' % (func.__name__, args)) os.chmod(path, stat.S_IRWXU) return func(*args) if sys.platform.startswith("win"): def _waitfor(func, pathname, waitall=False): # Perform the operation func(pathname) # Now setup the wait loop if waitall: dirname = pathname else: dirname, name = os.path.split(pathname) dirname = dirname or '.' # Check for `pathname` to be removed from the filesystem. # The exponential backoff of the timeout amounts to a total # of ~1 second after which the deletion is probably an error # anyway. # Testing on an i7@4.3GHz shows that usually only 1 iteration is # required when contention occurs. timeout = 0.001 while timeout < 1.0: # Note we are only testing for the existence of the file(s) in # the contents of the directory regardless of any security or # access rights. If we have made it this far, we have sufficient # permissions to do that much using Python's equivalent of the # Windows API FindFirstFile. # Other Windows APIs can fail or give incorrect results when # dealing with files that are pending deletion. L = os.listdir(dirname) if not (L if waitall else name in L): return # Increase the timeout and try again time.sleep(timeout) timeout *= 2 warnings.warn('tests may fail, delete still pending for ' + pathname, RuntimeWarning, stacklevel=4) def _unlink(filename): _waitfor(os.unlink, filename) def _rmdir(dirname): _waitfor(os.rmdir, dirname) def _rmtree(path): def _rmtree_inner(path): for name in _force_run(path, os.listdir, path): fullname = os.path.join(path, name) try: mode = os.lstat(fullname).st_mode except OSError as exc: print("support.rmtree(): os.lstat(%r) failed with %s" % (fullname, exc), file=sys.__stderr__) mode = 0 if stat.S_ISDIR(mode): _waitfor(_rmtree_inner, fullname, waitall=True) _force_run(fullname, os.rmdir, fullname) else: _force_run(fullname, os.unlink, fullname) _waitfor(_rmtree_inner, path, waitall=True) _waitfor(lambda p: _force_run(p, os.rmdir, p), path) def _longpath(path): try: import ctypes except ImportError: # No ctypes means we can't expands paths. pass else: buffer = ctypes.create_unicode_buffer(len(path) * 2) length = ctypes.windll.kernel32.GetLongPathNameW(path, buffer, len(buffer)) if length: return buffer[:length] return path else: _unlink = os.unlink _rmdir = os.rmdir def _rmtree(path): try: shutil.rmtree(path) return except OSError: pass def _rmtree_inner(path): for name in _force_run(path, os.listdir, path): fullname = os.path.join(path, name) try: mode = os.lstat(fullname).st_mode except OSError: mode = 0 if stat.S_ISDIR(mode): _rmtree_inner(fullname) _force_run(path, os.rmdir, fullname) else: _force_run(path, os.unlink, fullname) _rmtree_inner(path) os.rmdir(path) def _longpath(path): return path def unlink(filename): try: _unlink(filename) except (FileNotFoundError, NotADirectoryError): pass def rmdir(dirname): try: _rmdir(dirname) except FileNotFoundError: pass def rmtree(path): try: _rmtree(path) except FileNotFoundError: pass def make_legacy_pyc(source): """Move a PEP 3147/488 pyc file to its legacy pyc location. :param source: The file system path to the source file. The source file does not need to exist, however the PEP 3147/488 pyc file must exist. :return: The file system path to the legacy pyc file. """ pyc_file = importlib.util.cache_from_source(source) up_one = os.path.dirname(os.path.abspath(source)) legacy_pyc = os.path.join(up_one, source + 'c') os.rename(pyc_file, legacy_pyc) return legacy_pyc def forget(modname): """'Forget' a module was ever imported. This removes the module from sys.modules and deletes any PEP 3147/488 or legacy .pyc files. """ unload(modname) for dirname in sys.path: source = os.path.join(dirname, modname + '.py') # It doesn't matter if they exist or not, unlink all possible # combinations of PEP 3147/488 and legacy pyc files. unlink(source + 'c') for opt in ('', 1, 2): unlink(importlib.util.cache_from_source(source, optimization=opt)) # Check whether a gui is actually available def _is_gui_available(): if hasattr(_is_gui_available, 'result'): return _is_gui_available.result reason = None if sys.platform.startswith('win'): # if Python is running as a service (such as the buildbot service), # gui interaction may be disallowed import ctypes import ctypes.wintypes UOI_FLAGS = 1 WSF_VISIBLE = 0x0001 class USEROBJECTFLAGS(ctypes.Structure): _fields_ = [("fInherit", ctypes.wintypes.BOOL), ("fReserved", ctypes.wintypes.BOOL), ("dwFlags", ctypes.wintypes.DWORD)] dll = ctypes.windll.user32 h = dll.GetProcessWindowStation() if not h: raise ctypes.WinError() uof = USEROBJECTFLAGS() needed = ctypes.wintypes.DWORD() res = dll.GetUserObjectInformationW(h, UOI_FLAGS, ctypes.byref(uof), ctypes.sizeof(uof), ctypes.byref(needed)) if not res: raise ctypes.WinError() if not bool(uof.dwFlags & WSF_VISIBLE): reason = "gui not available (WSF_VISIBLE flag not set)" elif sys.platform == 'darwin': # The Aqua Tk implementations on OS X can abort the process if # being called in an environment where a window server connection # cannot be made, for instance when invoked by a buildbot or ssh # process not running under the same user id as the current console # user. To avoid that, raise an exception if the window manager # connection is not available. from ctypes import cdll, c_int, pointer, Structure from ctypes.util import find_library app_services = cdll.LoadLibrary(find_library("ApplicationServices")) if app_services.CGMainDisplayID() == 0: reason = "gui tests cannot run without OS X window manager" else: class ProcessSerialNumber(Structure): _fields_ = [("highLongOfPSN", c_int), ("lowLongOfPSN", c_int)] psn = ProcessSerialNumber() psn_p = pointer(psn) if ( (app_services.GetCurrentProcess(psn_p) < 0) or (app_services.SetFrontProcess(psn_p) < 0) ): reason = "cannot run without OS X gui process" # check on every platform whether tkinter can actually do anything if not reason: try: from tkinter import Tk root = Tk() root.withdraw() root.update() root.destroy() except Exception as e: err_string = str(e) if len(err_string) > 50: err_string = err_string[:50] + ' [...]' reason = 'Tk unavailable due to {}: {}'.format(type(e).__name__, err_string) _is_gui_available.reason = reason _is_gui_available.result = not reason return _is_gui_available.result def is_resource_enabled(resource): """Test whether a resource is enabled. Known resources are set by regrtest.py. If not running under regrtest.py, all resources are assumed enabled unless use_resources has been set. """ return use_resources is None or resource in use_resources def requires(resource, msg=None): """Raise ResourceDenied if the specified resource is not available.""" if not is_resource_enabled(resource): if msg is None: msg = "Use of the %r resource not enabled" % resource raise ResourceDenied(msg) if resource == 'gui' and not _is_gui_available(): raise ResourceDenied(_is_gui_available.reason) def _requires_unix_version(sysname, min_version): """Decorator raising SkipTest if the OS is `sysname` and the version is less than `min_version`. For example, @_requires_unix_version('FreeBSD', (7, 2)) raises SkipTest if the FreeBSD version is less than 7.2. """ def decorator(func): @functools.wraps(func) def wrapper(*args, **kw): if platform.system() == sysname: version_txt = platform.release().split('-', 1)[0] try: version = tuple(map(int, version_txt.split('.'))) except ValueError: pass else: if version < min_version: min_version_txt = '.'.join(map(str, min_version)) raise unittest.SkipTest( "%s version %s or higher required, not %s" % (sysname, min_version_txt, version_txt)) return func(*args, **kw) wrapper.min_version = min_version return wrapper return decorator def requires_freebsd_version(*min_version): """Decorator raising SkipTest if the OS is FreeBSD and the FreeBSD version is less than `min_version`. For example, @requires_freebsd_version(7, 2) raises SkipTest if the FreeBSD version is less than 7.2. """ return _requires_unix_version('FreeBSD', min_version) def requires_linux_version(*min_version): """Decorator raising SkipTest if the OS is Linux and the Linux version is less than `min_version`. For example, @requires_linux_version(2, 6, 32) raises SkipTest if the Linux version is less than 2.6.32. """ return _requires_unix_version('Linux', min_version) def requires_mac_ver(*min_version): """Decorator raising SkipTest if the OS is Mac OS X and the OS X version if less than min_version. For example, @requires_mac_ver(10, 5) raises SkipTest if the OS X version is lesser than 10.5. """ def decorator(func): @functools.wraps(func) def wrapper(*args, **kw): if sys.platform == 'darwin': version_txt = platform.mac_ver()[0] try: version = tuple(map(int, version_txt.split('.'))) except ValueError: pass else: if version < min_version: min_version_txt = '.'.join(map(str, min_version)) raise unittest.SkipTest( "Mac OS X %s or higher required, not %s" % (min_version_txt, version_txt)) return func(*args, **kw) wrapper.min_version = min_version return wrapper return decorator def requires_hashdigest(digestname, openssl=None): """Decorator raising SkipTest if a hashing algorithm is not available The hashing algorithm could be missing or blocked by a strict crypto policy. If 'openssl' is True, then the decorator checks that OpenSSL provides the algorithm. Otherwise the check falls back to built-in implementations. ValueError: [digital envelope routines: EVP_DigestInit_ex] disabled for FIPS ValueError: unsupported hash type md4 """ def decorator(func): @functools.wraps(func) def wrapper(*args, **kwargs): try: if openssl and _hashlib is not None: _hashlib.new(digestname) else: hashlib.new(digestname) except ValueError: raise unittest.SkipTest( f"hash digest '{digestname}' is not available." ) return func(*args, **kwargs) return wrapper return decorator # Don't use "localhost", since resolving it uses the DNS under recent # Windows versions (see issue #18792). HOST = "127.0.0.1" HOSTv6 = "::1" def find_unused_port(family=socket.AF_INET, socktype=socket.SOCK_STREAM): """Returns an unused port that should be suitable for binding. This is achieved by creating a temporary socket with the same family and type as the 'sock' parameter (default is AF_INET, SOCK_STREAM), and binding it to the specified host address (defaults to 0.0.0.0) with the port set to 0, eliciting an unused ephemeral port from the OS. The temporary socket is then closed and deleted, and the ephemeral port is returned. Either this method or bind_port() should be used for any tests where a server socket needs to be bound to a particular port for the duration of the test. Which one to use depends on whether the calling code is creating a python socket, or if an unused port needs to be provided in a constructor or passed to an external program (i.e. the -accept argument to openssl's s_server mode). Always prefer bind_port() over find_unused_port() where possible. Hard coded ports should *NEVER* be used. As soon as a server socket is bound to a hard coded port, the ability to run multiple instances of the test simultaneously on the same host is compromised, which makes the test a ticking time bomb in a buildbot environment. On Unix buildbots, this may simply manifest as a failed test, which can be recovered from without intervention in most cases, but on Windows, the entire python process can completely and utterly wedge, requiring someone to log in to the buildbot and manually kill the affected process. (This is easy to reproduce on Windows, unfortunately, and can be traced to the SO_REUSEADDR socket option having different semantics on Windows versus Unix/Linux. On Unix, you can't have two AF_INET SOCK_STREAM sockets bind, listen and then accept connections on identical host/ports. An EADDRINUSE OSError will be raised at some point (depending on the platform and the order bind and listen were called on each socket). However, on Windows, if SO_REUSEADDR is set on the sockets, no EADDRINUSE will ever be raised when attempting to bind two identical host/ports. When accept() is called on each socket, the second caller's process will steal the port from the first caller, leaving them both in an awkwardly wedged state where they'll no longer respond to any signals or graceful kills, and must be forcibly killed via OpenProcess()/TerminateProcess(). The solution on Windows is to use the SO_EXCLUSIVEADDRUSE socket option instead of SO_REUSEADDR, which effectively affords the same semantics as SO_REUSEADDR on Unix. Given the propensity of Unix developers in the Open Source world compared to Windows ones, this is a common mistake. A quick look over OpenSSL's 0.9.8g source shows that they use SO_REUSEADDR when openssl.exe is called with the 's_server' option, for example. See http://bugs.python.org/issue2550 for more info. The following site also has a very thorough description about the implications of both REUSEADDR and EXCLUSIVEADDRUSE on Windows: http://msdn2.microsoft.com/en-us/library/ms740621(VS.85).aspx) XXX: although this approach is a vast improvement on previous attempts to elicit unused ports, it rests heavily on the assumption that the ephemeral port returned to us by the OS won't immediately be dished back out to some other process when we close and delete our temporary socket but before our calling code has a chance to bind the returned port. We can deal with this issue if/when we come across it. """ tempsock = socket.socket(family, socktype) port = bind_port(tempsock) tempsock.close() del tempsock return port def bind_port(sock, host=HOST): """Bind the socket to a free port and return the port number. Relies on ephemeral ports in order to ensure we are using an unbound port. This is important as many tests may be running simultaneously, especially in a buildbot environment. This method raises an exception if the sock.family is AF_INET and sock.type is SOCK_STREAM, *and* the socket has SO_REUSEADDR or SO_REUSEPORT set on it. Tests should *never* set these socket options for TCP/IP sockets. The only case for setting these options is testing multicasting via multiple UDP sockets. Additionally, if the SO_EXCLUSIVEADDRUSE socket option is available (i.e. on Windows), it will be set on the socket. This will prevent anyone else from bind()'ing to our host/port for the duration of the test. """ if sock.family == socket.AF_INET and sock.type == socket.SOCK_STREAM: if hasattr(socket, 'SO_REUSEADDR'): if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) == 1: raise TestFailed("tests should never set the SO_REUSEADDR " \ "socket option on TCP/IP sockets!") if hasattr(socket, 'SO_REUSEPORT'): try: if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 1: raise TestFailed("tests should never set the SO_REUSEPORT " \ "socket option on TCP/IP sockets!") except OSError: # Python's socket module was compiled using modern headers # thus defining SO_REUSEPORT but this process is running # under an older kernel that does not support SO_REUSEPORT. pass if hasattr(socket, 'SO_EXCLUSIVEADDRUSE'): sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) sock.bind((host, 0)) port = sock.getsockname()[1] return port def bind_unix_socket(sock, addr): """Bind a unix socket, raising SkipTest if PermissionError is raised.""" assert sock.family == socket.AF_UNIX try: sock.bind(addr) except PermissionError: sock.close() raise unittest.SkipTest('cannot bind AF_UNIX sockets') def _is_ipv6_enabled(): """Check whether IPv6 is enabled on this host.""" if socket.has_ipv6: sock = None try: sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) sock.bind((HOSTv6, 0)) return True except OSError: pass finally: if sock: sock.close() return False IPV6_ENABLED = _is_ipv6_enabled() def system_must_validate_cert(f): """Skip the test on TLS certificate validation failures.""" @functools.wraps(f) def dec(*args, **kwargs): try: f(*args, **kwargs) except IOError as e: if "CERTIFICATE_VERIFY_FAILED" in str(e): raise unittest.SkipTest("system does not contain " "necessary certificates") raise return dec # A constant likely larger than the underlying OS pipe buffer size, to # make writes blocking. # Windows limit seems to be around 512 B, and many Unix kernels have a # 64 KiB pipe buffer size or 16 * PAGE_SIZE: take a few megs to be sure. # (see issue #17835 for a discussion of this number). PIPE_MAX_SIZE = 4 * 1024 * 1024 + 1 # A constant likely larger than the underlying OS socket buffer size, to make # writes blocking. # The socket buffer sizes can usually be tuned system-wide (e.g. through sysctl # on Linux), or on a per-socket basis (SO_SNDBUF/SO_RCVBUF). See issue #18643 # for a discussion of this number). SOCK_MAX_SIZE = 16 * 1024 * 1024 + 1 # decorator for skipping tests on non-IEEE 754 platforms requires_IEEE_754 = unittest.skipUnless( float.__getformat__("double").startswith("IEEE"), "test requires IEEE 754 doubles") requires_zlib = unittest.skipUnless(zlib, 'requires zlib') requires_gzip = unittest.skipUnless(gzip, 'requires gzip') requires_bz2 = unittest.skipUnless(bz2, 'requires bz2') requires_lzma = unittest.skipUnless(lzma, 'requires lzma') is_jython = sys.platform.startswith('java') _ANDROID_API_LEVEL = sysconfig.get_config_var('ANDROID_API_LEVEL') is_android = (_ANDROID_API_LEVEL is not None and _ANDROID_API_LEVEL > 0) if sys.platform != 'win32': unix_shell = '/system/bin/sh' if is_android else '/bin/sh' else: unix_shell = None # Filename used for testing if os.name == 'java': # Jython disallows @ in module names TESTFN = '$test' else: TESTFN = '@test' # Disambiguate TESTFN for parallel testing, while letting it remain a valid # module name. TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid()) # FS_NONASCII: non-ASCII character encodable by os.fsencode(), # or None if there is no such character. FS_NONASCII = None for character in ( # First try printable and common characters to have a readable filename. # For each character, the encoding list are just example of encodings able # to encode the character (the list is not exhaustive). # U+00E6 (Latin Small Letter Ae): cp1252, iso-8859-1 '\u00E6', # U+0130 (Latin Capital Letter I With Dot Above): cp1254, iso8859_3 '\u0130', # U+0141 (Latin Capital Letter L With Stroke): cp1250, cp1257 '\u0141', # U+03C6 (Greek Small Letter Phi): cp1253 '\u03C6', # U+041A (Cyrillic Capital Letter Ka): cp1251 '\u041A', # U+05D0 (Hebrew Letter Alef): Encodable to cp424 '\u05D0', # U+060C (Arabic Comma): cp864, cp1006, iso8859_6, mac_arabic '\u060C', # U+062A (Arabic Letter Teh): cp720 '\u062A', # U+0E01 (Thai Character Ko Kai): cp874 '\u0E01', # Then try more "special" characters. "special" because they may be # interpreted or displayed differently depending on the exact locale # encoding and the font. # U+00A0 (No-Break Space) '\u00A0', # U+20AC (Euro Sign) '\u20AC', ): try: # If Python is set up to use the legacy 'mbcs' in Windows, # 'replace' error mode is used, and encode() returns b'?' # for characters missing in the ANSI codepage if os.fsdecode(os.fsencode(character)) != character: raise UnicodeError except UnicodeError: pass else: FS_NONASCII = character break # TESTFN_UNICODE is a non-ascii filename TESTFN_UNICODE = TESTFN + "-\xe0\xf2\u0258\u0141\u011f" if sys.platform == 'darwin': # In Mac OS X's VFS API file names are, by definition, canonically # decomposed Unicode, encoded using UTF-8. See QA1173: # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html import unicodedata TESTFN_UNICODE = unicodedata.normalize('NFD', TESTFN_UNICODE) TESTFN_ENCODING = sys.getfilesystemencoding() # TESTFN_UNENCODABLE is a filename (str type) that should *not* be able to be # encoded by the filesystem encoding (in strict mode). It can be None if we # cannot generate such filename. TESTFN_UNENCODABLE = None if os.name == 'nt': # skip win32s (0) or Windows 9x/ME (1) if sys.getwindowsversion().platform >= 2: # Different kinds of characters from various languages to minimize the # probability that the whole name is encodable to MBCS (issue #9819) TESTFN_UNENCODABLE = TESTFN + "-\u5171\u0141\u2661\u0363\uDC80" try: TESTFN_UNENCODABLE.encode(TESTFN_ENCODING) except UnicodeEncodeError: pass else: print('WARNING: The filename %r CAN be encoded by the filesystem encoding (%s). ' 'Unicode filename tests may not be effective' % (TESTFN_UNENCODABLE, TESTFN_ENCODING)) TESTFN_UNENCODABLE = None # Mac OS X denies unencodable filenames (invalid utf-8) elif sys.platform != 'darwin': try: # ascii and utf-8 cannot encode the byte 0xff b'\xff'.decode(TESTFN_ENCODING) except UnicodeDecodeError: # 0xff will be encoded using the surrogate character u+DCFF TESTFN_UNENCODABLE = TESTFN \ + b'-\xff'.decode(TESTFN_ENCODING, 'surrogateescape') else: # File system encoding (eg. ISO-8859-* encodings) can encode # the byte 0xff. Skip some unicode filename tests. pass # TESTFN_UNDECODABLE is a filename (bytes type) that should *not* be able to be # decoded from the filesystem encoding (in strict mode). It can be None if we # cannot generate such filename (ex: the latin1 encoding can decode any byte # sequence). On UNIX, TESTFN_UNDECODABLE can be decoded by os.fsdecode() thanks # to the surrogateescape error handler (PEP 383), but not from the filesystem # encoding in strict mode. TESTFN_UNDECODABLE = None for name in ( # b'\xff' is not decodable by os.fsdecode() with code page 932. Windows # accepts it to create a file or a directory, or don't accept to enter to # such directory (when the bytes name is used). So test b'\xe7' first: it is # not decodable from cp932. b'\xe7w\xf0', # undecodable from ASCII, UTF-8 b'\xff', # undecodable from iso8859-3, iso8859-6, iso8859-7, cp424, iso8859-8, cp856 # and cp857 b'\xae\xd5' # undecodable from UTF-8 (UNIX and Mac OS X) b'\xed\xb2\x80', b'\xed\xb4\x80', # undecodable from shift_jis, cp869, cp874, cp932, cp1250, cp1251, cp1252, # cp1253, cp1254, cp1255, cp1257, cp1258 b'\x81\x98', ): try: name.decode(TESTFN_ENCODING) except UnicodeDecodeError: TESTFN_UNDECODABLE = os.fsencode(TESTFN) + name break if FS_NONASCII: TESTFN_NONASCII = TESTFN + '-' + FS_NONASCII else: TESTFN_NONASCII = None # Save the initial cwd SAVEDCWD = os.getcwd() # Set by libregrtest/main.py so we can skip tests that are not # useful for PGO PGO = False # Set by libregrtest/main.py if we are running the extended (time consuming) # PGO task. If this is True, PGO is also True. PGO_EXTENDED = False @contextlib.contextmanager def temp_dir(path=None, quiet=False): """Return a context manager that creates a temporary directory. Arguments: path: the directory to create temporarily. If omitted or None, defaults to creating a temporary directory using tempfile.mkdtemp. quiet: if False (the default), the context manager raises an exception on error. Otherwise, if the path is specified and cannot be created, only a warning is issued. """ dir_created = False if path is None: path = tempfile.mkdtemp() dir_created = True path = os.path.realpath(path) else: try: os.mkdir(path) dir_created = True except OSError: if not quiet: raise warnings.warn('tests may fail, unable to create temp dir: ' + path, RuntimeWarning, stacklevel=3) if dir_created: pid = os.getpid() try: yield path finally: # In case the process forks, let only the parent remove the # directory. The child has a diffent process id. (bpo-30028) if dir_created and pid == os.getpid(): rmtree(path) @contextlib.contextmanager def change_cwd(path, quiet=False): """Return a context manager that changes the current working directory. Arguments: path: the directory to use as the temporary current working directory. quiet: if False (the default), the context manager raises an exception on error. Otherwise, it issues only a warning and keeps the current working directory the same. """ saved_dir = os.getcwd() try: os.chdir(path) except OSError: if not quiet: raise warnings.warn('tests may fail, unable to change CWD to: ' + path, RuntimeWarning, stacklevel=3) try: yield os.getcwd() finally: os.chdir(saved_dir) @contextlib.contextmanager def temp_cwd(name='tempcwd', quiet=False): """ Context manager that temporarily creates and changes the CWD. The function temporarily changes the current working directory after creating a temporary directory in the current directory with name *name*. If *name* is None, the temporary directory is created using tempfile.mkdtemp. If *quiet* is False (default) and it is not possible to create or change the CWD, an error is raised. If *quiet* is True, only a warning is raised and the original CWD is used. """ with temp_dir(path=name, quiet=quiet) as temp_path: with change_cwd(temp_path, quiet=quiet) as cwd_dir: yield cwd_dir if hasattr(os, "umask"): @contextlib.contextmanager def temp_umask(umask): """Context manager that temporarily sets the process umask.""" oldmask = os.umask(umask) try: yield finally: os.umask(oldmask) # TEST_HOME_DIR refers to the top level directory of the "test" package # that contains Python's regression test suite TEST_SUPPORT_DIR = os.path.dirname(os.path.abspath(__file__)) TEST_HOME_DIR = os.path.dirname(TEST_SUPPORT_DIR) # TEST_DATA_DIR is used as a target download location for remote resources TEST_DATA_DIR = os.path.join(TEST_HOME_DIR, "data") def findfile(filename, subdir=None): """Try to find a file on sys.path or in the test directory. If it is not found the argument passed to the function is returned (this does not necessarily signal failure; could still be the legitimate path). Setting *subdir* indicates a relative path to use to find the file rather than looking directly in the path directories. """ if os.path.isabs(filename): return filename if subdir is not None: filename = os.path.join(subdir, filename) path = [TEST_HOME_DIR] + sys.path for dn in path: fn = os.path.join(dn, filename) if os.path.exists(fn): return fn return filename def create_empty_file(filename): """Create an empty file. If the file already exists, truncate it.""" fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) os.close(fd) def sortdict(dict): "Like repr(dict), but in sorted order." items = sorted(dict.items()) reprpairs = ["%r: %r" % pair for pair in items] withcommas = ", ".join(reprpairs) return "{%s}" % withcommas def make_bad_fd(): """ Create an invalid file descriptor by opening and closing a file and return its fd. """ file = open(TESTFN, "wb") try: return file.fileno() finally: file.close() unlink(TESTFN) def check_syntax_error(testcase, statement, *, lineno=None, offset=None): with testcase.assertRaises(SyntaxError) as cm: compile(statement, '', 'exec') err = cm.exception testcase.assertIsNotNone(err.lineno) if lineno is not None: testcase.assertEqual(err.lineno, lineno) testcase.assertIsNotNone(err.offset) if offset is not None: testcase.assertEqual(err.offset, offset) def open_urlresource(url, *args, **kw): import urllib.request, urllib.parse check = kw.pop('check', None) filename = urllib.parse.urlparse(url)[2].split('/')[-1] # '/': it's URL! fn = os.path.join(TEST_DATA_DIR, filename) def check_valid_file(fn): f = open(fn, *args, **kw) if check is None: return f elif check(f): f.seek(0) return f f.close() if os.path.exists(fn): f = check_valid_file(fn) if f is not None: return f unlink(fn) # Verify the requirement before downloading the file requires('urlfetch') if verbose: print('\tfetching %s ...' % url, file=get_original_stdout()) opener = urllib.request.build_opener() if gzip: opener.addheaders.append(('Accept-Encoding', 'gzip')) f = opener.open(url, timeout=15) if gzip and f.headers.get('Content-Encoding') == 'gzip': f = gzip.GzipFile(fileobj=f) try: with open(fn, "wb") as out: s = f.read() while s: out.write(s) s = f.read() finally: f.close() f = check_valid_file(fn) if f is not None: return f raise TestFailed('invalid resource %r' % fn) class WarningsRecorder(object): """Convenience wrapper for the warnings list returned on entry to the warnings.catch_warnings() context manager. """ def __init__(self, warnings_list): self._warnings = warnings_list self._last = 0 def __getattr__(self, attr): if len(self._warnings) > self._last: return getattr(self._warnings[-1], attr) elif attr in warnings.WarningMessage._WARNING_DETAILS: return None raise AttributeError("%r has no attribute %r" % (self, attr)) @property def warnings(self): return self._warnings[self._last:] def reset(self): self._last = len(self._warnings) def _filterwarnings(filters, quiet=False): """Catch the warnings, then check if all the expected warnings have been raised and re-raise unexpected warnings. If 'quiet' is True, only re-raise the unexpected warnings. """ # Clear the warning registry of the calling module # in order to re-raise the warnings. frame = sys._getframe(2) registry = frame.f_globals.get('__warningregistry__') if registry: registry.clear() with warnings.catch_warnings(record=True) as w: # Set filter "always" to record all warnings. Because # test_warnings swap the module, we need to look up in # the sys.modules dictionary. sys.modules['warnings'].simplefilter("always") yield WarningsRecorder(w) # Filter the recorded warnings reraise = list(w) missing = [] for msg, cat in filters: seen = False for w in reraise[:]: warning = w.message # Filter out the matching messages if (re.match(msg, str(warning), re.I) and issubclass(warning.__class__, cat)): seen = True reraise.remove(w) if not seen and not quiet: # This filter caught nothing missing.append((msg, cat.__name__)) if reraise: raise AssertionError("unhandled warning %s" % reraise[0]) if missing: raise AssertionError("filter (%r, %s) did not catch any warning" % missing[0]) @contextlib.contextmanager def check_warnings(*filters, **kwargs): """Context manager to silence warnings. Accept 2-tuples as positional arguments: ("message regexp", WarningCategory) Optional argument: - if 'quiet' is True, it does not fail if a filter catches nothing (default True without argument, default False if some filters are defined) Without argument, it defaults to: check_warnings(("", Warning), quiet=True) """ quiet = kwargs.get('quiet') if not filters: filters = (("", Warning),) # Preserve backward compatibility if quiet is None: quiet = True return _filterwarnings(filters, quiet) @contextlib.contextmanager def check_no_warnings(testcase, message='', category=Warning, force_gc=False): """Context manager to check that no warnings are emitted. This context manager enables a given warning within its scope and checks that no warnings are emitted even with that warning enabled. If force_gc is True, a garbage collection is attempted before checking for warnings. This may help to catch warnings emitted when objects are deleted, such as ResourceWarning. Other keyword arguments are passed to warnings.filterwarnings(). """ with warnings.catch_warnings(record=True) as warns: warnings.filterwarnings('always', message=message, category=category) yield if force_gc: gc_collect() testcase.assertEqual(warns, []) @contextlib.contextmanager def check_no_resource_warning(testcase): """Context manager to check that no ResourceWarning is emitted. Usage: with check_no_resource_warning(self): f = open(...) ... del f You must remove the object which may emit ResourceWarning before the end of the context manager. """ with warnings.catch_warnings(record=True) as warns: warnings.filterwarnings('always', category=ResourceWarning) yield gc_collect() testcase.assertEqual(warns, []) class CleanImport(object): """Context manager to force import to return a new module reference. This is useful for testing module-level behaviours, such as the emission of a DeprecationWarning on import. Use like this: with CleanImport("foo"): importlib.import_module("foo") # new reference """ def __init__(self, *module_names): self.original_modules = sys.modules.copy() for module_name in module_names: if module_name in sys.modules: module = sys.modules[module_name] # It is possible that module_name is just an alias for # another module (e.g. stub for modules renamed in 3.x). # In that case, we also need delete the real module to clear # the import cache. if module.__name__ != module_name: del sys.modules[module.__name__] del sys.modules[module_name] def __enter__(self): return self def __exit__(self, *ignore_exc): sys.modules.update(self.original_modules) class EnvironmentVarGuard(collections.abc.MutableMapping): """Class to help protect the environment variable properly. Can be used as a context manager.""" def __init__(self): self._environ = os.environ self._changed = {} def __getitem__(self, envvar): return self._environ[envvar] def __setitem__(self, envvar, value): # Remember the initial value on the first access if envvar not in self._changed: self._changed[envvar] = self._environ.get(envvar) self._environ[envvar] = value def __delitem__(self, envvar): # Remember the initial value on the first access if envvar not in self._changed: self._changed[envvar] = self._environ.get(envvar) if envvar in self._environ: del self._environ[envvar] def keys(self): return self._environ.keys() def __iter__(self): return iter(self._environ) def __len__(self): return len(self._environ) def set(self, envvar, value): self[envvar] = value def unset(self, envvar): del self[envvar] def __enter__(self): return self def __exit__(self, *ignore_exc): for (k, v) in self._changed.items(): if v is None: if k in self._environ: del self._environ[k] else: self._environ[k] = v os.environ = self._environ class DirsOnSysPath(object): """Context manager to temporarily add directories to sys.path. This makes a copy of sys.path, appends any directories given as positional arguments, then reverts sys.path to the copied settings when the context ends. Note that *all* sys.path modifications in the body of the context manager, including replacement of the object, will be reverted at the end of the block. """ def __init__(self, *paths): self.original_value = sys.path[:] self.original_object = sys.path sys.path.extend(paths) def __enter__(self): return self def __exit__(self, *ignore_exc): sys.path = self.original_object sys.path[:] = self.original_value class TransientResource(object): """Raise ResourceDenied if an exception is raised while the context manager is in effect that matches the specified exception and attributes.""" def __init__(self, exc, **kwargs): self.exc = exc self.attrs = kwargs def __enter__(self): return self def __exit__(self, type_=None, value=None, traceback=None): """If type_ is a subclass of self.exc and value has attributes matching self.attrs, raise ResourceDenied. Otherwise let the exception propagate (if any).""" if type_ is not None and issubclass(self.exc, type_): for attr, attr_value in self.attrs.items(): if not hasattr(value, attr): break if getattr(value, attr) != attr_value: break else: raise ResourceDenied("an optional resource is not available") # Context managers that raise ResourceDenied when various issues # with the Internet connection manifest themselves as exceptions. # XXX deprecate these and use transient_internet() instead time_out = TransientResource(OSError, errno=errno.ETIMEDOUT) socket_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET) ioerror_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET) @contextlib.contextmanager def transient_internet(resource_name, *, timeout=30.0, errnos=()): """Return a context manager that raises ResourceDenied when various issues with the Internet connection manifest themselves as exceptions.""" default_errnos = [ ('ECONNREFUSED', 111), ('ECONNRESET', 104), ('EHOSTUNREACH', 113), ('ENETUNREACH', 101), ('ETIMEDOUT', 110), # socket.create_connection() fails randomly with # EADDRNOTAVAIL on Travis CI. ('EADDRNOTAVAIL', 99), ] default_gai_errnos = [ ('EAI_AGAIN', -3), ('EAI_FAIL', -4), ('EAI_NONAME', -2), ('EAI_NODATA', -5), # Encountered when trying to resolve IPv6-only hostnames ('WSANO_DATA', 11004), ] denied = ResourceDenied("Resource %r is not available" % resource_name) captured_errnos = errnos gai_errnos = [] if not captured_errnos: captured_errnos = [getattr(errno, name, num) for (name, num) in default_errnos] gai_errnos = [getattr(socket, name, num) for (name, num) in default_gai_errnos] def filter_error(err): n = getattr(err, 'errno', None) if (isinstance(err, socket.timeout) or (isinstance(err, socket.gaierror) and n in gai_errnos) or (isinstance(err, urllib.error.HTTPError) and 500 <= err.code <= 599) or (isinstance(err, urllib.error.URLError) and (("ConnectionRefusedError" in err.reason) or ("TimeoutError" in err.reason) or ("EOFError" in err.reason))) or n in captured_errnos): if not verbose: sys.stderr.write(denied.args[0] + "\n") raise denied from err old_timeout = socket.getdefaulttimeout() try: if timeout is not None: socket.setdefaulttimeout(timeout) yield except nntplib.NNTPTemporaryError as err: if verbose: sys.stderr.write(denied.args[0] + "\n") raise denied from err except OSError as err: # urllib can wrap original socket errors multiple times (!), we must # unwrap to get at the original error. while True: a = err.args if len(a) >= 1 and isinstance(a[0], OSError): err = a[0] # The error can also be wrapped as args[1]: # except socket.error as msg: # raise OSError('socket error', msg).with_traceback(sys.exc_info()[2]) elif len(a) >= 2 and isinstance(a[1], OSError): err = a[1] else: break filter_error(err) raise # XXX should we catch generic exceptions and look for their # __cause__ or __context__? finally: socket.setdefaulttimeout(old_timeout) @contextlib.contextmanager def captured_output(stream_name): """Return a context manager used by captured_stdout/stdin/stderr that temporarily replaces the sys stream *stream_name* with a StringIO.""" import io orig_stdout = getattr(sys, stream_name) setattr(sys, stream_name, io.StringIO()) try: yield getattr(sys, stream_name) finally: setattr(sys, stream_name, orig_stdout) def captured_stdout(): """Capture the output of sys.stdout: with captured_stdout() as stdout: print("hello") self.assertEqual(stdout.getvalue(), "hello\\n") """ return captured_output("stdout") def captured_stderr(): """Capture the output of sys.stderr: with captured_stderr() as stderr: print("hello", file=sys.stderr) self.assertEqual(stderr.getvalue(), "hello\\n") """ return captured_output("stderr") def captured_stdin(): """Capture the input to sys.stdin: with captured_stdin() as stdin: stdin.write('hello\\n') stdin.seek(0) # call test code that consumes from sys.stdin captured = input() self.assertEqual(captured, "hello") """ return captured_output("stdin") def gc_collect(): """Force as many objects as possible to be collected. In non-CPython implementations of Python, this is needed because timely deallocation is not guaranteed by the garbage collector. (Even in CPython this can be the case in case of reference cycles.) This means that __del__ methods may be called later than expected and weakrefs may remain alive for longer than expected. This function tries its best to force all garbage objects to disappear. """ gc.collect() if is_jython: time.sleep(0.1) gc.collect() gc.collect() @contextlib.contextmanager def disable_gc(): have_gc = gc.isenabled() gc.disable() try: yield finally: if have_gc: gc.enable() def python_is_optimized(): """Find if Python was built with optimizations.""" cflags = sysconfig.get_config_var('PY_CFLAGS') or '' final_opt = "" for opt in cflags.split(): if opt.startswith('-O'): final_opt = opt return final_opt not in ('', '-O0', '-Og') _header = 'nP' _align = '0n' if hasattr(sys, "gettotalrefcount"): _header = '2P' + _header _align = '0P' _vheader = _header + 'n' def calcobjsize(fmt): return struct.calcsize(_header + fmt + _align) def calcvobjsize(fmt): return struct.calcsize(_vheader + fmt + _align) _TPFLAGS_HAVE_GC = 1<<14 _TPFLAGS_HEAPTYPE = 1<<9 def check_sizeof(test, o, size): import _testcapi result = sys.getsizeof(o) # add GC header size if ((type(o) == type) and (o.__flags__ & _TPFLAGS_HEAPTYPE) or\ ((type(o) != type) and (type(o).__flags__ & _TPFLAGS_HAVE_GC))): size += _testcapi.SIZEOF_PYGC_HEAD msg = 'wrong size for %s: got %d, expected %d' \ % (type(o), result, size) test.assertEqual(result, size, msg) #======================================================================= # Decorator for running a function in a different locale, correctly resetting # it afterwards. def run_with_locale(catstr, *locales): def decorator(func): def inner(*args, **kwds): try: import locale category = getattr(locale, catstr) orig_locale = locale.setlocale(category) except AttributeError: # if the test author gives us an invalid category string raise except: # cannot retrieve original locale, so do nothing locale = orig_locale = None else: for loc in locales: try: locale.setlocale(category, loc) break except: pass # now run the function, resetting the locale on exceptions try: return func(*args, **kwds) finally: if locale and orig_locale: locale.setlocale(category, orig_locale) inner.__name__ = func.__name__ inner.__doc__ = func.__doc__ return inner return decorator #======================================================================= # Decorator for running a function in a specific timezone, correctly # resetting it afterwards. def run_with_tz(tz): def decorator(func): def inner(*args, **kwds): try: tzset = time.tzset except AttributeError: raise unittest.SkipTest("tzset required") if 'TZ' in os.environ: orig_tz = os.environ['TZ'] else: orig_tz = None os.environ['TZ'] = tz tzset() # now run the function, resetting the tz on exceptions try: return func(*args, **kwds) finally: if orig_tz is None: del os.environ['TZ'] else: os.environ['TZ'] = orig_tz time.tzset() inner.__name__ = func.__name__ inner.__doc__ = func.__doc__ return inner return decorator #======================================================================= # Big-memory-test support. Separate from 'resources' because memory use # should be configurable. # Some handy shorthands. Note that these are used for byte-limits as well # as size-limits, in the various bigmem tests _1M = 1024*1024 _1G = 1024 * _1M _2G = 2 * _1G _4G = 4 * _1G MAX_Py_ssize_t = sys.maxsize def set_memlimit(limit): global max_memuse global real_max_memuse sizes = { 'k': 1024, 'm': _1M, 'g': _1G, 't': 1024*_1G, } m = re.match(r'(\d+(\.\d+)?) (K|M|G|T)b?$', limit, re.IGNORECASE | re.VERBOSE) if m is None: raise ValueError('Invalid memory limit %r' % (limit,)) memlimit = int(float(m.group(1)) * sizes[m.group(3).lower()]) real_max_memuse = memlimit if memlimit > MAX_Py_ssize_t: memlimit = MAX_Py_ssize_t if memlimit < _2G - 1: raise ValueError('Memory limit %r too low to be useful' % (limit,)) max_memuse = memlimit class _MemoryWatchdog: """An object which periodically watches the process' memory consumption and prints it out. """ def __init__(self): self.procfile = '/proc/{pid}/statm'.format(pid=os.getpid()) self.started = False def start(self): try: f = open(self.procfile, 'r') except OSError as e: warnings.warn('/proc not available for stats: {}'.format(e), RuntimeWarning) sys.stderr.flush() return watchdog_script = findfile("memory_watchdog.py") self.mem_watchdog = subprocess.Popen([sys.executable, watchdog_script], stdin=f, stderr=subprocess.DEVNULL) f.close() self.started = True def stop(self): if self.started: self.mem_watchdog.terminate() self.mem_watchdog.wait() def bigmemtest(size, memuse, dry_run=True): """Decorator for bigmem tests. 'size' is a requested size for the test (in arbitrary, test-interpreted units.) 'memuse' is the number of bytes per unit for the test, or a good estimate of it. For example, a test that needs two byte buffers, of 4 GiB each, could be decorated with @bigmemtest(size=_4G, memuse=2). The 'size' argument is normally passed to the decorated test method as an extra argument. If 'dry_run' is true, the value passed to the test method may be less than the requested value. If 'dry_run' is false, it means the test doesn't support dummy runs when -M is not specified. """ def decorator(f): def wrapper(self): size = wrapper.size memuse = wrapper.memuse if not real_max_memuse: maxsize = 5147 else: maxsize = size if ((real_max_memuse or not dry_run) and real_max_memuse < maxsize * memuse): raise unittest.SkipTest( "not enough memory: %.1fG minimum needed" % (size * memuse / (1024 ** 3))) if real_max_memuse and verbose: print() print(" ... expected peak memory use: {peak:.1f}G" .format(peak=size * memuse / (1024 ** 3))) watchdog = _MemoryWatchdog() watchdog.start() else: watchdog = None try: return f(self, maxsize) finally: if watchdog: watchdog.stop() wrapper.size = size wrapper.memuse = memuse return wrapper return decorator def bigaddrspacetest(f): """Decorator for tests that fill the address space.""" def wrapper(self): if max_memuse < MAX_Py_ssize_t: if MAX_Py_ssize_t >= 2**63 - 1 and max_memuse >= 2**31: raise unittest.SkipTest( "not enough memory: try a 32-bit build instead") else: raise unittest.SkipTest( "not enough memory: %.1fG minimum needed" % (MAX_Py_ssize_t / (1024 ** 3))) else: return f(self) return wrapper #======================================================================= # unittest integration. class BasicTestRunner: def run(self, test): result = unittest.TestResult() test(result) return result def _id(obj): return obj def requires_resource(resource): if resource == 'gui' and not _is_gui_available(): return unittest.skip(_is_gui_available.reason) if is_resource_enabled(resource): return _id else: return unittest.skip("resource {0!r} is not enabled".format(resource)) def requires_android_level(level, reason): if is_android and _ANDROID_API_LEVEL < level: return unittest.skip('%s at Android API level %d' % (reason, _ANDROID_API_LEVEL)) else: return _id def cpython_only(test): """ Decorator for tests only applicable on CPython. """ return impl_detail(cpython=True)(test) def impl_detail(msg=None, **guards): if check_impl_detail(**guards): return _id if msg is None: guardnames, default = _parse_guards(guards) if default: msg = "implementation detail not available on {0}" else: msg = "implementation detail specific to {0}" guardnames = sorted(guardnames.keys()) msg = msg.format(' or '.join(guardnames)) return unittest.skip(msg) _have_mp_queue = None def requires_multiprocessing_queue(test): """Skip decorator for tests that use multiprocessing.Queue.""" global _have_mp_queue if _have_mp_queue is None: import multiprocessing # Without a functioning shared semaphore implementation attempts to # instantiate a Queue will result in an ImportError (issue #3770). try: multiprocessing.Queue() _have_mp_queue = True except ImportError: _have_mp_queue = False msg = "requires a functioning shared semaphore implementation" return test if _have_mp_queue else unittest.skip(msg)(test) def _parse_guards(guards): # Returns a tuple ({platform_name: run_me}, default_value) if not guards: return ({'cpython': True}, False) is_true = list(guards.values())[0] assert list(guards.values()) == [is_true] * len(guards) # all True or all False return (guards, not is_true) # Use the following check to guard CPython's implementation-specific tests -- # or to run them only on the implementation(s) guarded by the arguments. def check_impl_detail(**guards): """This function returns True or False depending on the host platform. Examples: if check_impl_detail(): # only on CPython (default) if check_impl_detail(jython=True): # only on Jython if check_impl_detail(cpython=False): # everywhere except on CPython """ guards, default = _parse_guards(guards) return guards.get(platform.python_implementation().lower(), default) def no_tracing(func): """Decorator to temporarily turn off tracing for the duration of a test.""" if not hasattr(sys, 'gettrace'): return func else: @functools.wraps(func) def wrapper(*args, **kwargs): original_trace = sys.gettrace() try: sys.settrace(None) return func(*args, **kwargs) finally: sys.settrace(original_trace) return wrapper def refcount_test(test): """Decorator for tests which involve reference counting. To start, the decorator does not run the test if is not run by CPython. After that, any trace function is unset during the test to prevent unexpected refcounts caused by the trace function. """ return no_tracing(cpython_only(test)) def _filter_suite(suite, pred): """Recursively filter test cases in a suite based on a predicate.""" newtests = [] for test in suite._tests: if isinstance(test, unittest.TestSuite): _filter_suite(test, pred) newtests.append(test) else: if pred(test): newtests.append(test) suite._tests = newtests def _run_suite(suite): """Run tests from a unittest.TestSuite-derived class.""" runner = get_test_runner(sys.stdout, verbosity=verbose, capture_output=(junit_xml_list is not None)) result = runner.run(suite) if junit_xml_list is not None: junit_xml_list.append(result.get_xml_element()) if not result.testsRun: raise TestDidNotRun if not result.wasSuccessful(): if len(result.errors) == 1 and not result.failures: err = result.errors[0][1] elif len(result.failures) == 1 and not result.errors: err = result.failures[0][1] else: err = "multiple errors occurred" if not verbose: err += "; run in verbose mode for details" raise TestFailed(err) # By default, don't filter tests _match_test_func = None _match_test_patterns = None def match_test(test): # Function used by support.run_unittest() and regrtest --list-cases if _match_test_func is None: return True else: return _match_test_func(test.id()) def _is_full_match_test(pattern): # If a pattern contains at least one dot, it's considered # as a full test identifier. # Example: 'test.test_os.FileTests.test_access'. # # Reject patterns which contain fnmatch patterns: '*', '?', '[...]' # or '[!...]'. For example, reject 'test_access*'. return ('.' in pattern) and (not re.search(r'[?*\[\]]', pattern)) def set_match_tests(patterns): global _match_test_func, _match_test_patterns if patterns == _match_test_patterns: # No change: no need to recompile patterns. return if not patterns: func = None # set_match_tests(None) behaves as set_match_tests(()) patterns = () elif all(map(_is_full_match_test, patterns)): # Simple case: all patterns are full test identifier. # The test.bisect utility only uses such full test identifiers. func = set(patterns).__contains__ else: regex = '|'.join(map(fnmatch.translate, patterns)) # The search *is* case sensitive on purpose: # don't use flags=re.IGNORECASE regex_match = re.compile(regex).match def match_test_regex(test_id): if regex_match(test_id): # The regex matchs the whole identifier like # 'test.test_os.FileTests.test_access' return True else: # Try to match parts of the test identifier. # For example, split 'test.test_os.FileTests.test_access' # into: 'test', 'test_os', 'FileTests' and 'test_access'. return any(map(regex_match, test_id.split("."))) func = match_test_regex # Create a copy since patterns can be mutable and so modified later _match_test_patterns = tuple(patterns) _match_test_func = func def run_unittest(*classes): """Run tests from unittest.TestCase-derived classes.""" valid_types = (unittest.TestSuite, unittest.TestCase) suite = unittest.TestSuite() for cls in classes: if isinstance(cls, str): if cls in sys.modules: suite.addTest(unittest.findTestCases(sys.modules[cls])) else: raise ValueError("str arguments must be keys in sys.modules") elif isinstance(cls, valid_types): suite.addTest(cls) else: suite.addTest(unittest.makeSuite(cls)) _filter_suite(suite, match_test) _run_suite(suite) #======================================================================= # Check for the presence of docstrings. # Rather than trying to enumerate all the cases where docstrings may be # disabled, we just check for that directly def _check_docstrings(): """Just used to check if docstrings are enabled""" MISSING_C_DOCSTRINGS = (check_impl_detail() and sys.platform != 'win32' and not sysconfig.get_config_var('WITH_DOC_STRINGS')) HAVE_DOCSTRINGS = (_check_docstrings.__doc__ is not None and not MISSING_C_DOCSTRINGS) requires_docstrings = unittest.skipUnless(HAVE_DOCSTRINGS, "test requires docstrings") #======================================================================= # doctest driver. def run_doctest(module, verbosity=None, optionflags=0): """Run doctest on the given module. Return (#failures, #tests). If optional argument verbosity is not specified (or is None), pass support's belief about verbosity on to doctest. Else doctest's usual behavior is used (it searches sys.argv for -v). """ import doctest if verbosity is None: verbosity = verbose else: verbosity = None f, t = doctest.testmod(module, verbose=verbosity, optionflags=optionflags) if f: raise TestFailed("%d of %d doctests failed" % (f, t)) if verbose: print('doctest (%s) ... %d tests with zero failures' % (module.__name__, t)) return f, t #======================================================================= # Support for saving and restoring the imported modules. def modules_setup(): return sys.modules.copy(), def modules_cleanup(oldmodules): # Encoders/decoders are registered permanently within the internal # codec cache. If we destroy the corresponding modules their # globals will be set to None which will trip up the cached functions. encodings = [(k, v) for k, v in sys.modules.items() if k.startswith('encodings.')] sys.modules.clear() sys.modules.update(encodings) # XXX: This kind of problem can affect more than just encodings. In particular # extension modules (such as _ssl) don't cope with reloading properly. # Really, test modules should be cleaning out the test specific modules they # know they added (ala test_runpy) rather than relying on this function (as # test_importhooks and test_pkg do currently). # Implicitly imported *real* modules should be left alone (see issue 10556). sys.modules.update(oldmodules) #======================================================================= # Threading support to prevent reporting refleaks when running regrtest.py -R # Flag used by saved_test_environment of test.libregrtest.save_env, # to check if a test modified the environment. The flag should be set to False # before running a new test. # # For example, threading_cleanup() sets the flag is the function fails # to cleanup threads. environment_altered = False # NOTE: we use thread._count() rather than threading.enumerate() (or the # moral equivalent thereof) because a threading.Thread object is still alive # until its __bootstrap() method has returned, even after it has been # unregistered from the threading module. # thread._count(), on the other hand, only gets decremented *after* the # __bootstrap() method has returned, which gives us reliable reference counts # at the end of a test run. def threading_setup(): if _thread: return _thread._count(), threading._dangling.copy() else: return 1, () def threading_cleanup(*original_values): if not _thread: return _MAX_COUNT = 100 for count in range(_MAX_COUNT): values = _thread._count(), threading._dangling if values == original_values: break time.sleep(0.01) gc_collect() # XXX print a warning in case of failure? def reap_threads(func): """Use this function when threads are being used. This will ensure that the threads are cleaned up even when the test fails. If threading is unavailable this function does nothing. """ if not _thread: return func @functools.wraps(func) def decorator(*args): key = threading_setup() try: return func(*args) finally: threading_cleanup(*key) return decorator @contextlib.contextmanager def wait_threads_exit(timeout=60.0): """ bpo-31234: Context manager to wait until all threads created in the with statement exit. Use _thread.count() to check if threads exited. Indirectly, wait until threads exit the internal t_bootstrap() C function of the _thread module. threading_setup() and threading_cleanup() are designed to emit a warning if a test leaves running threads in the background. This context manager is designed to cleanup threads started by the _thread.start_new_thread() which doesn't allow to wait for thread exit, whereas thread.Thread has a join() method. """ old_count = _thread._count() try: yield finally: start_time = time.monotonic() deadline = start_time + timeout while True: count = _thread._count() if count <= old_count: break if time.monotonic() > deadline: dt = time.monotonic() - start_time msg = (f"wait_threads() failed to cleanup {count - old_count} " f"threads after {dt:.1f} seconds " f"(count: {count}, old count: {old_count})") raise AssertionError(msg) time.sleep(0.010) gc_collect() def reap_children(): """Use this function at the end of test_main() whenever sub-processes are started. This will help ensure that no extra children (zombies) stick around to hog resources and create problems when looking for refleaks. """ # Reap all our dead child processes so we don't leave zombies around. # These hog resources and might be causing some of the buildbots to die. if hasattr(os, 'waitpid'): any_process = -1 while True: try: # This will raise an exception on Windows. That's ok. pid, status = os.waitpid(any_process, os.WNOHANG) if pid == 0: break print("Warning -- reap_children() reaped child process %s" % pid, file=sys.stderr) except: break @contextlib.contextmanager def start_threads(threads, unlock=None): threads = list(threads) started = [] try: try: for t in threads: t.start() started.append(t) except: if verbose: print("Can't start %d threads, only %d threads started" % (len(threads), len(started))) raise yield finally: try: if unlock: unlock() endtime = starttime = time.time() for timeout in range(1, 16): endtime += 60 for t in started: t.join(max(endtime - time.time(), 0.01)) started = [t for t in started if t.isAlive()] if not started: break if verbose: print('Unable to join %d threads during a period of ' '%d minutes' % (len(started), timeout)) finally: started = [t for t in started if t.isAlive()] if started: faulthandler.dump_traceback(sys.stdout) raise AssertionError('Unable to join %d threads' % len(started)) @contextlib.contextmanager def swap_attr(obj, attr, new_val): """Temporary swap out an attribute with a new object. Usage: with swap_attr(obj, "attr", 5): ... This will set obj.attr to 5 for the duration of the with: block, restoring the old value at the end of the block. If `attr` doesn't exist on `obj`, it will be created and then deleted at the end of the block. The old value (or None if it doesn't exist) will be assigned to the target of the "as" clause, if there is one. """ if hasattr(obj, attr): real_val = getattr(obj, attr) setattr(obj, attr, new_val) try: yield real_val finally: setattr(obj, attr, real_val) else: setattr(obj, attr, new_val) try: yield finally: if hasattr(obj, attr): delattr(obj, attr) @contextlib.contextmanager def swap_item(obj, item, new_val): """Temporary swap out an item with a new object. Usage: with swap_item(obj, "item", 5): ... This will set obj["item"] to 5 for the duration of the with: block, restoring the old value at the end of the block. If `item` doesn't exist on `obj`, it will be created and then deleted at the end of the block. The old value (or None if it doesn't exist) will be assigned to the target of the "as" clause, if there is one. """ if item in obj: real_val = obj[item] obj[item] = new_val try: yield real_val finally: obj[item] = real_val else: obj[item] = new_val try: yield finally: if item in obj: del obj[item] def strip_python_stderr(stderr): """Strip the stderr of a Python process from potential debug output emitted by the interpreter. This will typically be run on the result of the communicate() method of a subprocess.Popen object. """ stderr = re.sub(br"\[\d+ refs, \d+ blocks\]\r?\n?", b"", stderr).strip() return stderr requires_type_collecting = unittest.skipIf(hasattr(sys, 'getcounts'), 'types are immortal if COUNT_ALLOCS is defined') def args_from_interpreter_flags(): """Return a list of command-line arguments reproducing the current settings in sys.flags and sys.warnoptions.""" return subprocess._args_from_interpreter_flags() def optim_args_from_interpreter_flags(): """Return a list of command-line arguments reproducing the current optimization settings in sys.flags.""" return subprocess._optim_args_from_interpreter_flags() #============================================================ # Support for assertions about logging. #============================================================ class TestHandler(logging.handlers.BufferingHandler): def __init__(self, matcher): # BufferingHandler takes a "capacity" argument # so as to know when to flush. As we're overriding # shouldFlush anyway, we can set a capacity of zero. # You can call flush() manually to clear out the # buffer. logging.handlers.BufferingHandler.__init__(self, 0) self.matcher = matcher def shouldFlush(self): return False def emit(self, record): self.format(record) self.buffer.append(record.__dict__) def matches(self, **kwargs): """ Look for a saved dict whose keys/values match the supplied arguments. """ result = False for d in self.buffer: if self.matcher.matches(d, **kwargs): result = True break return result class Matcher(object): _partial_matches = ('msg', 'message') def matches(self, d, **kwargs): """ Try to match a single dict with the supplied arguments. Keys whose values are strings and which are in self._partial_matches will be checked for partial (i.e. substring) matches. You can extend this scheme to (for example) do regular expression matching, etc. """ result = True for k in kwargs: v = kwargs[k] dv = d.get(k) if not self.match_value(k, dv, v): result = False break return result def match_value(self, k, dv, v): """ Try to match a single stored value (dv) with a supplied value (v). """ if type(v) != type(dv): result = False elif type(dv) is not str or k not in self._partial_matches: result = (v == dv) else: result = dv.find(v) >= 0 return result _can_symlink = None def can_symlink(): global _can_symlink if _can_symlink is not None: return _can_symlink symlink_path = TESTFN + "can_symlink" try: os.symlink(TESTFN, symlink_path) can = True except (OSError, NotImplementedError, AttributeError): can = False else: os.remove(symlink_path) _can_symlink = can return can def skip_unless_symlink(test): """Skip decorator for tests that require functional symlink""" ok = can_symlink() msg = "Requires functional symlink implementation" return test if ok else unittest.skip(msg)(test) _can_xattr = None def can_xattr(): global _can_xattr if _can_xattr is not None: return _can_xattr if not hasattr(os, "setxattr"): can = False else: tmp_dir = tempfile.mkdtemp() tmp_fp, tmp_name = tempfile.mkstemp(dir=tmp_dir) try: with open(TESTFN, "wb") as fp: try: # TESTFN & tempfile may use different file systems with # different capabilities os.setxattr(tmp_fp, b"user.test", b"") os.setxattr(tmp_name, b"trusted.foo", b"42") os.setxattr(fp.fileno(), b"user.test", b"") # Kernels < 2.6.39 don't respect setxattr flags. kernel_version = platform.release() m = re.match(r"2.6.(\d{1,2})", kernel_version) can = m is None or int(m.group(1)) >= 39 except OSError: can = False finally: unlink(TESTFN) unlink(tmp_name) rmdir(tmp_dir) _can_xattr = can return can def skip_unless_xattr(test): """Skip decorator for tests that require functional extended attributes""" ok = can_xattr() msg = "no non-broken extended attribute support" return test if ok else unittest.skip(msg)(test) def skip_if_pgo_task(test): """Skip decorator for tests not run in (non-extended) PGO task""" ok = not PGO or PGO_EXTENDED msg = "Not run for (non-extended) PGO task" return test if ok else unittest.skip(msg)(test) def fs_is_case_insensitive(directory): """Detects if the file system for the specified directory is case-insensitive.""" with tempfile.NamedTemporaryFile(dir=directory) as base: base_path = base.name case_path = base_path.upper() if case_path == base_path: case_path = base_path.lower() try: return os.path.samefile(base_path, case_path) except FileNotFoundError: return False def detect_api_mismatch(ref_api, other_api, *, ignore=()): """Returns the set of items in ref_api not in other_api, except for a defined list of items to be ignored in this check. By default this skips private attributes beginning with '_' but includes all magic methods, i.e. those starting and ending in '__'. """ missing_items = set(dir(ref_api)) - set(dir(other_api)) if ignore: missing_items -= set(ignore) missing_items = set(m for m in missing_items if not m.startswith('_') or m.endswith('__')) return missing_items def check__all__(test_case, module, name_of_module=None, extra=(), blacklist=()): """Assert that the __all__ variable of 'module' contains all public names. The module's public names (its API) are detected automatically based on whether they match the public name convention and were defined in 'module'. The 'name_of_module' argument can specify (as a string or tuple thereof) what module(s) an API could be defined in in order to be detected as a public API. One case for this is when 'module' imports part of its public API from other modules, possibly a C backend (like 'csv' and its '_csv'). The 'extra' argument can be a set of names that wouldn't otherwise be automatically detected as "public", like objects without a proper '__module__' attribute. If provided, it will be added to the automatically detected ones. The 'blacklist' argument can be a set of names that must not be treated as part of the public API even though their names indicate otherwise. Usage: import bar import foo import unittest from test import support class MiscTestCase(unittest.TestCase): def test__all__(self): support.check__all__(self, foo) class OtherTestCase(unittest.TestCase): def test__all__(self): extra = {'BAR_CONST', 'FOO_CONST'} blacklist = {'baz'} # Undocumented name. # bar imports part of its API from _bar. support.check__all__(self, bar, ('bar', '_bar'), extra=extra, blacklist=blacklist) """ if name_of_module is None: name_of_module = (module.__name__, ) elif isinstance(name_of_module, str): name_of_module = (name_of_module, ) expected = set(extra) for name in dir(module): if name.startswith('_') or name in blacklist: continue obj = getattr(module, name) if (getattr(obj, '__module__', None) in name_of_module or (not hasattr(obj, '__module__') and not isinstance(obj, types.ModuleType))): expected.add(name) test_case.assertCountEqual(module.__all__, expected) class SuppressCrashReport: """Try to prevent a crash report from popping up. On Windows, don't display the Windows Error Reporting dialog. On UNIX, disable the creation of coredump file. """ old_value = None old_modes = None def __enter__(self): """On Windows, disable Windows Error Reporting dialogs using SetErrorMode. On UNIX, try to save the previous core file size limit, then set soft limit to 0. """ if sys.platform.startswith('win'): # see http://msdn.microsoft.com/en-us/library/windows/desktop/ms680621.aspx # GetErrorMode is not available on Windows XP and Windows Server 2003, # but SetErrorMode returns the previous value, so we can use that import ctypes self._k32 = ctypes.windll.kernel32 SEM_NOGPFAULTERRORBOX = 0x02 self.old_value = self._k32.SetErrorMode(SEM_NOGPFAULTERRORBOX) self._k32.SetErrorMode(self.old_value | SEM_NOGPFAULTERRORBOX) # Suppress assert dialogs in debug builds # (see http://bugs.python.org/issue23314) try: import msvcrt msvcrt.CrtSetReportMode except (AttributeError, ImportError): # no msvcrt or a release build pass else: self.old_modes = {} for report_type in [msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT]: old_mode = msvcrt.CrtSetReportMode(report_type, msvcrt.CRTDBG_MODE_FILE) old_file = msvcrt.CrtSetReportFile(report_type, msvcrt.CRTDBG_FILE_STDERR) self.old_modes[report_type] = old_mode, old_file else: if resource is not None: try: self.old_value = resource.getrlimit(resource.RLIMIT_CORE) resource.setrlimit(resource.RLIMIT_CORE, (0, self.old_value[1])) except (ValueError, OSError): pass if sys.platform == 'darwin': # Check if the 'Crash Reporter' on OSX was configured # in 'Developer' mode and warn that it will get triggered # when it is. # # This assumes that this context manager is used in tests # that might trigger the next manager. cmd = ['/usr/bin/defaults', 'read', 'com.apple.CrashReporter', 'DialogType'] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) with proc: stdout = proc.communicate()[0] if stdout.strip() == b'developer': print("this test triggers the Crash Reporter, " "that is intentional", end='', flush=True) return self def __exit__(self, *ignore_exc): """Restore Windows ErrorMode or core file behavior to initial value.""" if self.old_value is None: return if sys.platform.startswith('win'): self._k32.SetErrorMode(self.old_value) if self.old_modes: import msvcrt for report_type, (old_mode, old_file) in self.old_modes.items(): msvcrt.CrtSetReportMode(report_type, old_mode) msvcrt.CrtSetReportFile(report_type, old_file) else: if resource is not None: try: resource.setrlimit(resource.RLIMIT_CORE, self.old_value) except (ValueError, OSError): pass def patch(test_instance, object_to_patch, attr_name, new_value): """Override 'object_to_patch'.'attr_name' with 'new_value'. Also, add a cleanup procedure to 'test_instance' to restore 'object_to_patch' value for 'attr_name'. The 'attr_name' should be a valid attribute for 'object_to_patch'. """ # check that 'attr_name' is a real attribute for 'object_to_patch' # will raise AttributeError if it does not exist getattr(object_to_patch, attr_name) # keep a copy of the old value attr_is_local = False try: old_value = object_to_patch.__dict__[attr_name] except (AttributeError, KeyError): old_value = getattr(object_to_patch, attr_name, None) else: attr_is_local = True # restore the value when the test is done def cleanup(): if attr_is_local: setattr(object_to_patch, attr_name, old_value) else: delattr(object_to_patch, attr_name) test_instance.addCleanup(cleanup) # actually override the attribute setattr(object_to_patch, attr_name, new_value) def run_in_subinterp(code): """ Run code in a subinterpreter. Raise unittest.SkipTest if the tracemalloc module is enabled. """ # Issue #10915, #15751: PyGILState_*() functions don't work with # sub-interpreters, the tracemalloc module uses these functions internally try: import tracemalloc except ImportError: pass else: if tracemalloc.is_tracing(): raise unittest.SkipTest("run_in_subinterp() cannot be used " "if tracemalloc module is tracing " "memory allocations") import _testcapi return _testcapi.run_in_subinterp(code) def check_free_after_iterating(test, iter, cls, args=()): class A(cls): def __del__(self): nonlocal done done = True try: next(it) except StopIteration: pass done = False it = iter(A(*args)) # Issue 26494: Shouldn't crash test.assertRaises(StopIteration, next, it) # The sequence should be deallocated just after the end of iterating gc_collect() test.assertTrue(done) def missing_compiler_executable(cmd_names=[]): """Check if the compiler components used to build the interpreter exist. Check for the existence of the compiler executables whose names are listed in 'cmd_names' or all the compiler executables when 'cmd_names' is empty and return the first missing executable or None when none is found missing. """ from distutils import ccompiler, sysconfig, spawn compiler = ccompiler.new_compiler() sysconfig.customize_compiler(compiler) for name in compiler.executables: if cmd_names and name not in cmd_names: continue cmd = getattr(compiler, name) if cmd_names: assert cmd is not None, \ "the '%s' executable is not configured" % name elif cmd is None: continue if spawn.find_executable(cmd[0]) is None: return cmd[0] _is_android_emulator = None def setswitchinterval(interval): # Setting a very low gil interval on the Android emulator causes python # to hang (issue #26939). minimum_interval = 1e-5 if is_android and interval < minimum_interval: global _is_android_emulator if _is_android_emulator is None: _is_android_emulator = (subprocess.check_output( ['getprop', 'ro.kernel.qemu']).strip() == b'1') if _is_android_emulator: interval = minimum_interval return sys.setswitchinterval(interval) @contextlib.contextmanager def disable_faulthandler(): # use sys.__stderr__ instead of sys.stderr, since regrtest replaces # sys.stderr with a StringIO which has no file descriptor when a test # is run with -W/--verbose3. fd = sys.__stderr__.fileno() is_enabled = faulthandler.is_enabled() try: faulthandler.disable() yield finally: if is_enabled: faulthandler.enable(file=fd, all_threads=True) def fd_count(): """Count the number of open file descriptors. """ if sys.platform.startswith(('linux', 'freebsd')): try: names = os.listdir("/proc/self/fd") # Substract one because listdir() opens internally a file # descriptor to list the content of the /proc/self/fd/ directory. return len(names) - 1 except FileNotFoundError: pass MAXFD = 256 if hasattr(os, 'sysconf'): try: MAXFD = os.sysconf("SC_OPEN_MAX") except OSError: pass old_modes = None if sys.platform == 'win32': # bpo-25306, bpo-31009: Call CrtSetReportMode() to not kill the process # on invalid file descriptor if Python is compiled in debug mode try: import msvcrt msvcrt.CrtSetReportMode except (AttributeError, ImportError): # no msvcrt or a release build pass else: old_modes = {} for report_type in (msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT): old_modes[report_type] = msvcrt.CrtSetReportMode(report_type, 0) try: count = 0 for fd in range(MAXFD): try: # Prefer dup() over fstat(). fstat() can require input/output # whereas dup() doesn't. fd2 = os.dup(fd) except OSError as e: if e.errno != errno.EBADF: raise else: os.close(fd2) count += 1 finally: if old_modes is not None: for report_type in (msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT): msvcrt.CrtSetReportMode(report_type, old_modes[report_type]) return count class SaveSignals: """ Save an restore signal handlers. This class is only able to save/restore signal handlers registered by the Python signal module: see bpo-13285 for "external" signal handlers. """ def __init__(self): import signal self.signal = signal self.signals = list(range(1, signal.NSIG)) # SIGKILL and SIGSTOP signals cannot be ignored nor catched for signame in ('SIGKILL', 'SIGSTOP'): try: signum = getattr(signal, signame) except AttributeError: continue self.signals.remove(signum) self.handlers = {} def save(self): for signum in self.signals: handler = self.signal.getsignal(signum) if handler is None: # getsignal() returns None if a signal handler was not # registered by the Python signal module, # and the handler is not SIG_DFL nor SIG_IGN. # # Ignore the signal: we cannot restore the handler. continue self.handlers[signum] = handler def restore(self): for signum, handler in self.handlers.items(): self.signal.signal(signum, handler) class FakePath: """Simple implementing of the path protocol. """ def __init__(self, path): self.path = path def __repr__(self): return f'' def __fspath__(self): if (isinstance(self.path, BaseException) or isinstance(self.path, type) and issubclass(self.path, BaseException)): raise self.path else: return self.path @contextlib.contextmanager def adjust_int_max_str_digits(max_digits): """Temporarily change the integer string conversion length limit.""" current = sys.get_int_max_str_digits() try: sys.set_int_max_str_digits(max_digits) yield finally: sys.set_int_max_str_digits(current) PK!\))support/script_helper.pynu[# Common utility functions used by various script execution tests # e.g. test_cmd_line, test_cmd_line_script and test_runpy import collections import importlib import sys import os import os.path import tempfile import subprocess import py_compile import contextlib import shutil import zipfile from importlib.util import source_from_cache from test.support import make_legacy_pyc, strip_python_stderr # Cached result of the expensive test performed in the function below. __cached_interp_requires_environment = None def interpreter_requires_environment(): """ Returns True if our sys.executable interpreter requires environment variables in order to be able to run at all. This is designed to be used with @unittest.skipIf() to annotate tests that need to use an assert_python*() function to launch an isolated mode (-I) or no environment mode (-E) sub-interpreter process. A normal build & test does not run into this situation but it can happen when trying to run the standard library test suite from an interpreter that doesn't have an obvious home with Python's current home finding logic. Setting PYTHONHOME is one way to get most of the testsuite to run in that situation. PYTHONPATH or PYTHONUSERSITE are other common environment variables that might impact whether or not the interpreter can start. """ global __cached_interp_requires_environment if __cached_interp_requires_environment is None: # If PYTHONHOME is set, assume that we need it if 'PYTHONHOME' in os.environ: __cached_interp_requires_environment = True return True # Try running an interpreter with -E to see if it works or not. try: subprocess.check_call([sys.executable, '-E', '-c', 'import sys; sys.exit(0)']) except subprocess.CalledProcessError: __cached_interp_requires_environment = True else: __cached_interp_requires_environment = False return __cached_interp_requires_environment class _PythonRunResult(collections.namedtuple("_PythonRunResult", ("rc", "out", "err"))): """Helper for reporting Python subprocess run results""" def fail(self, cmd_line): """Provide helpful details about failed subcommand runs""" # Limit to 80 lines to ASCII characters maxlen = 80 * 100 out, err = self.out, self.err if len(out) > maxlen: out = b'(... truncated stdout ...)' + out[-maxlen:] if len(err) > maxlen: err = b'(... truncated stderr ...)' + err[-maxlen:] out = out.decode('ascii', 'replace').rstrip() err = err.decode('ascii', 'replace').rstrip() raise AssertionError("Process return code is %d\n" "command line: %r\n" "\n" "stdout:\n" "---\n" "%s\n" "---\n" "\n" "stderr:\n" "---\n" "%s\n" "---" % (self.rc, cmd_line, out, err)) # Executing the interpreter in a subprocess def run_python_until_end(*args, **env_vars): env_required = interpreter_requires_environment() if '__isolated' in env_vars: isolated = env_vars.pop('__isolated') else: isolated = not env_vars and not env_required cmd_line = [sys.executable, '-X', 'faulthandler'] if isolated: # isolated mode: ignore Python environment variables, ignore user # site-packages, and don't add the current directory to sys.path cmd_line.append('-I') elif not env_vars and not env_required: # ignore Python environment variables cmd_line.append('-E') # But a special flag that can be set to override -- in this case, the # caller is responsible to pass the full environment. if env_vars.pop('__cleanenv', None): env = {} if sys.platform == 'win32': # Windows requires at least the SYSTEMROOT environment variable to # start Python. env['SYSTEMROOT'] = os.environ['SYSTEMROOT'] # Other interesting environment variables, not copied currently: # COMSPEC, HOME, PATH, TEMP, TMPDIR, TMP. else: # Need to preserve the original environment, for in-place testing of # shared library builds. env = os.environ.copy() # set TERM='' unless the TERM environment variable is passed explicitly # see issues #11390 and #18300 if 'TERM' not in env_vars: env['TERM'] = '' env.update(env_vars) cmd_line.extend(args) proc = subprocess.Popen(cmd_line, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) with proc: try: out, err = proc.communicate() finally: proc.kill() subprocess._cleanup() rc = proc.returncode err = strip_python_stderr(err) return _PythonRunResult(rc, out, err), cmd_line def _assert_python(expected_success, *args, **env_vars): res, cmd_line = run_python_until_end(*args, **env_vars) if (res.rc and expected_success) or (not res.rc and not expected_success): res.fail(cmd_line) return res def assert_python_ok(*args, **env_vars): """ Assert that running the interpreter with `args` and optional environment variables `env_vars` succeeds (rc == 0) and return a (return code, stdout, stderr) tuple. If the __cleanenv keyword is set, env_vars is used as a fresh environment. Python is started in isolated mode (command line option -I), except if the __isolated keyword is set to False. """ return _assert_python(True, *args, **env_vars) def assert_python_failure(*args, **env_vars): """ Assert that running the interpreter with `args` and optional environment variables `env_vars` fails (rc != 0) and return a (return code, stdout, stderr) tuple. See assert_python_ok() for more options. """ return _assert_python(False, *args, **env_vars) def spawn_python(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw): """Run a Python subprocess with the given arguments. kw is extra keyword args to pass to subprocess.Popen. Returns a Popen object. """ cmd_line = [sys.executable] if not interpreter_requires_environment(): cmd_line.append('-E') cmd_line.extend(args) # Under Fedora (?), GNU readline can output junk on stderr when initialized, # depending on the TERM setting. Setting TERM=vt100 is supposed to disable # that. References: # - http://reinout.vanrees.org/weblog/2009/08/14/readline-invisible-character-hack.html # - http://stackoverflow.com/questions/15760712/python-readline-module-prints-escape-character-during-import # - http://lists.gnu.org/archive/html/bug-readline/2007-08/msg00004.html env = kw.setdefault('env', dict(os.environ)) env['TERM'] = 'vt100' return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, stdout=stdout, stderr=stderr, **kw) def kill_python(p): """Run the given Popen process until completion and return stdout.""" p.stdin.close() data = p.stdout.read() p.stdout.close() # try to cleanup the child so we don't appear to leak when running # with regrtest -R. p.wait() subprocess._cleanup() return data def make_script(script_dir, script_basename, source, omit_suffix=False): script_filename = script_basename if not omit_suffix: script_filename += os.extsep + 'py' script_name = os.path.join(script_dir, script_filename) # The script should be encoded to UTF-8, the default string encoding script_file = open(script_name, 'w', encoding='utf-8') script_file.write(source) script_file.close() importlib.invalidate_caches() return script_name def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None): zip_filename = zip_basename+os.extsep+'zip' zip_name = os.path.join(zip_dir, zip_filename) zip_file = zipfile.ZipFile(zip_name, 'w') if name_in_zip is None: parts = script_name.split(os.sep) if len(parts) >= 2 and parts[-2] == '__pycache__': legacy_pyc = make_legacy_pyc(source_from_cache(script_name)) name_in_zip = os.path.basename(legacy_pyc) script_name = legacy_pyc else: name_in_zip = os.path.basename(script_name) zip_file.write(script_name, name_in_zip) zip_file.close() #if test.support.verbose: # zip_file = zipfile.ZipFile(zip_name, 'r') # print 'Contents of %r:' % zip_name # zip_file.printdir() # zip_file.close() return zip_name, os.path.join(zip_name, name_in_zip) def make_pkg(pkg_dir, init_source=''): os.mkdir(pkg_dir) make_script(pkg_dir, '__init__', init_source) def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, source, depth=1, compiled=False): unlink = [] init_name = make_script(zip_dir, '__init__', '') unlink.append(init_name) init_basename = os.path.basename(init_name) script_name = make_script(zip_dir, script_basename, source) unlink.append(script_name) if compiled: init_name = py_compile.compile(init_name, doraise=True) script_name = py_compile.compile(script_name, doraise=True) unlink.extend((init_name, script_name)) pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)] script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name)) zip_filename = zip_basename+os.extsep+'zip' zip_name = os.path.join(zip_dir, zip_filename) zip_file = zipfile.ZipFile(zip_name, 'w') for name in pkg_names: init_name_in_zip = os.path.join(name, init_basename) zip_file.write(init_name, init_name_in_zip) zip_file.write(script_name, script_name_in_zip) zip_file.close() for name in unlink: os.unlink(name) #if test.support.verbose: # zip_file = zipfile.ZipFile(zip_name, 'r') # print 'Contents of %r:' % zip_name # zip_file.printdir() # zip_file.close() return zip_name, os.path.join(zip_name, script_name_in_zip) PK!  support/testresult.pynu['''Test runner and result class for the regression test suite. ''' import functools import io import sys import time import traceback import unittest import xml.etree.ElementTree as ET from datetime import datetime class RegressionTestResult(unittest.TextTestResult): separator1 = '=' * 70 + '\n' separator2 = '-' * 70 + '\n' def __init__(self, stream, descriptions, verbosity): super().__init__(stream=stream, descriptions=descriptions, verbosity=0) self.buffer = True self.__suite = ET.Element('testsuite') self.__suite.set('start', datetime.utcnow().isoformat(' ')) self.__e = None self.__start_time = None self.__results = [] self.__verbose = bool(verbosity) @classmethod def __getId(cls, test): try: test_id = test.id except AttributeError: return str(test) try: return test_id() except TypeError: return str(test_id) return repr(test) def startTest(self, test): super().startTest(test) self.__e = e = ET.SubElement(self.__suite, 'testcase') self.__start_time = time.perf_counter() if self.__verbose: self.stream.write(f'{self.getDescription(test)} ... ') self.stream.flush() def _add_result(self, test, capture=False, **args): e = self.__e self.__e = None if e is None: return e.set('name', args.pop('name', self.__getId(test))) e.set('status', args.pop('status', 'run')) e.set('result', args.pop('result', 'completed')) if self.__start_time: e.set('time', f'{time.perf_counter() - self.__start_time:0.6f}') if capture: if self._stdout_buffer is not None: stdout = self._stdout_buffer.getvalue().rstrip() ET.SubElement(e, 'system-out').text = stdout if self._stderr_buffer is not None: stderr = self._stderr_buffer.getvalue().rstrip() ET.SubElement(e, 'system-err').text = stderr for k, v in args.items(): if not k or not v: continue e2 = ET.SubElement(e, k) if hasattr(v, 'items'): for k2, v2 in v.items(): if k2: e2.set(k2, str(v2)) else: e2.text = str(v2) else: e2.text = str(v) def __write(self, c, word): if self.__verbose: self.stream.write(f'{word}\n') @classmethod def __makeErrorDict(cls, err_type, err_value, err_tb): if isinstance(err_type, type): if err_type.__module__ == 'builtins': typename = err_type.__name__ else: typename = f'{err_type.__module__}.{err_type.__name__}' else: typename = repr(err_type) msg = traceback.format_exception(err_type, err_value, None) tb = traceback.format_exception(err_type, err_value, err_tb) return { 'type': typename, 'message': ''.join(msg), '': ''.join(tb), } def addError(self, test, err): self._add_result(test, True, error=self.__makeErrorDict(*err)) super().addError(test, err) self.__write('E', 'ERROR') def addExpectedFailure(self, test, err): self._add_result(test, True, output=self.__makeErrorDict(*err)) super().addExpectedFailure(test, err) self.__write('x', 'expected failure') def addFailure(self, test, err): self._add_result(test, True, failure=self.__makeErrorDict(*err)) super().addFailure(test, err) self.__write('F', 'FAIL') def addSkip(self, test, reason): self._add_result(test, skipped=reason) super().addSkip(test, reason) self.__write('S', f'skipped {reason!r}') def addSuccess(self, test): self._add_result(test) super().addSuccess(test) self.__write('.', 'ok') def addUnexpectedSuccess(self, test): self._add_result(test, outcome='UNEXPECTED_SUCCESS') super().addUnexpectedSuccess(test) self.__write('u', 'unexpected success') def printErrors(self): if self.__verbose: self.stream.write('\n') self.printErrorList('ERROR', self.errors) self.printErrorList('FAIL', self.failures) def printErrorList(self, flavor, errors): for test, err in errors: self.stream.write(self.separator1) self.stream.write(f'{flavor}: {self.getDescription(test)}\n') self.stream.write(self.separator2) self.stream.write('%s\n' % err) def get_xml_element(self): e = self.__suite e.set('tests', str(self.testsRun)) e.set('errors', str(len(self.errors))) e.set('failures', str(len(self.failures))) return e class QuietRegressionTestRunner: def __init__(self, stream, buffer=False): self.result = RegressionTestResult(stream, None, 0) self.result.buffer = buffer def run(self, test): test(self.result) return self.result def get_test_runner_class(verbosity, buffer=False): if verbosity: return functools.partial(unittest.TextTestRunner, resultclass=RegressionTestResult, buffer=buffer, verbosity=verbosity) return functools.partial(QuietRegressionTestRunner, buffer=buffer) def get_test_runner(stream, verbosity, capture_output=False): return get_test_runner_class(verbosity, capture_output)(stream) if __name__ == '__main__': class TestTests(unittest.TestCase): def test_pass(self): pass def test_pass_slow(self): time.sleep(1.0) def test_fail(self): print('stdout', file=sys.stdout) print('stderr', file=sys.stderr) self.fail('failure message') def test_error(self): print('stdout', file=sys.stdout) print('stderr', file=sys.stderr) raise RuntimeError('error message') suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestTests)) stream = io.StringIO() runner_cls = get_test_runner_class(sum(a == '-v' for a in sys.argv)) runner = runner_cls(sys.stdout) result = runner.run(suite) print('Output:', stream.getvalue()) print('XML: ', end='') for s in ET.tostringlist(result.get_xml_element()): print(s.decode(), end='') print() PK!E[// __init__.pynu[# Dummy file to make this directory a package. PK!Buxx)__pycache__/__init__.cpython-36.opt-1.pycnu[PK!Buxx)__pycache__/__init__.cpython-36.opt-2.pycnu[PK!Buxx#__pycache__/__init__.cpython-36.pycnu[PK!)-=-=1msupport/__pycache__/__init__.cpython-36.opt-1.pycnu[PK!JtVV1?support/__pycache__/__init__.cpython-36.opt-2.pycnu[PK!Bx==+ support/__pycache__/__init__.cpython-36.pycnu[PK!Yj6^support/__pycache__/script_helper.cpython-36.opt-1.pycnu[PK!>Z6zsupport/__pycache__/script_helper.cpython-36.opt-2.pycnu[PK!Yj0Tsupport/__pycache__/script_helper.cpython-36.pycnu[PK!r&XX3vsupport/__pycache__/testresult.cpython-36.opt-1.pycnu[PK! P  31support/__pycache__/testresult.cpython-36.opt-2.pycnu[PK!r&XX-support/__pycache__/testresult.cpython-36.pycnu[PK!}&Ssupport/__init__.pynu[PK!\))Nsupport/script_helper.pynu[PK!  support/testresult.pynu[PK!E[// p__init__.pynu[PK|