[.dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlmZddlmZmZddlmZddlmZmZddlmZmZmZddlmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*dd l m+Z+dd l,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2dd l,m3Z3dd l,m4Z4dd l,m5Z5ddl,m6Z6ddl7m8Z8m9Z9m:Z:m;Z;mZ>ddl?m@Z@mAZAddlBmCZCmDZDddlEmFZFe jGdkrdndZHe jGdkrdndZIGddZJGddZK d;ddddde;jLdddd d e$ed!e$e!e%eMefd"e$e!ed#e$e eMefd$eNd%eNd&e;d'eNd(eNd)eNd*efd+ZOdZPddddde;jLddfd,ed-e+d.ed/ed!e$e!e%eMefd"e$e!ed#e$e eMefd$eNd%eNd&e;d'eNd)eNd*e+fd0ZQGd1d2ZRGd3d4e jSeRZSGd5d6ZTGd7d8ZUGd9d:eeZVdS).cleanupls(%%eWf6I JJJJJr>rA _rmtreec.tjj|i|Sr7)tempfile_shutilrmtree)argskwargss r<rLz/TempfilePatcher.start_patching.._rmtreeus#+2DCFCCCr>r/cL||p tjdSr7rD)rGrFr;s r<closez-TempfilePatcher.start_patching..close}s&%%eV-@ryAAAAAr>r7) r8sys version_inforEnamerN_TemporaryFileCloserrIhasattrrLr9platformrT)r;rIrLrTs` r<start_patchingzTempfilePatcher.start_patchinggs  , F  w & &(*4 K K K K K K%-$A$ID !4;H ) 17**wx/K/K*DDD(0'7$#*   +***\W $ $ B B B B B B%-$A$GD !27H ) / / / % $r>c|jdStjdkr|jtj_n|jtj_d|_|j|jt_d|_dt_ dS)Nr@) r8rUrVrNrXrTrIr9rLtempdirr:s r< stop_patchingzTempfilePatcher.stop_patchingso  ( F  g % %262GH ) / /484IH ) 1 $   +#3H #'D r>N__name__ __module__ __qualname____doc__r=r[r^r>r<r5r5`sG66$$$8888      r>r5c$eZdZdZdZdZdZdS)LineCachePatcherz5Handles linecache patching for newer Python versions.c"d|_d|_dSr7)linecache_updatecachelinecache_checkcacher:s r<r=zLineCachePatcher.__init__s%)"$(!!!r>cjdSdfd }dfd }tjdks tjdkrLtjdkr>tj_|t_tj_|t_dSdSdS)Nc~t5|cdddS#1swxYwYdS)zZCalls the original linecache.checkcache making sure no fake OS calls are used.N)r)ri)filenamer;s r< checkcachez3LineCachePatcher.start_patching..checkcaches!"" ; ;00:: ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;s 266ct5tr'|ds"||cdddSgcdddS#1swxYwYdS)z[Calls the original linecache.updatecache making sure no fake OS calls are used.zpytest.exe\__main__.pyN)r)r+lowerendswithrh)rlmodule_globalsr;s r< updatecachez4LineCachePatcher.start_patching..updatecaches!""  PX^^%5%5%>%>-&&P 55hOO                             sAA/!A//A36A3rJr/r@r7)rhrUrVrZ linecacherrrmri)r;rmrrs` r<r[zLineCachePatcher.start_patchings  % 1 F ; ; ; ; ; ;         ' '|w&& G++*3)>D &$/I !(1(cx|jdS|jt_|jt_d|_d|_dSr7)rhrsrrrirmr:s r<r^zLineCachePatcher.stop_patchings<  % - F $ : #8 %)"$(!!!r>Nr_rdr>r<rfrfsH??))) . . .D)))))r>rfTF additional_skip_namesmodules_to_reloadmodules_to_patchallow_root_useruse_known_patchespatch_open_codepatch_default_args use_cacheuse_dynamic_patch_funcrvrwrxryrzr{r|r}r~returnc  dtdtf f d } |rRt|stdt|drt |j|_| |S| S)a=Convenience decorator to use patcher with additional parameters in a test function. Usage:: @patchfs def test_my_function(fake_fs): fake_fs.create_file('foo') @patchfs(allow_root_user=False) def test_with_patcher_args(fs): os.makedirs('foo/bar') frc Z tj f d}|S)Nc  t   5}t|}||j|i|cdddS#1swxYwYdS)Nru)Patcherlistappendfs) rQrRprvryrrxrwr|r{r}r~rzs r<wrappedz.patchfs..wrap_patchfs..wrappeds&;"3!1 /"3 /#5#"3    *Dzz AD!!!q$)&)) * * * * * * * * * * * * * * * * * *s1AAA) functoolswraps) rrrvryrxrwr|r{r}r~rzs ` r< wrap_patchfszpatchfs..wrap_patchfssf    * * * * * * * * * * * * *   * r>zYDecorator argument is not a function. Did you mean `@patchfs(additional_skip_names=...)`? patchings)r callable TypeErrorrYlenr nr_patches) rrvrwrxryrzr{r|r}r~rs ````````` r<patchfsrs6X( # F  5+ & & 4"5?33E |E""" r>loadertestsignoremodulec ptjdu} | s"t|||||| | | d t_tjJtjt|} |t j|| tjjtjj|S)aYLoad the doctest tests for the specified module into unittest. Args: loader, tests, ignore : arguments passed in from `load_tests()` module: module under test remaining args: see :py:class:`TestCase` for an explanation File `example_test.py` in the pyfakefs release provides a usage example. NT) rvrwrxryrzr{r|r~ is_doc_test)globssetUptearDown) r DOC_PATCHER replace_globsvarsaddTestsdoctest DocTestSuiterr)rrrrrvrwrxryrzr{r|r~ has_patcherrs r< load_doctestsrs,%T1K   %"7/-+/+1/       * * *   - -d6ll ; ;E NN %+(1     Lr>cVeZdZUdZdZeeeee fe d<dZ eee e d<dZ ee ee fe d<edZedefdZdddd d ejd d d f deeeee fdeee dee ee fd ed ed ededededdfdZedddd d ejd d d f deeeee fdeee dee ee fd ed ed ededededdfdZedZddZddZdS) TestCaseMixinaTest case mixin that automatically replaces file-system related modules by fake implementations. Attributes: additional_skip_names: names of modules where no module replacement shall be performed, in addition to the names in :py:attr:`fake_filesystem_unittest.Patcher.SKIPNAMES`. Instead of the module names, the modules themselves may be used. modules_to_reload: A list of modules that need to be reloaded to be patched dynamically; may be needed if the module imports file system modules under an alias .. caution:: Reloading modules may have unwanted side effects. modules_to_patch: A dictionary of fake modules mapped to the fully qualified patched module names. Can be used to add patching of modules not provided by `pyfakefs`. If you specify some of these attributes here, and you have DocTests, consider also specifying the same arguments to :py:func:`load_doctests`. Example usage in derived test classes:: from unittest import TestCase from fake_filesystem_unittest import TestCaseMixin class MyTestCase(TestCase, TestCaseMixin): def __init__(self, methodName='runTest'): super(MyTestCase, self).__init__( methodName=methodName, additional_skip_names=['posixpath']) import sut class AnotherTestCase(TestCase, TestCaseMixin): def __init__(self, methodName='runTest'): super(MyTestCase, self).__init__( methodName=methodName, modules_to_reload=[sut]) Nrvrwrxc`t|dr|jp tjStjS)N_patcher)rYrrPATCHERr:s r<patcherzTestCaseMixin.patcher_s+ 4 $ $ 4=3GO 3r>rc@tt|jjSr7)rr(rrr:s r<rzTestCaseMixin.fsesNDLO444r>TFryrzr{r|r}r~c (tjdS||j}||j}||j}t|||||||||  |_|jtt| |jj dS)a6Bind the file-related modules to the :py:class:`pyfakefs` fake file system instead of the real file system. Also bind the fake `open()` function. Invoke this at the beginning of the `setUp()` method in your unit test class. For the arguments, see the `TestCaseMixin` attribute description. If any of the arguments is not `None`, it overwrites the settings for the current test case. Settings the arguments here may be a more convenient way to adapt the setting than overwriting `__init__()`. Nru) rrrvrwrxrrrTestCase addCleanupr) r;rvrwrxryrzr{r|r}r~s r< setUpPyfakefszTestCaseMixin.setUpPyfakefsis0 ? & F ($($> !  $ $ 6   ##4 "7/-+/+1/       Xt'' (>?????r>c tjdkrtdtjdS||j}||j}||j}t|||||||||  t_tjtt| tjj dS)aCSimilar to :py:func:`setUpPyfakefs`, but as a class method that can be used in `setUpClass` instead of in `setUp`. The fake filesystem will live in all test methods in the test class and can be used in the usual way. Note that using both :py:func:`setUpClassPyfakefs` and :py:func:`setUpPyfakefs` in the same class will not work correctly. .. note:: This method is only available from Python 3.8 onwards. .. note:: If using `pytest` as testrunner, you need at least pytest 6.2 for this method to work. )rAzIsetUpClassPyfakefs is only available in Python versions starting from 3.8Nru) rUrVNotImplementedErrorrrrvrwrxrrraddClassCleanupr) clsrvrwrxryrzr{r|r}r~s r<setUpClassPyfakefsz TestCaseMixin.setUpClassPyfakefss0  f $ $%4  ? & F ($'$= !  $ # 5   #"3 !"7/-+/+1/      Xs++GO,DEEEEEr>c@tjrtjjSdS)zConvenience class method for accessing the fake filesystem. For use inside `setUpClass`, after :py:func:`setUpClassPyfakefs` has been called. N)rrrrs r<fake_fszTestCaseMixin.fake_fss ? &?% %tr>c8|jdS)Pause the patching of the file system modules until `resume` is called. After that call, all file system calls are executed in the real file system. Calling pause() twice is silently ignored. N)rpauser:s r<rzTestCaseMixin.pauses r>c8|jdSzResume the patching of the file system modules if `pause` has been called before. After that call, all file system calls are executed in the fake file system. Does nothing if patching is not paused. N)rresumer:s r<rzTestCaseMixin.resumes r>rN)r`rarbrcrvrrrstrr __annotations__rwrxrpropertyrr(rr'OFFboolr classmethodrrrrrdr>r<rr3s%%NEI8DsJ)?$@AHHH48xZ 018888<htCO45<<< X 5N555X5 IM8<<@ $"&%.]#("&.@.@'U3 ?-C(DE.@$D$45.@#4Z#89 .@  .@  .@#.@!.@.@ .@ .@.@.@.@`IM8<<@ $"&%.]#("&4F4F'U3 ?-C(DE4F$D$454F#4Z#89 4F  4F  4F#4F!4F4F 4F 4F4F4F[4Fl[r>rc eZdZdZ d dedeeeeefdeeedee eefffd Z d d Z xZ S) rzTest case class that automatically replaces file-system related modules by fake implementations. Inherits :py:class:`TestCaseMixin`. The arguments are explained in :py:class:`TestCaseMixin`. runTestN methodNamervrwrxctt|||_||_||_dS)zCreates the test class instance and the patcher used to stub out file system related modules. Args: methodName: The name of the test method (same as in unittest.TestCase) N)superr=rvrwrx)r;rrvrwrx __class__s r<r=zTestCase.__init__s; $$$%:"!2 0r>rcdS)zgThis method is deprecated and exists only for backward compatibility. It does nothing. Nrdr:s r<tearDownPyfakefszTestCase.tearDownPyfakefssr>)rNNNr) r`rarbrcrrrrr rr=r __classcell__rs@r<rrs$HL8<<@ 111 (U3 ?-C(DE1$D$45 1 #4Z#89 111111(        r>rc eZdZUdZ deeeeee e e e e eeeeejhZe jdkr3ddlZddlZeeeenKddlZddlZddlZeeeeeegddgdZeZeee d<iZ!e"e#ee$ee#ffe d <iZ%e"e$e#e#e#feefe d <gZ&e'e$e(e)e*d e+ffe d <iZ,e"e#ee$ee#ffe d <devs Jde jdvZ-eZ.ee#e d<eZ/ee#e d<eZ0ee#e d<dZ1dZ2e3de d<dZ4e3de d<dZ5dZ6fdZ7ddddde8j9ddddf de3e'e:e#efde3e'ede3e"e#efde;de;de8de;de;d e;d!e;d"dfd#ZdJd%Z?d&e#d'e*e#ge;ffd(Z@dJd)ZAdJd*ZBdKd+ZCd,e3eDeEd-e3eEd.e3eFd"dfd/ZGd0ed&e#d1e'e#d"e;fd2ZHd3e(d"e;fd4ZId5e(d"eJe$e(e)e+ffd6ZKd7eLe#e(fd"dfd8ZMdJd9ZNdJd:ZOdLd;e+d"dfd<ZPdJd=ZQd>ZRdJd?ZSdJd@ZTdJdAZUdBe"e#e+fd"e"e#e+ffdCZVdLd;e+fdDZWdMdJdEZXeYdFZZdJdGZ[dJdHZ\dJdIZ]xZ^S)Nrat Instantiate a stub creator to bind and un-bind the file-related modules to the :py:mod:`pyfakefs` fake modules. The arguments are explained in :py:class:`TestCaseMixin`. :py:class:`Patcher` is used in :py:class:`TestCaseMixin`. :py:class:`Patcher` also works as a context manager for other tests:: with Patcher(): doStuff() Nr/r)_pydevd_pydevd_pydev__jb_)r_jb_runner_toolsCACHED_MODULES FS_MODULES FS_FUNCTIONS. FS_DEFARGSSKIPPED_FS_MODULESz3sys.modules contains 'None' values; must skip them.)r/cygwin SKIPNAMESPATCHED_MODULE_NAMESADDITIONAL_SKIP_NAMESFrrc|ddr4|j&t||_|jS|j&t||_|jS)NrF)getrr__new__r)rrQrRrs r<rzPatcher.__new__csg ::mU + + #&"'''//#"6"6? " ; ''//#..CK{r>Trvrwrxryrzr{r|r}r~rrc | |_| r|jdkrdSnA|jdkr6|s|s|s|r|r|tjks|s|r| st jddS|stdtd|j |_ t|_ ||_t|_t#|_|&d|D} |j | i|_i|_i|_|g|_||j|||_||_| |_i|_d|_d|_i|_ i|_!d|_"d|_#d|_$d|_%d|_&d|_'|rddl(m)} m*} m+}m,}|pi}|| |j| |j||j||4|-D]\}}||j|<t]|}nt]}| }|rU||j/kr||j0_/d}|j |j1kr|j |j0_1d}||j2kr||j0_2d}|r|3i|_4|5dS) aV Args: additional_skip_names: names of modules where no module replacement shall be performed, in addition to the names in :py:attr:`fake_filesystem_unittest.Patcher.SKIPNAMES`. Instead of the module names, the modules themselves may be used. modules_to_reload: A list of modules that need to be reloaded to be patched dynamically; may be needed if the module imports file system modules under an alias .. caution:: Reloading modules may have unwanted side effects. modules_to_patch: A dictionary of fake modules mapped to the fully qualified patched module names. Can be used to add patching of modules not provided by `pyfakefs`. allow_root_user: If `True` (default), if the test is run as root user, the user in the fake file system is also considered a root user, otherwise it is always considered a regular user. use_known_patches: If `True` (the default), some patches for commonly used packages are applied which make them usable with pyfakefs. patch_open_code: If `True`, `io.open_code` is patched. The default is not to patch it, as it mostly is used to load compiled modules that are not in the fake file system. patch_default_args: If `True`, default arguments are checked for file system functions, which are patched. This check is expansive, so it is off by default. use_cache: If `True` (default), patched and non-patched modules are cached between tests for performance reasons. As this is a new feature, this argument allows to turn it off in case it causes any problems. use_dynamic_patch: If `True`, dynamic patching after setup is used (for example for modules loaded locally inside of functions). Can be switched off if it causes unwanted side effects. rNz{Nested fake filesystem invocation using custom arguments - using the existing fake filesystem, discarding custom arguments!cg|]E}tj|rtt|jntt |FSrd)inspectismodulerr r`r).0ms r< z$Patcher.__init__..sS180@0C0CUZ##,,cSTr>TF)get_modules_to_patchget_classes_to_patchget_fake_module_classesget_cleanup_handlers)6r DOC_REF_COUNT REF_COUNTr'rwarningswarnr$r%rcopy skip_namesopen original_openr{r5tempfile_patcherrflinecache_patcherupdate_fake_module_classes_unfaked_module_classes_class_modules_init_fake_module_classesrwextendr|r}r~cleanup_handlers_stubsr fake_modulesunfaked_modules_isStale _dyn_patcher _patching_pausedhas_copy_file_range has_copy_filepyfakefs.patched_packagesrrrritemssetrrrPATCH_DEFAULT_ARGS clear_cache_fake_module_functions_init_fake_module_functions)r;rvrwrxryrzr{r|r}r~rrrrrrrW fake_modulepatched_module_namesr s r<r=zPatcher.__init__ls^'  !A%%& ^a  % $ $ '   )   #im33%4 4)4  W F  AJJJ AJJJ.--//!. / 1 1!1!3!3 ,.J O " ": . . .46!79$46 &&(((46  (  " ) )*; < < <"4"!2BD48 ,0,./1 6: #( "  A             052   # #$8$8$:$: ; ; ;   & &';';'='= > > >  % , ,-D-D-F-F G G G  ! ( ()=)=)?)? @ @ @  '%5%;%;%=%= > >!k2=)$//#&'7#8#8 #&55 #m  ##t'@@@6J3" $"<<<7;4" !T%<<<4F1"        79# ((*****r>cdt|_i|_i|_g|_i|_dS)zClear the module cache.N)r rrrrrrs r<clear_fs_cachezPatcher.clear_fs_caches4!UU!#r>c8|jdS)z5Clear the module cache (convenience instance method).N)rrr:s r<r zPatcher.clear_cache s %%'''''r>rWhandlerc||j|<dS)aQRegister a handler for cleaning up a module after it had been loaded by the dynamic patcher. This allows to handle modules that cannot be reloaded without unwanted side effects. Args: name: The fully qualified module name. handler: A callable that may do any module cleanup, or do nothing and return `True` in case reloading shall be prevented. Returns: `True` if no further cleanup/reload shall occur after the handler is executed, `False` if the cleanup/reload shall still happen. N)r)r;rWrs r<register_cleanup_handlerz Patcher.register_cleanup_handlers'.d###r>cdtfd}dtfd}tjtjt j|d|_tj dddkr ||jd<tstj dkrt j |jd <tj d krtj|jd <ntj|jd <d g|jd<tj dddkr |jddt&j|jd <tj dddkrt&j|jd<t,rHt.j|jd<|jddt&j|jd<t2rt.j|jd<||jd<t&j|jd<dS)Nrc.tj|dSNT) from_patcher)r"FakePathlibModulers r<fake_pathlib_modulez>Patcher._init_fake_module_classes..fake_pathlib_module)s1"4HHH Hr>c.tj|dSr)r"FakePathlibPathModulers r<fake_path_modulez;Patcher._init_fake_module_classes..fake_path_module,s5btLLL Lr>)rEshutiliopathlibrJzpathlib._localr@_ior/r0fcntlr#Pathr,r-)r(r FakeOsModuler FakeShutilModuler FakeIoModulerrUrVr* FakeIoModule2rZr FakeNtModulerFakeFcntlModulerrr"RealPathlibModulerr,r!FakePathlib2Moduler-FakeScanDirModuleRealPathlibPathModule)r;rr s r<rz!Patcher._init_fake_module_classes!s IN I I I I M M M M M&,=&* % % !  BQB 7 * *;ND %&6 7  Ec&'11/6/DD %e , <7 " ".7.DD %d + +1@1PD %g . (1kF#  BQB 7 * *   ' . ./? @ @ @2>2P$Y/  BQB 7 * *.  ()9 :  V#6  %j 1   ' . .z : : :7C7UD ( 4  Y3F3XD %i 0,<!&)/;/Q$V,,,r>cp|jD]\}}t|dr|j}t j|rp|D][}t |||f}||j|i|<|dkr#||j|it<\tj }|D]Z}t ||tf}||j|id<||j|it<[dS)NdirrE genericpath) rr rYr3r isfunctiongetattrr  setdefault OS_MODULErFakePathModule PATH_MODULE)r;mod_namer module_dirfct_name module_attrs r<rz#Patcher._init_fake_module_functionsXs_&*%>%D%D%F%F , , !Hk{E** ,(_ %j11 ,$/OO$5$5,,'.{H'E'Ex&P (3>>xLL$$t++!,!7BB8RPP ) &4 #))  H";99;GK  ' 2 28R @ @ O  ' 2 28R @ @ M M   r>c.||S)zContext manager for usage outside of fake_filesystem_unittest.TestCase. Ensure that all patched modules are removed in case of an unhandled exception. )rr:s r< __enter__zPatcher.__enter__us  r>exc_typeexc_valexc_tbc.|dSr7)r)r;rArBrCs r<__exit__zPatcher.__exit__~s r>mod module_namesc tj|r |j|vp5tj|o!|j|j|gvS#t$rYdSwxYwNF)rrr`isclassrarr Exception)r;rFrWrGs r< _is_fs_modulezPatcher._is_fs_modules  %%1LL0H?3''HNd&9&=&=dB&G&GG      55  sAA A#"A#fctc tj|stj|o&|j|jvo|j|j|jvS#t $rYdSwxYwrI)rr5 isbuiltinr`r rarK)r;rMs r<_is_fs_functionzPatcher._is_fs_functions} #C((BG,=c,B,BPLD$??PNd&A#,&OO     55 sAA AAitemc#K |jrJtj|r6t|jD]!\}}||r|||fV"n#t $rYnwxYw tj|r{tj|tjD]\}tt|d}|jr6t|jD]!\}}||r|||fV"[dSdS#t $rYdSwxYw)zFind default arguments that are file-system functions to be patched in top-level functions and members of top-level classes.) predicaterN) __defaults__rr5 enumeraterPrKrJ getmembersrr )r;rQidrrs r< _def_valueszPatcher._def_valuessf    )W%7%=%= )%d&788))DAq++A..)"Aqj(((    D  t$$ .!+DGct tjtgz tj}|D]Q\} jr j vstj s,nH#t$r;jr1 j j  n#t$rYnwxYwYqwxYw jvpt# fdjD} j} fd|D}|ra|D]K\}}j j|t- |jfLn|D]K\}}j j|t- |jfLfd|D}|D]Q\}}j j||j|jft- Rjr|jrj j  SdS)zFind and cache all modules that import file system modules. Later, `setUp()` will stub these with the fake file system modules. cDg|]}|jSrd) startswithr`)rsnrs r<rz)Patcher._find_modules..s'JJJBv//JJJr>cJi|]\}}|||| Srd)rL)rrWrFrGr;s r< z)Patcher._find_modules..sGD#%%c4>>cr>cFi|]\}}|||Srd)rP)rrWrMr;s r<rcz)Patcher._find_modules..sC"+$t?S?STW?X?X#r>N)rrkeysr:rUmodulesrr r}rrrrKraddr SKIPMODULESanyr__dict__rr7r r`rrrar|r]) r;r rWskippedrZrfrF functionsrMrrGs ` @@r< _find_moduleszPatcher._find_moduless' D5::<<== M    ""((**!7 :7 :LD& N$"555"+F3366    >599&AAAA$  00CJJJJ$/JJJ55G"?//117799L!-G  8!(ID#N5@@suuMMQQ. ")ID#N-88suuEEII./; "+!2!2""ID#N/::s|S^ doctesterc.|jr(|jxjdz c_|jjdkrdSn'|jxjdz c_|jjdkrdSt j5t jd|dddn #1swxYwY||| |j |_ | |j t_|j t_dS)zBind the file-related modules to the :py:mod:`pyfakefs` fake modules real ones. Also bind the fake `file()` and `open()` functions. rNr)rrrrrcatch_warningsfilterwarningsrmrsrrr[rrsrtokenize _builtin_openr;rts r<rz Patcher.setUpsL    N ( (A - ( (~+a//0 N $ $ ) $ $~'!++  $ & & ! !  #H - - -     ! ! ! ! ! ! ! ! ! ! ! ! ! ! !   "00AAIO + !%!3s*)BB#&B#c|jsId|_d|_|j|||||jt||_ tj d|j |jD]7}tj|j|urt%|8|js<|j tj ddSdSdSNTFr)rrrr[ patch_modulespatch_functionspatch_defaults_set_glob_os_functionsrDynamicPatcherrrU meta_pathinsertrwrfrr`rr~rIpop)r;rs r<r[zPatcher.start_patching2s@~ %!DN DL  " 1 1 3 3 3     " " "    ! ! !  ' ' ) ) )  ! 0 0 2 2 2 .t 4 4D  M D$5 6 6 60 # #;??6?33v==6NNN) %!))+++ !!!$$$$$) % %$ % %r>c@tjdkrXtj}t t j|_tjdkrt t j|_tjdkr%t t jj |_ dSdS)NrJ)rA) rUrVglob_StringGlobber staticmethodrElstatr-rqlexists)r;globbers r<rzPatcher._set_glob_os_functionsIsu  w & &)G(22GM'))".rz":":  w & &*27?;;GOOO ' &r>c|jJ|jD]h\\}}}}|j||\}}|j|}|||j}|D]} |j| ||idSr7)rrr r r__get__r smart_set) r;rWft_nameft_modrfmethodr;rattrrs r<r~zPatcher.patch_functionsSs{&&&040A0G0G0I0I : : , #T7FW#:7CFK FH+H5K>>[2D" : : %%fdD9999 :  : :r>cg}|jD](\}}|tjvr||)t |}|jJ|jD]\}}|D]\}} |r|j |s(|j |||j |n0||j vr'|j |||j |{#t$rYwxYw|jD]=\}}|D]5\}}||j vr'|j |||j |6>dSr7)RUNTIME_SKIPMODULESr rUrfrtuplerrr`r`rrrrKr) r;skip_prefix_listrt_skip_moduleprefixes skip_prefixesrWrfrrs r<r}zPatcher.patch_modules^s(,(@(F(F(H(H 2 2 $NH,, ''111.// {&&&!_2244  MD' '    (X0J0J%11X --fdD>[2DL#///!#"233 + +188 ''---- ''****$\22C   3 3r>globs_c|}|jr||jD]$}||vr|j||j||<%|Sr7)rrrsrr)r;rrrWs r<rzPatcher.replace_globssf  =  MMOOO- G GDu}}=d7=dgFFd  r>c8|jr(|jxjdzc_|jjdkrdSn'|jxjdzc_|jjdkrdS|t |jrd|j_dSd|j_dS)z8Clear the fake filesystem bindings created by `setUp()`.rrN)rrrrr^r&rrrzs r<rzPatcher.tearDowns    N ( (A - ( (~+a//0 N $ $ ) $ $~'!++    *)-DN & & &%)DN " " "r>c|jrd|_d|_||_|jr|j||jr?|jr8|jtj d|j |j |dSdSr|)rrrrrpunset_defaultsr~rrIrUrrrr^rr)r; temporarys r<r^zPatcher.stop_patchings > * DM"DN$DL{ . ++---    ! ! !% %$*; %!))+++ !!!$$$  ! / / 1 1 1  " 0 0 2 2 2  ' ' ) ) ) ) ) * *r>c|jSr7)rr:s r< is_patchingzPatcher.is_patchings ~r>c |jD]z\}}}g}ttt|jD]6\}}||kr||!||7t ||_{dSr7)rrUrrrTrr)r;rMrrrrWrXs r<rzPatcher.unset_defaultss O 3 3LCbL!$uc.>"?"?@@ + +188 ''++++ ''****$\22C   3 3r>c2|ddS)rT)rN)r^r:s r<rz Patcher.pauses! T*****r>c@|jr|dSdSr)rr[r:s r<rzPatcher.resumes0 < "    ! ! ! ! ! " "r>r)rrr7)F)_r`rarbrcrr rrrrrrUrsrxrEr"r%r4rqrhrZr0r2rgr1r3r&rr rrr rrrrrrrrr intr r r IS_WINDOWSrrrr rrrrrrr'rrrr=rrr rrrr@r BaseExceptionr rErLrPrrYrr]rmrsrr[rr~r}rrrr^rrrrrrrs@r<rr s        !K$ |w     """ 433#H'*ceeNC O+++9;JS#eJO4556;;;@BL$uS#s]+S_<=BBBEGJU<hsCx.@@ABGGGACS#eJO&<"==>CCC ;    U   !44J#%%Is3x&)SUU#c(***&)cee3s8+++#'GXi ''''+K)$+++IMIM8<<@ $"&%.]#("&!V+V+'U3 ?-C(DEV+$D$45V+#4Z#89 V+  V+  V+#V+!V+V+ V+V+ V+V+V+V+p$$$[$((((.S.8SE4K;P.... 5R5R5R5Rn:4 ./-('   %(8:>:>:>:@$44s4d44446%%%%.<<< : : : :TTTT43333 DcNtCH~**#****$ * * * * *X3333++++""""""""r>rcLeZdZdZdeeeeffdZdefdZ de ddfdZ dS) PausezSimple context manager that allows to pause/resume patching the filesystem. Patching is paused in the context manager, and resumed after going out of its scope. callerct|ttfr|jJ|j|_dSt|t r ||_dSt d)zInitializes the context manager with the fake filesystem. Args: caller: either the FakeFilesystem instance, the Patcher instance or the pyfakefs test case. NzInvalid argument - should be of type "fake_filesystem_unittest.Patcher", "fake_filesystem_unittest.TestCase" or "fake_filesystem.FakeFilesystem") isinstancerrr_fsr( ValueError)r;rs r<r=zPause.__init__sj fw 6 7 7 9((('-yDHHH  / / DHHH6 r>rcB|j|jSr7)rrr:s r<r@zPause.__enter__s xr>rQNc8|jdSr7)rr)r;rQs r<rEzPause.__exit__s r>) r`rarbrcrrrr(r=r@r rErdr>r<rrs uWm^%KL(>cdr>rc eZdZdZdeddfdZddZdedefdZ dedefd Z dd ed e e e eefd e ede efd Zd edefdZdS)rzA file loader that replaces file system related modules by their fake implementation if they are loaded after calling `setUpPyfakefs()`. Implements the protocol needed for import hooks. rrNc||_i|_|jj|_t |_|j|_|jD]L}||r5|tjvr'tj||j|<tj|=M|j D]\}}|tj|<dSr7) r sysmodulesrrfr _loaded_module_namesr needs_patchrUr )r;rrWrs r<r=zDynamicPatcher.__init__s }1 .1ee! ' 8L & &D%% &$#+*=*=(+ D(9%K% L..00 ' 'LD& &CK   ' 'r>cn|jD]}|j|tj|<|jjD]$}|jtjvrt |%d|jjD}|jD]A}|tjvr1||vr-||jvr|j||r4tj|=BdS)Ncg|] }|j Srd)r`)rrs r<rz*DynamicPatcher.cleanup..s'! ! ! &FO! ! ! r>) rrUrfrrwr`rrr)r; module_namerreloaded_module_namesrWs r<rIzDynamicPatcher.cleanup s? D DK'+{'CCK $ $m5  F#+--v! ! *.-*I! ! !  - & &Ds{""t3H'H'H40005PT5J45PQU5V5V0K%  & &r>rWc||jvr|j|dS|tjvr.t tj||j|urdSdS)z;Checks if the module with the given name shall be replaced.FT)rfrrgrUtyperrs r<rzDynamicPatcher.needs_patchsb t| # #  % ) )$ / / /5 3;  4 D(9#:#:dl4>P#P#P5tr>c|jj}||d|j}tjD]}|||}|dz}||r||cS||d}||r||cSdS)zChecks if the module with the given name is a module existing in the fake filesystem and returns its path in this case. N.z.pyz __init__.py) rrreplacepath_separatorrUrq joinpathsexists absnormpath)r;rWr base_pathrq module_pathpy_module_path init_paths r<fake_module_pathzDynamicPatcher.fake_module_path&s]  > S"*;<>.99999LLmDD 99Y''5>>)444445rr>fullnamerqtargetc2||rt||S|jjtjkrW||}|r@t||}|r.t|}|tj |<t||SdS)zModule finder.N) rrrr{r'rrrrrUrf)r;rrqrrspecrs r< find_speczDynamicPatcher.find_spec8s   H % % .h-- - = (IM 9 9//99K 6.xEE6-d33F,2CK)%h555tr>cP|j|tj|<|j|S)z/Replaces the module by its fake implementation.)rfrU)r;rs r< load_modulezDynamicPatcher.load_moduleLs" $ X 6 H|H%%r>rr7)r`rarbrcrr=rIrrrrrrrbytesr rrrrdr>r<rrs ''D''''"&&&&"SS,(, xeSj 123$  *  (&C&J&&&&&&r>rr7)Wrcr%rrr4rrr"rsrErUrNrxunittestr importlibr importlib.abcrrimportlib.machineryrimportlib.utilrrtypesr r r typingr r rrrrrrrrrrrrpyfakefsrrrrrrr r!r"r#pyfakefs.fake_filesystemr$r%r&r'r(pyfakefs.fake_osr)pyfakefs.helpersr*r+pyfakefs.legacy_packagesr,r-pyfakefs.mox3_stuboutr.rZr8r:r5rfrrrrDOCTEST_PATCHERrrrrrrrdr>r<rs  .    00000000******DDDDDDDD9999999999WWWWWWWWWWWWWWWW++++++((((((!!!!!!!!!!!!-,,,,,,,,,,,,,66666666333333LG++DD ,'11hh{ / / / / / / / / d/)/)/)/)/)/)/)/)f!%9EI488< "!*$"999 H 9$DsJ)?$@A9 Z 01 9 tCO45 9  9999999999xEI488< "!*$"-- - - -  - $DsJ)?$@A -  Z 01 -tCO45----------`vvvvvvvvr     x -   BC "C "C "C "C "C "C "C "LDZ&Z&Z&Z&Z&^VZ&Z&Z&Z&Z&r>