e f!S@sdZdZddZddZdd Zd d Zd d ZddZddZddZ ddZ dddZ ddZ ddZ ee jZGdddZiZiZGdd d eZGd!d"d"ZGd#d$d$ZGd%d&d&Zd'd(Zd)d*Zd+d,Zd-jd.d/d0Zejed/Z d1Z!d2gZ"d3gZ#d4gZ$d5d6d7Z%d8d9Z&d:d;Z'd<d=Z(d>d?d@dAZ)dBdCZ*dDdEZ+dFdGZ,dHdIZ-dJdKZ.d5d5d5dLdMZ/d5d5d5dNdOZ0dPdPdQdRZ1dSdTZ2dUdVZ3GdWdXdXZ4GdYdZdZZ5d[d5d\d5d]d^Z6e7Z8d5d_d5d`e8dadbZ9d5d5dcddZ:GdedfdfZ;d5dgdhZ<GdidjdjZ=GdkdldlZ>GdmdndnZ?GdodpdpZ@Gdqdrdre@ZAGdsdtdtZBGdudvdveBeAZCGdwdxdxeBe@ZDgZEGdydzdzZFGd{d|d|ZGGd}d~d~ZHGdddZIGdddZJGdddZKddZLddZMd5ddZNddZOdZPePdZQddZRddZSd5dPddZTddZUddZVddZWd5d5fdPddZXddZYddZZddZ[d5S)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. wincygwindarwincCs1tjjtr!dd}n dd}|S)NcSs dtjkS)z5True if filenames must be checked case-insensitively.s PYTHONCASEOK)_osenvironrr9/opt/alt/python34/lib64/python3.4/importlib/_bootstrap.py _relax_casesz%_make_relax_case.._relax_casecSsdS)z5True if filenames must be checked case-insensitively.Frrrrrr"s)sysplatform startswith_CASE_INSENSITIVE_PLATFORMS)rrrr_make_relax_cases r cCst|d@jddS)z*Convert a 32-bit integer to little-endian.llittle)intto_bytes)xrrr_w_long(srcCstj|dS)z/Convert 4 bytes in little-endian to an integer.r)r from_bytes) int_bytesrrr_r_long-srcGstjdd|DS)zReplacement for os.path.join().cSs%g|]}|r|jtqSr)rstrippath_separators).0partrrr 4s z_path_join..)path_sepjoin) path_partsrrr _path_join2srcCsttdkr4|jt\}}}||fSxEt|D]7}|tkrA|j|dd\}}||fSqAWd|fS)z Replacement for os.path.split().maxsplit)lenr rpartitionrreversedrsplit)pathfront_tailrrrr _path_split8s  r+cCs tj|S)z~Stat the path. Made a separate function to make it easier to override in experiments (e.g. cache stat results). )rstat)r'rrr _path_statDsr-c Cs:yt|}Wntk r(dSYnX|jd@|kS)z1Test whether the path is the specified mode type.Fi)r-OSErrorst_mode)r'mode stat_inforrr_path_is_mode_typeNs   r2cCs t|dS)zReplacement for os.path.isfile.i)r2)r'rrr _path_isfileWsr3cCs"|stj}nt|dS)zReplacement for os.path.isdir.i@)rgetcwdr2)r'rrr _path_isdir\sr5icCsdj|t|}tj|tjtjBtjB|d@}y<tj|d}|j |WdQXtj ||Wn;t k rytj |Wnt k rYnXYnXdS)zBest-effort function to write data to a path atomically. Be prepared to handle a FileExistsError if concurrent writing of the temporary file is attempted.z{}.{}iwbN) formatidropenO_EXCLO_CREATO_WRONLY_ioFileIOwritereplacer.unlink)r'datar0path_tmpfdfilerrr _write_atomiccs !  rFcCs_xEddddgD]1}t||rt||t||qqW|jj|jdS)z/Simple substitute for functools.update_wrapper. __module____name__ __qualname____doc__N)hasattrsetattrgetattr__dict__update)newoldr@rrr_wrapys rRcCstt|S)N)typer )namerrr _new_modulesrUc@s:eZdZdZddZddZddZdS) _ManageReloadz?Manages the possible clean-up of sys.modules for load_module().cCs ||_dS)N)_name)selfrTrrr__init__sz_ManageReload.__init__cCs|jtjk|_dS)N)rWr modules _is_reload)rXrrr __enter__sz_ManageReload.__enter__c GsPtdd|DrL|j rLytj|j=WqLtk rHYqLXndS)Ncss|]}|dk VqdS)Nr)rargrrr sz)_ManageReload.__exit__..)anyr[r rZrWKeyError)rXargsrrr__exit__s # z_ManageReload.__exit__N)rHrGrIrJrYr\rbrrrrrVs   rVc@seZdZdS)_DeadlockErrorN)rHrGrIrrrrrcs rcc@sReZdZdZddZddZddZdd 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). cCsFtj|_tj|_||_d|_d|_d|_dS)N)_thread allocate_locklockwakeuprTownercountwaiters)rXrTrrrrYs    z_ModuleLock.__init__cCsXtj}|j}x<tj|}|dkr7dS|j}||krdSqWdS)NFT)rf get_identrj _blocking_onget)rXmetidrhrrr has_deadlocks     z_ModuleLock.has_deadlockcCstj}|t|.cb)r~r`rfrwrd_weakrefref)rTrhrr)rTrrzs    rzc CsGt|}tjy|jWntk r8Yn X|jdS)aRelease the global import lock, and acquires then release 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. Should only be called with the import lock taken.N)rzr{r|rsrcrt)rTrhrrr_lock_unlock_module%s   rcOs |||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)frakwdsrrr_call_with_frames_removed9sri rs __pycache__z.pyz.pycz.pyoNc Cs|dkrtjj n|}|r.t}nt}t|\}}|jd\}}}tjj} | dkrt dndj |r|n||| |dg} t |t | S)aGiven the path to a .py file, return the path to its .pyc/.pyo file. The .py file does not need to exist; this simply returns the path to the .pyc/.pyo file calculated as if the .py file were imported. The extension will be .pyc unless sys.flags.optimize is non-zero, then it will be .pyo. If debug_override is not None, then it must be a boolean and is used in place of sys.flags.optimize. If sys.implementation.cache_tag is None then NotImplementedError is raised. N.z$sys.implementation.cache_tag is Noner"re) r flagsoptimizeDEBUG_BYTECODE_SUFFIXESOPTIMIZED_BYTECODE_SUFFIXESr+r$implementation cache_tagNotImplementedErrorrr_PYCACHE) r'debug_overridedebugsuffixesheadr*basesepresttagfilenamerrrcache_from_sources    +rcCstjjdkr!tdnt|\}}t|\}}|tkrltdjt|n|jddkrtdj|n|j dd}t ||t dS)ayGiven the path to a .pyc./.pyo file, return the path to its .py file. The .pyc/.pyo file does not need to exist; this simply returns the path to the .py file calculated to correspond to the .pyc/.pyo file. If path does not conform to PEP 3147 format, ValueError will be raised. If sys.implementation.cache_tag is None then NotImplementedError is raised. Nz$sys.implementation.cache_tag is Nonez%{} not bottom-level directory in {!r}rrzexpected only 2 dots in {!r}re) r rrrr+r ValueErrorr7rk partitionrSOURCE_SUFFIXES)r'rpycache_filenamepycache base_filenamerrrsource_from_caches    rc Cst|dkrdS|jd\}}}| sQ|jdddkrU|Syt|}Wn(ttfk r|dd }YnXt|r|S|S) zConvert a bytecode file path to a source path (if possible). This function exists purely for backwards-compatibility for PyImport_ExecCodeModuleWithFilenames() in the C API. reNrr pyr)r#r$lowerrrrr3) bytecode_pathrr) extension source_pathrrr_get_sourcefiles#rc Cs<yt|j}Wntk r-d}YnX|dO}|S)z3Calculate the mode permissions for a bytecode file.i)r-r/r.)r'r0rrr _calc_modes    r verbosityr cGsQtjj|krM|jds.d|}nt|j|dtjndS)z=Print the message to stderr if -v/PYTHONVERBOSE is turned on.#import z# rEN)rr)r rverboser printr7stderr)messagerrarrr_verbose_messages rcs&dfdd}t||S)zDecorator to verify that the module being requested matches the one the loader can handle. The first argument (self) must define _name which the second argument is compared against. If the comparison fails then ImportError is raised. NcsS|dkr|j}n(|j|kr@td|d|n||||S)Nzloader cannot handle %srT)rT ImportError)rXrTrar})methodrr_check_name_wrappers   z(_check_name.._check_name_wrapper)rR)rrr)rr _check_names rcs#fdd}t||S)z1Decorator to verify the named module is built-in.cs:|tjkr-tdj|d|n||S)Nz{!r} is not a built-in modulerT)r builtin_module_namesrr7)rXfullname)fxnrr_requires_builtin_wrapper"s z4_requires_builtin.._requires_builtin_wrapper)rR)rrr)rr_requires_builtin s rcs#fdd}t||S)z/Decorator to verify the named module is frozen.cs:tj|s-tdj|d|n||S)Nz{!r} is not a frozen modulerT)r{ is_frozenrr7)rXr)rrr_requires_frozen_wrapper-s z2_requires_frozen.._requires_frozen_wrapper)rR)rrr)rr_requires_frozen+s rcCsW|j|\}}|dkrSt|rSd}tj|j|dtn|S)zTry to find a loader for the specified module by delegating to self.find_loader(). This method is deprecated in favor of finder.find_spec(). Nz,Not importing directory {}: missing __init__re) find_loaderr# _warningswarnr7 ImportWarning)rXrloaderportionsmsgrrr_find_module_shim6s  rcCs]t||}t|}|tjkrOtj|}|j|tj|S|jSdS)zLoad the specified module into sys.modules and return it. This method is deprecated. Use loader.exec_module instead. N)spec_from_loader _SpecMethodsr rZexecload)rXrspecmethodsmodulerrr_load_module_shimGs    rc Csi}|dk r||d||dr'r zbad magic number in {!r}: {!r}z{}z+reached EOF while reading timestamp in {!r}z0reached EOF while reading size of source in {!r}mtimezbytecode is stale for {!r}sizel) MAGIC_NUMBERr7rrr#EOFErrorrr`r) rB source_statsrTr' exc_detailsmagic raw_timestampraw_sizer source_mtime source_sizerrr_validate_bytecode_headerWsL             rcCsstj|}t|trNtd||dk rJtj||n|Stdj|d|d|dS)zzz) rMrKr Exception__spec__AttributeErrorrrH__file__r7)rrrrTrrrr _module_reprs.           rc@s4eZdZddZddZddZdS)_installed_safelycCs||_|j|_dS)N)_moduler_spec)rXrrrrrYs z_installed_safely.__init__cCs&d|j_|jtj|jj.zimport {!r} # {!r}F) rr_r rZrTr`rrr)rXrarrrrrbs  z_installed_safely.__exit__N)rHrGrIrYr\rbrrrrrs   rc@seZdZdZddddddddZdd Zd d Zed d Zej dd ZeddZ 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. originN loader_state is_packagecCsO||_||_||_||_|r0gnd|_d|_d|_dS)NF)rTrrrsubmodule_search_locations _set_fileattr_cached)rXrTrrrrrrrrYs     zModuleSpec.__init__cCsdj|jdj|jg}|jdk rO|jdj|jn|jdk rz|jdj|jndj|jjdj|S)Nz name={!r}z loader={!r}z origin={!r}zsubmodule_search_locations={}z{}({})z, ) r7rTrrappendr __class__rHr)rXrarrrrv"s zModuleSpec.__repr__c Cs|j}yk|j|jkor|j|jkor|j|jkor||jkor|j|jkor|j|jkSWntk rdSYnXdS)NF)rrTrrcached has_locationr)rXothersmslrrr__eq__,s  zModuleSpec.__eq__c Cs|jdkr|jdk r|jr|j}|jttrpyt||_Wqtk rlYqXq|jttr||_qqn|jS)N) rrrendswithtuplerrrBYTECODE_SUFFIXES)rXrrrrr 8s  zModuleSpec.cachedcCs ||_dS)N)r)rXr rrrr FscCs.|jdkr#|jjddS|jSdS)z The name of the module's parent.Nrre)rrTr$)rXrrrparentJszModuleSpec.parentcCs|jS)N)r)rXrrrr RszModuleSpec.has_locationcCst||_dS)N)boolr)rXvaluerrrr Vs) rHrGrIrJrYrvrpropertyr setterrr rrrrrs # rrrcCst|drS|dkr+t|d|S|r7gnd}t|d|d|S|dkrt|dry|j|}Wqtk rd}YqXqd}nt||d|d|S)z5Return a module spec based on various loader methods. get_filenameNrrrFr)rKspec_from_file_locationrrr)rTrrrsearchrrrr[s    rrrc Csn|dkrOd}t|drOy|j|}WqLtk rHYqLXqOnt||d|}d|_|dkrxOtD]=\}}|jt|r|||}||_PqqWdSn|t kr&t|dr/y|j |}Wntk r Yq#X|r#g|_ q#q/n ||_ |j gkrj|rjt |d}|j j |qjn|S)a=Return a module spec based on a file location. To indicate that the module is a package, set submodule_search_locations to a list of directory paths. An empty list is sufficient, though its not otherwise useful to the import system. The loader must take a spec as its only __init__() arg. Nz rrTrre)rKrrrr_get_supported_file_loadersrrr _POPULATErrr+r) rTlocationrrr loader_classrrdirnamerrrrts<         rc5Cs|y |j}Wntk r!YnX|dk r2|S|j}|dkrly |j}Wqltk rhYqlXny |j}Wntk rd}YnX|dkr|dkry |j}Wqtk rd}YqXq|}ny |j}Wntk rd}YnXyt|j}Wntk r5d}YnXt ||d|}|dkr]dnd|_ ||_ ||_ |S)NrFT) rrrHrr_ORIGIN __cached__list__path__rrr r)rrrrrTrr rrrr_spec_from_modulesH                      r#c@seZdZdZddZddZdddd d d Zd d ZddZddZ ddZ ddZ ddZ dS)rzMConvenience wrapper around spec objects to provide spec-specific methods.cCs ||_dS)N)r)rXrrrrrYsz_SpecMethods.__init__cCs|j}|jdkrdn|j}|jdkrh|jdkrRdj|Sdj||jSn2|jrdj||jSdj|j|jSdS)z&Return the repr to use for the module.Nrz zzz)rrTrrr7r )rXrrTrrrrs   z_SpecMethods.module_repr _overrideF _force_nameTc;CsK|j}|s-|s-t|dddkrUy|j|_WqUtk rQYqUXn|sst|dddkr|j}|dkr|jdk rtjt}|j|_ qny ||_ Wqtk rYqXn|st|dddkr#y|j |_ Wq#tk rYq#Xny ||_ Wntk rDYnX|sct|dddkr|jdk ry|j|_Wqtk rYqXqn|jrG|st|dddkry|j|_Wqtk rYqXn|s t|dddkrG|jdk rDy|j|_WqAtk r=YqAXqDqGndS)aSet the module's attributes. All missing import-related module attributes will be set. Here is how the spec attributes map onto the module: spec.name -> module.__name__ spec.loader -> module.__loader__ spec.parent -> module.__package__ spec -> module.__spec__ Optional: spec.origin -> module.__file__ (if spec.set_fileattr is true) spec.cached -> module.__cached__ (if __file__ also set) spec.submodule_search_locations -> module.__path__ (if set) rHNr __package__r"rr )rrMrTrHrrr_NamespaceLoader__new___pathrrr&rr"r rrr r )rXrr$r%rrrrrinit_module_attrssX               z_SpecMethods.init_module_attrscCse|j}t|jdr0|jj|}nd}|dkrTt|j}n|j||S)zReturn a new module to be loaded. The import-related module attributes are also set with the appropriate values from the spec. create_moduleN)rrKrr+rUrTr*)rXrrrrrcreateKs   z_SpecMethods.createcCs|jjj|dS)zDo everything necessary to execute the module. The namespace of `module` is used as the target of execution. This method uses the loader's `exec_module()` method. N)rr exec_module)rXrrrr_execbsz_SpecMethods._execc Cs|jj}tjt|tjj||k r_dj|}t |d|n|jj dkr|jj dkrt dd|jjn|j |dd|S|j |ddt |jj ds|jj j|n |j|WdQXtj|S)z3Execute the spec in an existing module's namespace.zmodule {!r} not in sys.modulesrTNzmissing loaderr$Tr-)rrTr{ acquire_lockrxr rZror7rrrr*rK load_moduler.)rXrrTrrrrrls    z_SpecMethods.execcCs|j}|jj|jtj|j}t|dddkrly|j|_Wqltk rhYqlXnt|dddkry;|j |_ t |ds|jj dd|_ nWqtk rYqXnt|dddkry ||_ Wqtk rYqXn|S)Nrr&r"rrer)rrr0rTr rZrMrrrHr&rKr$r)rXrrrrr_load_backward_compatibles*       z&_SpecMethods._load_backward_compatiblec Cs|jjdk r4t|jjds4|jSn|j}t|T|jjdkr|jjdkrtdd|jjqn |j |WdQXt j |jjS)Nr-zmissing loaderrT) rrrKr1r,rrrrTr.r rZ)rXrrrr_load_unlockeds   z_SpecMethods._load_unlockedcCs1tjt|jj|jSWdQXdS)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{r/rxrrTr2)rXrrrrs z_SpecMethods.loadN) rHrGrIrJrYrr*r,r.rr1r2rrrrrrs   P    rc Cs|jd}|jd}|sf|r6|j}qf||krTt||}qft||}n|st||d|}ny,||d<||d<||d<||d)r7rH)rrrrrszBuiltinImporter.module_reprNcCs:|dk rdStj|r2t||ddSdSdS)Nrzbuilt-in)r{ is_builtinr)clsrr'targetrrr 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)r=r)r;rr'rrrr find_modules zBuiltinImporter.find_modulec Cs;t|ttj|}WdQX||_d|_|S)zLoad a built-in module.Nr")rVrr{ init_builtinrr&)r;rrrrrr0s    zBuiltinImporter.load_modulecCsdS)z9Return None as built-in modules do not have code objects.Nr)r;rrrrget_codeszBuiltinImporter.get_codecCsdS)z8Return None as built-in modules do not have source code.Nr)r;rrrr get_sourceszBuiltinImporter.get_sourcecCsdS)z4Return False as built-in modules are never packages.Fr)r;rrrrrszBuiltinImporter.is_package)rHrGrIrJ staticmethodr classmethodr=r>rr0r@rArrrrrr9s    r9c@seZdZdZeddZeddddZedddZed d Z ed d Z ee d dZ ee ddZ ee ddZdS)FrozenImporterzMeta path import for frozen modules. All methods are either class or static methods to avoid the need to instantiate the class. cCsdj|jS)zsReturn repr for the module. The method is deprecated. The import machinery does the job itself. z)r7rH)mrrrr/szFrozenImporter.module_reprNcCs*tj|r"t||ddSdSdS)Nrfrozen)r{rr)r;rr'r<rrrr=8szFrozenImporter.find_speccCstj|r|SdS)z]Find a frozen module. This method is deprecated. Use find_spec() instead. N)r{r)r;rr'rrrr>?szFrozenImporter.find_modulecCs_|jj}tj|s9tdj|d|nttj|}t||j dS)Nz{!r} is not a frozen modulerT) rrTr{rrr7rget_frozen_objectrrN)rrTrrrrr-Hs   zFrozenImporter.exec_modulecCs t||S)z_Load a frozen module. This method is deprecated. Use exec_module() instead. )r)r;rrrrr0QszFrozenImporter.load_modulecCs tj|S)z-Return the code object for the frozen module.)r{rG)r;rrrrr@ZszFrozenImporter.get_codecCsdS)z6Return None as frozen modules do not have source code.Nr)r;rrrrrA`szFrozenImporter.get_sourcecCs tj|S)z.Return True if the frozen module is a package.)r{is_frozen_package)r;rrrrrfszFrozenImporter.is_package)rHrGrIrJrBrrCr=r>r-r0rr@rArrrrrrD&s    rDc@syeZdZdZdZdZdZeddZeddZ ed d d d Z ed d d Z d S)WindowsRegistryFinderz>Meta path finder for modules declared in the Windows registry.z;Software\Python\PythonCore\{sys_version}\Modules\{fullname}zASoftware\Python\PythonCore\{sys_version}\Modules\{fullname}\DebugFc CsCytjtj|SWn%tk r>tjtj|SYnXdS)N)_winregOpenKeyHKEY_CURRENT_USERr.HKEY_LOCAL_MACHINE)r;keyrrr_open_registryys z$WindowsRegistryFinder._open_registrycCs|jr|j}n |j}|jd|dtjdd}y.|j|}tj|d}WdQXWnt k rdSYnX|S)Nr sys_versionrr") DEBUG_BUILDREGISTRY_KEY_DEBUG REGISTRY_KEYr7r versionrOrJ QueryValuer.)r;r registry_keyrNhkeyfilepathrrr_search_registrys     z&WindowsRegistryFinder._search_registryNcCs|j|}|dkrdSyt|Wntk rEdSYnXxNtD]C\}}|jt|rPt||||d|}|SqPWdS)Nr)rYr-r.rrrr)r;rr'r<rXrrrrrrr=s    zWindowsRegistryFinder.find_speccCs-|j||}|dk r%|jSdSdS)zlFind module named in the registry. This method is deprecated. Use exec_module() instead. N)r=r)r;rr'rrrrr>s z!WindowsRegistryFinder.find_module) rHrGrIrJrSrRrQrCrOrYr=r>rrrrrIms rIc@s4eZdZdZddZddZeZdS) _LoaderBasicszSBase class of common code needed by both SourceLoader and SourcelessFileLoader.cCsXt|j|d}|jddd}|jdd}|dkoW|dkS)zConcrete implementation of InspectLoader.is_package by checking if the path returned by get_filename has a filename of '__init__.py'.r rrerrY)r+rr&r$)rXrr filename_base tail_namerrrrsz_LoaderBasics.is_packagecCsP|j|j}|dkr9tdj|jntt||jdS)zExecute the module.Nz4cannot load module {!r} when get_code() returns None)r@rHrr7rrrN)rXrrrrrr-s   z_LoaderBasics.exec_moduleN)rHrGrIrJrr-rr0rrrrrZs   rZc@sjeZdZddZddZddZddZd d Zd dd dZddZ dS) SourceLoadercCs tdS)zOptional method that returns the modification time (an int) for the specified path, where path is a str. Raises IOError when the path cannot be handled. N)IOError)rXr'rrr path_mtimeszSourceLoader.path_mtimecCsi|j|d6S)aOptional method returning a metadata dict for the specified path to by the path (str). Possible keys: - 'mtime' (mandatory) is the numeric timestamp of last source code modification; - 'size' (optional) is the size in bytes of the source code. Implementing this method allows the loader to read bytecode files. Raises IOError when the path cannot be handled. r)r_)rXr'rrr path_statss zSourceLoader.path_statscCs|j||S)zOptional method which writes data (bytes) to a file path (a str). Implementing this method allows for the writing of bytecode files. The source path is needed in order to correctly transfer permissions )set_data)rXr cache_pathrBrrr_cache_bytecodeszSourceLoader._cache_bytecodecCsdS)zOptional method which writes data (bytes) to a file path (a str). Implementing this method allows for the writing of bytecode files. Nr)rXr'rBrrrraszSourceLoader.set_datacCsi|j|}y|j|}Wn:tk r^}ztdd||WYdd}~XnXt|S)z4Concrete implementation of InspectLoader.get_source.z'source not available through get_data()rTN)rget_datar.rr)rXrr'rexcrrrrAs zSourceLoader.get_source _optimizer c Cstt||dddd|S)zReturn the code object compiled from source. The 'data' argument can be any object type that compile() supports. r dont_inheritTr)rcompile)rXrBr'rfrrrsource_to_codeszSourceLoader.source_to_codec +Cs|j|}d}yt|}Wntk r?d}YnXy|j|}Wntk rgYnXt|d}y|j|}Wntk rYnjXy"t|d|d|d|}Wnt t fk rYn-Xt d||t |d|d|d|S|j|}|j ||} t d |tj r|dk r|dk rt| |t|}y$|j|||t d |Wqtk rYqXn| S) zConcrete implementation of InspectLoader.get_code. Reading of bytecode requires path_stats to be implemented. To write bytecode, set_data must also be implemented. NrrrTr'z {} matches {}rrzcode object from {}z wrote {!r})rrrr`r^rrdr.rrrrrrir dont_write_bytecoderr#rc) rXrrrrstrB bytes_datar code_objectrrrr@sN            zSourceLoader.get_codeNr) rHrGrIr_r`rcrarArir@rrrrr]s    r]cspeZdZdZddZddZddZefdd Zed d Z d d Z S) FileLoaderzgBase file loader class which implements the loader protocol methods that require file system usage.cCs||_||_dS)zKCache the module name and the path to the file found by the finder.N)rTr')rXrr'rrrrY7s zFileLoader.__init__cCs"|j|jko!|j|jkS)N)r rN)rXr rrrr=szFileLoader.__eq__cCst|jt|jAS)N)hashrTr')rXrrr__hash__AszFileLoader.__hash__cstt|j|S)zdLoad a module from a file. This method is deprecated. Use exec_module() instead. )superrnr0)rXr)r rrr0Ds zFileLoader.load_modulecCs|jS)z:Return the path to the source file as found by the finder.)r')rXrrrrrPszFileLoader.get_filenamecCs)tj|d}|jSWdQXdS)z'Return the data from path as raw bytes.rN)r=r>read)rXr'rErrrrdUszFileLoader.get_data) rHrGrIrJrYrrprr0rrdrr)r rrn2s     rnc@s@eZdZdZddZddZdddd Zd S) r4z>Concrete implementation of SourceLoader using the file system.cCs$t|}i|jd6|jd6S)z!Return the metadata for the path.rr)r-st_mtimest_size)rXr'rkrrrr`_s zSourceFileLoader.path_statscCs"t|}|j||d|S)N_mode)rra)rXrrrBr0rrrrcds z SourceFileLoader._cache_bytecodervic Cs5t|\}}g}x6|rPt| rPt|\}}|j|qWxt|D]v}t||}ytj|Wq^tk rw^Yq^tk r}zt d||dSWYdd}~Xq^Xq^Wy!t |||t d|Wn5tk r0}zt d||WYdd}~XnXdS)zWrite bytes data to a file.zcould not create {!r}: {!r}Nz created {!r}) r+r5rr%rrmkdirFileExistsErrorr.rrF) rXr'rBrvrrrrrerrrrais& zSourceFileLoader.set_dataN)rHrGrIrJr`rcrarrrrr4[s   r4c@s.eZdZdZddZddZdS)r3z-Loader which handles sourceless file imports.cCsL|j|}|j|}t|d|d|}t|d|d|S)NrTr'r)rrdrr)rXrr'rBrlrrrr@szSourcelessFileLoader.get_codecCsdS)z'Return None as there is no source code.Nr)rXrrrrrAszSourcelessFileLoader.get_sourceN)rHrGrIrJr@rArrrrr3s  r3c@seZdZdZddZddZddZedd Zd d Z d d Z ddZ eddZ dS)ExtensionFileLoaderz]Loader for extension modules. The constructor is designed to work with FileFinder. cCs||_||_dS)N)rTr')rXrTr'rrrrYs zExtensionFileLoader.__init__cCs"|j|jko!|j|jkS)N)r rN)rXr rrrrszExtensionFileLoader.__eq__cCst|jt|jAS)N)rorTr')rXrrrrpszExtensionFileLoader.__hash__c Cst|ttj||j}WdQXtd|j|j|}|r|t|d r|t|jdg|_ n||_ |j |_ |s|j j dd|_ n|S)zLoad an extension module.Nz!extension module loaded from {!r}r"rer)rVrr{ load_dynamicr'rrrKr+r"rrHr&r$)rXrrrrrrr0s    zExtensionFileLoader.load_modulecs0t|jdtfddtDS)z1Return True if the extension module is a package.r c3s|]}d|kVqdS)rYNr)rsuffix) file_namerrr^sz1ExtensionFileLoader.is_package..)r+r'r_EXTENSION_SUFFIXES)rXrr)r|rrszExtensionFileLoader.is_packagecCsdS)z?Return None as an extension module cannot create a code object.Nr)rXrrrrr@szExtensionFileLoader.get_codecCsdS)z5Return None as extension modules have no source code.Nr)rXrrrrrAszExtensionFileLoader.get_sourcecCs|jS)z:Return the path to the source file as found by the finder.)r')rXrrrrrsz ExtensionFileLoader.get_filenameN) rHrGrIrJrYrrprr0rr@rArrrrrrys       ryc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS)_NamespacePatha&Represents a namespace package's path. It uses the module name to find its parent module, and from there it looks up the parent's __path__. When this changes, the module's own path is recomputed, using path_finder. For top-level modules, the parent module's path is sys.path.cCs4||_||_t|j|_||_dS)N)rWr)r_get_parent_path_last_parent_path _path_finder)rXrTr' path_finderrrrrYs  z_NamespacePath.__init__cCs5|jjd\}}}|dkr+dS|dfS)z>Returns a tuple of (parent-module-name, parent-path-attr-name)rr"r r'r")zsyszpath)rWr$)rXrdotrprrr_find_parent_path_namess z&_NamespacePath._find_parent_path_namescCs&|j\}}ttj||S)N)rrMr rZ)rXparent_module_namepath_attr_namerrrrsz_NamespacePath._get_parent_pathcCst|j}||jkrx|j|j|}|dk rl|jdkrl|jrl|j|_qln||_n|jS)N)rrrrrWrrr))rX parent_pathrrrr _recalculates  z_NamespacePath._recalculatecCst|jS)N)iterr)rXrrr__iter__sz_NamespacePath.__iter__cCst|jS)N)r#r)rXrrr__len__sz_NamespacePath.__len__cCsdj|jS)Nz_NamespacePath({!r}))r7r))rXrrrrvsz_NamespacePath.__repr__cCs||jkS)N)r)rXitemrrr __contains__sz_NamespacePath.__contains__cCs|jj|dS)N)r)r)rXrrrrrsz_NamespacePath.appendN) rHrGrIrJrYrrrrrrvrrrrrrr~s       r~c@sjeZdZddZeddZddZddZd d Zd d Z d dZ dS)r'cCst||||_dS)N)r~r))rXrTr'rrrrrY sz_NamespaceLoader.__init__cCsdj|jS)zsReturn repr for the module. The method is deprecated. The import machinery does the job itself. z)r7rH)r;rrrrrsz_NamespaceLoader.module_reprcCsdS)NTr)rXrrrrrsz_NamespaceLoader.is_packagecCsdS)Nr"r)rXrrrrrAsz_NamespaceLoader.get_sourcecCstdddddS)Nr"zrrgT)rh)rXrrrrr@sz_NamespaceLoader.get_codecCsdS)Nr)rXrrrrr-!sz_NamespaceLoader.exec_modulecCstd|jt||S)zbLoad a namespace module. This method is deprecated. Use exec_module() instead. z&namespace module loaded with path {!r})rr)r)rXrrrrr0$sz_NamespaceLoader.load_moduleN) rHrGrIrYrCrrrAr@r-r0rrrrr' s      r'c@seZdZdZeddZeddZeddZedd Zed d d Z ed d d dZ ed ddZ d S) PathFinderz>Meta path finder for sys.path and package __path__ attributes.cCs:x3tjjD]"}t|dr|jqqWdS)z}Call the invalidate_caches() method on all path entry finders stored in sys.path_importer_caches (where implemented).invalidate_cachesN)r path_importer_cachevaluesrKr)r;finderrrrr5szPathFinder.invalidate_cachesc Cs^tjstjdtnx;tjD],}y||SWq&tk rQw&Yq&Xq&WdSdS)zqSearch sequence of hooks for a finder for 'path'. If 'hooks' is false then use sys.path_hooks. zsys.path_hooks is emptyN)r path_hooksrrrr)r;r'hookrrr _path_hooks=s   zPathFinder._path_hooksc Csa|dkrtj}nytj|}Wn.tk r\|j|}|tj|rrr)r;rrrrrrrr_legacy_get_spec_s   zPathFinder._legacy_get_specNc Csg}x|D]}t|ttfs.q n|j|}|dk r t|drm|j||}n|j||}|dkrq n|jdk r|S|j}|dkrt dn|j |q q Wt |d}||_|SdS)z?Find the loader or namespace_path for this module/package name.Nr=zspec missing loader) rstrbytesrrKr=rrrrrr) r;rr'r<namespace_pathentryrrrrrr _get_specns(      zPathFinder._get_speccCs|dkrtj}n|j|||}|dkr=dS|jdkr|j}|rd|_t|||j|_|SdSn|SdS)zbfind the module on sys.path or 'path' based on sys.path_hooks and sys.path_importer_cache.N namespace)r r'rrrrr~)r;rr'r<rrrrrr=s     zPathFinder.find_speccCs)|j||}|dkr"dS|jS)zfind the module on sys.path or 'path' based on sys.path_hooks and sys.path_importer_cache. This method is deprecated. Use find_spec() instead. N)r=r)r;rr'rrrrr>s zPathFinder.find_module) rHrGrIrJrCrrrrrr=r>rrrrr1s rc@seZdZdZddZddZeZddZdd Z d d d Z d dZ e ddZ ddZd S) FileFinderzFile-based finder. Interactions with the file system are cached for performance, being refreshed when the directory the finder is handling has been modified. cszg}x4|D],\}|jfdd|Dq W||_|pOd|_d|_t|_t|_dS)zInitialize with the path to search on and a variable number of 2-tuples containing the loader and the file suffixes the loader recognizes.c3s|]}|fVqdS)Nr)rr{)rrrr^sz&FileFinder.__init__..rr Nr)r_loadersr' _path_mtimeset _path_cache_relaxed_path_cache)rXr'loader_detailsloadersrr)rrrYs$   zFileFinder.__init__cCs d|_dS)zInvalidate the directory mtime.r Nr)r)rXrrrrszFileFinder.invalidate_cachescCs;|j|}|dkr%dgfS|j|jp7gfS)zTry to find a loader for the specified module, or the namespace package portions. Returns (loader, list-of-portions). This method is deprecated. Use find_spec() instead. N)r=rr)rXrrrrrrs  zFileFinder.find_loadercCs(|||}t||d|d|S)Nrr)r)rXrrr'r r<rrrrrszFileFinder._get_specNcCsd}|jdd}y"t|jp1tjj}Wntk rUd }YnX||jkr{|j||_nt r|j }|j }n|j }|}||kr3t |j|}xg|jD]M\} } d| } t || } t| r|j| || |g|SqWt|}nx~|jD]s\} } t |j|| } tdj| dd|| |kr=t| r|j| || d|Sq=q=W|rtd j|t|d} |g| _| SdS) z}Try to find a loader for the specified module, or the namespace package portions. Returns (loader, list-of-portions).Frrr rYz trying {}rNzpossible namespace for {}r)r$r-r'rr4rtr.r _fill_cacherrrrrrr3rr5rr7rr)rXrr< is_namespace tail_modulercache cache_module base_pathr{r init_filename full_pathrrrrr=sD"              zFileFinder.find_specc Cs|j}ytj|p!tj}Wn!tttfk rKg}YnXtjj dspt ||_ not }xZ|D]R}|j d\}}}|rdj ||j}n|}|j|qW||_ tjj tr dd|D|_ndS)zDFill the cache of potential modules and packages for this directory.rrz{}.{}cSsh|]}|jqSr)r)rfnrrr *s z)FileFinder._fill_cache..N)r'rlistdirr4FileNotFoundErrorPermissionErrorNotADirectoryErrorr r r rrrr7raddr r) rXr'contentslower_suffix_contentsrrTrr{new_namerrrr s"     zFileFinder._fill_cachecsfdd}|S)aA class method which returns a closure to use on sys.path_hook which will return an instance using the specified loaders and the path called on the closure. If the path called on the closure is not a directory, ImportError is raised. cs.t|s!tdd|n|S)z-Path hook for importlib.machinery.FileFinder.zonly directories are supportedr')r5r)r')r;rrrpath_hook_for_FileFinder6s z6FileFinder.path_hook..path_hook_for_FileFinderr)r;rrr)r;rr path_hook,s zFileFinder.path_hookcCsdj|jS)NzFileFinder({!r}))r7r')rXrrrrv>szFileFinder.__repr__)rHrGrIrJrYrrr>rrr=rrCrrvrrrrrs    - rc@s.eZdZdZddZddZdS)_ImportLockContextz$Context manager for the import lock.cCstjdS)zAcquire the import lock.N)r{r/)rXrrrr\Hsz_ImportLockContext.__enter__cCstjdS)zr)rrTr'rrrr_find_spec_legacyZs rc Cs"tjstjdtn|tjk}xtjD]}t]y |j}Wn6tk rt |||}|dkrw5nYnX||||}WdQX|dk r5| r|tjkrtj|}y |j }Wntk r|SYqX|dkr|S|Sq|Sq5q5WdSdS)zFind a module's loader.zsys.meta_path is emptyN) r meta_pathrrrrZrr=rrr) rTr'r< is_reloadrr=rrrrrr _find_speccs0             rcCst|ts-tdjt|n|dkrHtdn|rt|tsltdq|tjkrd}t|j|qn| r|dkrtdndS)zVerify arguments are "sane".zmodule name must be str, not {}rezlevel must be >= 0z__package__ not set to a stringz=Parent module {!r} not loaded, cannot perform relative importzEmpty module nameN) rr TypeErrorr7rSrr rZ SystemError)rTrrrrrr _sanity_checks rzNo module named z{!r}c Cs4d}|jdd}|r|tjkr>t||n|tjkrXtj|Stj|}y |j}Wqtk rtdj||}t|d|YqXnt ||}|dkrttj|d|nt |j }|r0tj|}t ||jdd|n|S)Nrrez; {!r} is not a packagerTr) r$r rZrr"r_ERR_MSGr7rrrr2rL)rTimport_r'r parent_modulerrrrrr_find_and_load_unlockeds*       rc Cs$t|t||SWdQXdS)z6Find and load the module, and release the import lock.N)rxr)rTrrrr_find_and_loads rcCst||||dkr1t|||}ntj|tjkrWt|tStj|}|dkrtjdj |}t |d|nt ||S)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. reNz(import of {} halted; None in sys.modulesrT) rrr{r/r rZr _gcd_importr|r7rr)rTrrrrrrrrs         rcCst|drd|krYt|}|jdt|drY|j|jqYnx|D]}t||s`dj|j|}yt||Wqtk r}z5t |j t r|j |krw`qnWYdd}~XqXq`q`Wn|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. r"*__all__z{}.{}N) rKr!removerrr7rHrrrr _ERR_MSG_PREFIXrT)rfromlistrr from_namererrr_handle_fromlists"       rcCsN|jd}|dkrJ|d}d|krJ|jdd}qJn|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. r&NrHr"rre)ror$)globalsrrrr_calc___package__s    rcCs7ttjf}ttf}ttf}|||gS)z_Returns a list of file-based module loaders. Each item is a tuple (loader, suffixes). )ryr{extension_suffixesr4rr3r) extensionssourcebytecoderrrr s  rc Cs|dkrt|}n6|dk r-|ni}t|}t|||}|s|dkrzt|jddS|s|St|t|jdd}tj|jdt|j|Snt||tSdS)aImport a module. The 'globals' argument is used to infer where the import is occuring 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). reNr)rrrr#r rZrHr) rTrlocalsrrrglobals_rcut_offrrr __import__ s   #(rcCsDtj|}|dkr.td|nt|}|jS)Nzno built-in module named )r9r=rrr2)rTrrrrr_builtin_from_name5 s   rc Cs|a|atjjr!tantatt}xtjj D]y\}}t ||rC|tj krvt }ntj |rCt}nqCt||}t|}|j|qCqCWtjt}xIdD]A} | tjkrt| } n tj| } t|| | qWddgfdddgff} xs| D]_\} } | d }| tjkrwtj| }PqAyt| }PWqAtk rwAYqAXqAWtd t|d |t|d |t|d dj| ytd}Wntk rd}YnXt|d|td}t|d|| dkrktd}t|d|nt|dttjtj| dkrtjddtkrdt_qndS)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. r=rbuiltinsrposix/nt\rezimportlib requires posix or ntrrrr"rfNrwinregrJrz.pywz_d.pydT)z_ioz _warningszbuiltinszmarshal)r{r rrrrrrSrZitemsrrr9rrDr#rr*rHrrLrrr r}rrrrrIrQ) sys_module _imp_module module_typerTrrrr self_module builtin_namebuiltin_module os_details builtin_osrr os_module thread_moduleweakref_module winreg_modulerrr_setup= sj          !              rcCst||t}tjjtj|gtjjt tjjt t j dkrttjjt ntjjtdS)z2Install importlib as the implementation of import.rN)rrr rrrrrrr9rDrrHrIr)rrsupported_loadersrrr_install s  r)zwinzcygwinzdarwin)\rJr r rrrr+r-r2r3r5rFrRrUrS__code__rrVr~rnrurcrdrwrxrzrrrrrr_RAW_MAGIC_NUMBERrrrrrrrrrrrrrrrrrrrrrrobjectrrr#rr8r9rDrIrZr]rnr4r3r}ryr~r'rrrrrrrrrrrrrrrrrrrrrrrs       D   r         7   $j  ?-FG?n)+99& (      #  W