Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/trial/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/trial/util.pyc


[XMc@sdZddlZddlZddlmZddlmZmZmZddl	m
Z
ddlmZm
Z
ddlmZddlmZeZd	Zd
efdYZdefd
YZdZddZdZdZeZedZeje
jddddde ddZ!e"dZ#ej$e#Z#defdYZ%dZ&defd YZ'd!Z(d"d#gZ)dS($s
A collection of utility functions and classes, used internally by Trial.

This code is for Trial's internal use.  Do NOT use this code if you are writing
tests.  It is subject to change at the Trial maintainer's whim.  There is
nothing here in this module for you to use unless you are maintaining Trial.

Any non-Trial Twisted code that uses this module will be shot.

Maintainer: Jonathan Lange
iN(t	randrange(tdefertutilst
interfaces(tFailure(t	deprecatetversions(tFilesystemLock(tFilePathg^@tDirtyReactorAggregateErrorcBs#eZdZddZdZRS(s
    Passed to L{twisted.trial.itrial.IReporter.addError} when the reactor is
    left in an unclean state after a test.

    @ivar delayedCalls: The L{DelayedCall} objects which weren't cleaned up.
    @ivar selectables: The selectables which weren't cleaned up.
    cCs||_||_dS(N(tdelayedCallstselectables(tselfR
R((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyt__init__)s	cCstd}|jr;|d7}|djtt|j7}n|jrp|d7}|djtt|j7}n|S(sR
        Return a multi-line message describing all of the unclean state.
        sReactor was unclean.sM
DelayedCalls: (set twisted.internet.base.DelayedCall.debug = True to debug)
s
s
Selectables:
(R
tjointmaptstrR(Rtmsg((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyt__str__-s	
"	
"N(t__name__t
__module__t__doc__tNoneR
R(((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyR	 st_JanitorcBsveZdZddZdZdZdZdZe	j
ed
ied6dd	6fZd
ZdZ
RS(s
    The guy that cleans up after you.

    @ivar test: The L{TestCase} to report errors about.
    @ivar result: The L{IReporter} to report errors to.
    @ivar reactor: The reactor to use. If None, the global reactor
        will be used.
    cCs||_||_||_dS(s
        @param test: See L{_Janitor.test}.
        @param result: See L{_Janitor.result}.
        @param reactor: See L{_Janitor.reactor}.
        N(ttesttresulttreactor(RRRR((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyR
Gs		cCsB|j}|r>t|}|jj|jt|tStS(s|
        Called by L{unittest.TestCase} after a test to catch any logged errors
        or pending L{DelayedCall}s.
        (t
_cleanPendingR	RtaddErrorRRtFalsetTrue(Rtcallst	aggregate((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pytpostCaseCleanupRscCs`|j}|j}|s$|rRt||}|jj|jt|n|jdS(s
        Called by L{unittest.TestCase} after the last test in a C{TestCase}
        subclass. Ensures the reactor is clean by murdering the threadpool,
        catching any pending L{DelayedCall}s, open sockets etc.
        N(t
_cleanReactorRR	RRRRt
_cleanThreads(RRRR ((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pytpostClassCleanup_scCs/|jdk	r|j}nddlm}|S(sI
        Get either the passed-in reactor or the global reactor.
        i(RN(RRttwisted.internet(RR((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyt_getReactormscCs~|j}|jd|jdg}xK|jD]=}|jrdt|}|jndGH|j|q9W|S(sS
        Cancel all pending calls and return their string representations.
        is)WEIRDNESS! pending timed call not active!(R&titeratetgetDelayedCallstactiveRtcanceltappend(RRtdelayedCallStringstpt
delayedString((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyRxs


tignoretcategorys!reactor\.iterate cannot be used.*tmessagecCsA|j}tjj|r=|jdk	r=|jq=ndS(N(R&RtIReactorThreadst
providedByt
threadpoolRt_stopThreadPool(RR((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyR#scCsb|j}g}xI|jD];}tjj|rG|jdn|jt|qW|S(s
        Remove all selectables from the reactor, kill any of them that were
        processes, and return their string representation.
        tKILL(R&t	removeAllRtIProcessTransportR3t
signalProcessR+trepr(RRtselectableStringstsel((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyR"sN(signore(RRRRR
R!R$R&RRtsuppressWarningstDeprecationWarningR#R"(((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyR>s	
				
cCs1t|tr-|j|j|jf}n|S(s'
    Coerce a Failure to an _exc_info, if err is a Failure.

    @param err: Either a tuple such as returned by L{sys.exc_info} or a
        L{Failure} object.
    @return: A tuple like the one returned by L{sys.exc_info}. e.g.
        C{exception_type, exception_object, traceback_object}.
    (t
isinstanceRttypetvaluetgetTracebackObject(terr((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pytexcInfoOrFailureToExcInfos	R/cKs
|f|fS(s:
    Sets up the .suppress tuple properly, pass options to this method as you
    would the stdlib warnings.filterwarnings()

    So, to use this with a .suppress magic attribute you would do the
    following:

      >>> from twisted.trial import unittest, util
      >>> import warnings
      >>>
      >>> class TestFoo(unittest.TestCase):
      ...     def testFooBar(self):
      ...         warnings.warn("i am deprecated", DeprecationWarning)
      ...     testFooBar.suppress = [util.suppress(message='i am deprecated')]
      ...
      >>>

    Note that as with the todo and timeout attributes: the module level
    attribute acts as a default for the class attribute which acts as a default
    for the method attribute. The suppress attribute can be overridden at any
    level by specifying C{.suppress = []}
    ((tactiontkwarg((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pytsuppressscsfd}|S(Ncstjdd!dkrvddl}|j}y&|j||}|jWntk
rgnX|j|Sddl}|j}z|j||SWd|j	j
}|j|jd|jdXdS(Niiiitcumid(ii(
tsystversion_infotprofiletProfiletruncallt
dump_statst
SystemExittprint_statst
hotshot.statststatstloadt
strip_dirst
sort_stats(targstkwargsRKtprofRthotshotRR(t
outputFiletf(s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyt_s$



((R[RZR\((RZR[s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pytprofiledscCsg}|j|j|jj}x_|dk	rtjj|d}|dkrat|}n|j|t|dd}q%W|S(sjWalk up the Python tree from method 'meth', finding its class, its module
    and all containing packages.RN(	R+tim_classRRRItmodulestgett
__import__tgetattr(tmetht
containerst
moduleNametmodule((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pytgetPythonContainerss
cCsWx*|D]"}t||rt||SqW|tk	r=|Std||fdS(sGo through the list 'objects' sequentially until we find one which has
    attribute 'attr', then return the value of that attribute.  If not found,
    return 'default' if set, otherwise, raise AttributeError. sattribute %r not found in %rN(thasattrRbt_DEFAULTtAttributeError(tobjectstattrtdefaulttobj((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pytacquireAttributes
tTwistedi
iis3Please use twisted.python.reflect.namedAny instead.t
findObjectcCs=|jd}d}|}x|sdj|}t|dkrPtdfSyt|}Wqtk
rtj}tt	j
|ddkrytj|=Wntk
rnX|d|d|dn|j
qXqW|}x@|dD]4}yt||}Wqtk
r.t|fSXqWt|fS(sBGet a fully-named package, module, module-global object or attribute.
    Forked from twisted.python.reflect.namedAny.

    Returns a tuple of (bool, obj).  If bool is True, the named object exists
    and is returned as obj.  If bool is False, the named object does not exist
    and the value of obj is unspecified.
    t.iiiN(tsplitRRtlenRRatImportErrorRItexc_infot	tracebackt
extract_tbR_tKeyErrortpopRbRjR(tnametnamesttopLevelPackagetmoduleNamest	trialnameRvRntn((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyRqs2	



ccsg}x~|D]v}tj|}tj|}|Vy |jtj|jfWq
|jtjtf|rPqq
Xq
W|VdS(s$
    Run the given callables one after the other. If a callable returns a
    Deferred, wait until it has finished before running the next callable.

    @param callables: An iterable of callables that take no parameters.

    @param stopOnFirstError: If True, then stop running callables as soon as
        one raises an exception or fires an errback. False by default.

    @return: A L{Deferred} that fires a list of C{(flag, value)} tuples. Each
        tuple will be either C{(SUCCESS, <return value>)} or C{(FAILURE,
        <Failure>)}.
    N(Rt
maybeDeferredtwaitForDeferredR+tSUCCESSt	getResulttFAILURER(t	callableststopOnFirstErrortresultsR[tdtthing((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyt_runSequentially7s
 t_NoTrialMarkercBseZdZRS(s
    No trial marker file could be found.

    Raised when trial attempts to remove a trial temporary working directory
    that does not contain a marker file.
    (RRR(((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyRUscCs|jdjs+td|fny|jWntk
r}d||j|jfGHy*tdtdf}|j	|Wqtk
r}d|j|jfGHqXnXdS(s
    Safely remove a path, recursively.

    If C{path} does not contain a node named C{_trial_marker}, a
    L{_NoTrialmarker} exception is raised and the path is not removed.
    t
_trial_markers7%r is not a trial temporary path, refusing to remove its2could not remove %r, caught OSError [Errno %s]: %ss_trial_temp_old%si@Bs4could not rename path, caught OSError [Errno %s]: %sN(
tchildtexistsRtremovetOSErrorterrnotstrerrorRRtmoveTo(tpathtetnewPath((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyt
_removeSafely_s t_WorkingDirectoryBusycBseZdZRS(sv
    A working directory was specified to the runner, but another test run is
    currently using that directory.
    (RRR(((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyRyscCsd}xtr|r7|jd|j|f}n|}t|jd}|jr|jrut|n|j|j	dj
d||fS|jdkr|d7}q	tq	WdS(	s
    Find an unused directory named similarly to C{base}.

    Once a directory is found, it will be locked and a marker dropped into it to
    identify it as a trial temporary directory.

    @param base: A template path for the discovery process.  If this path
        exactly cannot be used, a path which varies only in a suffix of the
        basename will be used instead.
    @type base: L{FilePath}

    @return: A two-tuple.  The first element is a L{FilePath} representing the
        directory which was found and created.  The second element is a locked
        L{FilesystemLock}.  Another call to C{_unusedTestDirectory} will not be
        able to reused the the same name until the lock is released, either
        explicitly or by this process exiting.
    is%s-%ds.lockRtt_trial_tempiN(RtsiblingtbasenameRRtlockRRtmakedirsRt
setContentR(tbasetcounterttestdirttestDirLock((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyt_unusedTestDirectorys	"



RDRG(*RRwRItrandomRR%RRRttwisted.python.failureRttwisted.pythonRRttwisted.python.lockfileRttwisted.python.filepathRtobjecttDEFAULT_TIMEOUTtDEFAULT_TIMEOUT_DURATIONt	ExceptionR	RRDRGR]RgRiRotdeprecatedModuleAttributetVersionRRqRRtdeferredGeneratorRRRRt__all__(((s6/usr/lib/python2.7/dist-packages/twisted/trial/util.pyt<module>s:	j				

	)
		-