e[^dZdZgdZdadZdZdZdZGdd eZ Gd d e Z Gd d e Z GddZGddZGddZGddeZGddeZGddZdZddZdZy)aj Compute a resolution order for an object and its bases. .. versionchanged:: 5.0 The resolution order is now based on the same C3 order that Python uses for classes. In complex instances of multiple inheritance, this may result in a different ordering. In older versions, the ordering wasn't required to be C3 compliant, and for backwards compatibility, it still isn't. If the ordering isn't C3 compliant (if it is *inconsistent*), zope.interface will make a best guess to try to produce a reasonable resolution order. Still (just as before), the results in such cases may be surprising. .. rubric:: Environment Variables Due to the change in 5.0, certain environment variables can be used to control errors and warnings about inconsistent resolution orders. They are listed in priority order, with variables at the bottom generally overriding variables above them. ZOPE_INTERFACE_WARN_BAD_IRO If this is set to "1", then if there is at least one inconsistent resolution order discovered, a warning (:class:`InconsistentResolutionOrderWarning`) will be issued. Use the usual warning mechanisms to control this behaviour. The warning text will contain additional information on debugging. ZOPE_INTERFACE_TRACK_BAD_IRO If this is set to "1", then zope.interface will log information about each inconsistent resolution order discovered, and keep those details in memory in this module for later inspection. ZOPE_INTERFACE_STRICT_IRO If this is set to "1", any attempt to use :func:`ro` that would produce a non-C3 ordering will fail by raising :class:`InconsistentResolutionOrderError`. .. important:: ``ZOPE_INTERFACE_STRICT_IRO`` is intended to become the default in the future. There are two environment variables that are independent. ZOPE_INTERFACE_LOG_CHANGED_IRO If this is set to "1", then if the C3 resolution order is different from the legacy resolution order for any given object, a message explaining the differences will be logged. This is intended to be used for debugging complicated IROs. ZOPE_INTERFACE_USE_LEGACY_IRO If this is set to "1", then the C3 resolution order will *not* be used. The legacy IRO will be used instead. This is a temporary measure and will be removed in the future. It is intended to help during the transition. It implies ``ZOPE_INTERFACE_LOG_CHANGED_IRO``. .. rubric:: Debugging Behaviour Changes in zope.interface 5 Most behaviour changes from zope.interface 4 to 5 are related to inconsistent resolution orders. ``ZOPE_INTERFACE_STRICT_IRO`` is the most effective tool to find such inconsistent resolution orders, and we recommend running your code with this variable set if at all possible. Doing so will ensure that all interface resolution orders are consistent, and if they're not, will immediately point the way to where this is violated. Occasionally, however, this may not be enough. This is because in some cases, a C3 ordering can be found (the resolution order is fully consistent) that is substantially different from the ad-hoc legacy ordering. In such cases, you may find that you get an unexpected value returned when adapting one or more objects to an interface. To debug this, *also* enable ``ZOPE_INTERFACE_LOG_CHANGED_IRO`` and examine the output. The main thing to look for is changes in the relative positions of interfaces for which there are registered adapters. restructuredtext)ro InconsistentResolutionOrderError"InconsistentResolutionOrderWarningNcLtddl}|jtatS)N)__loggerlogging getLogger__name__)r s 3/usr/lib/python3/dist-packages/zope/interface/ro.py_loggerr ]s $$X. Oct}g}t|D]:}t|D]*}||vs|j||jd|,<|S)aMerge multiple orderings so that within-ordering order is preserved Orderings are constrained in such a way that if an object appears in two or more orderings, then the suffix that begins with the object must be in both orderings. For example: >>> _mergeOrderings([ ... ['x', 'y', 'z'], ... ['q', 'z'], ... [1, 3, 5], ... ['z'] ... ]) ['x', 'y', 'q', 1, 3, 5, 'z'] r)setreversedaddinsert) orderingsseenresultorderingos r _legacy_mergeOrderingsrds_& 5D FY'$(# $A}  a# $$ MrcX|g}d}t|D]}|dz }|j||||S)Nr)iter __bases__)beginriobs r _legacy_flattenr!sBWF A6l# Q llq # Mrc,tt|gSN)rr!)r s r _legacy_ror$s !?2#6"7 88rceZdZdZy)rz> The warning issued when an invalid IRO is requested. N)r __module__ __qualname____doc__rr rrsrrceZdZdZdZdZy)rzK The error raised when an invalid IRO is requested in strict mode. c|j|_|j}t|jjDcic]\}}|||dzc}}|_||_tj|ycc}}w)Nr) leafC base_tree enumeraterbase_rosbase_tree_remaining TypeError__init__)selfc3r1r.rbases r r3z)InconsistentResolutionOrderError.__init__spLL %TVV%5%56 4 )AE" "  $7 4  sA9cddl}dj|jj|j|j |j |j |jS)Nrz7{}: For object {!r}. Base ROs: {} Conflict Location: {})pprintformat __class__r r-pformatr0r1)r4r8s r __str__z(InconsistentResolutionOrderError.__str__sNLSS NN # # FF NN4== ) NN433 4   rN)r r&r'r(r3r<r)rr rrs ! rrceZdZfdZxZS) _NamedBoolc>t|t ||}||_|Sr#)superr>__new__r )clsvalnameinstr:s r rAz_NamedBool.__new__s"S*-c37  r)r r&r'rA __classcell__)r:s@r r>r>s rr>ceZdZdZdZy)_ClassBoolFromEnvz Non-data descriptor that reads a transformed environment variable as a boolean, and caches the result in the class. cTddl}|jD]<}d}t|D]&}||jvs|j||us$|}n|<n t dd|z}|j j |ddk(}t||}t|||t|d|z||S)NrzUnable to find selfZOPE_INTERFACE_1ORIG_) os__mro__dir__dict__ RuntimeErrorenvirongetr>setattr) r4rEklassrNrBmy_namekenv_namerCs r __get__z_ClassBoolFromEnv.__get__s== 6CGZ  $aD)@G " 645 5$w.jjnnXr*c1g&w$w($/ rN)r r&r'r(rZr)rr rHrHs  rrHceZdZdZdZdZy) _StaticMRONc2||_t||_yr#)r,tuple_StaticMRO__mro)r4r-mros r r3z_StaticMRO.__init__s 3Z rc,t|jSr#)listr_r4s r r`z_StaticMRO.mrosDJJr)r r&r'had_inconsistencyr3r`r)rr r\r\s  rr\ceZdZedZdZdZdZdZe dZ e dZ e Z e Ze Ze Ze ZdZdZed Zed Zd Zd ZGd deZdZdZdZy)C3c||ntj}t}|rt}ntjrt}i}|xsi}|j D]$\}}||j vsJt||||<&|||Sr#)rf STRICT_IRO _StrictC3 TRACK_BAD_IRO _TrackingC3itemsrr\)r-strict base_mrosfactorymemor6r`s r resolverz C3.resolvers!-2== G   !GO "* /ID#1;;& &&#D#.DJ /q$rNFc||_||_|j}g}|jD](}||vr|||}|||<|j ||*|gg|jDcgc]}||j c}zt |jgz|_td|D|_ t|jdk(r*|g||jdj z|_ yycc}w)Nc34K|]}|jywr#)rd).0r6s r zC3.__init__..s*]d4+A+A*]srr) r,rpr:rappendr`rbr.anybases_had_inconsistencylen_C3__mro)r4r-rpkindbase_resolversr6rqs r r3z C3.__init__s  ~~KK .D4d+%T  ! !$t* -  .C *+ !%DJNN      (+*]n*]']$ q{{ q tAKKN37799DJ ! s%C:c6|jxs |jSr#)direct_inconsistencyrxrcs r rdzC3.had_inconsistencys((HD,H,HHrc|j#tt|j|_t |jSr#)_C3__legacy_ror^r$r,rbrcs r legacy_roz C3.legacy_ro"s5    #$Z %:;D D$$%%rr)cR|jsyddl}|jdty)NraAn inconsistent resolution order is being requested. (Interfaces should follow the Python class rules known as C3.) For backwards compatibility, zope.interface will allow this, making the best guess it can to produce as meaningful an order as possible. In the future this might be an error. Set the warning filter to error, or set the environment variable 'ZOPE_INTERFACE_TRACK_BAD_IRO' to '1' and examine ro.C3.BAD_IROS to debug, or set 'ZOPE_INTERFACE_STRICT_IRO' to raise exceptions.) WARN_BAD_IROwarningswarnr)r4rs r _warn_iroz C3._warn_iro/s*     _ / rcB|D]}|r|d|ur |D] }||usyy)NrFTr))r6r1basesbs r _can_choose_basezC3._can_choose_baseAsB) !EE!H, !9  !  !rcttd|Dcgc]}|Dcgc] }||us| c}c}}Scc}wcc}}wr#)rbfilter)r.ignoringrrs r _nonempty_bases_ignoringzC3._nonempty_bases_ignoringNsJF4"  31(!2Q 3"    3" s > 99>>cN|j|}||S|j|S)z Return the next base. The return value will either fit the C3 constraints or be our best guess about what to do. If we cannot guess, this may raise an exception. )_find_next_C3_base_guess_next_base)r4r1r6s r _choose_next_basezC3._choose_next_baseVs2&&':;  K$$%899rcH|D]}|d}|j||s|cSy)za Return the next base that fits the constraints, or ``None`` if there isn't one. rN)r)r4r1rr6s r rzC3._find_next_C3_basebs7) E8D$$T+>?  rc eZdZy)C3._UseLegacyRON)r r&r'r)rr _UseLegacyROrls rrc\|jt|||_|jr#)rrr~rr4r1s r rzC3._guess_next_baseos+X $DTK^$_!rc gx}|_|j}d} |j||}|s|S |j|}|j |:#|j$r |j |_|j cYSwxYwr#)rzr.rrrrrv)r4rr1r6s r _mergez C3._merges  "nn #'"?"?@SUY"Z &  &--.AB MM$ $$ &!^^ ~~% &sA,BBc|jt|j|_t|jSr#)rzr^rrbrcs r r`zC3.mros- :: t{{}-DJDJJr)r r&r' staticmethodrqrzrr~r3propertyrdrrHrjrhrLOG_CHANGED_IROUSE_LEGACY_IROBAD_IROSrrrrr Exceptionrrrr`r)rr rfrfs  EK :2II&& &'M"$J$&L')O&(NH $     : y . ` , rrfceZdZdZdZy)rir)ct||r#)rrs r rz_StrictC3._guess_next_bases.t5HIIrNr r&r' __slots__rr)rr riris IJrriceZdZdZdZy)rkr)cJddl}tj}|j|vrl|dk(rddl}|j x}t_t |||jfx||j<}tjd|dtj||S)Nrr)zTracking inconsistent IRO: %s) tracebackrfrr,weakrefWeakKeyDictionaryr format_stackr warningr)r4r1rbad_irosrts r rz_TrackingC3._guess_next_bases;; 99H $2~)0)B)B)DD2;07JK&&(' HTYY ! I  =qt D""4)<==rNrr)rr rkrks I >rrkceZdZGddZGddeZGddeZeZGddZGd d eZ d Z d Z d Z d Z dZedZdZy ) _ROComparisonceZdZdZdZdZy)_ROComparison.Item c||_yr#)item)r4rs r r3z_ROComparison.Item.__init__s DIrcNdj|j|jS)Nz{}{})r9prefixrrcs r r<z_ROComparison.Item.__str__s!==   rN)r r&r'rr3r<r)rr Itemrs  rrceZdZdZy)_ROComparison.Deleted- Nr r&r'rr)rr DeletedrrrceZdZdZy)_ROComparison.Inserted+ Nrr)rr Insertedrrrrc eZdZdZdZdZdZy)_ROComparison.ReplacedByrrKc ||_||_yr#)chunk total_count)r4rrs r r3z!_ROComparison.ReplacedBy.__init__sDJ*D rc|jDcgc]'}|jt|z|jz)}}t ||j kr*|j dt ||j kr*t|Scc}w)NrK)rrstrsuffixryrrvr)r4rliness r __iter__z!_ROComparison.ReplacedBy.__iter__s!JJ c$i'$++5Ee*t/// R e*t///; s,B N)r r&r'rrr3rr)rr ReplacedByrs + rrceZdZdZdZy)_ROComparison.ReplacingrrKN)r r&r'rrr)rr Replacingrs rrNc.||_||_||_yr#)r5c3_ror)r4r5rrs r r3z_ROComparison.__init__s "rc~|D]8}|j|||j|j:yr#)rvEmpty)r4from_to_r operationxs r __movez_ROComparison.__move s3 'A JJy| $ LL & 'rcjeddl}|jdjj}gx_}gx_}|j D]\}}}}} j|| } j||} |dk(r4|jfd| D|jfd| D|dk(r"| rJj||| j|dk(r"| rJj||| j|dk(stt| t| } |jj| | |jj| | jj fS)Nrequalc3@K|]}j|ywr#rrtrr4s r ruz1_ROComparison._generate_report..s$DaTYYq\$Dc3@K|]}j|ywr#rrs r ruz1_ROComparison._generate_report.. s(L!1(Lrdeleterreplace) _c3_reportdifflibSequenceMatcherrr_legacy_report get_opcodesextend_ROComparison__moverrmaxryrr) r4rmatcher c3_report legacy_reportopcodeleg1leg2c31c32c3_chunk legacy_chunk chunk_sizes ` r _generate_reportz_ROComparison._generate_reportsq ?? " --dDNNDJJOG+- ,DOi24 4D -070C0C0E T,dC::c#.#~~d48 W$$$$D8$DD!(((L|(LLX%''<KK =, UX%+++KK y(DMMRY&!$S]C 4E!FJ$$T^^Hj%IJ!((z)RS) T, 3 333rcg}|jjr|jd|jjr|jd|rdj |SdS)Ndirectr+no)r5r~rvrxjoin)r4 inconsistents r _inconsistent_labelz!_ROComparison._inconsistent_label1sS 77 ' '    ) 77 * *    ()5sxx %?4?rc |j\}}t|t|k(sJ|Dcgc] }t|}}|Dcgc] }t|}}t|t|k(sJd}td|D}td|D}dj t|j } dj t|j |j} || j|z|z| j|z|d|t|z|zzzg} | t||D cgc]\} } || j|z|z| z!c} } z } dj| Scc}wcc}wcc} } w)Nrc32K|]}t|ywr#ryrtrs r ruz(_ROComparison.__str__..Fs2!s1v2c32K|]}t|ywr#rrs r ruz(_ROComparison.__str__..Gs41A4rzLegacy RO (len={})zC3 RO (len={}; inconsistent={})= ) rryrrr9rrrljustzipr)r4rrr left_lines right_linespaddingmax_left max_right left_title right_titlerleftrights r r<z_ROComparison.__str__:s#'#8#8#: =9~]!3333&34c!f4 4'01!s1v1 1:#k"22222z224 44 )00T^^1DE 7>>  O  $ $ z''1 1G ;k>O>OPY>Z Z cXG 4y@A A  ":{; e djj* *W 4u <   yy551( sE: E??$F)r r&r'rrrrrrrrrr3rrrrr<r)rr rrsx$4 E"J JN# ' 4D@@ rrcltj|||}|j}||n |j}||n |j}|s|r|j } t | tsJt |tsJ| |k7} | r5| D cgc] } | tus | } } |D cgc] } | tus | } } | | k7} | r(t||| }tjd|||jr8| |k(r3t||| }tjd|||j|r| S|Scc} wcc} w)aR ro(C) -> list Compute the precedence list (mro) according to C3. :return: A fresh `list` object. .. versionchanged:: 5.0.0 Add the *strict*, *log_changed_ro* and *use_legacy_ro* keyword arguments. These are provisional and likely to be removed in the future. They are most useful for testing. z.Object %r has different legacy and C3 MROs: %szVObject %r had inconsistent IRO and used the legacy RO: %s Inconsistency entered at: %s)rfrqr`rrr isinstancerb_ROOTrr rrdr~)r-rmrnlog_changed_ro use_legacy_rorqr` log_changed use_legacyrchangedrlegacy_without_rootmro_without_root comparisons r rr_sG{{1fi0H ,,.C$2$>.HD\D\K"/";AXAXJj&& )T***#t$$$s" /8"J1E>1"J "J+.Aa!5.A A)-==G &xi@J I  A:   % %)s*:&xi@J I  2:x<<    J)#KAs D,D, D1-D1cFtj|ddj S)zn Check if the resolution order for *C*, as computed by :func:`ro`, is consistent according to C3. FN)rfrqrd)r-s r is_consistentrs {{1eT*<< <rHr\rfrirkrrrrr)rr r"sDJ#   : 9)B  y :6   K K \JJ >">$E E T 2j=r