o
    ki'                     @   s   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eZG d	d
 d
eZG dd deZG dd deZG dd deZe ZG d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__ r   r   `/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/package/importer.pyr          r   c                   @   r   )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      r   r   c                	   @   sr   e Zd ZU dZeeef ed< ededefddZ	dde
d	edB deeef fd
dZde
d	edefd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                 C   s   dS )zvImport `module_name` from this environment.

        The contract is the same as for importlib.import_module.
        Nr   selfr   r   r   r   import_module3   s    zImporter.import_moduleNobjnamec                    sj   du r0|r0t jt|du r0t|dd}|dur0z| }t|tr%| W n	 ty/   Y nw  du r:t|dd  du rA|j 	| }t
|}z|}tjdkr_t| d}nt| \}}	W n tttfy~   t| d| d  dw ||u r| fS  fd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                    sT    d u rt d|  }t|}|rt|nd}|r#dt| nd}|||fS )Nzname must not be Nonezthe current Python environmentzthe importer for z'sys_importer')AssertionErrorr   r   r   )r    r   is_mangled_locationimporter_namer!   r   r   r   get_obj_infou   s   

z'Importer.get_name.<locals>.get_obj_infoz

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msgr   r*   r   get_name:   s`    


zImporter.get_namec              	   C   s   t |dd}|dur|S | j  D ]*\}}|dks#|dks#|du r$qzt||d |u r3|W   S W q ty=   Y q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   )r/   r   copyitemsr   r8   )r   r    r!   r   r<   r   r   r   r      s    
zImporter.whichmoduleN)r   r   r   r   dictr1   r   __annotations__r   r   r   tuplerF   r   r   r   r   r   r      s   
 $Wr   c                   @   s4   e Zd ZdZdefddZdededefdd	Zd
S )_SysImporterz;An importer that implements the default behavior of Python.r   c                 C   s
   t |S rK   )	importlibr   r   r   r   r   r      s   
z_SysImporter.import_moduler    r!   r   c                 C   s"   t |dd }|d ur|S t||S )Nr   )r/   _pickle_whichmodule)r   r    r!   r   r   r   r   r      s   
z_SysImporter.whichmoduleN)r   r   r   r   r1   r   r   r   r   r   r   r   rO      s    rO   c                	   @   sl   e Zd ZdZdd Zdd ZddededB d	eeef fd
dZ	ded	e
fddZdeded	efd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                 G   s   t || _d S rK   )list
_importers)r   argsr   r   r   __init__   s   zOrderedImporter.__init__c                 C   s6   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)r/   hasattrrX   )r   r<   r   r   r   _is_torchpackage_dummy   s   



z&OrderedImporter._is_torchpackage_dummyNr    r!   r   c                 C   s   | j D ]2}z	|||W   S  ttfy5 } zd| d| d| d| }t| W Y d }~qd }~w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 )rS   rF   r   r   logwarning)r   r    r!   importerewarning_messager   r   r   rF      s$   
zOrderedImporter.get_namer   c                 C   s   d }| j D ]3}t|tst| dz||}| |r W q|W   S  ty8 } z|}W Y d }~qd }~ww |d ur?|t|)NzP is not a Importer. All importers in OrderedImporter must inherit from Importer.)rS   r0   r   	TypeErrorr   rZ   ModuleNotFoundError)r   r   last_errr]   r<   errr   r   r   r      s$   




zOrderedImporter.import_modulec                 C   s,   | j D ]}|||}|dkr|  S qdS )NrG   )rS   r   )r   r    r!   r]   r   r   r   r   r      s   
zOrderedImporter.whichmodulerK   )r   r   r   r   rU   rZ   r   r1   rN   rF   r   r   r   r   r   r   r   r      s    $r   )rP   loggingr3   abcr   r   pickler   r   r   rQ   typesr   typingr   	_manglingr
   r   r   __all__	getLoggerr   r[   r2   r   r   r   rO   sys_importerr   r   r   r   r   <module>   s"    
 