Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/python/test/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyc


=Nc@sdZddlZddlZddlZddlmZddlmZddlm	Z	ddl
mZddl
mZddl
m
Z
mZdd	l
mZdd
l
mZddlmZddlmZdd
lmZddlmZddlmZdZdZdefdYZdefdYZdefdYZ defdYZ!defdYZ"defdYZ#defdYZ$dS( sI
Tests for Twisted's deprecation framework, L{twisted.python.deprecate}.
iN(tnormcase(tTestCase(t	deprecate(t_appendToDocstring(t_getDeprecationDocstring(t
deprecatedtgetDeprecationWarningString(t_getDeprecationWarningString(tDEPRECATION_WARNING_FORMAT(tfullyQualifiedName(tVersion(tFilePath(tdeprecatedattributes(tTwistedModulesTestCasecCsdS(sK
    Do nothing.

    This is used to test the deprecation decorators.
    N((((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt
dummyCallablescCsdS(s[
    Do nothing.

    This is used to test the replacement parameter to L{deprecated}.
    N((((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pytdummyReplacementMethod%stTestDeprecationWarningscBskeZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZRS(cCs5tdddd}|jt|j|ddS(s
        L{getDeprecationWarningString} returns a string that tells us that a
        callable was deprecated at a certain released version of Twisted.
        tTwistediis{twisted.python.test.test_deprecate.TestDeprecationWarnings.test_getDeprecationWarningString was deprecated in Twisted 8.0.0N(R
tassertEqualRt test_getDeprecationWarningString(tselftversion((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyR/s
	cCsEtdddd}tjd}|jt|j||ddS(s
        L{getDeprecationWarningString} returns a string that tells us that a
        callable was deprecated at a certain released version of Twisted, with
        a message containing additional information about the deprecation.
        Riis: This is a messagestwisted.python.test.test_deprecate.TestDeprecationWarnings.test_getDeprecationWarningString was deprecated in Twisted 8.0.0: This is a messageN(R
RRRRR(RRtformat((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt*test_getDeprecationWarningStringWithFormat=s
		csYtdddd}t|tfd}|jttt|t|dS(sK
        Decorating a callable with L{deprecated} emits a warning.
        RiicsdS(N(((tdummy(sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt
addStackLevelSsN(R
RRtassertWarnstDeprecationWarningRt__file__(RRR((RsF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_deprecateEmitsWarningMscCs]tdddd}t|t}|jtj|j|jttt|dS(sK
        The decorated function has the same name as the original.
        RiiN(R
RRRt__name__R	(RRR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_deprecatedPreservesName\s
cCs2tdddd}|jdt|ddS(sr
        L{_getDeprecationDocstring} returns a note about the deprecation to go
        into a docstring.
        RiisDeprecated in Twisted 8.0.0.tN(R
RR(RR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_getDeprecationDocstringgscCsWtdddd}t|t}ttt|d|jtj|jdS(sv
        The docstring of the deprecated function is appended with information
        about the deprecation.
        RiiR N(R
RRRRRt__doc__(RRR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_deprecatedUpdatesDocstringrscCs>tdddd}t|t}|j||jdS(st
        Deprecating a function adds version information to the decorated
        version of that function.
        RiiN(R
RRRtdeprecatedVersion(RRR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_versionMetadatascCsQtdddd}t|j|dd}|j|dt|jfdS(s
        L{getDeprecationWarningString} takes an additional replacement parameter
        that can be used to add information to the deprecation.  If the
        replacement parameter is a string, it will be interpolated directly into
        the result.
        Riitreplacementssomething.foobarsG%s was deprecated in Twisted 8.0.0; please use something.foobar insteadN(R
RRRR	(RRt
warningString((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt+test_getDeprecationWarningStringReplacements	cCsQtdddd}t|j|dt}|j|dt|jfdS(s
        L{getDeprecationWarningString} takes an additional replacement parameter
        that can be used to add information to the deprecation. If the
        replacement parameter is a callable, its fully qualified name will be
        interpolated into the result.
        RiiR&sp%s was deprecated in Twisted 8.0.0; please use twisted.python.test.test_deprecate.dummyReplacementMethod insteadN(R
RRRRR	(RRR'((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt7test_getDeprecationWarningStringReplacementWithCallables	cCsAtdddd}t|dt}|j|jddS(s
        L{deprecated} takes an additional replacement parameter that can be used
        to indicate the new, non-deprecated method developers should use.  If
        the replacement parameter is a string, it will be interpolated directly
        into the warning message.
        Riissomething.foobars
    Do nothing.

    This is used to test the deprecation decorators.

    Deprecated in Twisted 8.0.0; please use something.foobar instead.
    N(R
RRRR"(RRR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_deprecatedReplacementscCsJtdddd}t|dt}|t}|j|jddS(s)
        L{deprecated} takes an additional replacement parameter that can be used
        to indicate the new, non-deprecated method developers should use.  If
        the replacement parameter is a callable, its fully qualified name will
        be interpolated into the warning message.
        RiiR&s
    Do nothing.

    This is used to test the deprecation decorators.

    Deprecated in Twisted 8.0.0; please use twisted.python.test.test_deprecate.dummyReplacementMethod instead.
    N(R
RRRRR"(RRt	decoratorR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt&test_deprecatedReplacementWithCallables
(
Rt
__module__RRRRR!R#R%R(R)R*R,(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyR.s							
			tTestAppendToDocstringcBs)eZdZdZdZdZRS(sk
    Test the _appendToDocstring function.

    _appendToDocstring is used to add text to a docstring.
    cCs-d}t|d|jd|jdS(sP
        Appending to an empty docstring simply replaces the docstring.
        cSsdS(N((((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pytnoDocstringssAppended text.N(RRR"(RR/((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_appendToEmptyDocstrings	
cCsUd}t|d|jdddg|jj|j|jjddS(s
        Appending to a single line docstring places the message on a new line,
        with a blank line separating it from the rest of the docstring.

        The docstring ends with a newline, conforming to Twisted and PEP 8
        standards. Unfortunately, the indentation is incorrect, since the
        existing docstring doesn't have enough info to help us indent
        properly.
        cSsdS(s;This doesn't comply with standards, but is here for a test.N((((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pytsingleLineDocstringssAppended text.s;This doesn't comply with standards, but is here for a test.R s
N(RRR"t
splitlinest
assertTruetendswith(RR1((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt test_appendToSingleLineDocstrings	
cCs9d}d}t|d|j|j|jdS(s
        Appending to a multi-line docstring places the messade on a new line,
        with a blank line separating it from the rest of the docstring.

        Because we have multiple lines, we have enough information to do
        indentation.
        cSsdS(s9
            This is a multi-line docstring.
            N((((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pytmultiLineDocstringscSsdS(sU
            This is a multi-line docstring.

            Appended text.
            N((((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pytexpectedDocstringssAppended text.N(RRR"(RR6R7((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_appendToMultilineDocstrings
			
(RR-R"R0R5R8(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyR.s		t_MockDeprecatedAttributecBs eZdZdZdZRS(sq
    Mock of L{twisted.python.deprecate._DeprecatedAttribute}.

    @ivar value: The value of the attribute.
    cCs
||_dS(N(tvalue(RR:((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt__init__$scCs|jS(s$
        Get a known value.
        (R:(R((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pytget(s(RR-R"R;R<(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyR9s	tModuleProxyTestscBsDeZdZdZdZdZdZdZdZRS(s
    Tests for L{twisted.python.deprecate._ModuleProxy}, which proxies
    access to module-level attributes, intercepting access to deprecated
    attributes and passing through access to normal attributes.
    cKsItjd}x*|jD]\}}t|||qWtj|S(s
        Create a temporary module proxy object.

        @param **kw: Attributes to initialise on the temporary module object

        @rtype: L{twistd.python.deprecate._ModuleProxy}
        tfoo(ttypest
ModuleTypet	iteritemstsetattrRt_ModuleProxy(RtattrstmodtkeyR:((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt
_makeProxy6scCs?|jdd}|j|jd|jtt|ddS(s
        Getting a normal attribute on a L{twisted.python.deprecate._ModuleProxy}
        retrieves the underlying attribute's value, and raises C{AttributeError}
        if a non-existant attribute is accessed.
        tSOME_ATTRIBUTEthellotDOES_NOT_EXISTN(RGtassertIdenticalRHtassertRaisestAttributeErrortgetattr(Rtproxy((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_getattrPassthroughDscCsE|j}tj|d}td|d<|j|jddS(s
        Getting an attribute marked as being deprecated on
        L{twisted.python.deprecate._ModuleProxy} results in calling the
        deprecated wrapper's C{get} method.
        t_deprecatedAttributesi*R>N(RGtobjectt__getattribute__R9RR>(RRORQ((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_getattrInterceptOs
cCs<|j}|jtt|d|jtt|ddS(s
        Private attributes of L{twisted.python.deprecate._ModuleProxy} are
        inaccessible when regular attribute access is used.
        t_moduleRQN(RGRLRMRN(RRO((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_privateAttributes\scCsH|j}d|_|jtj|dd|j|jddS(s
        Setting attributes on L{twisted.python.deprecate._ModuleProxy} proxies
        them through to the wrapped module.
        iRUN(RGRUtassertNotEqualsRRRSR(RRO((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_setattrgs	cCsK|j}tj|d}|jt|dt|j|fdS(s
        L{twisted.python.deprecated._ModuleProxy.__repr__} produces a string
        containing the proxy type and a representation of the wrapped module
        object.
        RUs<%s module=%r>N(RGRRRSRtreprttypeR(RROt
realModule((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt	test_reprrs(	RR-R"RGRPRTRVRXR\(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyR=0s			
		tDeprecatedAttributeTestscBs;eZdZdZdZdZdZdZRS(s
    Tests for L{twisted.python.deprecate._DeprecatedAttribute} and
    L{twisted.python.deprecate.deprecatedModuleAttribute}, which issue
    warnings for deprecated module-level attributes.
    cCs)tj|_tj|_td|_dS(Ns.foo(RRtmessageRt_testModuleName(R((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pytsetUpscCs)ttjd|tjtdtjS(sJ
        Create the warning string used by deprecated attributes.
        t.s: (RRRRRR^(Rtattr((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt_getWarningStringscsd}tt|dtjt||j|j|jj|fd}||j|j	g}|j
|ddt|j|dd|j||jt
|ddS(	s
        L{twisted.python.deprecate._DeprecatedAttribute} correctly sets its
        __name__ to match that of the deprecated attribute and emits a warning
        when the original attribute value is accessed.
        tANOTHER_DEPRECATED_ATTRIBUTEi*csjdS(N(R<((Rb(sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyRsitcategoryR^iN(RBRRt_DeprecatedAttributeRR^RRt
flushWarningsttest_deprecatedAttributeHelperRKRRctlen(RtnameRt
warningsShown((RbsF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyRhscCstj|j|jg}|jt|dd}tt||j|jg}|jt|d|j|ddt|j|dd|j	|dS(s
        L{twisted.python.deprecate.deprecatedModuleAttribute} wraps a
        module-level attribute in an object that emits a deprecation warning
        when it is accessed the first time only, while leaving other unrelated
        attributes alone.
        itDEPRECATED_ATTRIBUTEiReR^N(
RtANOTHER_ATTRIBUTERgttest_deprecatedAttributeRRiRNRKRRc(RRkRj((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyRns
cCstjdtj|j<}|jtjj|jt|ddt|ddtj	t
ddddd	|jdtj|j}|j||tj	t
ddddd	|jd|j|tj|jd
S(s
        Deprecating an attribute in a module replaces and wraps that module
        instance, in C{sys.modules}, with a
        L{twisted.python.deprecate._ModuleProxy} instance but only if it hasn't
        already been wrapped.
        R>tfirstitsecondiRiiR^N(
R?R@tsystmodulesR_t
addCleanuptpopRBRtdeprecatedModuleAttributeR
tassertNotEqualRK(RRERO((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_wrappedModules"(RR-R"R`RcRhRnRw(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyR]s		
		tImportedModuleAttributeTestscBsAeZdZdZdZdZdZdZdZRS(sa
    Tests for L{deprecatedModuleAttribute} which involve loading a module via
    'import'.
    sfrom twisted.python.deprecate import deprecatedModuleAttribute
from twisted.python.versions import Version

deprecatedModuleAttribute(
    Version('Package', 1, 2, 3), 'message', __name__, 'module')
csnfdt|j}|j||}|j|jgtj|jtjj|S(s_
        Create some files in a hierarchy, based on a dictionary describing those
        files.  The resulting hierarchy will be placed onto sys.path for the
        duration of the test.

        @param tree: A dictionary representing a directory structure.  Keys are
            strings, representing filenames, dictionary values represent
            directories, string values represent file contents.

        @return: another dictionary similar to the input, with file content
            strings replaced with L{FilePath} objects pointing at where those
            contents are now stored.
        csi}x|jD]\}}|j|}t|trW|||<|j|qt|tr|j||||<qtdqW|S(Ns(only strings and dicts allowed as values(titemstchildt
isinstancetstrt
setContenttdicttcreateDirectoryt
ValueError(tpathobjtdirdicttpathdictRFR:Rz(t
makeSomeFiles(sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyR	s

(	RtmktemptmakedirstreplaceSysPathtpathRqtreplaceSysModulesRrtcopy(Rttreetbasetresult((RsF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt
pathEntryTrees

cCs3|jii|jd6dd6d6}|ddS(s
        Add a sample module and package to the path, returning a L{FilePath}
        pointing at the module which will be loadable as C{package.module}.
        s__init__.pyR s	module.pytpackage(Rt_packageInit(Rtpaths((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pytsimpleModuleEntryscCsddlm}|j|j|j|j|jg}|jt|d|j|ddd|j|ddtdS(	sB
        Verification logic for L{test_deprecatedModule}.
        i(tmoduleiiR^s7package.module was deprecated in Package 1.2.3: messageReN(	RRRRRRgtcheckOneWarningRiR(Rt
modulePathRtemitted((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyR*scCs|j|jdS(s
        If L{deprecatedModuleAttribute} is used to deprecate a module attribute
        of a package, only one deprecation warning is emitted when the
        deprecated module is imported.
        N(RR(R((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_deprecatedModule9scCsN|j}|j||j|x!tdD]}|j|q3WdS(s
        If L{deprecatedModuleAttribute} is used to deprecate a module attribute
        of a package, only one deprecation warning is emitted when the
        deprecated module is subsequently imported.
        iN(RRtrange(Rtmptx((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt"test_deprecatedModuleMultipleTimesBs


(	RR-R"RRRRRR(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyRxs	$				tWarnAboutFunctionTestscBsMeZdZdZdZdZdZdZdZdZ	RS(s
    Tests for L{twisted.python.deprecate.warnAboutFunction} which allows the
    callers of a function to issue a C{DeprecationWarning} about that function.
    cst|jjd|_|jj|jjdjd|jjdjdtjjd|jj	j|j
tjj|jj	jtjj
|j
fddS(	sY
        Create a file that will have known line numbers when emitting warnings.
        ttwisted_private_helpers__init__.pyR s	module.pys
"A module string"

from twisted.python import deprecate

def testFunction():
    "A doc string"
    a = 1 + 2
    return a

def callTestFunction():
    b = testFunction()
    if b == 3:
        deprecate.warnAboutFunction(testFunction, "A Warning String")
icstjjtjjfS(N(RqRrtcleartupdate((Rr(sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt<lambda>xsN(RRRzRRR}RqRtinserttparentRstremoveRrR(R((RrsF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyR`]s
"cCsd}tj|d|j}t}|jjdrM|d }n|jt|ddt||j|ddddS(	s
        L{deprecate.warnAboutFunction} emits a warning the file and line number
        of which point to the beginning of the implementation of the function
        passed to it.
        cSsdS(N((((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pytaFuncssA Warning Messages.pyciitfilenameR^N(	RtwarnAboutFunctionRgRtlowerR4tassertSamePathRR(RRRkR((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_warning{s	
cCsddlm}|j|j}|jt|dd|jjdjd|j	|ddd|j	|dd	d
|j	t
|ddS(
s
        L{deprecate.warnAboutFunction} emits a C{DeprecationWarning} with the
        number of a line within the implementation of the function passed to it.
        i(RiRRs	module.pytlinenoi	R^sA Warning StringiN(RRtcallTestFunctionRgRRRtsiblingRzRRi(RRRk((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_warningLineNumbers
cCs6|jt|jt|jkd||fdS(s
        Assert that the two paths are the same, considering case normalization
        appropriate for the current platform.

        @type first: L{FilePath}
        @type second: L{FilePath}

        @raise C{self.failureType}: If the paths are not the same.
        s%r != %rN(R3RR(RRoRp((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyRs
cCs!ddlm}tjd=tj|j=|jj|jjdddlm}|j	tjj
d|j	tjj
|j|j|j}t
|dd}|jjdjd}|j|||j|ddd	|j|dd
d|jt|dd
S(s
        Even if the implementation of a deprecated function is moved around on
        the filesystem, the line number in the warning emitted by
        L{deprecate.warnAboutFunction} points to a line in the implementation of
        the deprecated function.
        i(RRttwisted_renamed_helperiRs	module.pyRi	R^sA Warning StringiN(RRRqRrRRtmoveToRRRsRtRRgRRzRRRi(RRRkt
warnedPathtexpectedPath((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_renamedFiles 


	cCs]tj2tjddddddlm}|j|j}|jt|ddS(	s
        L{deprecate.warnAboutFunction} emits a warning that will be filtered if
        L{warnings.filterwarning} is called with the module name of the
        deprecated function.
        tactiontignoreRRi(RiN(	twarningstfilterstfilterwarningsRRRRgRRi(RRRk((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_filteredWarnings		

cCstj2tjddddddlm}|j|j|j}|jt|d|dd}|dd	}|dd
}|dd}tj	||||}|j
|jdd
|fdS(s
        L{deprecate.warnAboutFunction} emits a warning that will be filtered
        once if L{warnings.filterwarning} is called with the module name of the
        deprecated function and an action of once.
        RRRi(RiiR^ReRRs=module.py:9: DeprecationWarning: A Warning String
  return a
sUnexpected warning string: %rN(RRRRRRRgRRit
formatwarningR3R4(RRRkR^ReRRtmsg((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyttest_filteredOnceWarnings 		


(
RR-R"R`RRRRRR(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyRXs					 	(%R"RqR?Rtos.pathRttwisted.trial.unittestRttwisted.pythonRttwisted.python.deprecateRRRRRRttwisted.python.reflectR	ttwisted.python.versionsR
ttwisted.python.filepathRttwisted.python.testRt#twisted.python.test.modules_helpersR
RRRR.RRR9R=R]RxR(((sF/usr/lib/python2.7/dist-packages/twisted/python/test/test_deprecate.pyt<module>s0			EOml