3 \@sdZdaddZddZiZiZGdddeZGdd d ZGd d d Z Gd d d Z ddZ ddZ ddZ ddddZddZddZddZddZGd d!d!ZGd"d#d#Zddd$d%d&Zd]d'd(Zd)d*d+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8ZGd9d:d:ZGd;d<d<ZGd=d>d>Z d?d@Z!dAdBZ"d^dCdDZ#dEdFZ$dGZ%e%dHZ&dIdJZ'e(Z)dKdLZ*d_dNdOZ+d)dPdQdRZ,dSdTZ-ddfdMfdUdVZ.dWdXZ/dYdZZ0d[d\Z1dS)`aSCore implementation of import. This module is NOT meant to be directly imported! It has been designed such that it can be bootstrapped into Python as the implementation of import. As such it requires the injection of specific modules and attributes in order to work. One should use importlib as the public-facing version of this module. NcCs<x(dD] }t||rt||t||qW|jj|jdS)z/Simple substitute for functools.update_wrapper. __module____name__ __qualname____doc__N)rrrr)hasattrsetattrgetattr__dict__update)newoldreplacer ,/usr/lib64/python3.6/importlib/_bootstrap.py_wraps  rcCs tt|S)N)typesys)namer r r _new_module#src@s eZdZdS)_DeadlockErrorN)rrrr r r rr0src@s8eZdZdZddZddZddZdd Zd d Zd S) _ModuleLockzA recursive lock implementation which is able to detect deadlocks (e.g. thread 1 trying to take locks A then B, and thread 2 trying to take locks B then A). cCs0tj|_tj|_||_d|_d|_d|_dS)N)_thread allocate_locklockwakeuprownercountwaiters)selfrr r r__init__:s   z_ModuleLock.__init__cCs@tj}|j}x,tj|}|dkr&dS|j}||krdSqWdS)NFT)r get_identr _blocking_onget)rmetidrr r r has_deadlockBs z_ModuleLock.has_deadlockcCstj}|t|<zx|j`|jdks0|j|krH||_|jd7_dS|jr\td||jj drv|j d7_ WdQRX|jj |jj qWWdt|=XdS)z Acquire the module lock. If a potential deadlock is detected, a _DeadlockError is raised. Otherwise, the lock is always acquired and True is returned. rTzdeadlock detected by %rFN) rr r!rrrr%rracquirerrelease)rr$r r rr'Ns    z_ModuleLock.acquirec Csztj}|jb|j|kr"td|jdks0t|jd8_|jdkrld|_|jrl|jd8_|jj WdQRXdS)Nzcannot release un-acquired lockrr&) rr rr RuntimeErrorrAssertionErrorrrr()rr$r r rr(gs  z_ModuleLock.releasecCsdj|jt|S)Nz_ModuleLock({!r}) at {})formatrid)rr r r__repr__tsz_ModuleLock.__repr__N) rrrrrr%r'r(r-r r r rr4s   rc@s0eZdZdZddZddZddZdd Zd S) _DummyModuleLockzVA simple _ModuleLock equivalent for Python builds without multi-threading support.cCs||_d|_dS)Nr)rr)rrr r rr|sz_DummyModuleLock.__init__cCs|jd7_dS)Nr&T)r)rr r rr'sz_DummyModuleLock.acquirecCs$|jdkrtd|jd8_dS)Nrzcannot release un-acquired lockr&)rr))rr r rr(s z_DummyModuleLock.releasecCsdj|jt|S)Nz_DummyModuleLock({!r}) at {})r+rr,)rr r rr-sz_DummyModuleLock.__repr__N)rrrrrr'r(r-r r r rr.xs r.c@s$eZdZddZddZddZdS)_ModuleLockManagercCs||_d|_dS)N)_name_lock)rrr r rrsz_ModuleLockManager.__init__cCst|j|_|jjdS)N)_get_module_lockr0r1r')rr r r __enter__s z_ModuleLockManager.__enter__cOs|jjdS)N)r1r()rargskwargsr r r__exit__sz_ModuleLockManager.__exit__N)rrrrr3r6r r r rr/sr/cCstjzjyt|}Wntk r0d}YnX|dkrptdkrLt|}nt|}|fdd}tj||t|<Wdtj X|S)zGet or create the module lock for a given module name. Acquire/release internally the global import lock to protect _module_locks.Nc Ss0tjztj||krt|=WdtjXdS)N)_imp acquire_lock _module_locksr" release_lock)refrr r rcbs  z_get_module_lock..cb) r7r8r9KeyErrorrr.r_weakrefr;r:)rrr<r r rr2s    r2c Cs6t|}y |jWntk r(Yn X|jdS)zAcquires then releases the module lock for a given module name. This is used to ensure a module is completely initialized, in the event it is being imported by another thread. N)r2r'rr()rrr r r_lock_unlock_modules  r?cOs |||S)a.remove_importlib_frames in import.c will always remove sequences of importlib frames that end with a call to this function Use it instead of a normal call in places where including the importlib frames introduces unwanted noise into the traceback (e.g. when executing module code) r )fr4kwdsr r r_call_with_frames_removedsrBr&) verbositycGs6tjj|kr2|jdsd|}t|j|tjddS)z=Print the message to stderr if -v/PYTHONVERBOSE is turned on.#import z# )fileN)rDrE)rflagsverbose startswithprintr+stderr)messagerCr4r r r_verbose_messages  rMcsfdd}t||S)z1Decorator to verify the named module is built-in.cs&|tjkrtdj||d||S)Nz{!r} is not a built-in module)r)rbuiltin_module_names ImportErrorr+)rfullname)fxnr r_requires_builtin_wrappers  z4_requires_builtin.._requires_builtin_wrapper)r)rQrRr )rQr_requires_builtins  rScsfdd}t||S)z/Decorator to verify the named module is frozen.cs&tj|stdj||d||S)Nz{!r} is not a frozen module)r)r7 is_frozenrOr+)rrP)rQr r_requires_frozen_wrappers  z2_requires_frozen.._requires_frozen_wrapper)r)rQrUr )rQr_requires_frozens  rVcCs>t||}|tjkr2tj|}t||tj|St|SdS)zLoad the specified module into sys.modules and return it. This method is deprecated. Use loader.exec_module instead. N)spec_from_loaderrmodules_exec_load)rrPspecmoduler r r_load_module_shims      r]c#Cst|dd}t|dr6y |j|Stk r4YnXy |j}Wntk rTYnX|dk rft|Sy |j}Wntk rd}YnXy |j}Wn2tk r|dkrdj |Sdj ||SYnXdj ||SdS)N __loader__ module_repr?z zz) rrr_ Exception__spec__AttributeError_module_repr_from_specr__file__r+)r\loaderr[rfilenamer r r _module_repr s.        rhc@s$eZdZddZddZddZdS)_installed_safelycCs||_|j|_dS)N)_modulerb_spec)rr\r r rr3sz_installed_safely.__init__cCsd|j_|jtj|jj<dS)NT)rk _initializingrjrrXr)rr r rr37sz_installed_safely.__enter__cGsbzR|j}tdd|Dr@ytj|j=WqPtk r<YqPXntd|j|jWdd|j_XdS)Ncss|]}|dk VqdS)Nr ).0argr r r Asz-_installed_safely.__exit__..zimport {!r} # {!r}F) rkanyrrXrr=rMrfrl)rr4r[r r rr6>sz_installed_safely.__exit__N)rrrrr3r6r r r rri1sric@sreZdZdZddddddZddZdd Zed d Zej d d Zed dZ eddZ e j ddZ dS) ModuleSpecaThe specification for a module, used for loading. A module's spec is the source for information about the module. For data associated with the module, including source, use the spec's loader. `name` is the absolute name of the module. `loader` is the loader to use when loading the module. `parent` is the name of the package the module is in. The parent is derived from the name. `is_package` determines if the module is considered a package or not. On modules this is reflected by the `__path__` attribute. `origin` is the specific location used by the loader from which to load the module, if that information is available. When filename is set, origin will match. `has_location` indicates that a spec's "origin" reflects a location. When this is True, `__file__` attribute of the module is set. `cached` is the location of the cached bytecode file, if any. It corresponds to the `__cached__` attribute. `submodule_search_locations` is the sequence of path entries to search when importing submodules. If set, is_package should be True--and False otherwise. Packages are simply modules that (may) have submodules. If a spec has a non-None value in `submodule_search_locations`, the import system will consider modules loaded from the spec as packages. Only finders (see importlib.abc.MetaPathFinder and importlib.abc.PathEntryFinder) should modify ModuleSpec instances. N)origin loader_state is_packagecCs6||_||_||_||_|r gnd|_d|_d|_dS)NF)rrfrrrssubmodule_search_locations _set_fileattr_cached)rrrfrrrsrtr r rrqszModuleSpec.__init__cCsfdj|jdj|jg}|jdk r4|jdj|j|jdk rP|jdj|jdj|jjdj|S)Nz name={!r}z loader={!r}z origin={!r}zsubmodule_search_locations={}z{}({})z, ) r+rrfrrappendru __class__rjoin)rr4r r rr-}s    zModuleSpec.__repr__c Csf|j}yF|j|jkoL|j|jkoL|j|jkoL||jkoL|j|jkoL|j|jkStk r`dSXdS)NF)rurrfrrcached has_locationrc)rothersmslr r r__eq__s      zModuleSpec.__eq__cCs:|jdkr4|jdk r4|jr4tdkr&ttj|j|_|jS)N)rwrrrv_bootstrap_externalNotImplementedError _get_cached)rr r rr{s  zModuleSpec.cachedcCs ||_dS)N)rw)rr{r r rr{scCs$|jdkr|jjddS|jSdS)z The name of the module's parent.N.r)rur rpartition)rr r rparents zModuleSpec.parentcCs|jS)N)rv)rr r rr|szModuleSpec.has_locationcCst||_dS)N)boolrv)rvaluer r rr|s) rrrrrr-rpropertyr{setterrr|r r r rrqLs#     rq)rrrtcCst|drJtdkrttj}|dkr0|||dS|r8gnd}||||dS|dkrt|dry|j|}Wqtk rd}YqXnd}t||||dS)z5Return a module spec based on various loader methods. get_filenameN)rf)rfrurtF)rrrt)rrrspec_from_file_locationrtrOrq)rrfrrrtrsearchr r rrWs"     rWc5Cs8y |j}Wntk rYnX|dk r,|S|j}|dkrZy |j}Wntk rXYnXy |j}Wntk r|d}YnX|dkr|dkry |j}Wqtk rd}YqXn|}y |j}Wntk rd}YnXyt|j}Wntk rd}YnXt |||d}|dkr"dnd|_ ||_ ||_ |S)N)rrFT) rbrcrr^re_ORIGIN __cached__list__path__rqrvr{ru)r\rfrrr[rlocationr{rur r r_spec_from_modulesH         rF)overridec;Cs|st|dddkr6y |j|_Wntk r4YnX|sJt|dddkr|j}|dkr|jdk rtdkrnttj}|j |}|j|_ y ||_ Wntk rYnX|st|dddkry |j |_ Wntk rYnXy ||_Wntk rYnX|st|dddkrD|jdk rDy |j|_Wntk rBYnX|jr|sdt|dddkry |j|_Wntk rYnX|st|dddkr|jdk ry |j|_Wntk rYnX|S)Nrr^ __package__rrer)rrrrcrfrurr_NamespaceLoader__new___pathr^rrrbrr|rrrer{r)r[r\rrfrr r r_init_module_attrss\           rcCsRd}t|jdr|jj|}nt|jdr2td|dkrDt|j}t|||S)z+Create a module based on the provided spec.N create_module exec_modulezBloaders that define exec_module() must also define create_module())rrfrrOrrr)r[r\r r rmodule_from_spec4s    rcCsj|jdkrdn|j}|jdkrB|jdkr2dj|Sdj||jSn$|jrVdj||jSdj|j|jSdS)z&Return the repr to use for the module.Nr`z zzz)rrrrfr+r|)r[rr r rrdEs   rdc Cs|j}t|tjj||k r6dj|}t||d|jdkrj|jdkrXtd|jdt ||dd|St ||ddt |jds|jj |n |jj |WdQRXtj|S)zFExecute the spec's specified module in an existing module's namespace.zmodule {!r} not in sys.modules)rNzmissing loaderT)rr) rr/rrXr"r+rOrfrurr load_moduler)r[r\rmsgr r rrYVs      rYcCs|jj|jtj|j}t|dddkrLy |j|_Wntk rJYnXt|dddkry(|j|_ t |ds|jj dd|_ Wntk rYnXt|dddkry ||_ Wntk rYnX|S)Nr^rrrrrb) rfrrrrXrr^rcrrrrrb)r[r\r r r_load_backward_compatiblens(    rc Csv|jdk rt|jdst|St|}t|6|jdkrT|jdkr`td|jdn |jj|WdQRXt j |jS)Nrzmissing loader)r) rfrrrrirurOrrrrX)r[r\r r r_load_unlockeds     rc Cst|j t|SQRXdS)zReturn a new module object, loaded by the spec's loader. The module is not added to its parent. If a module is already in sys.modules, that existing module gets clobbered. N)r/rr)r[r r rrZs rZc@seZdZdZeddZedddZedddZed d Z ed d Z ee d dZ ee ddZ ee ddZeeZdS)BuiltinImporterzMeta path import for built-in modules. All methods are either class or static methods to avoid the need to instantiate the class. cCs dj|jS)zsReturn repr for the module. The method is deprecated. The import machinery does the job itself. z)r+r)r\r r rr_szBuiltinImporter.module_reprNcCs,|dk r dStj|r$t||ddSdSdS)Nzbuilt-in)rr)r7 is_builtinrW)clsrPpathtargetr r r find_specs  zBuiltinImporter.find_speccCs|j||}|dk r|jSdS)zFind the built-in module. If 'path' is ever specified then the search is considered a failure. This method is deprecated. Use find_spec() instead. N)rrf)rrPrr[r r r find_modules zBuiltinImporter.find_modulecCs.|jtjkr"tdj|j|jdttj|S)zCreate a built-in modulez{!r} is not a built-in module)r)rrrNrOr+rBr7create_builtin)rr[r r rrs   zBuiltinImporter.create_modulecCsttj|dS)zExec a built-in moduleN)rBr7 exec_builtin)rr\r r rrszBuiltinImporter.exec_modulecCsdS)z9Return None as built-in modules do not have code objects.Nr )rrPr r rget_codeszBuiltinImporter.get_codecCsdS)z8Return None as built-in modules do not have source code.Nr )rrPr r r get_sourceszBuiltinImporter.get_sourcecCsdS)z4Return False as built-in modules are never packages.Fr )rrPr r rrtszBuiltinImporter.is_package)NN)N)rrrr staticmethodr_ classmethodrrrrrSrrrtr]rr r r rrs    rc@seZdZdZeddZedddZedddZed d Z ed d Z ed dZ ee ddZ ee ddZee ddZdS)FrozenImporterzMeta path import for frozen modules. All methods are either class or static methods to avoid the need to instantiate the class. cCs dj|jS)zsReturn repr for the module. The method is deprecated. The import machinery does the job itself. z)r+r)mr r rr_szFrozenImporter.module_reprNcCs tj|rt||ddSdSdS)Nfrozen)rr)r7rTrW)rrPrrr r rr s zFrozenImporter.find_speccCstj|r|SdS)z]Find a frozen module. This method is deprecated. Use find_spec() instead. N)r7rT)rrPrr r rrszFrozenImporter.find_modulecCsdS)z*Use default semantics for module creation.Nr )rr[r r rrszFrozenImporter.create_modulecCs@|jj}tj|s$tdj||dttj|}t||j dS)Nz{!r} is not a frozen module)r) rbrr7rTrOr+rBget_frozen_objectexecr)r\rcoder r rr s    zFrozenImporter.exec_modulecCs t||S)z_Load a frozen module. This method is deprecated. Use exec_module() instead. )r])rrPr r rr)szFrozenImporter.load_modulecCs tj|S)z-Return the code object for the frozen module.)r7r)rrPr r rr2szFrozenImporter.get_codecCsdS)z6Return None as frozen modules do not have source code.Nr )rrPr r rr8szFrozenImporter.get_sourcecCs tj|S)z.Return True if the frozen module is a package.)r7is_frozen_package)rrPr r rrt>szFrozenImporter.is_package)NN)N)rrrrrr_rrrrrrrVrrrtr r r rrs     rc@s eZdZdZddZddZdS)_ImportLockContextz$Context manager for the import lock.cCs tjdS)zAcquire the import lock.N)r7r8)rr r rr3Ksz_ImportLockContext.__enter__cCs tjdS)z= 0z__package__ not set to a stringz6attempted relative import with no known parent packagezEmpty module nameN) isinstancestr TypeErrorr+rrrO)rrrr r r _sanity_checks   rzNo module named z{!r}c Csd}|jdd}|r|tjkr*t|||tjkr>tj|Stj|}y |j}Wn2tk rtdj||}t||ddYnXt ||}|dkrttj||dnt |}|rtj|}t ||jdd||S)Nrrz; {!r} is not a package)r) rrrXrBrrc_ERR_MSGr+ModuleNotFoundErrorrrr)rimport_rr parent_modulerr[r\r r r_find_and_load_unlockeds*        rc Cs^t|&tjj|t}|tkr*t||SWdQRX|dkrRdj|}t||dt||S)zFind and load the module.Nz(import of {} halted; None in sys.modules)r) r/rrXr"_NEEDS_LOADINGrr+rr?)rrr\rLr r r_find_and_loads  rrcCs*t||||dkr t|||}t|tS)a2Import and return the module based on its name, the package the call is being made from, and the level adjustment. This function represents the greatest common denominator of functionality between import_module and __import__. This includes setting __package__ if the loader did not. r)rrr _gcd_import)rrrr r rrs  r) recursivecCst|drx|D]}t|tsN|r.|jd}nd}td|dt|jq|dkrz| rt|drt||j|dd qt||sd j|j|}yt ||Wqt k r}z&|j |krt j j|td k rwWYd d }~XqXqW|S) zFigure out what __import__ should return. The import_ parameter is a callable which takes the name of module to import. It is required to decouple the function from assuming importlib's import implementation is desired. rz.__all__z ``from list''zItem in z must be str, not *__all__T)rz{}.{}N)rrrrrr_handle_fromlistrr+rBrrrrXr"r)r\fromlistrrxwhere from_nameexcr r rrs*        rcCs|jd}|jd}|dk rR|dk rN||jkrNtjd|d|jdtdd|S|dk r`|jStjd tdd|d }d |kr|jd d }|S)zCalculate what __package__ should be. __package__ is not guaranteed to be defined or could be set to None to represent that its proper value is unknown. rrbNz __package__ != __spec__.parent (z != )) stacklevelzYcan't resolve package from __spec__ or __package__, falling back on __name__ and __path__rrrr)r"rrrrr)globalsrr[r r r_calc___package__ s    rc Cs|dkrt|}n$|dk r|ni}t|}t|||}|s|dkrTt|jddS|s\|St|t|jdd}tj|jdt|j|Sn t||tSdS)aImport a module. The 'globals' argument is used to infer where the import is occurring from to handle relative imports. The 'locals' argument is ignored. The 'fromlist' argument specifies what should exist as attributes on the module being imported (e.g. ``from module import ``). The 'level' argument represents the package location to import from in a relative import (e.g. ``from ..pkg import mod`` would have a 'level' of 2). rNr)rr partitionrrrXrr) rrlocalsrrr\globals_rcut_offr r r __import__&s    rcCs&tj|}|dkrtd|t|S)Nzno built-in module named )rrrOr)rr[r r r_builtin_from_nameIs  rc Cs|a|att}xVtjjD]H\}}t||r|tjkr>t}ntj|rt }nqt ||}t ||qWtjt }x6dD].}|tjkrt |} n tj|} t||| qxWy t d} Wntk rd} YnXt|d| t d} t|d| dS)zSetup importlib by importing needed built-in modules and injecting them into the global namespace. As sys is needed for sys.modules access and _imp is needed to load built-in modules, those two modules must be explicitly passed in. rrNr>)r)r7rrrXitemsrrNrrTrrrrrrrO) sys_module _imp_module module_typerr\rfr[ self_module builtin_namebuiltin_module thread_moduleweakref_moduler r r_setupPs2             rcCsBt||tjjttjjtddl}|a|jtj t dS)z2Install importlib as the implementation of import.rN) rrrrxrr_frozen_importlib_externalr_installrXr)rrrr r rrs    r)NN)N)Nr)2rrrrr9r!r)rrr.r/r2r?rBrMrSrVr]rhrirqrWrrrrdrYrrrZrrrrrrr_ERR_MSG_PREFIXrrobjectrrrrrrrrrr r r rs^D%   $e -<IM   / &#/