L iZ%ddlZddlZddlmZmZddlmZmZmZ ddl m Z ddl m Z mZddlmZmZmZgdZej(eZGd d eZGd d eZGd deZGddeZeZGddeZy)N)ABCabstractmethod) _getattribute_Pickler whichmodule) ModuleType)AnyOptional)demangleget_mangle_prefix is_mangled)ObjNotFoundErrorObjMismatchErrorImporterOrderedImporterceZdZdZy)rzHRaised when an importer cannot find an object by searching for its name.N__name__ __module__ __qualname____doc__\/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/package/importer.pyrrsRrrceZdZdZy)rz]Raised when an importer found a different object with the same name as the user-provided one.Nrrrrrrsgrrc zeZdZUdZeeefed<ededefdZ d de de ede eeffd Z de dedefd Zy) radRepresents an environment to import modules from. By default, you can figure out what module an object belongs by checking __module__ and importing the result using __import__ or importlib.import_module. torch.package introduces module importers other than the default one. Each PackageImporter introduces a new namespace. Potentially a single name (e.g. 'foo.bar') is present in multiple namespaces. It supports two main operations: import_module: module_name -> module object get_name: object -> (parent module name, name of obj within module) The guarantee is that following round-trip will succeed or throw an ObjNotFoundError/ObjMisMatchError. module_name, obj_name = env.get_name(obj) module = env.import_module(module_name) obj2 = getattr(module, obj_name) assert obj1 is obj2 modules module_namereturncy)zvImport `module_name` from this environment. The contract is the same as for importlib.import_module. Nrselfrs r import_modulezImporter.import_module3srNobjnamec hS|rQtjjt|)t |dd}| |}t |t r| t |dd |jj|}t|} j|}t|\}} ||ur|fSfd} | |\} } } | |\}}}d| d| d |d |d | d |d }t#|#t$rYwxYw#tttf$rt!|d|ddwxYw) aiGiven an object, return a name that can be used to retrieve the object from this environment. Args: obj: An object to get the module-environment-relative name for. name: If set, use this name instead of looking up __name__ or __qualname__ on `obj`. This is only here to match how Pickler handles __reduce__ functions that return a string, don't use otherwise. Returns: A tuple (parent_module_name, attr_name) that can be used to retrieve `obj` from this environment. Use it like: mod = importer.import_module(parent_module_name) obj = getattr(mod, attr_name) Raises: ObjNotFoundError: we couldn't retrieve `obj by name. ObjMisMatchError: we found a different object with the same name as `obj`. N __reduce__rz was not found as .cJj|}t|}|r t|nd}|rdt|nd}|||fS)Nzthe current Python environmentzthe importer for z'sys_importer')rrr )r%r is_mangled_location importer_namer&r#s r get_obj_infoz'Importer.get_name..get_obj_infopsq# ##**35K$[1K"+.5 $$5k$B#CD%  -7 7rz The object provided is from 'z', which is coming from z. However, when we import 'z', it's coming from z@. To fix this, make sure this 'PackageExporter's importer lists z before )rdispatchgettypegetattr isinstancestr Exceptionrrr r$r ImportErrorKeyErrorAttributeErrorrr)r#r%r&reducervorig_module_namermoduleobj2_r.obj_module_name obj_locationobj_importer_nameobj2_module_name obj2_locationobj2_importer_namemsgs` ` rget_namezImporter.get_name:s& J4>P;-);//@A$$0>2**:);;OP]_O !*<)=Q  @ s##e!"X~6 "%)+av>  sC9 D9 DD)D1ct|dd}||S|jjjD](\}}|dk(s|dk(s| t ||d|ur|cS*y#t $rY7wxYw)aFind the module name an object belongs to. This should be considered internal for end-users, but developers of an importer can override it to customize the behavior. Taken from pickle.py, but modified to exclude the search into sys.modules rN__main__ __mp_main__r)r2rcopyitemsrr8)r#r%r&rr<s rrzImporter.whichmodulesc<6  " $(<<#4#4#6#<#<#>  Kz)-/>  .q1S8&&9 "  sA&& A21A2N)rrrrdictr4r__annotations__rr$r r tuplerFrrrrrrs}(#z/ ""    O$CO$x}O$c3hO$bs##rrc0eZdZdZdefdZdededefdZy) _SysImporterz;An importer that implements the default behavior of Python.rc,tj|SrL) importlibr$r"s rr$z_SysImporter.import_modules&&{33rr%r&r ct||SrL)_pickle_whichmodule)r#r%r&s rrz_SysImporter.whichmodules"3--rN)rrrrr4r$r rrrrrQrQs+E44.s.#.#.rrQc feZdZdZdZdZd dedeede eeffdZ d ede fd Z dededefd Z y) rzA compound importer that takes a list of importers and tries them one at a time. The first importer in the list that returns a result "wins". c$t||_yrL)list _importers)r#argss r__init__zOrderedImporter.__init__s *.t*rcnt|ddsyt|dsyt|dsy|jduS)aReturns true iff this module is an empty PackageNode in a torch.package. If you intern `a.b` but never use `a` in your code, then `a` will be an empty module with no source. This can break cases where we are trying to re-package an object after adding a real dependency on `a`, since OrderedImportere will resolve `a` to the dummy package and stop there. See: https://github.com/pytorch/pytorch/pull/71520#issuecomment-1029603769 __torch_package__F__path____file__TN)r2hasattrr_)r#r<s r_is_torchpackage_dummyz&OrderedImporter._is_torchpackage_dummys=v2E:vz*vz*$&&rNr%r&r c |jD]} |j||cStd|d|d|j#ttf$r-}d|d|d|d|}tj |Yd}~nd}~wwxYw)Nz Tried to call get_name with obj z , and name z on z and got zCould not find obj z and name z in any of the importers )rYrFrrlogwarning)r#r%r&importerewarning_messages rrFzOrderedImporter.get_names -H -((d33 -!#j6OPTP_P_O` a  %&67 -6se< $vT(9QCA  O,,  -sAB#A;;Brcd}|jD]G}t|tst|d |j |}|j |rE|cS||t |#t $r }|}Yd}~id}~wwxYw)NzP is not a Importer. All importers in OrderedImporter must inherit from Importer.)rYr3r TypeErrorr$raModuleNotFoundError)r#rlast_errrer<errs rr$zOrderedImporter.import_modules Hh1j!SS !// <..v6    N%k2 2 '  s"A(A(( A=1A88A=c\|jD]}|j||}|dk7s|cSy)NrH)rYr)r#r%r&rers rrzOrderedImporter.whichmodules: #H"..sD9Kj("" # rrL)rrrrr[rar r r4rOrFrr$rrrrrrsd 5'$  C  x}  c3h  333*s##rr)rSloggingabcrrpicklerrrrUtypesrtypingr r _manglingr r r__all__ getLoggerrrcr5rrrrQ sys_importerrrrrrws#  >> Rg!SyShyhJsJZ.8.~ DhDr