
    Χg"                        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dS )    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dS )r   zHRaised when an importer cannot find an object by searching for its name.N__name__
__module____qualname____doc__     R/var/www/html/ai-engine/env/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e         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_module1   s      r   Nobjnamec                     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          |          \  }}	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_infon   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'   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   s5   & <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#FD11GD!!X~6 	 	 	">>+>>>> 	
 $;;$$	8 	8 	8 	8 	8 	8  <H<;L;L8'8>Jl4>P>P;-);@ @ @$0@ @*:@ @P]@ @ !	@ @ +=	@ @ @ 	 s###s   !A) )
A65A6;(C$ $0D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   )r5   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   r
   r7   r   __annotations__r   r'   r	   r   r   rI   r   r   r   r   r   r      s          ( #z/""""     ^O$ O$C O$x} O$c3h O$ O$ O$ O$b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 rO   )	importlibr'   r%   s     r   r'   z_SysImporter.import_module   s    &{3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E4 4 4 4 4.s .# .# . . . . . .r   rR   c                   B    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
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 rO   )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)r5   hasattrr`   )r&   r?   s     r   _is_torchpackage_dummyz&OrderedImporter._is_torchpackage_dummy   sY     v2E:: 	5vz** 	5vz** 	4$&&r   r"   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.)rZ   r6   r   	TypeErrorr'   rb   ModuleNotFoundError)r&   r"   last_errimporterr?   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r(   r)   c                 Z    | j         D ]"}|                    ||          }|dk    r|c S #dS )NrK   )rZ   r   )r&   r(   r)   rg   r"   s        r   r   zOrderedImporter.whichmodule   sK     	# 	#H"..sD99Kj(("""" ) zr   N)r   r   r   r   r\   rb   r7   r   r'   r	   r   r   r   r   r   r      s         
5 5 5' ' '$3 3 3 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      s       # # # # # # # #         
       3 3 3 3 3 3 3 3 3 3 3 3 3 3 > > > > > > > > > > R
Q
QS S S S Sy S S Sh h h h hy h h hJ J J J Js J J JZ. . . . .8 . . . |~~6 6 6 6 6h 6 6 6 6 6r   