
    i(                        d dl Z d dlZd dlZd dlmZmZ d dlmZmZm	Z
 d dlmZ d dlmZ ddlmZmZmZ g dZ ej(                  e      Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z e       Z G d de      Zy)    N)ABCabstractmethod)_getattribute_Picklerwhichmodule)
ModuleType)Any   )demangleget_mangle_prefix
is_mangled)ObjNotFoundErrorObjMismatchErrorImporterOrderedImporterc                       e Zd ZdZy)r   zHRaised when an importer cannot find an object by searching for its name.N__name__
__module____qualname____doc__     P/var/www/html/engine/venv/lib/python3.12/site-packages/torch/package/importer.pyr   r      s    Rr   r   c                       e Zd ZdZy)r   z]Raised when an importer found a different object with the same name as the user-provided one.Nr   r   r   r   r   r      s    gr   r   c            	       z    e Zd ZU dZeeef   ed<   ededefd       Z	dde
dedz  deeef   fd	Zde
dedefd
Zy)r   ad  Represents 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_namereturnc                      y)zvImport `module_name` from this environment.

        The contract is the same as for importlib.import_module.
        Nr   selfr   s     r   import_modulezImporter.import_module6   s    r   Nobjnamec                     S|rQt         j                  j                  t        |            )t	        |dd      }|	  |       }t        |t              r|t	        |dd      |j                   j                  |      }t        |      }	  j                  |      }t        j                  dk\  rt        |j                  d            }nt        |      \  }}	||u r|fS  fd}
 |
|      \  }}} |
|      \  }}}d| d	| d
| d| d| d| d}t)        |      # t        $ r Y w xY w# t         t"        t$        f$ r t'        | d| d       dw xY w)ai  Given 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__r   )      .z was not found as c                     J j                  |       }t        |      }|rt        |      nd}|rdt        |       nd}|||fS )Nzthe current Python environmentzthe importer for z'sys_importer')r   r   r   )r$   r   is_mangled_locationimporter_namer%   r"   s        r   get_obj_infoz'Importer.get_name.<locals>.get_obj_infox   sq    ###**35K$[1K  "+.5   $$5k$B#CD% 
 -77r   z

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 )r   dispatchgettypegetattr
isinstancestr	Exceptionr   r   r   r#   sysversion_infor   splitImportErrorKeyErrorAttributeErrorr   r   )r"   r$   r%   reducervorig_module_namer   moduleobj2_r/   obj_module_nameobj_locationobj_importer_nameobj2_module_nameobj2_locationobj2_importer_namemsgs   ` `               r   get_namezImporter.get_name=   s   & <CH$5$5$9$9$s)$D$L S,5F!B!"c*! <35D<<<D++C6 /0	''4F7* %VTZZ_='5a $;$$	8  <H;L8'8>J4>P;-);//@ A$$0> 2**:);;OP] _O !*<)=Q	@ 	 s##o ! , X~6 	"%)+av>	s   D( AD7 (	D43D47)E c                     t        |dd      }||S | j                  j                         j                         D ](  \  }}|dk(  s|dk(  s|	 t	        ||      d   |u r|c S * y# t
        $ r Y 7w xY w)a  Find 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
        r   N__main____mp_main__r   )r3   r   copyitemsr   r<   )r"   r$   r%   r   r@   s        r   r   zImporter.whichmodule   s     c<6" $(<<#4#4#6#<#<#> 	Kz)-/> .q1S8&& 9	  " s   A&&	A21A2N)r   r   r   r   dictr5   r   __annotations__r   r#   r	   tuplerJ   r   r   r   r   r   r      s}    ( #z/""   T$C T$sTz T$U38_ T$ls # # r   r   c                   0    e Zd ZdZdefdZdededefdZy)	_SysImporterz;An importer that implements the default behavior of Python.r   c                 ,    t        j                  |      S rP   )	importlibr#   r!   s     r   r#   z_SysImporter.import_module   s    &&{33r   r$   r%   r   c                 <    t        |dd       }||S t        ||      S )Nr   )r3   _pickle_whichmodule)r"   r$   r%   r   s       r   r   z_SysImporter.whichmodule   s+     c<6""3--r   N)r   r   r   r   r5   r#   r	   r   r   r   r   rU   rU      s+    E4 4.s .# .# .r   rU   c            	       f    e Zd ZdZd Zd Zddededz  deeef   fdZ	d	ede
fd
ZdededefdZy)r   zA 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        |      | _        y rP   )list
_importers)r"   argss     r   __init__zOrderedImporter.__init__   s    *.t*r   c                 n    t        |dd      syt        |d      syt        |d      sy|j                  du S )a  Returns 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)r3   hasattrrc   )r"   r@   s     r   _is_torchpackage_dummyz&OrderedImporter._is_torchpackage_dummy   s=     v2E:vz*vz*$&&r   Nr$   r%   r   c           
         | j                   D ]  }	 |j                  ||      c S  t        d| d| d| j                          # t        t        f$ r-}d| d| d| d| }t        j                  |       Y d }~nd }~ww xY w)Nz Tried to call get_name with obj z, and name z on z	 and got zCould not find obj z
 and name z in any of the importers )r]   rJ   r   r   logwarning)r"   r$   r%   importerewarning_messages         r   rJ   zOrderedImporter.get_name   s     	-H-((d33	- !#j6OPTP_P_O`a
 	
 %&67 -6se <  $vT(9QCA   O,,-s   AB #A;;B r   c                     d }| j                   D ]G  }t        |t              st        | d      	 |j	                  |      }| j                  |      rE|c S  ||t        |      # t        $ r}|}Y d }~id }~ww xY w)NzP is not a Importer. All importers in OrderedImporter must inherit from Importer.)r]   r4   r   	TypeErrorr#   re   ModuleNotFoundError)r"   r   last_errri   r@   errs         r   r#   zOrderedImporter.import_module   s     	Hh1j !S S !//<..v6	 N%k22 ' s   "A(A((	A=1A88A=c                 \    | j                   D ]  }|j                  ||      }|dk7  s|c S  y)NrL   )r]   r   )r"   r$   r%   ri   r   s        r   r   zOrderedImporter.whichmodule  s:     	#H"..sD9Kj(""	#
 r   rP   )r   r   r   r   r_   re   r	   r5   rS   rJ   r   r#   r   r   r   r   r   r      sd    
5'$
C 
sTz 
U38_ 
3 3 3*s # # r   r   )rW   loggingr7   abcr   r   pickler   r   r   rY   typesr   typingr	   	_manglingr   r   r   __all__	getLoggerr   rg   r6   r   r   r   rU   sys_importerr   r   r   r   <module>r{      s      
 # 
   > > Rg!Sy Shy hOs Od.8 .  ~Dh Dr   