o ,]@sdZddlmZmZmZmZddlmZddlm Z m Z m Z m Z m Z mZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZeedsYddlZddlZddlZddlZddlZddl Z zddl!Z!Wne"yddl#m!Z!Ynwddl$Z$ddl%Z&ddl'Z'ddl(Z(zej)rddl*Z*ddl+Z+nddl,Z*ddl+Z+Wn e"ydZ*dZ+Ynwzddl-Z.Wn e"ydZ.Ynwzddl/Z/Wn e"ydZ/Ynwzddl0Z0Wn e"ydZ0Ynwzddl1Z1Wn e"ydZ1Ynwzddl2Z2Wn e"ydZ2YnwgdZ3Gd d d e4Z5Gd d d e5Z6Gd ddej7Z8ej9dddZ:dddZ;ddZdddZ?ddZ@d ZAdZBdaCdaDdZEdZFdaGd!d"ZHd#d$ZId%d&ZJejKd'rdd(d)ZLd*d+ZMd,d-ZNd.d/ZOn ejPZMejQZNejRZOd0d1ZPd2d3ZQd4d5ZRd6d7ZSd8d9ZTejKd'rddlUZUddlVZUd:d;ZWndZXdd?d@ZYdAdBZZdCdDZ[dEdFZ\dGdHZ]dIZ^dJZ_ej`ejafdKdLZbe^fdMdNZcdOdPZdedZedQZfdRZgehe/dSZiehe1dTZjehe2dUZkejKdVZlejmdVkrdWZnndXZndYoenepZneqZrej9dd[d\Zseed]r4ej9d^d_Zteudfd`daZvdbdcZwdddeZxdfdgZydhdiZzdjdkZ{Gdldmdme|Z}ddndoZ~ej9dpdqZGdrdsdse|Zej)rxddlZejjZnddlZejZGdtdudueZGdvdwdwe|ZGdxdydye|ZeeejdzZeejejdzZeeejdzZej9dd|d}Zej9d~dZddZddZddZddZej9ddZddZdZdZeedrdeZdZedZddZddZdZdZddZddZddZdZdeZdeZdeZejZddZGddde|ZdddZddZGddde|ZddZddZddZdddZddZddZddZddZddZddZddZdddÄZddńZddDŽZddɄZdd˄Zdd̈́ZddτZej9ddфZej9ddӄZddՄZddׄZGddلde&jjƃZGddۄde|Zdadd݄Zdd߄ZdaddZddZejKd'rej9ddZnej9ddZddZdS)zwSupporting definitions for the Python regression tests. Backported for python-future from Python 3.3 test/support.py. )absolute_importdivisionprint_functionunicode_literals)utils)strrangeopenintmaplistNskip) sysconfig)JError TestFailedResourceDenied import_moduleverbose use_resources max_memuserecord_original_stdoutget_original_stdoutunloadunlinkrmtreeforgetis_resource_enabledrequiresrequires_freebsd_versionrequires_linux_versionrequires_mac_verfind_unused_port bind_port IPV6_ENABLED is_jythonTESTFNHOSTSAVEDCWDtemp_cwdfindfilecreate_empty_filesortdictcheck_syntax_erroropen_urlresourcecheck_warnings CleanImportEnvironmentVarGuardTransientResourcecaptured_stdoutcaptured_stdincaptured_stderrtime_outsocket_peer_resetioerror_peer_resetrun_with_locale temp_umasktransient_internet set_memlimit bigmemtestbigaddrspacetestBasicTestRunner run_unittest run_doctestthreading_setupthreading_cleanup reap_children cpython_onlycheck_impl_detail get_attribute swap_item swap_attrrequires_IEEE_754 TestHandlerMatcher can_symlinkskip_unless_symlinkskip_unless_xattrimport_fresh_module requires_zlib PIPE_MAX_SIZEfailfastanticipate_failure run_with_tz requires_gzip requires_bz2 requires_lzmasuppress_crash_popupc@eZdZdZdS)rz*Base class for regression test exceptions.N__name__ __module__ __qualname____doc__r_r_?/usr/lib/python3/dist-packages/future/backports/test/support.pyrbrc@rY)rz Test failed.NrZr_r_r_r`rerarc@rY)rzTest 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. NrZr_r_r_r`rhrarTccsR|r$ttddtdVWddS1swYdSdVdS)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)rbr_r_r`_ignore_deprecated_importsps " rgFc Cs`t|"z t|WWdSty%}ztt|d}~ww1s)wYdS)zImport 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.N)rg importlibr ImportErrorunittestSkipTestr)name deprecatedmsgr_r_r`rs  rcCsZ|tjvr t|tj|=ttjD]}||ks||dr*tj|||<tj|=qdS)zyHelper function to save and remove a module from sys.modules Raise ImportError if the module can't be imported. .N)sysmodules __import__r startswith)rl orig_modulesmodnamer_r_r`_save_and_remove_modules rvcCs<d}z tj|||<Wn tyd}Ynwdtj|<|S)zHelper function to save and block a module in sys.modules Return True if the module was in sys.modules, False otherwise. TFNrprqKeyError)rlrtsavedr_r_r`_save_and_block_modules  rzcCs|rtjSddS)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. cS|SNr_fr_r_r`sz$anticipate_failure..)rjexpectedFailure) conditionr_r_r`rSsrSr_c Cst|qi}g}t||zEz |D]}t||q|D] }t||s(||qt|}Wn ty:d}YnwW|D] \} } | tj | <q@|D]} tj | =qLn|D] \} } | tj | <qX|D]} tj | =qdw|WdS1sxwYdS)aVImport 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. If deprecated is True, any module or package deprecation messages will be suppressed. N) rgrvrzappendrhrriitemsrprq) rlfreshblockedrmrtnames_to_remove fresh_name blocked_name fresh_module orig_namemodulename_to_remover_r_r`rOs8          $rOcCs2zt||}W|Stytd||fw)z?Get an attribute, raising SkipTest if AttributeError is raised.zobject %r has no attribute %r)getattrAttributeErrorrjrk)objrl attributer_r_r`rFs   rFcCs|adSr|)_original_stdout)stdoutr_r_r`rsrcCs tptjSr|)rrprr_r_r_r`r rcCs$ztj|=WdStyYdSwr|rw)rlr_r_r`rs  rwincCs|||r |}n tj|\}}|pd}d}|dkr9t|}|r&|s,dS||vs,dSt||d9}|dkstjd|tdddS)NrogMbP?g?z)tests may fail, delete still pending for  stacklevel) ospathsplitlistdirtimesleeprcwarnRuntimeWarning)funcpathnamewaitalldirnamerltimeoutLr_r_r`_waitfor s$    rcCttj|dSr|)rrr)filenamer_r_r`_unlink+rcCrr|rrrmdir)rr_r_r`_rmdir.rrcs*fddt|ddttj|dS)NcsRt|D]!}tj||}tj|r!t|ddt|qt|qdS)NTr)rrrjoinisdirrrr)rrlfullname _rmtree_innerr_r`r2s   z_rmtree.._rmtree_innerTrr)rr_rr`_rmtree1s rc CsJzt|WdSty$}z|jtjtjfvrWYd}~dSd}~wwr|)rOSErrorerrnoENOENTENOTDIR)rerrorr_r_r`rAsrc CDzt|WdSty!}z|jtjkrWYd}~dSd}~wwr|)rrrr)rrr_r_r`rIs rc Crr|)rrrr)rrr_r_r`rQs rcCs@t|}tjtj|}tj||d}t|||S)alMove a PEP 3147 pyc/pyo file to its legacy pyc/pyo location. The choice of .pyc or .pyo extension is done based on the __debug__ flag value. :param source: The file system path to the source file. The source file does not need to exist, however the PEP 3147 pyc file must exist. :return: The file system path to the legacy pyc file. Tc)impcache_from_sourcerrrabspathrrename)sourcepyc_fileup_one legacy_pycr_r_r`make_legacy_pycXs  rcCsft|tjD])}tj||d}t|dt|dttj|ddttj|ddqdS)z'Forget' a module was ever imported. This removes the module from sys.modules and deletes any PEP 3147 or legacy .pyc and .pyo files. z.pyroT)debug_overrideFN)rrprrrrrr)rurrr_r_r`rhs   rc Csd}d}Gdddtj}tjj}|}|st|}tj}|||t |t |t |}|s;tt |j |@S)Nrc@s.eZdZdejjfdejjfdejjfgZdS)z*_is_gui_available..USEROBJECTFLAGSfInherit fReserveddwFlagsN)r[r\r]ctypeswintypesBOOLDWORD_fields_r_r_r_r`USEROBJECTFLAGSs    r) r Structurewindlluser32GetProcessWindowStationWinErrorrrGetUserObjectInformationWbyrefsizeofboolr) UOI_FLAGS WSF_VISIBLErdllhuofneededresr_r_r`_is_gui_available}s$ rcCdS)NTr_r_r_r_r`rcCstduo|tvS)zPTest whether a resource is enabled. Known resources are set by regrtest.py.N)rresourcer_r_r`rsrcCsV|dkr ts tdtdjddkrdSt|s)|dur%d|}t|dS)zRaise ResourceDenied if the specified resource is not available. If the caller's module is __main__ then automatically return True. The possibility of False being returned occurs when regrtest.py is executing. guizCannot use the 'gui' resourcerr[__main__Nz"Use of the %r resource not enabled) rrjrkrp _getframe f_globalsgetrr)rrnr_r_r`rs rcfdd}|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$tfdd}|_|S)Ncstkr.decorator..wrapper functoolswrapsrrrrrrr` decoratorsz)_requires_unix_version..decoratorr_)rrrr_rr`_requires_unix_versionsrcG 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. FreeBSDrrr_r_r`r rcGr)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. Linuxrrr_r_r`rrrcfdd}|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"tfdd}|_|S)Ncsztjdkr6td}z ttt|d}Wn ty Ynw|kr6dtt }t d||f|i|S)Ndarwinrroz&Mac OS X %s or higher required, not %s) rprmac_verrr r rrrrrjrkr)rrr_r`rs   z4requires_mac_ver..decorator..wrapperrrrrr`rsz#requires_mac_ver..decoratorr_)rrr_rr`r s r z 127.0.0.1::1cCs"t||}t|}|~|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 socket.error 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. )socketr"close)familysocktypetempsockportr_r_r`r!s 8r!cCs|jtjkrM|jtjkrMttdr|tjtjdkrt dttdr?z|tjtj dkr3t dWn tj y>YnwttdrM| tjtj d||df|d}|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_INETtype SOCK_STREAMhasattr getsockopt SOL_SOCKETrrrr setsockoptrbind getsockname)sockhostrr_r_r`r"9s"    r"c Cstjr>d}z0zttjtj}|dWW|r|dSdStjtjfy,YnwW|r4|dS|r=|wwdS)z+Check whether IPv6 is enabled on this host.N)r rTF)rhas_ipv6AF_INET6rrrrgaierror)r r_r_r`_is_ipv6_enabled^s(   r%i@iz requires zlibz requires bz2z requires lzmajavaz$testz@testz {0}_{1}_tmptempcwdc cst}d}|dur-|}z t|d}Wnty,|s tjd|tddYnwzt|WntyJ|s>tjd|tddYnwztVWt||r_t|dSdSt||rmt|ww)a Context manager that temporarily changes the CWD. An existing path may be provided as *path*, in which case this function makes no changes to the file system. Otherwise, the new CWD is created in the current directory and it's named *name*. If *quiet* is False (default) and it's not possible to create or change the CWD, an error is raised. If it's True, only a warning is raised and the original CWD is used. FNTz*tests may fail, unable to create temp CWD rz,tests may fail, unable to change the CWD to ) rgetcwdmkdirrrcrrchdirr)rlquietr saved_dir is_temporaryr_r_r`r(sD             r(umaskc cs0t|}z dVWt|dSt|w)z8Context manager that temporarily sets the process umask.N)rr/)r/oldmaskr_r_r`r9<s  r9cCsntj|r|S|durtj||}tj}tj|g|}|D]}tj||}tj|r4|Sq!|S)zTry to find a file on sys.path and the working 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).N)rrisabsrrprexists)fileheresubdirrdnfnr_r_r`r)Fs r)cCs(t|tjtjBtjB}t|dS)z>Create an empty file. If the file already exists, truncate it.N)rr O_WRONLYO_CREATO_TRUNCr)rfdr_r_r`r*Usr*cCs,t|}dd|D}d|}d|S)z%Like repr(dict), but in sorted order.cSsg|]}d|qS)z%r: %rr_).0pairr_r_r` ]szsortdict..z, z{%s})sortedrr)dictr reprpairs withcommasr_r_r`r+Zs  r+cCs8ttd}z |W|ttS|ttw)z` Create an invalid file descriptor by opening and closing a file and return its fd. wb)r r%filenorrr3r_r_r` make_bad_fdas   rFcCs|tt|dddS)Nz exec) assertRaises SyntaxErrorcompile)testcase statementr_r_r`r,ms r,c s0ddlm}m}dd||ddd}tjtj t d|}fdd }tj |rB||}|dur>|St |t d td |td |j|d d}z,t|d} |} | rn| | |} | scWdn1sxwYW|n|w||}|dur|Std|)Nr)requestparsecheckr/datacsDt|gRi}dur|S|r|d|S|dSNr)r seekr)r7r~rrOrr_r`check_valid_file{s  z*open_urlresource..check_valid_fileurlfetchz fetching %s ...rE)rrCzinvalid resource %r)future.backports.urllibrMrNpopurlparserrrrr__file__r2rrprintrurlopenr readwriterr) urlrrurllib_request urllib_parserr7rVr~outsr_rUr`r-qs8     r-c@s4eZdZdZddZddZeddZdd Zd S) WarningsRecorderzyConvenience wrapper for the warnings list returned on entry to the warnings.catch_warnings() context manager. cCs||_d|_dSrS _warnings_last)self warnings_listr_r_r`__init__ zWarningsRecorder.__init__cCs@t|j|jkrt|jd|S|tjjvrdStd||f)NrQz%r has no attribute %r)lenrhrirrcWarningMessage_WARNING_DETAILSr)rjattrr_r_r` __getattr__s  zWarningsRecorder.__getattr__cCs|j|jdSr|rgrjr_r_r`rcszWarningsRecorder.warningscCst|j|_dSr|)rnrhrirsr_r_r`resetrzWarningsRecorder.resetN) r[r\r]r^rlrrpropertyrcrtr_r_r_r`rfs  rfc cs4td}|jd}|r#tjr|n tt|D]}| qt j dd}tj d dt|VWdn1sAwYt|}g}|D]5\}} d} |ddD]}|j} t|t| tjrvt| j| rvd} ||qZ| s|s||| jfqN|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. r__warningregistry__T)recordrcalwaysNFzunhandled warning %srz)filter (%r, %s) did not catch any warning)rprrrrPY3clearrrnrZrcrdrq simplefilterrfr messagerematchrI issubclass __class__removerr[AssertionError) filtersr,frameregistryiwreraisemissingrncatseenwarningr_r_r`_filterwarningssD        rcOs.|d}|sdtff}|durd}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) r,NT)rWarningr)rkwargsr,r_r_r`r.s   r.c@(eZdZdZddZddZddZdS) r/a,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 cGsJtj|_|D]}|tjvr"tj|}|j|krtj|j=tj|=qdSr|)rprqcopyoriginal_modulesr[)rj module_names module_namerr_r_r`rls     zCleanImport.__init__cCr{r|r_rsr_r_r` __enter__rzCleanImport.__enter__cGstj|jdSr|)rprqupdaterrj ignore_excr_r_r`__exit__zCleanImport.__exit__Nr[r\r]r^rlrrr_r_r_r`r/s   r/c@sheZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZdS)r0z_Class to help protect the environment variable properly. Can be used as a context manager.cCstj|_i|_dSr|)renviron_environ_changedrsr_r_r`rl&s zEnvironmentVarGuard.__init__cCs |j|Sr|)rrjenvvarr_r_r` __getitem__*rzEnvironmentVarGuard.__getitem__cCs*||jvr|j||j|<||j|<dSr|rrrrjrvaluer_r_r` __setitem__-s zEnvironmentVarGuard.__setitem__cCs6||jvr|j||j|<||jvr|j|=dSdSr|rrr_r_r` __delitem__3s   zEnvironmentVarGuard.__delitem__cCs |jSr|)rkeysrsr_r_r`r:rzEnvironmentVarGuard.keyscC t|jSr|)iterrrsr_r_r`__iter__=rzEnvironmentVarGuard.__iter__cCrr|)rnrrsr_r_r`__len__@rzEnvironmentVarGuard.__len__cCs |||<dSr|r_rr_r_r`setC zEnvironmentVarGuard.setcCs ||=dSr|r_rr_r_r`unsetFrzEnvironmentVarGuard.unsetcCr{r|r_rsr_r_r`rIrzEnvironmentVarGuard.__enter__cGsF|jD]\}}|dur||jvr|j|=q||j|<q|jt_dSr|)rrrrr)rjrkvr_r_r`rLs   zEnvironmentVarGuard.__exit__N)r[r\r]r^rlrrrrrrrrrrr_r_r_r`r0!s r0c@r) 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|_tj|dSr|)rproriginal_valueoriginal_objectextend)rjpathsr_r_r`rlbszDirsOnSysPath.__init__cCr{r|r_rsr_r_r`rgrzDirsOnSysPath.__enter__cGs|jt_|jtjdd<dSr|)rrprrrr_r_r`rjszDirsOnSysPath.__exit__Nrr_r_r_r`rVs   rc@s*eZdZdZddZddZd ddZdS) r1zRaise ResourceDenied if an exception is raised while the context manager is in effect that matches the specified exception and attributes.cKs||_||_dSr|)excattrs)rjrrr_r_r`rltrmzTransientResource.__init__cCr{r|r_rsr_r_r`rxrzTransientResource.__enter__NcCs\|dur*t|j|r,|jD]\}}t||sdSt|||kr%dSqtddSdS)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)rrrrrrr)rjtype_r tracebackrq attr_valuer_r_r`r{s zTransientResource.__exit__)NNNrr_r_r_r`r1os r1)r>@c #sgd}gd}td||gs#dd|Ddd|Dfdd}t}zRz|d ur:t|d VWn;tyy}z/ |j}t|d kr\t|d tr\|d }nt|d krnt|d trn|d }nnqG||d }~wwWt|d St|w)zReturn a context manager that raises ResourceDenied when various issues with the Internet connection manifest themselves as exceptions.)) ECONNREFUSEDo) ECONNRESETh) EHOSTUNREACHq) ENETUNREACHe) ETIMEDOUTn)) EAI_AGAIN)EAI_FAIL) EAI_NONAME) EAI_NODATA) WSANO_DATAi*zResource %r is not availablecSg|] \}}tt||qSr_)rrr<rlnumr_r_r`r>z&transient_internet..cSrr_)rrrr_r_r`r>rcs`t|dd}t|tjst|tjr|vs|vr.ts'tjj dd}||_ |dS)Nrr ) r isinstancerrr$rrpstderrr`r __cause__)errnrcaptured_errnosdenied gai_errnosr_r` filter_errors   z(transient_internet..filter_errorNTrrr)rrgetdefaulttimeoutsetdefaulttimeoutIOErrorrrnr) resource_namererrnosdefault_errnosdefault_gai_errnosr old_timeoutrar_rr`r:sD       r:c csRddl}tt|}tt||ztt|VWtt||dStt||w)zReturn a context manager used by captured_stdout/stdin/stderr that temporarily replaces the sys stream *stream_name* with a StringIO.rN)iorrpsetattrStringIO) stream_namer orig_stdoutr_r_r`captured_outputs rcCtdS)zCapture the output of sys.stdout: with captured_stdout() as s: print("hello") self.assertEqual(s.getvalue(), "hello") rrr_r_r_r`r2sr2cCr)Nrrr_r_r_r`r4r4cCr)Nstdinrr_r_r_r`r3rr3cCs*ttr tdttdS)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. 皙?N)gccollectr$rrr_r_r_r` gc_collects    rccs@t}tzdVW|rtdSdS|rtwwr|)r isenableddisableenable)have_gcr_r_r` disable_gcs  rcCsFddl}|dp d}d}|D] }|dr|}q|dko"|dkS)z,Find if Python was built with optimizations.rN PY_CFLAGSrz-Oz-O0)rget_config_varrrs)rcflags final_optoptr_r_r`python_is_optimized s  rnP0ngettotalrefcount2P0PrcCtt|tSr|)structcalcsize_header_alignfmtr_r_r` calcobjsizerrcCrr|)rr _vheaderr r r_r_r` calcvobjsize"rri@icCsht|}t|tkr|jt@st|tkr"t|jt@r"|tj7}dt|||f}||||dS)Nz&wrong size for %s: got %d, expected %d) rp getsizeofr __flags___TPFLAGS_HEAPTYPE_TPFLAGS_HAVE_GC _testcapiSIZEOF_PYGC_HEAD assertEqual)testrsizeresultrnr_r_r` check_sizeof)s   rcr)Ncs$fdd}j|_j|_|S)Nc szddl}t|}||}Wntyd}}YnD]}z |||WnYq#z|i|W|rG|rH|||SSS|rT|rU|||wwwrS)localer setlocaler)rkwdsrcategory orig_localeloc)catstrrlocalesr_r`inner9s*     z1run_with_locale..decorator..innerr[r^rr$r"r#rr`r8sz"run_with_locale..decoratorr_)r"r#rr_r'r`r87sr8cr )Ncs"fdd}j|_j|_|S)Nc sztj}Wn tytdwdtjvrtjd}nd}tjd<|z|i|W|dur8tjd=n|tjd<tS|durKtjd=n|tjd<tw)Nztzset requiredTZ)rtzsetrrjrkrr)rrr)orig_tz)rtzr_r`r$]s(            z-run_with_tz..decorator..innerr%r&r+rr`r\szrun_with_tz..decoratorr_)r+rr_r,r`rT[s rTirrcCsdttdtd}td|tjtjB}|durtd|ftt| d|| d }|a |t kr9t }|t dkrFtd|f|adS)Nr-)rmgtz(\d+(\.\d+)?) (K|M|G|T)b?$zInvalid memory limit %rrr(z$Memory limit %r too low to be useful)_1M_1Gr}r~ IGNORECASEVERBOSErr floatgrouplowerreal_max_memuseMAX_Py_ssize_t_2Gr)limitsizesr.memlimitr_r_r`r;s" $ r;c@r) _MemoryWatchdogz`An object which periodically watches the process' memory consumption and prints it out. cCsdjtd|_d|_dS)Nz/proc/{pid}/statm)pidF)formatrgetpidprocfilestartedrsr_r_r`rls z_MemoryWatchdog.__init__c Cszt|jd}Wn!ty)}ztd|ttj WYd}~dSd}~wwt d}t j tj |g|t jd|_|d|_dS)Nrz"/proc not available for stats: {0}zmemory_watchdog.py)rrT)r rBrrcrr@rrprflushr) subprocessPopen executableDEVNULL mem_watchdogrrC)rjr~ewatchdog_scriptr_r_r`starts     z_MemoryWatchdog.startcCs"|jr|j|jdSdSr|)rCrJ terminatewaitrsr_r_r`stops z_MemoryWatchdog.stopN)r[r\r]r^rlrMrPr_r_r_r`r>s  r>csfdd}|S)aADecorator for bigmem tests. 'minsize' is the minimum useful size for the test (in arbitrary, test-interpreted units.) 'memuse' is the number of 'bytes per size' for the test, or a good estimate of it. if 'dry_run' is False, it means the test doesn't support dummy runs when -M is not specified. cs fdd__S)Ncsj}j}ts d}n|}tss"t||kr"td||dtr=tr=ttdj||ddt}| nd}z ||W|rL| SS|rT| ww)Ni'not enough memory: %.1fG minimum needed@z* ... expected peak memory use: {peak:.1f}G)peak) rmemuser8rjrkrr]r@r>rMrP)rjrrTmaxsizewatchdog)dry_runr~rr_r`rs6       z.bigmemtest..decorator..wrapper)rrTr}rWrTrr~rr`rszbigmemtest..decoratorr_)rrTrWrr_rXr`r<s !r<cr )z0Decorator for tests that fill the address space.cs<ttkrtdkrtdkrtdtdtd|S)Nllz-not enough memory: try a 32-bit build insteadrQrR)rr9rjrkrsr}r_r`rsz!bigaddrspacetest..wrapperr_rYr_r}r`r=s  r=c@seZdZddZdS)r>cCst}|||Sr|)rj TestResult)rjrrr_r_r`runszBasicTestRunner.runN)r[r\r]r[r_r_r_r`r>s r>cCr{r|r_)rr_r_r`_idrr\cCs4|dkr ts tdSt|rtStd|S)Nrzresource 'gui' is not availablezresource {0!r} is not enabled)rrjr rr\r@rr_r_r`requires_resources  r]cCstdd|S)z9 Decorator for tests only applicable on CPython. T)cpython) impl_detailrr_r_r`rDsrDcKsZtdi|r tS|dur(t|\}}|rd}nd}t|}|d|}t|S)Nz*implementation detail not available on {0}z%implementation detail specific to {0}z or r_) rEr\ _parse_guardsr?rr@rrjr )rnguards guardnamesdefaultr_r_r`r_s   r_cCsH|sddidfSt|d}t||gt|ksJ|| fS)Nr^TFr)r valuesrn)rbis_truer_r_r`ras   racKs t|\}}|t|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 )rarrpython_implementationr7)rbrdr_r_r`rE%s rEcs(ttdsStfdd}|S)zEDecorator to temporarily turn off tracing for the duration of a test.gettracec s:t}ztd|i|Wt|St|wr|)rprhsettrace)rroriginal_tracerr_r`r5s  zno_tracing..wrapper)rrprrrr_rr` no_tracing0s rkcCs 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. )rkrDr`r_r_r` refcount_test@s rlcCsNg}|jD]}t|tjrt||||q||r!||q||_dS)z>Recursively filter test cases in a suite based on a predicate.N)_testsrrj TestSuite _filter_suiter)suiteprednewtestsrr_r_r`roKs      rocCstr tjtjdtd}nt}||}|sNt |j dkr-|j s-|j dd}t |t |j dkrB|j sB|j dd}t |d}tsJ|d7}t |dS)z2Run tests from a unittest.TestSuite-derived class.r) verbosityrRrrzmultiple errors occurredz!; run in verbose mode for detailsN) rrjTextTestRunnerrprrRr>r[ wasSuccessfulrnerrorsfailuresr)rprunnerrrr_r_r` _run_suiteWs"   rycGstjtjf}t}|D]/}t|tr(|tjvr$|ttj|q t dt||r3||q |t |q dd}t ||t |dS)z1Run tests from unittest.TestCase-derived classes.z)str arguments must be keys in sys.modulescSs6tdurdS|dD] }t|trdSq dS)NTroF) match_testsidrfnmatch fnmatchcase)rrlr_r_r` case_predys zrun_unittest..case_predN) rjrnTestCaserrrprqaddTest findTestCasesr makeSuiterory)classes valid_typesrpclsr~r_r_r`r?ks       r?cCs`ddl}|dur t}nd}|j|||d\}}|r!td||ftr,td|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)doctestrtestmodrr]r[)rrsrrr~r0r_r_r`r@sr@cCs tjfSr|)rprqrr_r_r_r` modules_setuprrcCsNddtjD}tttjD]}tjqtj|tj|dS)NcSs"g|] \}}|dr||fqS)z encodings.)rs)r<rrr_r_r`r>s z#modules_cleanup..)rprqrrrnrZr) oldmodules encodingsrr_r_r`modules_cleanups   rcCstrtfSdS)N)r)_thread_countr_r_r_r`rAs rAcCs>tsdSd}t|D]}t}||krdStdq dS)N r)rrrrr) nb_threads _MAX_COUNTcountrr_r_r`rBs  rBcs"tsStfdd}|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. cs$t}z |Wt|St|wr|)rArB)rkeyrr_r`rszreap_threads..decorator)rrr)rrr_rr` reap_threadss rcCsHttdr"d} zt|tj\}}|dkrWdSWnYdSqdS)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. waitpidrQTrN)rrrWNOHANG) any_processr?statusr_r_r`rCs rCc cszt||r%t||}t|||z dVWt|||dSt|||wt|||z dVWt||dSt||w)amTemporary 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. N)rrrdelattr)rrqnew_valreal_valr_r_r`rH s   rHc cs^||vr||}|||<z dVW|||<dS|||<w|||<z dVW||=dS||=w)akTemporary 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. Nr_)ritemrrr_r_r`rG%s rGcCstdd|}|S)zStrip 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. s\[\d+ refs\]\r?\n?)r}substrip)rr_r_r`strip_python_stderr@srcCstS)znReturn a list of command-line arguments reproducing the current settings in sys.flags and sys.warnoptions.)rF_args_from_interpreter_flagsr_r_r_r`args_from_interpreter_flagsJsrc@s,eZdZddZddZddZddZd S) rJcCstjj|d||_dSrS)logginghandlersBufferingHandlerrlmatcher)rjrr_r_r`rlTs zTestHandler.__init__cCr)NFr_rsr_r_r` shouldFlush]rzTestHandler.shouldFlushcCs|||j|jdSr|)r@bufferr__dict__)rjrwr_r_r`emit`s zTestHandler.emitcKs2d}|jD]}|jj|fi|rd}|Sq|S)zW Look for a saved dict whose keys/values match the supplied arguments. FT)rrmatches)rjrrdr_r_r`rds zTestHandler.matchesN)r[r\r]rlrrrr_r_r_r`rJSs   rJc@s eZdZdZddZddZdS)rK)rnr|cKs<d}|D]}||}||}||||sd}|Sq|S)a. 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. TF)r match_value)rjrrrrrdvr_r_r`rss zMatcher.matchescCsLt|t|kr d}|St|tus||jvr||k}|S||dk}|S)zT Try to match a single stored value (dv) with a supplied value (v). Fr)rr_partial_matchesfind)rjrrrrr_r_r`rszMatcher.match_valueN)r[r\r]rrrr_r_r_r`rKos rKc Cs^tdurtStd}z tt|d}Wntttfy%d}Y|a|Swt||a|S)NrLTF) _can_symlinkr%rsymlinkrNotImplementedErrorrr) symlink_pathcanr_r_r`rLs  rLcC t}d}|r |St||S)z8Skip decorator for tests that require functional symlinkz*Requires functional symlink implementation)rLrjr rokrnr_r_r`rMrMc Cs tdurtSttdsd}|a|St\}}zdttd=}z)t|ddt|ddt }t d|}|dupEt |ddk}Wn tyRd}YnwWdn1s]wYWttt||a|SWttt||a|Sttt|w) NsetxattrFrCs user.testrz 2.6.(\d{1,2})r') _can_xattrrrtempfilemkstempr r%rrDrrr}r~r r6rr)rtmp_fptmp_namefpkernel_versionr.r_r_r` can_xattrs>        rcCr)zDSkip decorator for tests that require functional extended attributesz(no non-broken extended attribute support)rrjr rr_r_r`rNrrNc csRddl}|jj}d}||}|||Bz dVW||dS||w)z;Disable Windows Error Reporting dialogs using SetErrorMode.rNr)rrkernel32 SetErrorMode)rk32SEM_NOGPFAULTERRORBOXold_error_moder_r_r`rXs rXccs dVdSr|r_r_r_r_r`rXs c sptdzjWnttfytdYnwdfdd}||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"r tdStdSr|)rrr_ attr_is_local attr_nameobject_to_patch old_valuer_r`cleanupszpatch..cleanup)rrrrx addCleanupr) test_instancerr new_valuerr_rr`patchs  r)T)F)r_r_Fr|)r'FN)rr_rS)r^ __future__rrrrfuturerfuture.builtinsrrr r r r contextlibrrrrrprrshutilrcrjr unittest2rhr}rFrrrri distutilsr|logging.handlersrrrryr threadingthreadmultiprocessing.processmultiprocessingzlibgzipbz2lzma__all__ ExceptionrrrkrcontextmanagerrgrrvrzrSrOrFrrrr8rRrzrrrrrsrrrrrrrrrrctypes.wintypesrrrrrrr r&HOSTv6rrr!r"r%r#rQ SOCK_MAX_SIZE skipUnlessrPrVrWr$rlr%r@rAr)r'r(r9r\r)r*r+rFr,r-objectrfrr.r/collections.abc collectionsabcMutableMappingmybaseUserDict DictMixinr0rr1rrr5rrr6r7r:rr2r4r3rrrr r rrrrrrr8rTr1r2r:_4GrUr9r;r>r<r=r>r\r]rDr_rarErkrlroryr?r@rrrArBrrCrHrGrrrrrJrKrrLrMrrrNrXrr_r_r_r`s                   4  !       >%     }  '   - ,   5 D       $# -       '#     "