bg0ddlZddlmZddlmZddlmZmZddlm Z m Z ddl m Z ddl mZmZmZmZmZddlmZmZmZmZmZdd lmZmZddlZeeegefZd Zed Z Gd d Z! ddedede"dedededeegeffdZ#ededefdZ$ed dededefdZ%ed dedede fdZ&Gdde'Z(dZ)ee)e!j*e!j#e#e(gdS)!N)contextmanager)partial) get_referrers get_referents)setitemgetitem) ModuleType)AnyTypeVarCallableDictTuple)resolve not_thereResolvedclassmethod_type class_type)wrapextend_docstringcDt||ot|| SN) isinstance)xy descriptors e/builddir/build/BUILD/cloudlinux-venv-1.0.7/venv/lib/python3.11/site-packages/testfixtures/replace.pynot_same_descriptorrs$ a $ $ FZ:-F-F)FFRceZdZdZdZdefdZ ddeded e d ed e d e d efdZ ddeded e d ed e d e d dfdZ d e ded dfdZd e de fdZddeded e d dfdZddededed dfdZddZdZdZdZdS)Replacerz These are used to manage the mocking out of objects so that units of code can be tested without having to rely on their normal dependencies. ci|_dSr) originalsselfs r__init__zReplacer.__init__ s :<rresolvedc0|turk|jtur, t|j|jn#t $rYnwxYw|jtur! |j|j=dS#t$rYdSwxYwdS||j|j|dSr) rsettersetattrdelattr containernameAttributeErrorrKeyError)r%r'values r_replacezReplacer._replace#s I  '))H. >>>>%D')) *8=999DD*) OOH. u E E E E Es!4 AA A"" A0/A0TNtarget replacementstrictr,accessorr-returnc||tdt|tr"|tdt||}nt}||}|pt |dd}|td|I t }|||}nY#t$rYnMt$rt }|||t}Yn)wxYw |||}n#ttf$rYnwxYw|r4|tus+||us'|||} || urt|d|d|t||t urtnt||}|j td|jtur|rtd |jz|} t|jt$r|j|jjvr|jj|j|_t)|j|t*rt+|} n*t)|j|t,rt-|} ||| t1|} | |jvr ||f|j| <|S) M Replace the specified target with the supplied replacement. Nz-accessor is not used unless name is specifiedz0name cannot be specified when target is a string__name__z2name must be specified when target is not a stringz resolved to z , expected z%target must contain at least one dot!zOriginal %r not found) TypeErrorrstrrrgetattrrr/r.AssertionErrorrrr*r) ValueErrorfoundr-r,type__dict__r classmethod staticmethodr1idr#) r%r2r3r4r,r5r-r'r?expectedreplacement_to_usekeys r__call__zReplacer.__call__2s Y & &6 & !88=!HII I( h($ / / ?} 2 ;;;!)!3!; LL ?%0%=%=""$X^[,OO ?%1+%>%>" h 2333jj dn $ $"(("2DN3 s*=B C"CC CC'&C'c&|||||||dS)r8Nr%r2r3r4r,r5r-s rreplacezReplacer.replace{s$ V[&)XtDDDDDrc ~|tj|td|turtnt |dS)a This method provides a convenient way of ensuring an environment variable in :any:`os.environ` is set to a particular value. If you wish to ensure that an environment variable is *not* present, then use :any:`not_there` as the ``replacement``. F)r-r5r4r3N)osenvironrrr;)r%r-r3s r in_environzReplacer.in_environsP RZdWU&1Y&>&>C DTDT V V V V V Vrbreak_on_staticc"t|D]~}|rt|trd|fcSt|trJd|vrF|||ur/t|D]}t|t r|dfccS dS)NrA)NN)rrrCdictgetr@)r% attributer-rQreferrerr,s r_find_containerzReplacer._find_containers%i00 3 3H 3:h #E#E 3X~%%%Hd++ 3 h0F0F<<%%22%28%<%<33 %i663#,d?222223zrrUct|std|pt|dd}d}t|tr*t |D]}t|t r|}n8|||d\}}||||d\}}|td|d||||t| dS) au This method provides a convenient way to replace methods, static methods and class methods on their classes. If the attribute being replaced has a ``__name__`` that differs from the attribute name on the class, such as that returned by poorly implemented decorators, then ``name`` must be used to provide the correct name. zattribute must be callabler9NT)rQFzcould not find container of z using name r-r5r3) callabler:r<rrrrrWr.)r%rUr3r-r,referred staticmethod__s ron_classzReplacer.on_classs "" :899 9;wy*d;; i!1 2 2 `))44 ) )h 33) (I )(,';';It]a';'b'b $I}(#33M4Y^3__ 1   !a !a!aY]!a!abb b YTGMMMMMMrmodulecr|pt|jj}|j}|||t|dS)a This method provides a convenient way to replace targets that are module globals, particularly functions or other objects with a ``__name__`` attribute. If an object has been imported into a module other than the one where it has been defined, then ``module`` should be used to specify the module where you would like the replacement to occur. rYN)r __module__r?r9r<)r%r2r3r_r,r-s r in_modulezReplacer.in_modulesB>gf&788>  YTGMMMMMMrct|jD]+\}\}}|||j|j|=,dS)z Restore all the original objects that have been replaced by calls to the :meth:`replace` method of this :class:`Replacer`. N)tupler#itemsr1r?)r%id_r2originals rrestorezReplacer.restores` (-T^-A-A-C-C'D'D $ $ #C#&( MM(HN 3 3 3s## $ $rc|SrrJr$s r __enter__zReplacer.__enter__s rc.|dSr)rh)r%r@r0 tracebacks r__exit__zReplacer.__exit__s rc|jr:tjdd|jDzdSdS)Nz;Replacer deleted without being restored, originals left: %rci|]\}}|| SrJrJ).0kvs r z$Replacer.__del__..s'R'R'RA!'R'R'Rr)r#warningswarnvaluesr$s r__del__zReplacer.__del__s` >  M%'R'R$.:O:O:Q:Q'R'R'RS       rTNNNr)r6N)r9ra __qualname____doc__r&rr1r rboolAccessorr;rHrLrPrWr r^r rbrhrjrmrwrJrrr!r!s' === F F F F FDHOSGGsGGDGG2:GILGXYGGGGREINREEcEETEE19EHKEW[EEEE Vs V V V V V V s T    NN(NNCNSWNNNN6 N N N# Nz NUY N N N N$$$$rr!Tr2r3r4r,r5r-r6c xt}tt|j|||||||jS)zY A decorator to replace a target object for the duration of a test function. )r!rrrHrh)r2r3r4r,r5r-rs rrLrLs=  A  FKHdSS   rc#Kt5}|||dVddddS#1swxYwYdS)zW This context manager provides a quick way to use :meth:`Replacer.in_environ`. N)r!rP)r-r3r~s rreplace_in_environrs q T;''' s 9==rUc#Kt5}||||dVddddS#1swxYwYdS)zU This context manager provides a quick way to use :meth:`Replacer.on_class`. N)r!r^)rUr3r-r~s rreplace_on_classrs q 9k4000  :>>r_c#Kt5}||||dVddddS#1swxYwYdS)zV This context manager provides a quick way to use :meth:`Replacer.in_module`. N)r!rb)r2r3r_r~s rreplace_in_modulers q FK000 rc JeZdZdZ ddededededed ef d Z d efd Z d Z dS)ReplacezU A context manager that uses a :class:`Replacer` to replace a single target. TNr2r3r4r,r5r-c||_||_||_||_||_||_t |_dSr)r2r3r4r,r5r-r! _replacerrKs rr&zReplace.__init__ s> & '"*  !rr6cr||j|j|j|j|j|jSr)rr2r3r4r,r5r-r$s rrjzReplace.__enter__s4~~ K)4; W[W`   rc8|jdSr)rrh)r%exc_typeexc_valexc_tbs rrmzReplace.__exit__s      rrx) r9raryrzr rr{r|r;r&rjrmrJrrrrs ?CJN $ $ $,- $7; $ $-5 $DG $ $ $ $ 1    !!!!!rra :param target: This must be one of the following: - A string containing the dotted-path to the object to be replaced, in which case it will be resolved the the object to be replaced. This path may specify a module in a package, an attribute of a module, or any attribute of something contained within a module. - The container of the object to be replaced, in which case ``name`` must be specified. - The object to be replaced, in which case ``container`` must be specified. ``name`` must also be specified if it cannot be obtained from the ``__name__`` attribute of the object to be replaced. :param replacement: The object to use as a replacement. :param strict: When `True`, an exception will be raised if an attempt is made to replace an object that does not exist or if the object that is obtained using the ``accessor`` to access the ``name`` from the ``container`` is not identical to the ``target``. :param container: The container of the object from which ``target`` can be accessed using either :func:`getattr` or :func:`~operator.getitem`. :param accessor: Either :func:`getattr` or :func:`~operator.getitem`. If not supplied, this will be inferred preferring :func:`~operator.getitem` over :func:`getattr`. :param name: The name used to access the ``target`` from the ``container`` using the ``accessor``. If required but not specified, the ``__name__`` attribute of the ``target`` will be used. rxr)+rN contextlibr functoolsrgcrroperatorrrtypesr typingr r r r rtestfixtures.resolverrrrrtestfixtures.utilsrrrtr;r|rrr!r{rLrrrobjectrreplace_params_docrHrJrrrs %%%%%%++++++++%%%%%%%%66666666666666[[[[[[[[[[[[[[55555555 S#JO $GGG GCLL}}}}}}}}B7;FJ   "% /3  )1 @C xj("#    Sss#cZ!!!!!f!!!4#L##X%5wHJJJJJr