Ϫf_dZddlZddlZddlmZddlmZddlmZm Z m Z m Z m Z ddl mZGddZGd d eZGd d ZGd deZGddeZGddeZGddeZGddZGddeZGddZGddeeZedefdddZGd d!eZGd"d#eZGd$d%eZGd&d'eZ Gd(d)eZ!Gd*d+eZ"Gd,d-eZ#y).z6 Test cases for the L{twisted.python.reflect} module. N)deque)reflect)accumulateMethodsaddMethodNamesToDictfullyQualifiedNameprefixedMethodNamesprefixedMethods)SynchronousTestCaseceZdZdZdZy)Basez` A no-op class which can be used to verify the behavior of method-discovering APIs. cy)z9 A no-op method which can be discovered. Nselfs ;/usr/lib/python3/dist-packages/twisted/test/test_reflect.pymethodz Base.methodN)__name__ __module__ __qualname____doc__rrrrr r s   rr ceZdZdZy)SubzF A subclass of a class with a method which can be discovered. N)rrrrrrrrr$srrceZdZdZdZdZy)Separatez= A no-op class with methods with differing prefixes. cy)zJ A no-op method which a matching prefix to be discovered. Nrrs r good_methodzSeparate.good_method/rrcy)zO A no-op method with a mismatched prefix to not be discovered. Nrrs r bad_methodzSeparate.bad_method4rrN)rrrrrr rrrrr*s  rrc"eZdZdZdZdZdZy)AccumulateMethodsTestszt Tests for L{accumulateMethods} which finds methods on a class hierarchy and adds them to a dictionary. cpt}i}t|||jd|ji|y)z If x is and instance of Base and Base defines a method named method, L{accumulateMethods} adds an item to the given dictionary with C{"method"} as the key and a bound method object for Base.method value. rN)r r assertEqualrrxoutputs r test_ownClassz$AccumulateMethodsTests.test_ownClass@s3 F!V$ (AHH-v6rcpt}i}t|||jd|ji|y)a If x is an instance of Sub and Sub is a subclass of Base and Base defines a method named method, L{accumulateMethods} adds an item to the given dictionary with C{"method"} as the key and a bound method object for Base.method as the value. rN)rrr$rr%s rtest_baseClassz%AccumulateMethodsTests.test_baseClassKs3 E!V$ (AHH-v6rcrt}i}t||d|jd|ji|y)z If a prefix is given, L{accumulateMethods} limits its results to methods beginning with that prefix. Keys in the resulting dictionary also have the prefix removed from them. good_rN)rrr$rr%s r test_prefixz"AccumulateMethodsTests.test_prefixWs5 J!VW- (AMM2F;rN)rrrrr(r*r-rrrr"r":s 7 7 .ChildNrrrrrrChildr9 rr;rr,N)rr$r)rr;s rtest_inheritedMethodz-PrefixedMethodNamesTests.test_inheritedMethods*  H  (%8%HIrN)rrrrr6r=rrrr4r4|sM Jrr4ceZdZdZdZy)AddMethodNamesToDictTestsz, Tests for L{addMethodNamesToDict}. cGdd}Gddt|}i}t||d||jddi|y) z If C{baseClass} is passed to L{addMethodNamesToDict}, only methods which are a subclass of C{baseClass} are added to the result dictionary. c eZdZy);AddMethodNamesToDictTests.test_baseClass..AlternateNr:rrr AlternaterBr<rrCceZdZdZy)7AddMethodNamesToDictTests.test_baseClass..ChildcyNrrs rgood_alternatezFAddMethodNamesToDictTests.test_baseClass..Child.good_alternaterN)rrrrHrrrr;rE rr;r, alternateN)rrr$)rrCr;results rr*z(AddMethodNamesToDictTests.test_baseClasssD    Hi UFGY? +q)62rN)rrrrr*rrrr?r?s 3rr?ceZdZdZdZy)Summerz9 A class we look up as part of the LookupsTests. cy)z Do something. Nrrs r reallySetzSummer.reallySetrrN)rrrrrQrrrrOrOs  rrOc^eZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZy) LookupsTestszC Tests for L{namedClass}, L{namedModule}, and L{namedAny}. cV|jtjdty)zY L{namedClass} should return the class object for the name it is passed. twisted.test.test_reflect.SummerN)assertIsr namedClassrOrs rtest_namedClassLookupz"LookupsTests.test_namedClassLookups g(()KLfUrcZddlm}|jtjd|y)zc L{namedModule} should return the module object for the name it is passed. rmonkeytwisted.python.monkeyN)twisted.pythonr[rVr namedModulerr[s rtest_namedModuleLookupz#LookupsTests.test_namedModuleLookups! * g))*ABFKrcjddl}|jtjd|jy)zY L{namedAny} should return the package object for the name it is passed. rNtwisted.python)r]rVrnamedAnypythonrtwisteds rtest_namedAnyPackageLookupz'LookupsTests.test_namedAnyPackageLookups%  g&&'78'..IrcZddlm}|jtjd|y)zX L{namedAny} should return the module object for the name it is passed. rrZr\N)r]r[rVrrcr_s rtest_namedAnyModuleLookupz&LookupsTests.test_namedAnyModuleLookups! * g&&'>?HrcV|jtjdty)zW L{namedAny} should return the class object for the name it is passed. rUN)rVrrcrOrs rtest_namedAnyClassLookupz%LookupsTests.test_namedAnyClassLookups g&&'IJFSrcj|jtjdtjy)z L{namedAny} should return the object an attribute of a non-module, non-package object is bound to for the name it is passed. z*twisted.test.test_reflect.Summer.reallySetN)r$rrcrOrQrs rtest_namedAnyAttributeLookupz)LookupsTests.test_namedAnyAttributeLookups*    I J    rc~|jtjdtjj y)z L{namedAny} should return the object an attribute of an object which itself was an attribute of a non-module, non-package object is bound to for the name it is passed. z2twisted.test.test_reflect.Summer.reallySet.__doc__N)rVrrcrOrQrrs r"test_namedAnySecondAttributeLookupz/LookupsTests.test_namedAnySecondAttributeLookups.   T U    $ $ rc,|jttjd|jttjd|jttjd|jt tjdy)z Exceptions raised by modules which L{namedAny} causes to be imported should pass through L{namedAny} to the caller. ztwisted.test.reflect_helper_ZDEztwisted.test.reflect_helper_VEztwisted.test.reflect_helper_IEN) assertRaisesZeroDivisionErrorrrc ValueError ImportErrorrs rtest_importExceptionsz"LookupsTests.test_importExceptionssz  w//1R   w//1R   ((*J  ))+K rc|jttjd|jttjd|jttjdy)z If segments on the end of a fully-qualified Python name represents attributes which aren't actually present on the object represented by the earlier segments, L{namedAny} should raise an L{AttributeError}. ztwisted.nosuchmoduleintheworldz twisted.nosuch.modulein.theworldz0twisted.test.test_reflect.Summer.nosuchattributeN)rqAttributeErrorrrcrs rtest_attributeExceptionsz%LookupsTests.test_attributeExceptions s]  G,,.N  G,,.P       > rc|jtjtjd}|j t |d|jtj tjd}|j t |d|jtj tjd}|j t |d|jtjtjd}|j t |dd D]O}|jtjtj|}|j t |d |Qy ) a Passing a name which isn't a fully-qualified Python name to L{namedAny} should result in one of the following exceptions: - L{InvalidName}: the name is not a dot-separated list of Python objects - L{ObjectNotFound}: the object doesn't exist - L{ModuleNotFound}: the object doesn't exist and there is only one component in the name nosuchmoduleintheworldz(No module named 'nosuchmoduleintheworld'z@#$@(#.!@(#!@#z('@#$@(#.!@(#!@#' does not name an objectztcelfer.nohtyp.detsiwtz0'tcelfer.nohtyp.detsiwt' does not name an objectzEmpty module name)z.twistedztwisted.ztwisted..pythonzMname must be a string giving a '.'-separated list of Python identifiers, not N)rqrModuleNotFoundrcr$strObjectNotFound InvalidName)rerr invalidNames rtest_invalidNameszLookupsTests.test_invalidNames"s8  " "G$4$46N  S#MN  " "G$4$46F  S#MN  " "G$4$46N  S#UV 3 3W5E5ErJ S#67F K##G$7$79I9I;WC   C)47  rcxdD]5}t}tj||}|j||7y)zk When module import fails with ImportError it returns the specified default value. )nosuchmtopoduleno.such.moduledefaultN)objectr requireModulerV)rnamerrMs rtest_requireModuleImportErrorz*LookupsTests.test_requireModuleImportErrorGs: : +DhG**4AF MM&' *  +rcPtjd}|j|y)zI When module import fails it returns L{None} by default. rN)rr assertIsNone)rrMs rtest_requireModuleDefaultNonez*LookupsTests.test_requireModuleDefaultNoneSs#&&'78 &!rcrddlm}t}|jt j d||y)ze When module import succeed it returns the module and not the default value. rrZr\rN)r]r[rrVrr)rr[rs r!test_requireModuleRequestedImportz.LookupsTests.test_requireModuleRequestedImport[s. *(  ! !"97 K  rN)rrrrrXr`rgrirkrmrorurxrrrrrrrrSrSsNV LJIT     * *#J +"  rrSc,eZdZdZdZdefdZdefdZy) BreakableFreturnc2|jr tdy)Nzstr!z )breakStr RuntimeErrorrs r__str__zBreakable.__str__ns ==v& & rc2|jr tdy)Nzrepr!z Breakable()) breakReprrrs r__repr__zBreakable.__repr__ts >>w' ' rN)rrrrrr}rrrrrrrjs$IH!! !#!rrc eZdZdZedZy) BrokenTypeFc2|jr tdy)Nzno namer) breakNamerrs rrzBrokenType.__name__~s >>y) )rN)rrrrpropertyrrrrr{sI rrBTBaseT)rrceZdZedZy) NoClassAttrc|jSrG) not_class)r&s rzNoClassAttr.s 1;;rN)rrrr __class__rrrrrs ./Irrc@eZdZdZdZdZdZdZdZdZ dZ d Z y ) SafeReprTestsz2 Tests for L{reflect.safe_repr} function. c gddf}|jtttj|ttt |y)zg L{reflect.safe_repr} produces the same output as C{repr} on a working object. rLaN)r$listmapr safe_reprrepr)rxss rtest_workingReprzSafeReprTests.test_workingReprs;   c'"3"3R894D" ;NOrct}d|_tj|}|j d||j t j jtd||j d|y)z L{reflect.safe_repr} returns a string with class name, address, and traceback when the repr call failed. TzBreakable instance at 0xrRuntimeError: repr!N) rrrrassertInospathsplitext__file__rbbReprs rtest_brokenReprzSafeReprTests.test_brokenReprsd K !!!$ 0%8 bgg&&x03U; +U3rct}d|_|jtj|t |y)zT L{reflect.safe_repr} isn't affected by a broken C{__str__} method. TN)rrr$rrrrrs rtest_brokenStrzSafeReprTests.test_brokenStrs1 K  **1-tAw7rcGddt}tj|tj|y)NceZdZdZy)-SafeReprTests.test_brokenClassRepr..XTNrrrrrrrXrIrrrrrrrs rtest_brokenClassReprz"SafeReprTests.test_brokenClassReprs.   !!#rcGddt}tj|}dt|dd}|j ||j ddy) z C{id} is used to print the ID of the object in case of an error. L{safe_repr} includes a traceback after a newline, so we only check against the first line of the repr. ceZdZdZy)2SafeReprTests.test_brokenReprIncludesID..XTNrrrrrrrrrz.XTNrrrrrrrrrHrrrrs rtest_brokenClassStrz!SafeReprTests.test_brokenClassStrs.   !!#rct}d|_tj|}|j d||j t j jtd||j d|y)z If an object raises an exception when accessing its C{__class__} attribute, L{reflect.safe_repr} uses C{type} to retrieve the class object. TNoClassAttr instance at 0xrrN) rrrrrrrrrrs rtest_brokenClassAttributez'SafeReprTests.test_brokenClassAttributesb M !!!$ 2E: bgg&&x03U; +U3rc Gddt}tj|}|jd||jtj j td||jd|y)z If a class raises an exception when accessing its C{__name__} attribute B{and} when calling its C{__str__} implementation, L{reflect.safe_repr} returns 'BROKEN CLASS' instead of the class name. ceZdZdZy)6SafeReprTests.test_brokenClassNameAttribute..XTNrrrrrrrrrrrr 4 4rrcLeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z y ) SafeStrTestsz1 Tests for L{reflect.safe_str} function. chgd}|jtj|t|y)Nr)r$rsafe_strr}rr&s rtest_workingStrzSafeStrTests.test_workingStrs&  ))!,c!f5rcPt}d|_tj|yNT)rrrrrs rrzSafeStrTests.test_brokenStrs K rcRd}|jtj|dy)zh L{safe_str} for C{str} with ascii-only data should return the value unchanged. aN)r$rrrs rtest_workingAsciizSafeStrTests.test_workingAsciis$  ))!,c2rcpd}|jtj||jdy)zw L{safe_str} for C{bytes} with utf-8 encoded data should return the value decoded into C{str}. stüstutf-8N)r$rrdecoders rtest_workingUtf8_3zSafeStrTests.test_workingUtf8_3s-  ))!,ahhw.?@rchd}tj|}|j|t|y)z= Use str() for non-utf8 bytes: "b'non-utf8'" N)rrr$r})rr&xStrs rtest_brokenUtf8zSafeStrTests.test_brokenUtf8s- " s1v&rcPt}d|_tj|yr)rrrrrs rrzSafeStrTests.test_brokenReprs K rcGddt}tj|tj|y)NceZdZdZy)+SafeStrTests.test_brokenClassStr..XTNrrrrrrrrrrrrrs rrz SafeStrTests.test_brokenClassStrs.   rcGddt}tj|tj|y)NceZdZdZy),SafeStrTests.test_brokenClassRepr..XTNrrrrrrrrrrrs rrz!SafeStrTests.test_brokenClassReprs.   rct}d|_tj|}|j d||j t j jtd||j d|y)z If an object raises an exception when accessing its C{__class__} attribute, L{reflect.safe_str} uses C{type} to retrieve the class object. TrrRuntimeError: str!N) rrrrrrrrr)rrbStrs rrz&SafeStrTests.test_brokenClassAttribute"sb M " 2D9 bgg&&x03T: *D1rc Gddt}tj|}|jd||jtj j td||jd|y)z If a class raises an exception when accessing its C{__name__} attribute B{and} when calling its C{__str__} implementation, L{reflect.safe_str} returns 'BROKEN CLASS' instead of the class name. ceZdZdZy)5SafeStrTests.test_brokenClassNameAttribute..XTNrrrrrr6rrrrrrN)rrrrrrrr)rrrs rrz*SafeStrTests.test_brokenClassNameAttribute/sb  $ +T2 bgg&&x03T: *D1rN)rrrrrrrrrrrrrrrrrrrs:6 3A'  2 2rrc(eZdZdZdZdZdZdZy)FilenameToModuleTestsz1 Test L{filenameToModuleName} detection. cLtjj|jdd|_tj|jt tjj|jdd5}|j ddddt tjjtjj|jdd5}|j ddddy#1swYuxYw#1swYyxYw)N fakepackagetestz __init__.pywr{)rrjoinmktempmakedirsopenwritedirname)rfs rsetUpzFilenameToModuleTests.setUpDsGGLL vF  DII "'',,tyy-8# > ! GGBK  "'',,rwwtyy9=I3 O ST GGBK      sD3DDD#ctj|j}|j|dtj|jtjj z}|j|dy)zk L{filenameToModuleName} returns the correct module (a package) given a directory. zfakepackage.testN)rfilenameToModuleNamerr$rseprmodules rtest_directoryz$FilenameToModuleTests.test_directoryLs[ --dii8 !34--dii"''++.EF !34rctjtjj |jd}|j |dy)zh L{filenameToModuleName} returns the correct module given the path to its file. ztest_reflect.pyfakepackage.test.test_reflectN)rrrrr r$rs r test_filezFilenameToModuleTests.test_fileVs> -- GGLL$5 6  !@Arctjtjj |jj dd}|j |dy)zo L{filenameToModuleName} returns the correct module given a C{bytes} path to its file. rstest_reflect.pyrN)rrrrr encoder$rs r test_bytesz FilenameToModuleTests.test_bytes`sI -- GGLL))'24F G  !@ArN)rrrrrrrrrrrrr?s5B Brrc:eZdZdZdZdZdZdZdZdZ dZ y ) FullyQualifiedNameTestsz) Test for L{fullyQualifiedName}. c:|jt||y)ze Helper to check that fully qualified name of C{obj} results to C{expected}. N)r$r)robjexpecteds r_checkFullyQualifiedNamez0FullyQualifiedNameTests._checkFullyQualifiedNameqs +C0(;rcpddl}|j|dddl}|j|jdy)zd L{fullyQualifiedName} returns the full name of a package and a subpackage. rNrfrb)rfr#r]rdres r test_packagez$FullyQualifiedNameTests.test_packagexs/  %%gy9 %%gnn6FGrcXddl}|j|jjdy)zV L{fullyQualifiedName} returns the name of a module inside a package. rNztwisted.python.compat)twisted.python.compatr#rdcompatres r test_modulez#FullyQualifiedNameTests.test_modules" % %%gnn&;&;=TUrc>|jttdy)zS L{fullyQualifiedName} returns the name of a class and its module. z.FullyQualifiedNameTestsN)r#rrrs r test_classz"FullyQualifiedNameTests.test_classs %% #z1I%J rc0|jtdy)zY L{fullyQualifiedName} returns the name of a function inside its module. z)twisted.python.reflect.fullyQualifiedNameN)r#rrs r test_functionz%FullyQualifiedNameTests.test_functions %%  K rcx|j|jtd|jjdy)zs L{fullyQualifiedName} returns the name of a bound method inside its class and its module. .z.test_boundMethodN)r#test_boundMethodrrrs rr0z(FullyQualifiedNameTests.test_boundMethods7 %%  ! !j$..1122C D rc|j|jjtd|jjdy)zv L{fullyQualifiedName} returns the name of an unbound method inside its class and its module. r/z.test_unboundMethodN)r#rtest_unboundMethodrrs rr2z*FullyQualifiedNameTests.test_unboundMethods; %% NN - -j$..1122E F rN) rrrrr#r%r)r+r-r0r2rrrrrls,< HV    rrcBeZdZdZdZdZdZdZdZdZ dZ d Z y ) ObjectGrepTestsct}d|i}|di}|jdtj||tj|jdtj||tjy)zV Test references search through a dictionary, as a key or as a value. Nz[None]z{None}rrrobjgrepisSame)rod1d2s rtest_dictionaryzObjectGrepTests.test_dictionarys[ HAYY hAw~~ FG hAw~~ FGrct}d|g}|jdtj||tjy)z8 Test references search through a list. N[1]r6)rr9Ls r test_listzObjectGrepTests.test_lists3 H 1I eW__Q7>>BCrct}|df}|jdtj||tjy)z9 Test references search through a tuple. N[0]r6)rr9Ts r test_tuplezObjectGrepTests.test_tuples3 H I eW__Q7>>BCrcGdd}t}|}||_|jdtj||tj y)zE Test references search through an object attribute. c eZdZy),ObjectGrepTests.test_instance..DummyNr:rrrDummyrGr<rrHz.oN)rr9rrr7r8)rrHr9ds r test_instancezObjectGrepTests.test_instancesB    H G dGOOAq'..ABrcGdd}|}tj|}|jdtj||tj y)zB Test references search through a weakref object. c eZdZy)+ObjectGrepTests.test_weakref..DummyNr:rrrrHrMr<rrHz()N)weakrefrefrrr7r8)rrHr9w1s r test_weakrefzObjectGrepTests.test_weakrefsA    G [[^ dGOOB7>>BCrcGdd}|}|j}|jdtj||jtj |jdtj||jj tj |jdtj||jtj y)zK Test references search through method special attributes. ceZdZdZy)/ObjectGrepTests.test_boundMethod..DummycyrGrrs rdummyz5ObjectGrepTests.test_boundMethod..Dummy.dummyrIrN)rrrrVrrrrHrTrJrrHz .__self__z.__self__.__class__z .__func__N)rVrrr7__self__r8r__func__)rrHr9ms rr0z ObjectGrepTests.test_boundMethods    G GG k7??1ajj'..#QR ! OOAqzz33W^^ D  k7??1ajj'..#QRrc*Gdd}|}dddd|di}dd|dg}|i|f}d d |d }|}||_|j}tj|} |j d t j | |t jy) zF Test references search using complex set of objects. ceZdZdZy).ObjectGrepTests.test_everything..DummycyrGrrs rrz5ObjectGrepTests.test_everything..Dummy.methodrIrN)rrrrrrrrHr\rJrrHrbazNQuuxFooshrfoobar)rrLrz"().__self__.attr[2][0][2]{'Foosh'})attrrrNrOrrr7r8) rrHr9D1r?rCD2irYr s rtest_everythingzObjectGrepTests.test_everythings    G%vq' 2 2r1  EG 5Q ' G HH KKN 0'//!Q2W rc rg}|g}||g}||g}|jdgtj||tjd|jddgtj||tjd|jgdtj||tjdy) z6 Test the depth of references search. rBrL)maxDepth[1][0]r)rBrjz [1][1][0]rN)r$rr7r8)rrrcrIs rtest_depthLimitzObjectGrepTests.test_depthLimits  C F F %'//!QQR"ST  H wq!W^^aP   * OOAq'..1 = rct}t}|jd|j||jdt j ||tj y)z@ Test references search through a deque object. Nr>)rrappendrrr7r8)rr9Ds r test_dequezObjectGrepTests.test_deque&sG H G    eW__Q7>>BCrN) rrrr<r@rDrJrQr0rgrlrprrrr4r4s6 HDD C DS& . $ Drr4ceZdZdZy) GetClassTestscGdd}|}|jtj|jd|jtj|jdy)Nc eZdZy)(GetClassTests.test_new..NewClassNr:rrrNewClassru4r<rrvtype)r$rgetClassr)rrvnews rtest_newzGetClassTests.test_new3sW  j ))(3<rs*  "C   $   &