bg :dZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z ddlm Z dd lm Z dd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZejdZedZedZede dfdZdedefdZdedededefdZ dede!deeeffdZ"Gdd Z#e#Z$eGd!d"Z%dS)#z)Monkeypatching and mocking functionality.N)contextmanager)Any) Generator)List)Mapping)MutableMapping)Optional)overload)Tuple)TypeVar)Union)final)fixture) PytestWarningz^No module named (.*)$KVreturn MonkeyPatchNNc#VKt}|V|dS)a A convenient fixture for monkey-patching. The fixture provides these methods to modify objects, dictionaries, or :data:`os.environ`: * :meth:`monkeypatch.setattr(obj, name, value, raising=True) ` * :meth:`monkeypatch.delattr(obj, name, raising=True) ` * :meth:`monkeypatch.setitem(mapping, name, value) ` * :meth:`monkeypatch.delitem(obj, name, raising=True) ` * :meth:`monkeypatch.setenv(name, value, prepend=None) ` * :meth:`monkeypatch.delenv(name, raising=True) ` * :meth:`monkeypatch.syspath_prepend(path) ` * :meth:`monkeypatch.chdir(path) ` * :meth:`monkeypatch.context() ` All modifications will be undone after the requesting test function or fixture has finished. The ``raising`` parameter determines if a :class:`KeyError` or :class:`AttributeError` will be raised if the set/deletion operation does not have the specified target. To undo modifications done by the fixture in a contained scope, use :meth:`context() `. N)rundo)mpatchs d/builddir/build/BUILD/cloudlinux-venv-1.0.7/venv/lib/python3.11/site-packages/_pytest/monkeypatch.py monkeypatchrs+2]]F LLL KKMMMMMnamec|d}|d}t|}|D]}|d|zz } t||}#t$rYnwxYw t|nV#t $rI}t |d}||krt d|d||d}~wwxYwt|||}|S)N.rzimport error in z: )splitpop __import__getattrAttributeError ImportErrorstrannotated_getattr)rpartsusedfoundpartexexpecteds rresolver.;s  JJsOOE 99Q<selfs r__repr__zNotset.__repr__nszrN)r4 __module__ __qualname__r&rGrDrrrBrBms/#rrBc eZdZdZddZeededfdZe dde d e d e d e ddf d Ze dde d e d e d e ddf d Zedfdee e fd ee e fd e d e ddf dZedfdee e fd ee e fd e ddfdZdeeefd ed eddfdZddeeefd ed e ddfdZd d e d e dee ddfdZdd e d e ddfdZddZdee dfddfdZddZdS)!raHelper to conveniently monkeypatch attributes/items/environment variables/syspath. Returned by the :fixture:`monkeypatch` fixture. .. versionchanged:: 6.2 Can now also be used directly as `pytest.MonkeyPatch()`, for when the fixture is not available. In this case, use :meth:`with MonkeyPatch.context() as mp: ` or remember to call :meth:`undo` explicitly. rNc>g|_g|_d|_d|_dSN)_setattr_setitem_cwd _savesyspathrEs r__init__zMonkeyPatch.__init__s%:< HJ #' 15rrc#K|} |V|dS#|wxYw)a]Context manager that returns a new :class:`MonkeyPatch` object which undoes any patching done inside the ``with`` block upon exit. Example: .. code-block:: python import functools def test_partial(monkeypatch): with monkeypatch.context() as m: m.setattr(functools, "partial", 3) Useful in situations where it is desired to undo some patches before the test ends, such as mocking ``stdlib`` functions that might break pytest itself if mocked (for examples of this see :issue:`3290`). N)r)clsms rcontextzMonkeyPatch.contextsD* CEE GGG FFHHHHHAFFHHHHs(>.r?rvaluer7cdSrLrDrFr?rrVr7s rsetattrzMonkeyPatch.setattr  rcdSrLrDrXs rrYzMonkeyPatch.setattrrZrTc(d}ddl}t|tr:t|tst d|}t ||\}}n$t|tst dt ||t}|r|turt|d|| |r |j |t}|j |||ft|||dS)a Set attribute value on target, memorizing the old value. For example: .. code-block:: python import os monkeypatch.setattr(os, "getcwd", lambda: "/") The code above replaces the :func:`os.getcwd` function by a ``lambda`` which always returns ``"/"``. For convenience, you can specify a string as ``target`` which will be interpreted as a dotted import path, with the last part being the attribute name: .. code-block:: python monkeypatch.setattr("os.getcwd", lambda: "/") Raises :class:`AttributeError` if the attribute does not exist, unless ``raising`` is set to False. **Where to patch** ``monkeypatch.setattr`` works by (temporarily) changing the object that a name points to with another one. There can be many names pointing to any individual object, so for patching to work you must ensure that you patch the name used by the system under test. See the section :ref:`Where to patch ` in the :mod:`unittest.mock` docs for a complete explanation, which is meant for :func:`unittest.mock.patch` but applies to ``monkeypatch.setattr`` as well. TrNzcuse setattr(target, name, value) or setattr(target, value) with target being a dotted import stringz|use setattr(target, name, value) with name being a string or setattr(target, value) with target being a dotted import stringz has no attribute )inspectr:rBr&r;r@r#notsetr$isclass__dict__getrMappendrY)rFr?rrVr7__tracebackhide__r]oldvals rrYzMonkeyPatch.setattrs8T! eV $ $ fc** $ E,VW==LD&&dC(( $ v..  Jv'' F!H!H!H!HII I ??6 " " 7_((v66F fdF3444e$$$$$rcd}ddl}t|tr7t|tst dt ||\}}t ||s|rt|dSt||t}| |r |j |t}|j |||ft||dS)aKDelete attribute ``name`` from ``target``. If no ``name`` is specified and ``target`` is a string it will be interpreted as a dotted import path with the last part being the attribute name. Raises AttributeError it the attribute does not exist, unless ``raising`` is set to False. TrNzUuse delattr(target, name) or delattr(target) with target being a dotted import string)r]r:rBr&r;r@hasattrr$r#r^r_r`rarMrbdelattr)rFr?rr7rcr]rds rrgzMonkeyPatch.delattrs! dF # # >fc** $ -VW==LD&vt$$ " +$T*** + +VT622Fv&& ;,,T6:: M &$!7 8 8 8 FD ! ! ! ! !rdicc||j||||tf|||<dS)z'Set dictionary entry ``name`` to value.N)rNrbrar^)rFrhrrVs rsetitemzMonkeyPatch.setitem&s: c4v)>)>?@@@D rc||vr|rt|dS|j||||tf||=dS)zDelete ``name`` from dict. Raises ``KeyError`` if it doesn't exist, unless ``raising`` is set to False. N)KeyErrorrNrbrar^)rFrhrr7s rdelitemzMonkeyPatch.delitem,sc s?? %tnn$ % % M #tSWWT6-B-B!C D D DD rprependc vt|tsZtjt d||t |jdt|}|r&|tj vr||ztj |z}| tj ||dS)zSet environment variable ``name`` to ``value``. If ``prepend`` is a character, read the current environment variable value and prepend the ``value`` adjoined with the ``prepend`` character. zvValue of environment variable {name} type should be str, but got {value!r} (type: {type}); converted to str implicitly)rrVr3) stacklevelN) r:r&warningswarnrr2r3r4osenvironrj)rFrrVrns rsetenvzMonkeyPatch.setenv:s%%%  MLLRF!T%[[5IMSMM     JJE  7trz))GObj&66E RZu-----rcNtj}||||dS)zDelete ``name`` from the environment. Raises ``KeyError`` if it does not exist, unless ``raising`` is set to False. )r7N)rtrurm)rFrr7rus rdelenvzMonkeyPatch.delenvPs) -/J WdG 44444rc|jtjdd|_tjdt |dtjvrddlm}|t |ddlm }|dS)z:Prepend ``path`` to ``sys.path`` list of import locations.Nr pkg_resources)fixup_namespace_packages)invalidate_caches) rPsyspathinsertr&modulesrzr{ importlibr|)rFr~r{r|s rsyspath_prependzMonkeyPatch.syspath_prependYs   $ # D  3t99%%% ck ) ) > > > > > > $ $SYY / / / 0/////rr~zos.PathLike[str]cl|jtj|_tj|dS)zChange the current working directory to the specified path. :param path: The path to change into. N)rOrtgetcwdchdir)rFr~s rrzMonkeyPatch.chdirrs* 9  DI rct|jD]1\}}}|turt|||!t ||2g|jdd<t|jD])\}}}|tur ||=#t $rY wxYw|||<*g|jdd<|j|jtj dd<d|_|j "tj |j d|_ dSdS)aUndo previous changes. This call consumes the undo stack. Calling it a second time has no effect unless you do more monkeypatching after the undo call. There is generally no need to call `undo()`, since it is called automatically during tear-down. .. note:: The same `monkeypatch` fixture is used across a single test function invocation. If `monkeypatch` is used both by the test function itself and one of the test fixtures, calling `undo()` will undo all of the changes made in both functions. Prefer to use :meth:`context() ` instead. N) reversedrMr^rYrgrNrlrPr}r~rOrtr)rFr/rrV dictionarykeys rrzMonkeyPatch.undo|s+$!) 7 7 # # CuF""T5))))T"""" aaa&.t}&=&= ( ( "JU"3D#( 3 aaa   (+CHQQQK $D  9 HTY   DIII ! s7A;; BB)rN)..).)TrL)r4rHrI__doc__rQ classmethodrrrUr r&objectrBboolrYr^r rgrrrrjrmr rvrxrrrrDrrrrus  6666  ";<^[2              X               X  F%F%c6k"F%FCK F% F%  F%  F%F%F%F%V$* $"$"fck"$"CK $" $"  $"$"$"$"L71a4=!  71a4=  D D    ..3.s.Xc].d....,5535555552%%7 78T))))))rr)&rrtrer}rr contextlibrtypingrrrrrr r r r r _pytest.compatr_pytest.fixturesr_pytest.warning_typesrcompileRE_IMPORT_ERROR_NAMErrrr&rr.r'rr@rBr^rrDrrrs// %%%%%%!!!!!! $$$$$$//////!rz";<< GCLL GCLL Y89 :#&8 6  3 6    3%V :L oooooooooor