
    sg"                         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mZmZmZmZ ddlmZmZmZ g d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DictListOptionalTuple   )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__     I/var/www/html/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e   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_module1   s    r   Nobjnamec                 h    S|rQt         j                  j                  t        |            )t	        |dd      }|	  |       }t        |t              r|t	        |dd      |j                   j                  |      }t        |      }	  j                  |      }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_infon   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'   r   ImportErrorKeyErrorAttributeErrorr   r   )r&   r(   r)   reducervorig_module_namer"   moduleobj2_r1   obj_module_nameobj_locationobj_importer_nameobj2_module_nameobj2_locationobj2_importer_namemsgs   ` `               r   get_namezImporter.get_name8   s   & <CH$5$5$9$9$s)$D$L S,5F!B!"c*! <35D<<<D++C6 /0	''4F#FD1GD! $;$$	8  <H;L8'8>J4>P;-);//@ A$$0> 2**:);;OP] _O !*<)=Q	@ 	 s##e ! " X~6 	"%)+av>	s   C9  D 9	DD)D1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   )r5   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   r
   r7   r   __annotations__r   r'   r	   r   r   rI   r   r   r   r   r   r      s}    ( #z/""   O$C O$x} O$c3h O$b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 rO   )	importlibr'   r%   s     r   r'   z_SysImporter.import_module   s    &&{33r   r(   r)   r#   c                     t        ||      S rO   )_pickle_whichmodule)r&   r(   r)   s      r   r   z_SysImporter.whichmodule   s    "3--r   N)r   r   r   r   r7   r'   r	   r   r   r   r   rR   rR      s+    E4 4.s .# .# .r   rR   c                   @    e Zd ZdZd Z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 rO   )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)r5   hasattrr`   )r&   r?   s     r   _is_torchpackage_dummyz&OrderedImporter._is_torchpackage_dummy   s=     v2E:vz*vz*$&&r   r"   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.)rZ   r6   r   	TypeErrorr'   rb   ModuleNotFoundError)r&   r"   last_errimporterr?   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=r(   r)   c                 \    | j                   D ]  }|j                  ||      }|dk7  s|c S  y)NrK   )rZ   r   )r&   r(   r)   rg   r"   s        r   r   zOrderedImporter.whichmodule   s:     	#H"..sD9Kj(""	#
 r   N)r   r   r   r   r\   rb   r7   r   r'   r	   r   r   r   r   r   r      s>    
5'$3 3 3*s # # r   r   )rT   abcr   r   pickler   r   r   rV   typesr   typingr	   r
   r   r   r   	_manglingr   r   r   __all__r8   r   r   r   rR   sys_importerr   r   r   r   <module>rq      sz     # 
  3 3 > > RSy Shy hJs JZ.8 . ~6h 6r   