
    [i(                     6   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dS )    N)ABCabstractmethod)_getattribute_Picklerwhichmodule)
ModuleType)Any   )demangleget_mangle_prefix
is_mangled)ObjNotFoundErrorObjMismatchErrorImporterOrderedImporterc                       e Zd ZdZdS )r   zHRaised when an importer cannot find an object by searching for its name.N__name__
__module____qualname____doc__     I/var/www/icac/venv/lib/python3.11/site-packages/torch/package/importer.pyr   r      s        RRRRr   r   c                       e Zd ZdZdS )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        ggggr   r   c            	           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dS )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                     dS )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                 6    t|rrt           j                            t          |                    Ft	          |dd          }|3	  |            }t          |t                    r|n# t          $ r Y nw xY wt	          |dd          |j         	                    |          }t          |          }	                      |          }t          j        dk    r$t          |                    d                    }nt          |          \  }}	n3# t           t"          t$          f$ r t'          | d| d           dw xY w||u r|fS  fd}
 |
|          \  }}} |
|          \  }}}d| d	| d
| d| d| d| d}t)          |          )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                      |           }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   s    ###**355K$[11K 6!+...5  &D$5k$B$BDD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_   & <C<H$5$9$9$s))$D$D$L S,55F!B!"c** "!    D<355D<<D++C66 /00	''44F7** %VTZZ__=='55aX~6 	 	 	">>+>>>> 	
 $;;$$	8 	8 	8 	8 	8 	8  <H<;L;L8'8>Jl4>P>P;-);@ @ @$0@ @*:@ @P]@ @ !	@ @ +=	@ @ @ 	 s###s   !A) )
A65A6;AD 0Ec                 
   t          |dd          }||S | j                                                                        D ]A\  }}|dk    s|dk    s|	 t	          ||          d         |u r|c S 2# t
          $ r Y >w xY wdS )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6" $(<#4#4#6#6#<#<#>#> 	 	Kz))-//> ..q1S88&&&& 9!    zs   A33
B ?B N)r   r   r   r   dictr5   r   __annotations__r   r#   r	   tuplerJ   r   r   r   r   r   r      s          ( #z/""""     ^T$ T$C T$sTz T$U38_ T$ T$ T$ T$ls # #      r   r   c                   2    e Zd ZdZdefdZdededefdZdS )	_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33r   r$   r%   r   c                 L    t          |dd           }||S t          ||          S )Nr   )r3   _pickle_whichmodule)r"   r$   r%   r   s       r   r   z_SysImporter.whichmodule   s1     c<66""3---r   N)r   r   r   r   r5   r#   r	   r   r   r   r   rU   rU      s^        EE4 4 4 4 4.s .# .# . . . . . .r   rU   c            	       n    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dS )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          |          | _        d S rP   )list
_importers)r"   argss     r   __init__zOrderedImporter.__init__   s    *.t**r   c                     t          |dd          sdS t          |d          sdS t          |d          sdS |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   sY     v2E:: 	5vz** 	5vz** 	4$&&r   Nr$   r%   r   c           
         | j         D ]`}	 |                    ||          c S # t          t          f$ r2}d| d| d| d| }t                              |           Y d }~Yd }~ww xY wt          d| d| d| j                    )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333$&67 - - -As A A $A A*2A A=>A A   O,,,,,,,,- a#aaaaPTP_aa
 
 	
s   #A&(A!!A&r   c                    d }| j         D ]q}t          |t                    st          | d          	 |                    |          }|                     |          rU|c S # t          $ r}|}Y d }~jd }~ww xY w||t          |          )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1  S S S  !//<<..v66 &    N%k222s   *A#A##
A9-A44A9c                 Z    | j         D ]"}|                    ||          }|dk    r|c S #dS )NrL   )r]   r   )r"   r$   r%   ri   r   s        r   r   zOrderedImporter.whichmodule  sK     	# 	#H"..sD99Kj(("""" ) zr   rP   )r   r   r   r   r_   re   r	   r5   rS   rJ   r   r#   r   r   r   r   r   r      s         
5 5 5' ' '$
 
C 
sTz 
U38_ 
 
 
 
3 3 3 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
Q
Qg!!S S S S Sy S S Sh h h h hy h h hO O O O Os O O Od. . . . .8 . . .  |~~D D D D Dh D D D D Dr   